Saturday, 15 August 2015

sql - MySQL - Conditional SELECT to output separate columns -


I am trying to get a simple problem in MySQL: the table I created by 3 fields And I want to have a field in the form of a query that gives the field selected as DISTINCT and two more columns, which corresponds to a specific situation, when one of the remaining two fields returns back.

For example ( ago , second and third left to right):

 < Code> | WojokAAD | AAA | 0 | | VziAAAT | BBB | 0 | | VziAAAT | CCC | 1 | | VziAAAT | DDD | 0 | | W0cZAAT | I | 1 | | VziNAAT | FFF | 1 | | VzpqAAD | | Jig | 1 | | VzpqAAD | | HHH | 1 |  

My existing query is structured as follows:

  select field_one as the difference (first), second from the table (number) third = 0 group By field_one;  

will return the above questions:

  | WojokAAD | 1 | | VziAAAT | 2 |  

I can change the query to do something like this:

  SELECT DISTINCT (first) AS field_one, COUNT (seconds) WHERE third from the table = 1 GROUP by Field_on;  

And it will return:

  | VzpqAAD | | 2 | | VziNAAT | 1 | | W0cZAAT | 1 | | VziAAAT | 1 |  

How can I get both questions together, so that I got the first column group and distint and got two extra columns, respectively, with the third = 1 and the third = 0 .

Actually something like this (wrong course):

  select difference (first) AS field_one, COUNT (second WHERE third = 1) alpha, COUNT (second WHERE Third = 0) field beta on AS beta from table group;  

I tried to use the CASE and IF control flow function though I managed to get results in only one column.

Thanks for giving thanks to someone

You just use this query First choose: First, choose SM (IF (third = 0,1,0)) as Col_one from group group Col_zero, SUM (IF (third = 1,1, 0));


No comments:

Post a Comment