I am working on a table for a client where they desire only specific column value to be counted I have done one way of it but its not the outcome they need so it was out aside.
I have a table like this.(Note: Not actual table)
company_name | type | ................
com A | type1 | ................
com A | type2 | ................
com B | type1 | ................
com A | type2 | ................
com A | type1 | ................
com C | type1 | ................
com C | type2 | ................
com A | type1 | ................
so on so forth with the type column though there are columns in it that they don't want counted at all
The table that is needed would look like this
company_name | Type1 | Type2 | ..........
com A | 3 | 2 | ..........
com B | 1 | 0 | ..........
com C | 1 | 1 | ..........
What I'm getting so far is:
company_name | Type | count | ..........
com A | type1 | 3 | ..........
com A | type2 | 2 | ..........
com B | type1 | 1 | ..........
com C | type1 | 1 | ..........
com C | type2 | 1 | ..........
Please help I have been scratching my skull away over this.
Try this:
SELECT
company_name,
SUM(IIF(Type = 'type1', 1, 0)) AS Type1,
SUM(IIF(Type = 'type2', 1, 0)) AS Type2,
SUM(IIF(Type = 'type3', 1, 0)) AS Type3
FROM table
GROUP BY company_name;
In MySQL, you can use the IF
statement instead of Access's IIF
, also you can write it without IF
or CASE
because of MySQL implicit conversions:
SELECT Promoter,
Sum(NoticeType = 'VARIATION') AS 'Variation',
Sum(NoticeType = 'TWO HOURS AFTER') AS 'Two Hours After',
Sum(NoticeType = 'THREE MONTHS') AS 'Three Months',
Sum(NoticeType = 'THREE DAY') AS 'Three Day',
Sum(NoticeType = 'TEN DAY') AS 'Ten Day'
FROM notices
GROUP BY Promoter;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With