Represent MYSQL result with specific condition - mysql

i need help for represent my query result into specific report with condition
Here's my query result
after get the result i need represent it as report with some condition, it would be like this:
City A - Company A
City A - Company B
City A - Company C - Service A
I've no idea how to do it since I'm really new to MySQL.
I want to put the query into PHP.
Thanks in Advance

You can do it this way:
City A - Company A:
select pick_date, sum(Weight),
sum(case when site = 'SiteA' then 1 else 0 end) as SiteA,
sum(case when site = 'SiteB' then 1 else 0 end) as SiteB,
sum(case when site = 'SiteC' then 1 else 0 end) as SiteC
From tbl
where company_name = 'CompanyA'
group by pick_date
For City A - Company B, replace 'CompanyA' with CompanyB in the query
For Company C - Service A:
select pick_date, sum(Weight),
sum(case when site = 'SiteA' then 1 else 0 end) as SiteA,
sum(case when site = 'SiteB' then 1 else 0 end) as SiteB,
sum(case when site = 'SiteC' then 1 else 0 end) as SiteC
From tbl
where company_name = 'CompanyC' and service_name = 'ServiceA'
group by pick_date

This should do the trick. If your original query is also from SQL, put it into a temp table, and then using the following query will get you 3 answers.
SELECT
pickup_date
,SUM( CASE WHEN [site] = 'SiteA' THEN 1 ELSE 0 END) as [SiteA]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteB]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteC]
FROM
tbl
WHERE
[city] = 'CityA'
AND
[company_name] = 'CompanyA'
GROUP BY
pickup_date
SELECT
pickup_date
,SUM( CASE WHEN [site] = 'SiteA' THEN 1 ELSE 0 END) as [SiteA]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteB]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteC]
FROM
tbl
WHERE
[city] = 'CityA'
AND
[company_name] = 'CompanyB'
GROUP BY
pickup_date
SELECT
pickup_date
,SUM( CASE WHEN [site] = 'SiteA' THEN 1 ELSE 0 END) as [SiteA]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteB]
,SUM(CASE WHEN [site] = 'SiteB' THEN 1 ELSE 0 END) as [SiteC]
FROM
tbl
WHERE
[city] = 'CityA'
AND
[company_name] = 'CompanyC'
AND
[service_name] = 'ServiceA'
GROUP BY
pickup_date

Related

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

Grouping data by type and displaying

I have a table called 'product', with a 'created_at' date, and a 'type' which is a varchar.
Essentially what I want is an output of the COUNT(*) of each product for every day, for all of the product types. I know that there are only 5 different values for 'type', so that would narrow it down a bit with the query I imagine.
Example:
I have been playing around with this with no success, can't seem to wrap my mind around it. Any ideas?
Try this query
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
created_at
GROUP BY created_at
For specific date
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
created_at
WHERE created_at = '2015-12-21'
GROUP BY created_at
With date format
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
DATE_FORMAT(created_at,'%Y/%m/%d') AS created_date
GROUP BY created_at
HAVING created_date = '2015/12/21'

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))

Counting number of Sales from two tables

I am using SUM ( CASE WHEN ) to count number of Yes and No, it work fine.
I am havin problem counting number of matching mobile number from two tables. It dont seem to be counting correctly.
There is a MobileNO field in dairy table and mobile field in the sales table
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
(SELECT SUM(CASE WHEN D.MobileNo = S.mobile THEN 1 ELSE 0 END) from sales as S) as Sales,
COUNT(*) as TOTAL FROM dairy as D
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S ON D.MobileNo = S.mobile
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC