I am working on project where I have reports that have three states its "checked","inprocess" and "completed"
Got two tables!
tbl_tracking - for keeping track of reports
report_id usr_id date status
----------------------------------------------
0000 abc 2014/04/05 checked
0001 abc 2014/04/05 checked
0000 abc 2014/04/05 inprocess
0001 abc 2014/04/05 completed
0002 abc 2014/04/06 completed
0004 xyz 2014/04/05 checked
0005 xyz 2014/04/06 checked
tbl_timestatus- for keeping track of time employees have worked on reports
usr_id date time_worked (hrs)
----------------------------------------------
abc 2014/04/05 6
abc 2014/04/06 5
Now I want to create a view to always display up-to-date status of everything so I code this
CREATE VIEW VW_STATUS AS
SELECT tb1.usr_id, tb1.date,
COUNT(CASE tb1.status WHEN 'checked' THEN 1 ELSE NULL END) AS checkedcount,
COUNT(CASE tb1.status WHEN 'inprocess' THEN 1 ELSE NULL END) AS inprocesscount,
COUNT(CASE tb1.status WHEN 'completed' THEN 1 ELSE NULL END) AS compltedcount,
CASE WHEN (tb1.usr_id=tb2.usr_id AND tb1.date=tb2.date) THEN tb2.time_worked ELSE NULL END AS timeworked
FROM tbl_tracking tb1, tbl_timestatus tb2
GROUP BY tb1.usr_id, tb1.date;
Expected Output:
usr_id date checkedcount inprocesscount completedcount timeworked
---------------------------------------------------------------------------------------
abc 2014/04/05 2 1 1 6
abc 2014/04/06 0 0 1 5
xyz 2014/04/05 1 0 0 NULL
xyz 2014/04/06 1 0 0 NULL
Actual Output:
usr_id date checkedcount inprocesscount completedcount timeworked
---------------------------------------------------------------------------------------
abc 2014/04/05 4 2 2 6
abc 2014/04/06 0 0 2 5
xyz 2014/04/05 2 0 0 NULL
xyz 2014/04/06 2 0 0 NULL
While the "timeworked" values remain correct, the count(*)'s add themselves twice!! There is some problem with join! Need help on same..
You should try like below. See a sample fiddle here http://sqlfiddle.com/#!3/0b24c/6
CREATE VIEW VW_STATUS AS
SELECT tb1.usr_id, tb1.date,
COUNT(CASE tb1.status WHEN 'checked' THEN 1 ELSE NULL END) AS checkedcount,
COUNT(CASE tb1.status WHEN 'inprocess' THEN 1 ELSE NULL END) AS inprocesscount,
COUNT(CASE tb1.status WHEN 'completed' THEN 1 ELSE NULL END) AS compltedcount,
tb2.time_worked
FROM tbl_tracking tb1
LEFT JOIN tbl_timestatus tb2
ON tb1.usr_id=tb2.usr_id
AND tb1.date=tb2.date
GROUP BY tb1.usr_id, tb1.date , tb2.time_worked
ORDER BY tb1.usr_id;
Which results in
join tbl1 and tb2 on tb1.usr_id=tb2.usr_id AND tb1.date=tb2.date. Note that the time worked is correct.
Related
This question already has answers here:
How do I use properly CASE..WHEN in MySQL
(6 answers)
Closed 4 years ago.
I'm trying to display the sum of my severity levels.
My Table
client dateadded problem level
abc 2019-02-02 12345a 1
abc 2019-02-02 12345b 1
abc 2019-02-02 12345c 2
abc 2019-02-02 12345d 5
abc 2019-02-09 12345e 3
abc 2019-02-09 12345f 3
abc 2019-02-09 12345g 4
abc 2019-02-09 12345h 10
abc 2019-02-09 12345j 8
abc 2019-02-16 12345x 7
abc 2019-02-16 12345s 9
abc 2019-02-16 12345w 4
abc 2019-02-16 12345bs 5
This is my code
select client, dateadded,
count(case when level= '1,2,3' then 1 end) as Moderate,
count(case when level= '4,5,6,7' then 1 end) as Severe,
count(case when level= '8,9,10' then 1 end) as Critical
from table1 where client = 'abc'
group by client, dateadded
I tried
count(case when level= '1' and '2' and '3' then 1 end) as Moderate,
My desired output
dateadded Moderate severe critical
2019-02-02 3 1 0
2019-02-09 2 1 2
2019-02-16 0 3 2
Thanks!
Nathalie
CASE WHEN level IN(1,2,3) THEN 1 END ... was the way to go.. thanks all!
Can you try with IN condition in case and group by dateadded
select client, dateadded,
count(case when level IN (1,2,3) then 1 end) as Moderate,
count(case when level IN (4,5,6,7) then 1 end) as Severe,
count(case when level IN (8,9,10) then 1 end) as Critical
from table1 where client = 'abc'
group by dateadded, client
Let me explain what I've got and what I'm trying to achieve.
I have Table Look like this.
Let's say I have Table A: Booking
RoomNum ArrivalDate DepartureDate
101 2014-12-01 2014-12-03
102 2014-12-02 2014-12-03
103 2014-12-03 2014-12-04
101 2014-12-05 2014-12-06
the expected result should be something like the following :
RoomNum Des-01 Des-02 Des-03 Des-04 Des-05 Des-06
101 1 1 1 0 1 1
102 0 1 1 0 0 0
103 0 1 1 0 1 1
Any ideas would be very much appreciated.
Thanks in advance
One option you have, while it may not be efficient or dynamic, is to use the SUM() function. Inside of there, you can include a case statement.
In this case, I wrote a case statement that checks if the current date is between the arrival and departure dates of a room.
SELECT roomnum,
SUM(CASE WHEN '2014-12-01' BETWEEN arrivalDate AND departureDate THEN 1 ELSE 0 END) AS 'Dec-01',
SUM(CASE WHEN '2014-12-02' BETWEEN arrivalDate AND departureDate THEN 1 ELSE 0 END) AS 'Dec-02',
SUM(CASE WHEN '2014-12-03' BETWEEN arrivalDate AND departureDate THEN 1 ELSE 0 END) AS 'Dec-03',
SUM(CASE WHEN '2014-12-04' BETWEEN arrivalDate AND departureDate THEN 1 ELSE 0 END) AS 'Dec-04',
SUM(CASE WHEN '2014-12-05' BETWEEN arrivalDate AND departureDate THEN 1 ELSE 0 END) AS 'Dec-05',
SUM(CASE WHEN '2014-12-06' BETWEEN arrivalDate AND departureDate THEN 1 ELSE 0 END) AS 'Dec-06'
FROM booking
GROUP BY roomnum;
Here is an SQL Fiddle.
Please help to update TotalPending and TotalAccepted in table tblInvitation using tblInvitationEmpStatus
1.tblInvitation
ID Invitation TotalPending TotalAccepted
1 Group Meeting 0 0
2 Project discussion 0 0
3 Skype Call 0 0
tblInvitationEmpStatus
ID EmpID StatusType(1=Pending,2=Accepted)
1 101 1
1 102 1
1 103 2
2 101 2
2 102 2
3 104 1
3 105 2
3 106 2
Output tblInvitation
ID Invitation TotalPending TotalAccepted
1 Group Meeting 2 1
2 Project discussion 0 2
3 Skype Call 1 2
Use Conditional Aggregate to find the count of Pending and Accepted status.
SELECT a.ID,
a.Invitation,
TotalPending = Count(CASE WHEN StatusType = 1 THEN 1 END),
TotalAccepted = Count(CASE WHEN StatusType = 2 THEN 1 END)
FROM tblInvitation a
JOIN tblInvitationEmpStatus b
ON a.ID = b.ID
GROUP BY a.ID,
a.Invitation
SQL FIDDLE DEMO
To Update the tblInvitation table
Update A set
TotalPending = Count(CASE WHEN StatusType = 1 THEN 1 END),
TotalAccepted = Count(CASE WHEN StatusType = 2 THEN 1 END)
FROM tblInvitation a
JOIN tblInvitationEmpStatus b
ON a.ID = b.ID
GROUP BY a.ID,
a.Invitation
Product Table
id name description price
1 AAA AAAAAA 10.00
2 BBB BBBBBB 12.00
3 CCC CCCCCC 15.00
4 DDD DDDDDD 8.00
5 EEE EEEEEE 12.50
Sales Table
trackid productid affiliateid paymentstatus refundid
1 2 1 COMPLETED 1
2 2 0 DONE null
3 3 1 COMPLETED null
4 3 0 COMPLETED null
5 3 0 COMPLETED null
6 5 5 DONE null
7 5 0 COMPLETED 2
8 5 2 COMPLETED null
9 2 0 DONE null
10 3 1 COMPLETED 3
For Sales table
If there is no affiliate for a particular sale, then affiliateid would be 0 otherwise affiliateid (I have another table for user which lists vendors and affiliates)
If a particular sales is been refunded (for any reason), then refundid would be set to some value, otherwise it would be null
paymentstatus can have any 1 of 2 values, COMPLETED and DONE
Now I need query that list following data for each product in product table
productid name totalsales affsales refunds
1 AAA 0 0 0
2 BBB 1 1 1
3 CCC 4 2 1
4 DDD 0 0 0
5 EEE 2 1 1
totalsales: sales with paymentstatus as "COMPLETED"
affsales: sales with paymentstatus as "COMPLETED" and affiliateid NOT EQUAL TO 0
refunds: sales with paymentstatus as "COMPLETED" and refundid NOT null
How can I form this particular query?
select
p.name,
SalesSummary.productid,
COALESCE( SalesSummary.TotalSales, 0 ) TotalSales
COALESCE( SalesSummary.AffSales, 0 ) AffSales
COALESCE( SalesSummary.Refunds, 0 ) Refunds
from
product p
left join
( select s.productid,
count(*) TotalSales
sum( if( s.affiliateID > 0, 1, 0 )) affSales,
sum( if( ifnull( s.RefundID, 0 ) > 0, 1, 0 )) Refunds
from
sales s
where
s.PaymentStatus = 'COMPLETED'
group by
s.productid
) SalesSummary
on p.id = SalesSummary.productid
How do i count null values while making cross tab query?
I have a table with three colums [id, name, answer]
i have following records:
ID NAME ANS
1 ABC 1
1 ABC 0
1 ABC NULL
2 XYZ 1
2 XYZ NULL
2 XYZ NULL
2 XYZ 1
2 XYZ 0
1 ABC 0
now i would like to get my result:
ID Name NULLCOUNT TRUE COUNT FALSE COUNT
1 ABC 1 1 2
2 XYZ 2 2 1
I am using following SQL Statement:
select ID, NAME,
sum(case ANS when null then 1 else 0 end) as NULLCOUNT,
sum(case ANS when 1 then 1 else 0 end) as TRUECOUNT,
sum(case ANS when 0 then 1 else 0 end) as FALSECOUNT
from
TBL1
Group By ID, Name
Getting my result:
ID Name NULLCOUNT TRUE COUNT FALSE COUNT
1 ABC 0 1 2
2 XYZ 0 2 1
The NULL Count is getting error. Why and how can i solve this?
I believe instead of this:
sum(case ANS when null then 1 else 0 end) as NULLCOUNT
You should use this:
sum(case when ANS is null then 1 else 0 end) as NULLCOUNT
null -> is null?
NULL doesn't even compare with itself, you could use "CASE WHEN ANS is NULL"(you're also missing GROUP BY). Or try:
select ID, NAME,
sum(if(ans IS NULL, 1, 0)) as NULLCOUNT,
sum(case ANS when 1 then 1 else 0 end) as TRUECOUNT,
sum(case ANS when 0 then 1 else 0 end) as FALSECOUNT
from
TBL1
group by ID,NAME