Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread Can this be done with single query?
Wed, Dec 5 2007 2:23 PMPermanent Link

Kai
Hi ~

Given a table like this

StaffID   ClientID   Date                WorkType  MinutesSpent
---------------------------------------------------------------------
10          22           2007-10-01      DC            90
10          44           2007-10-01      IC             270
10          33           2007-10-02      DC            120
10          22           2007-10-22      IC             105
10          22           2007-10-23      DC            30  

Is is possible to obtain the following results with a *single* SQL query?

ClientID           DC Minutes        IC Minutes
--------------------------------------------------
22                   120                   105
33                   120                   0
44                   0                      270

Thanks for all help,
Kai
Wed, Dec 5 2007 2:28 PMPermanent Link

"Robert"
Are the work types limited to DC and IC, or can you have any number?

If it is limited to those two, then it's simple

SELECT CLIENTID,
SUM (IF (WORKTYPE = DC THEN MINUTESSPENT ELSE 0)) DCMINUTES,
SUM (IF (WORKTYPE = IC THEN MINUTESSPENT ELSE 0)) ICMINUTES,
GROUP BY CLIENTID;

Robert

"Kai" <platysternon@hotmail.com> wrote in message
news:A2A453FA-36A9-4D6C-923E-F494D6F193EB@news.elevatesoft.com...
> Hi ~
>
> Given a table like this
>
> StaffID   ClientID   Date                WorkType  MinutesSpent
> ---------------------------------------------------------------------
> 10          22           2007-10-01      DC            90
> 10          44           2007-10-01      IC             270
> 10          33           2007-10-02      DC            120
> 10          22           2007-10-22      IC             105
> 10          22           2007-10-23      DC            30
>
> Is is possible to obtain the following results with a *single* SQL query?
>
> ClientID           DC Minutes        IC Minutes
> --------------------------------------------------
> 22                   120                   105
> 33                   120                   0
> 44                   0                      270
>
> Thanks for all help,
> Kai
>

Wed, Dec 5 2007 2:33 PMPermanent Link

Kai
"Robert" <ngsemail2005withoutthis@yahoo.com.ar> wrote:

Are the work types limited to DC and IC, or can you have any number?


Yes, work types is limited to a handful of values - so your solution works great!

Thanks,
Kai
Image