I'm new to MySQL so please be patient :)
I have a table where I would like to retrieve data based on date and do Sum
Here is my table
client datescanned problem title
abc 2019-02-02 12345a High xyz
abc 2019-02-02 12345b High xyz
abc 2019-02-02 12345c High xyz
abc 2019-02-02 12345d Medium xyz
abc 2019-02-09 12345e High xyz
abc 2019-02-09 12345f High xyz
abc 2019-02-09 12345g Low xyz
abc 2019-02-09 12345h Low xyz
abc 2019-02-09 12345j Low xyz
abc 2019-02-16 12345x High xyz
abc 2019-02-16 12345s High xyz
abc 2019-02-16 12345w High xyz
abc 2019-02-16 12345bs Medium xyz
My desired output would be
client datescanned problem High Medium Low
abc 2019-02-02 12345x 3 1 0
abc 2019-02-09 12345s 2 0 3
abc 2019-02-16 12345w 3 1 0
This is my code
select client,datescanned, problem, severity,
count(case when severity = 'High' then 1 end) as High,
count(case when severity = 'Medium' then 1 end) as Medium,
count(case when severity = 'Low' then 1 end) as Low
from ssstest where client = "myuser"
group by client,datescanned, problem
This will fetch all the numbers correctly, however I want to "sum" up High, Medium, Low per date.
Now I get..
client datescanned problem severity High Medium Low
abc 2019-02-02 12345a High 1 0 0
abc 2019-02-02 12345b High 1 0 0
abc 2019-02-02 12345c High 1 0 0
abc 2019-02-02 12345d Medium 0 1 0
abc 2019-02-09 12345e High 1 0 0
abc 2019-02-09 12345f High 1 0 0
abc 2019-02-09 12345g Low 0 0 1
abc 2019-02-09 12345h Low 0 0 1
abc 2019-02-09 12345j Low 0 0 1
abc 2019-02-16 12345x High 1 0 0
abc 2019-02-16 12345s High 1 0 0
abc 2019-02-16 12345w High 1 0 0
abc 2019-02-16 12345bs Medium 0 1 0
So basically, I just want to add up High, Medium, Low, group the same date together as one.
client datescanned problem High Medium Low
abc 2019-02-02 12345x 3 1 0
abc 2019-02-09 12345s 2 0 3
abc 2019-02-16 12345w 3 1 0
Thanks again for your help!
You can try using conditional aggregation
select client,dateadded, computername, severity,
count(case when severity='High' then 1 end) as High,
count(case when severity='Medium' then 1 end) as Medium,
count(case when severity='Low' then 1 end) as Low,
from mytable where client =%CURRENT_USER_LOGIN%
group by client,dateadded, computername
Update(1):
In MySQL/MariaDB query the following doesn't much sense:
=%CURRENT_USER_LOGIN%
If substring you are searching for is CURRENT_USER_LOGIN then use
like '%CURRENT_USER_LOGIN%'
select client,dateadded, computername, severity,
count(case when severity='High' then 1 end) as High,
count(case when severity='Medium' then 1 end) as Medium,
-- comma mark ',' before 'from' !!! produces error
count(case when severity='Low' then 1 end) as Low,
from mytable where client =%CURRENT_USER_LOGIN%
group by client,dateadded, computername
Related
I'm triying to add quantities of fuels from two tables.
Payments table contains data about customers and dates.
Fuels table contains data about fuels recharged.
I need to add the litres by customer in 3 groups:
from yesterday until 46 days
47 days ago until 91
366 days ago until 410
Then I created this code:
The structure of the tables are:
TABLE payments
customer
id
date
AAA
001
2022-10-17
BBB
002
2021-10-01
CCC
003
2022-09-30
DDD
004
2022-07-10
DDD
005
2022-06-03
EEE
006
2021-09-27
EEE
007
2022-10-01
FFF
008
2021-08-31
TABLE fuels
id
litres
001
30
002
20
003
40
004
30
005
20
006
10
007
56
008
22
SELECT payments.customer,
SUM(CASE
WHEN ((TIMESTAMPDIFF(DAY, payments.date, now()) > 1) AND (TIMESTAMPDIFF(DAY, payments.date, now()) <= 46))
THEN fuels.litres
ELSE 0
END) AS "Quantity_d",
SUM(CASE
WHEN ((TIMESTAMPDIFF(DAY, payments.date, now()) > 46) AND (TIMESTAMPDIFF(DAY, payments.date, now()) <= 91))
THEN fuels.litres
ELSE 0
END) AS "Quantity_d2",
SUM(CASE
WHEN ((TIMESTAMPDIFF(DAY, payments.date, now()) > 365) AND (TIMESTAMPDIFF(DAY, payments.date, now()) <= 410))
THEN fuels.litres
ELSE 0
END) AS "Quantity_d3"
FROM payments
INNER JOIN fuels ON fuels.id=payments.id
WHERE payments.date > ADDDATE(now(),-411)
GROUP BY customer
The result is that Quantity_d1 is adding from yesterday until day 410, Quantity_d2 from 45 days ago until 410 and Quantity_d3 from 365 days ago until 410.
What am I doing wrong?
Thanks
Without knowing the table structures and relationships, I do find it odd that you're joining on payments.id and fuels.id as those both look like primary key fields. Is it possible that you are joining the wrong id fields, e.g. fuels.id = payments.fuel_id?
If I run your SQL without changes I get exactly what is specified:
customer
Quantity_d
Quantity_d2
Quantity_d3
AAA
30
0
0
BBB
0
0
20
CCC
40
0
0
DDD
0
0
0
EEE
56
0
10
If you still have issues, please explain!
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
I'm trying to display only the latest records based on latest date.
Here is my table
client dateadded computername title
abc 2019-02-02 12345a xyz
abc 2019-02-02 12345b xyz
abc 2019-02-02 12345c xyz
abc 2019-02-02 12345d xyz
abc 2019-02-09 12345e xyz
abc 2019-02-09 12345f xyz
abc 2019-02-09 12345g xyz
abc 2019-02-09 12345h xyz
abc 2019-02-09 12345j xyz
abc 2019-02-16 12345x xyz
abc 2019-02-16 12345s xyz
abc 2019-02-16 12345w xyz
abc 2019-02-16 12345bs xyz
My desired output is
abc 2019-02-16 12345x xyz
abc 2019-02-16 12345s xyz
abc 2019-02-16 12345w xyz
abc 2019-02-16 12345bs xyz
In WordPress, I have that WHERE client = %CURRENT_USER_LOGIN%
This is my code
SELECT computername
, title
FROM admin_mydb.s1
WHERE client = %CURRENT_USER_LOGIN%
order
by datescanned
This of course will display all records for the currently logged in user. OK.
Now the code I've tried.
SELECT computername
, title
FROM admin_mydb.s1
WHERE client = %CURRENT_USER_LOGIN%
and dateadded IN (SELECT max(dateadded) )
No luck!
Try
SELECT computername, title
FROM admin_mydb.s1
WHERE client = %CURRENT_USER_LOGIN%
and
dateadded IN (
SELECT max(dateadded)
FROM admin_mydb.s1
)
You can also use self join inplace of sub query.
My source table (wplott_wpkl_winner) contains the field "lottery_number" that carries 1 to 6 digit numbers and the corresponding "draw_date".
lottery_number | draw_date
==================================
0024 | 2018-11-10
4456 | 2018-11-10
3895 | 2018-11-10
4557 | 2018-11-10
4225 | 2018-11-10
2896 | 2018-11-10
3354 | 2018-11-10
1895 | 2018-11-10
78466 | 2018-11-10
998556 | 2018-11-10
My current MYSQL query is as below (I am trying to group the data into ranges)
select
count(case when wplott_wpkl_winner.lottery_number between 0 and 999 then 1 end) `0-999`,
count(case when wplott_wpkl_winner.lottery_number between 1000 and 1999 then 1 end) `1000-1999`,
count(case when wplott_wpkl_winner.lottery_number between 2000 and 2999 then 1 end) `2000-2999`,
count(case when wplott_wpkl_winner.lottery_number between 3000 and 3999 then 1 end) `3000-3999`,
count(case when wplott_wpkl_winner.lottery_number between 4000 and 4999 then 1 end) `4000-4999`,
count(case when wplott_wpkl_winner.lottery_number between 5000 and 5999 then 1 end) `5000-5999`,
count(case when wplott_wpkl_winner.lottery_number between 6000 and 6999 then 1 end) `6000-6999`,
count(case when wplott_wpkl_winner.lottery_number between 7000 and 7999 then 1 end) `7000-7999`,
count(case when wplott_wpkl_winner.lottery_number between 8000 and 8999 then 1 end) `8000-8999`,
count(case when wplott_wpkl_winner.lottery_number between 9000 and 9999 then 1 end) `9000-9999`
from wplott_wpkl_winner
where CHAR_LENGTH(wplott_wpkl_winner.lottery_number) = 4 AND wplott_wpkl_winner.draw_date > '2013-06-30'
It provides the below output
0-999 | 1000-1999 | 2000-2999 | 3000-3999 | 4000- 4999 .... etc
=====================================================================
1 | 1 | 1 | 2 | 3
However, I would like to get the output in the below format.
Range | Count
=======================
0-999 | 1
1000-1999 | 1
2000-2999 | 1
3000-3999 | 2
4000-4999 | 3
.
.
.
Any help is highly appreciated. I did search in SO for a similar answer but none of the answers helped my particular case.
Thanks in advance!
One approach uses a series of unions:
SELECT
`range`,
count
FROM
(
SELECT 1 AS pos, '0-999' AS `range`, COUNT(*) AS count
FROM wplott_wpkl_winner
WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999
UNION ALL
SELECT 2, '1000-1999', COUNT(*)
FROM wplott_wpkl_winner
WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999
UNION ALL
... -- fill in remaining ranges here
) t
ORDER BY pos;
Note that I introduce a computed column pos so that we may maintain the desired ordering of the ranges in the final output. Also, I removed the check on the CHAR_LENGTH of the lottery_number, since the conditional sums already handle this logic.
I have 2 Tables with following data:
TABLE 1 (dummy_daily)
Entry storenum busidate daily_budget
1 1 2017-07-01 4000
2 1 2017-07-02 3500
3 1 2017-07-03 2000
4 1 2017-07-04 6000
5 1 2017-07-05 1500
TABLE 2 (site_labour)
Lab_id storenum busidate lab_hour
1123 1 2017-07-01 128
1124 1 2017-07-02 103
1125 1 2017-07-03 114
1126 1 2017-07-04 108
1127 1 2017-07-05 118
This is my current query to combine the 2 tables that have the same date to give result of daily_budget and lab_hour
QUERY:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum JOIN
site_labour c ON b.storenum=c.storenum
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)
But my current query give me a wrong result :
Wrong Result of Current Query
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 128 1 2017-07-02
2000 128 1 2017-07-03
6000 128 1 2017-07-04
1500 103 1 2017-07-05
4000 103 1 2017-07-01
3500 103 1 2017-07-02
2000 103 1 2017-07-03
6000 103 1 2017-07-04
1500 103 1 2017-07-05
This data will continue until end of Actual 118
Expected Result
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 103 1 2017-07-02
2000 114 1 2017-07-03
6000 108 1 2017-07-04
1500 118 1 2017-07-05
You have missed one more table so its make confusion to create logic,
As per my understanding, I have created a SELECT statement. Please try this:-
SELECT
dummy_daily.daily_budget as Ideal, site_labour.lab_hour as Actual,
site_store.store_name, site_store.storenum, dummy_daily.busidate
FROM dummy_daily
JOIN site_store
ON dummy_daily.storenum = site_store.storenum
JOIN site_labour
ON dummy_daily.storenum = site_labour.storenum
WHERE (dummy_daily.storenum = 1)
AND (dummy_daily.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (site_labour.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (dummy_daily.busidate = site_labour.busidate);
Let's try the SQL below:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum AND b.busidate=a.busidate JOIN
site_labour c ON b.storenum=c.storenum AND b.busidate=c.busidate
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)