Multiple rows into one field on GROUPED results Mysql - mysql

I have a query results like this.
status count
-----------------
OTHER 5
ATTENDED 74
I want result like this:
OTHERS ATTENDED
5 74
Note this is already a grouped result. So I am thinking, Group_Concat might not work.
Query:
SELECT CASE Rstat WHEN 1 THEN 'ATTENDED'
WHEN 2 THEN 'TENTATIVE'
WHEN 3 THEN 'REJECTED'
WHEN 4 THEN 'OUTSTANDING'
WHEN 6 THEN 'ACCEPTED' ELSE 'OTHER' END AS 'status',
COUNT(Rstat) AS 'count'
FROM `eStatus`
GROUP BY RSTAT
Any other ideas?

SELECT SUM(CASE WHEN Rstat = 1 THEN 1 ELSE 0 END) AS ATTENDED,
SUM(CASE WHEN Rstat = 2 THEN 1 ELSE 0 END) AS TENTATIVE,
SUM(CASE WHEN Rstat = 3 THEN 1 ELSE 0 END) AS REJECTED,
SUM(CASE WHEN Rstat = 4 THEN 1 ELSE 0 END) AS OUTSTANDING,
SUM(CASE WHEN Rstat = 6 THEN 1 ELSE 0 END) AS ACCEPTED,
SUM(CASE WHEN Rstat NOT IN (1,2,3,4,6) THEN 1 ELSE 0 END) AS OTHER
FROM eStatus

Related

MySQL- Count total records based on different values of same column

I have a table as follows
table_user
id name status flag country
====================================================================================================
1 AB 1 0 US
2 BC 1 0 UK
3 CD 0 0 IN
4 DE 3 0 BR
5 EF 3 0 UK
6 FG 2 0 IN
7 GH 4 0 IN
I want to count the no. of records where
status = 1 as totalactiverecords,
status = 0 as totalinactiverecords,
status = 3 as totalrecentactiverecords,
status = 2 as totalemailnotverifiedrecords,
status = 4 as totalemailverifiedrecords,
, and country is UK, all in a single SELECT statement.
Is there any specific way to do it?
I have thought of something like
SELECT COUNT(*) as totalactiverecords WHERE status=1
COUNT(*) as totalinactiverecordsWHERE status=0,
COUNT(*) as totalemailnotverifiedrecords WHERE status=2,
COUNT(*) as totalrecentactiverecords WHERE status=3,
COUNT(*) as totalemailverifiedrecords WHERE status=4
FROM table_user where country=IN
,but that does not seem to work.
You can try this query:
SELECT count(case status when '1' then 1 else null end) as totalactiverecords,
count(case status when '0' then 1 else null end) as totalinactiverecords,
count(case status when '2' then 1 else null end) as totalemailnotverifiedrecords,
count(case status when '3' then 1 else null end) as totalrecentactiverecords,
count(case status when '4' then 1 else null end) as totalemailverifiedrecords
FROM table_user where country='IN'
I usually use CASE WHEN in this kind of problem:
SELECT sum(case when status = 1 then 1 end) as totalactiverecords,
COUNT sum(case when status = 0 then 1 end) as totalinactiverecords,
COUNT sum(case when status = 2 then 1 end) as totalemailnotverifiedrecord,
COUNT sum(case when status = 3 then 1 end) as totalrecentactiverecords,
COUNT sum(case when status = 4 then 1 end) as totalemailverifiedrecords
FROM table_user where country=IN

Is there a way to include the sum of all cases?

Given SQL: Is there a way to bring in the total of the result set?
SELECT
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
FROM
Table1
WHERE
id = 2;
Result:
Open,Close
5,5
Desired Result:
Open,Close,Total
5,5,10
just add another case statement
SELECT
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
SUM(CASE
WHEN status IN (3, 4) THEN 1
ELSE 0
END) AS Total
FROM
Table1
WHERE
id = 2;
You can use a CTE:
WITH sumCase AS (
SELECT
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
FROM
Table1
WHERE
id = 2;)
SELECT Open,Close, Open + Close AS Total FROM Table1;
http://www.mysqltutorial.org/mysql-cte/
using sub-query
select open,close,open+close as total from
(
SELECT
SUM(CASE WHEN status = 3 THEN 1
ELSE 0
END ) AS Open,
SUM(CASE WHEN status = 4 THEN 1
ELSE 0
END) AS Close
FROM
Table1
WHERE id = 2 ) as T

Need help in Merging Two query for HP ALM

I have two different query in HP ALM, but i want to merge and get it into one. I am not that good in SQL query so I am facing hard time in merging the query.
Query 1: Getting the execution Count for the tester
Select
TESTCYCL.TC_ACTUAL_TESTER as 'Tester',
sum(case when TC_Status In('Blocked','Passed','Failed','Not Completed') then 1 else 0 end) as 'Total',
sum(case when TC_Status = 'Passed' then 1 else 0 end) as 'Pass',
sum(case when TC_Status = 'Failed' then 1 else 0 end) as 'Fail',
sum(case when TC_Status = 'Blocked' then 1 else 0 end) as 'Blocked',
sum(case when TC_Status In('Not Completed','Defferred','N/A') then 1 else 0 end) as 'Others'
From TESTCYCL
Where
TESTCYCL.TC_EXEC_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
And
TESTCYCL.TC_ACTUAL_TESTER in ('Username1')
Group by TC_ACTUAL_TESTER
Query 2: Getting the Defect raise by the tester
SELECT
BG_DETECTED_BY,
Sum(case when BG_Status Not in ('Closed','Defect Resolved','Rejected')then 1 else 0 end) as 'Defect Raised'
FROM BUG
Where BUG.BG_DETECTED_BY in ('username1')
AND BUG.BG_DETECTION_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
Group by BG_DETECTED_BY
I have tried inner join/ Left Join but the count of the defect raised by the user is not matching
Query3 : That i have tried:
Select
TESTCYCL.TC_ACTUAL_TESTER as 'Tester',
sum(case when TC_Status In('Blocked','Passed','Failed','Not Completed') then 1 else 0 end) as 'Total',
sum(case when TC_Status = 'Passed' then 1 else 0 end) as 'Pass',
sum(case when TC_Status = 'Failed' then 1 else 0 end) as 'Fail',
sum(case when TC_Status = 'Blocked' then 1 else 0 end) as 'Blocked',
sum(case when TC_Status In('Not Completed','Defferred','N/A') then 1 else 0 end) as 'Others',
Sum(case when BG_Status Not in ('Closed','Defect Resolved','Rejected')then 1 else 0 end) as 'Defect Raised'
From TESTCYCL
Left Join BUG
on TESTCYCL.TC_ACTUAL_TESTER = BUG.BG_DETECTED_BY
AND BUG.BG_DETECTION_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
Where TESTCYCL.TC_EXEC_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
And TESTCYCL.TC_ACTUAL_TESTER in ('username1')
Group by TC_ACTUAL_TESTER
Out is mentioned as below:
Expected Output:
Tester Total Execution Passed Failed ... Defect Raised
A 5 3 2 10
Actual Output:
Tester Total Execution Passed Failed ... Defect Raised
A 56 3 2 45
The problem is you are doing the COUNT() over the product cartesian. instead of concatenating the result.
Right now you are doing
COUNT(A*B) instead of COUNT(A) || COUNT(B)
general example, if you have two queries
SELECT * FROM A (ex: 10 rows)
SELECT * FROM B (ex: 10 rows)
What you need is:
SELECT temp1.*, temp2.*
FROM (SELECT * FROM A) as temp1
JOIN (SELECT * FROM B) as temp2
ON temp1.ID = temp2.ID

Calculate date difference in sql for specific year

I have two dates, say start_date is 20141215 and end_date = 20150115. I would like to use SQL DATEDIFF to only count the dates within the year 2015 which I will specify in the query. Here is the current SQL I have written:
SELECT COUNT(leave_id),
sum(case when leave_status = 1 then 1 else 0 end) pending,
sum(case when leave_status = 2 then 1 else 0 end) declined,
sum(case when leave_status = 3 then 1 else 0 end) approved,
sum(case when leave_status = 4 then 1 else 0 end) rostered,
SUM(DATEDIFF(end_date, start_date)+1) as datetotals
FROM employee_leave WHERE
((YEAR(start_date) = :year) OR (YEAR(end_date) = :year))
AND employee_id = :emp_id
Thanks
You need to fix datediff() to only consider dates during the year. I think this does what you want:
SELECT COUNT(leave_id),
sum(case when leave_status = 1 then 1 else 0 end) pending,
sum(case when leave_status = 2 then 1 else 0 end) declined,
sum(case when leave_status = 3 then 1 else 0 end) approved,
sum(case when leave_status = 4 then 1 else 0 end) rostered,
SUM(DATEDIFF(least(end_date, date(concat_ws('-', :year, 12, 31))),
greatest(start_date, date(concat_ws('-', :year, 1, 1)))
) + 1) as datetotals
FROM employee_leave
WHERE ((YEAR(start_date) = :year) OR (YEAR(end_date) = :year)) AND
employee_id = :emp_id
Make it a AND condition rather like
WHERE
((YEAR(start_date) = :year) AND (YEAR(end_date) = :year))

How could I check the value of an aggregation function inside the same query?

This MySQL query gives me this error 'Unknown column 'winnings' in 'field list'
SELECT
o.user_id,
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no,
sum(case when o.result = 1 then 1 else 0 end) as winnings,
sum(case when o.result = 2 then 1 else 0 end) as loses,
sum(case when winnings = 10 then 0.5 else 0 end) as counter
FROM `odds_tahminler` o
I know that winnings is the value of the sum() aggregation function, But is there any way to check the winnings value within the query?
You can't use an aggregated column inside the select. However you can use a subquery to obtain the counter value after all the aggregated columns have been computed.
How is the counter value calculated? I assumed that the counter should be (winnings - 10) / 2 if there's at least 10 winnings and 0 otherwise. In that case you can obtain it with this query
SELECT O.*,
GREATEST( (O.winnings - 10) / 2, 0) as counter
FROM
(
SELECT u.username,
o.user_id,
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no,
sum(case when o.result = 1 then 1 else 0 end) as winnings,
sum(case when o.result = 2 then 1 else 0 end) as loses
FROM `odds_tahminler` o
) as O
you can try:
SELECT
o.user_id,
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no,
sum(case when o.result = 1 then 1 else 0 end) as winnings,
sum(case when o.result = 2 then 1 else 0 end) as loses,
sum(case when winnings = 10 then 0.5 else 0 end) as counter
FROM `odds_tahminler` o
GROUP BY o.user_id
HAVING counter>2