Show the status of daily file loads from mysql database - mysql

I am working on a project where we receive some files daily which are loaded into the database, their status is recorded into meta-data.
For e.g.,
There are 2 different clients from which we get three different type of files, type_a, type_b and type_c.
CLIENT_MASTER
----------------------
client_id|client_name
1|xxx
2|yyy
File status is stored in FILE_MASTER table
FILE_MASTER
-----------------------------------
file_key|client_id|filename|status
1|1|type_a_2010-10-07.csv|12
2|1|type_b_2010-10-07.csv|12
3|1|type_c_2010-10-07.csv|12
4|2|type_a_2010-10-07.csv|12
5|2|type_b_2010-10-07.csv|12
The status keys are stored in STATUS_MASTER table
STATUS_MASTER
-------------------
status_key|status
12|Completed
I want to develop a dashboard showing status of daily file loads, in the below format -
Client|type_a|type_b|type_c
xxx|Yes|Yes|Yes
yyy|Yes|Yes|No
Any help on this would be much appreciated.

This query will return result for current date:
SELECT cm.client_name,
CASE WHEN v.type_a_count > 0 THEN 'Yes' ELSE 'No' END AS type_a,
CASE WHEN v.type_b_count > 0 THEN 'Yes' ELSE 'No' END AS type_b,
CASE WHEN v.type_c_count > 0 THEN 'Yes' ELSE 'No' END AS type_c
FROM client_master cm
LEFT OUTER JOIN (
SELECT fm.client_id,
SUBSTRING(fm.client_name, 8, 10) AS file_date,
SUM(CASE WHEN SUBSTRING(fm.client_name, 1, 6) = 'type_a' THEN 1 ELSE 0 END) AS type_a_count,
SUM(CASE WHEN SUBSTRING(fm.client_name, 1, 6) = 'type_b' THEN 1 ELSE 0 END) AS type_b_count,
SUM(CASE WHEN SUBSTRING(fm.client_name, 1, 6) = 'type_c' THEN 1 ELSE 0 END) AS type_c_count
FROM file_master fm
WHERE SUBSTRING(fm.client_name, 8, 10) = CURDATE()
AND fm.status = 12
GROUP BY fm.client_id, SUBSTRING(fm.client_name, 8, 10)
) v ON cm.client_id = v.client_id

Try this query
SELECT c.name,
IF(FIND_IN_SET('type_a', GROUP_CONCAT(SUBSTRING_INDEX(f1.filename,'_',2))), 'Yes', 'No') as type_a,
IF(FIND_IN_SET('type_b', GROUP_CONCAT(SUBSTRING_INDEX(f1.filename,'_',2))), 'Yes', 'No') as type_b,
IF(FIND_IN_SET('type_c', GROUP_CONCAT(SUBSTRING_INDEX(f1.filename,'_',2))), 'Yes', 'No') as type_c
FROM client_master c
JOIN file_master f1 ON(f1.client_id = c.client_id)
JOIN status_master sm ON(sm.status = f1.status)
WHERE sm.status='Completed' AND SUBSTRING_INDEX(f1.filename,'_',-1) = CURDATE()
GROUP BY c.id

Related

SQL query to DQL

I have a big query and I want to add a subquery to get the availability of accommodation-rooms.
This is the SQL subquery:
,(SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as available
FROM
(
SELECT count(rtd.room_type_id) as _days
FROM room_type_day as rtd
WHERE rtd.date IN ('2018-06-20', '2018-06-21', '2018-06-22')
GROUP BY rtd.room_type_id
HAVING COUNT(rtd.room_type_id) = 3
) as sub) as availability
Can anyone tell me how to convert this SQL in DQL?
Thak you
EDIT
I try with this changes but the response is null every time:
,(SELECT (CASE WHEN count(rtd.roomType) > 0 THEN 'yes' ELSE 'no' END)
FROM AppBundle:RoomTypeDayCancelationConditionAccommodation as RTDCCA2
LEFT JOIN RTDCCA2.roomTypeDay as rtd
WHERE rtd.date IN ('2018-06-20', '2018-06-21', '2018-06-22')
GROUP BY rtd.roomType
HAVING COUNT(rtd.roomType) = 3
) as disponible

Appending data from different tables - mysql

I have the following tables Job description and Candidate.
Each job description can be related to n candidates. The candidates are obtained from different sources(a, b, c d) - candidate.source.
I want a single query to list me the JD ids, with count of candidates for each source for each JD, as shown below:
JD id | candidate name | count of candidates - source a | count of candidates - source b | count of candidates - source | c..............
Try using the query as a template:
select
JobDescriptionName,
SUM(ACount) CountOfCandidatesOfA ,
SUM(BCount) CountOfCandidatesOfB,
SUM(CCount) CountOfCandidatesOfC ,
SUM(DCount) CountOfCandidatesOfD
from
( select JobDescriptionID, (CASE WHEN Source = 'a' THEN 1 ELSE 0 END) AS ACount,
(CASE WHEN Source = 'b' THEN 1 ELSE 0 END) AS BCount,
(CASE WHEN Source = 'c' THEN 1 ELSE 0 END) AS CCount,
(CASE WHEN Source = 'd' THEN 1 ELSE 0 END) AS DCount
from Candidate) AS DerivedCandidate
inner join JobDescription ON JobDescription.JobDescriptionID = DerivedCandidate.JobDescriptionID group by JobDescriptionID;
I know there is already an accepted answer but in the effort of learning myself more about SQL here is an alternative way applying the case directly in the initial select without the sub-select:
Select
jd.id,
jd.name,
sum(case when c.source = 'a' then 1 else 0 end) as sourceA,
sum(case when c.source = 'b' then 1 else 0 end) as sourceB,
sum(case when c.source = 'c' then 1 else 0 end) as sourceC,
sum(case when c.source = 'd' then 1 else 0 end) as sourceD
from JobDescription as jd
join Candidate as c on c.jobId = jd.id
group by jd.id, jd.name
SQL Fiddle Demo
SELECT JD id, COUNT(Candidate), source
FROM table1
GROUP BY JD id,source

mysql sum case not working

I am working on a mysql query using sum case. The query works for some of the cases but not for one case.
select orders.orderid, campers.description, dealers.name, orders.act_delivery,
sum(case orderitems.std
when 0 then
(case when orderitems.price = 0 then retail_price.price * orderitems. qty else orderitems.price * orderitems.qty end)
when 1 then
(case when orderitems.origqty = 0 then
0 else
(case when orderitems.price = 0
then retail_price.price * (orderitems.qty - orderitems.origqty)
else orderitems.price * (orderitems.qty - orderitems.origqty)
end)
end)
when 2 then
0
else
(case when orderitems.deletedprice = 0 then retail_price.price * orderitems. qty else orderitems.deletedprice * orderitems.qty end)
end) as retail
from orderitems, orders, dealers, campers, products, retail_price
where orderitems.orderid = orders.orderid
and orderitems.productid = products.prodid
and orderitems.productid =retail_price.prodid
and orders.dealerid = retail_price.dealer
and orders.dealerid = dealers.dealerid
and orders.camper = campers.camperid
and act_delivery > 0
and orderitems.std in (0, 1, 2)
and orders.dealerid not in (1, 9, 10, 12,15,16)
and spares_od = 0
and orders.act_delivery > '2016-10-01'
and orders.act_delivery <= '2016-10-31'
group by orders.orderid,orders.dealerid, orders.act_delivery order by orders.act_delivery, orders.orderid
The problem occurs when orderitems.std = 3. In either case I cannot get a result.
All other cases appear to be working correctly.

Is it possible to use SUM - CASE - IF together?

When I try to use this:
SUM(CASE WHEN IF(SUM(CASE WHEN "b.count_students_status" = 1 THEN 1 ELSE 0 END) >= 1, 1, 0) = 1 THEN 1 ELSE 0 END)
It says Query Error: Improper usage of Group Function
Below is the complete code:
SELECT
"a.batch" AS Batch,
SUM(
CASE
WHEN IF(
SUM(
CASE
WHEN "b.count_students_status" = 1
THEN 1
ELSE 0
END
) >= 1,
1,
0
) = 1
THEN 1
ELSE 0
END
) AS Payments_Not_Received
FROM
"DBU - Complete"
WHERE "a.suspended" = 'no'
GROUP BY "a.batch"
I wanted to convert the status to 1, if there are multiple occurrences and then sum the total occurrences.
Any help please - I am using ZOHO to build the query?
You're trying to reference the result of the GROUP BY before is has run. Try something like this:
select t.BATCH,
sum(case when t.pnr >=1 then 1 else 0 end) as Payments_Not_Received
from
(
select
a.batch as BATCH,
SUM(CASE WHEN "b.count_students_status" = 1 THEN 1
ELSE 0
END) as pnr
from yourTable a
WHERE a.suspended = 'no'
group by a.batch
) t
group by t.BATCH;

Sum record between two dates for different users in mysql query Issue

I have two tables lead and lead_details. There is a field status. If status=1 is open, status=2 is close and status=3 is not specified.
I want to find sum of all Open,close and not specified for each user/agent.
Here is what I tried but it give me wrong data
select agent_id,
type,
status,
created_date,
category_id,
sum(case when status = 2 then val else 0 end) as closed1,
sum(case when status = 1 then val else 0 end) as opened1,
sum(case when status = 3 then val else 0 end) as notspecefied1
from ( select l.agent_id,
l.type,
ld.category_id,
l.status,
l.created_date,
count(*) as val
from crm_leads l,
crm_leads_details ld
where l.id=ld.lead_id AND
status in (2, 1, 3)
GROUP BY status, agent_id
) t
WHERE created_date BETWEEN '2013-8-2' AND '2013-9-2'
GROUP BY agent_id
You need to put the WHERE created_date clause in the subquery.
select agent_id,type,status,created_date,category_id,
sum(case when status = 2 then val else 0 end) as closed1,
sum(case when status = 1 then val else 0 end) as opened1,
sum(case when status = 3 then val else 0 end) as notspecefied1
from ( select l.agent_id,l.type,ld.category_id,l.status,l.created_date,
count(*) as val from crm_leads l JOIN crm_leads_details ld
ON l.id=ld.lead_id
WHERE created_date BETWEEN '2013-8-2' AND '2013-9-2' AND status in (2, 1, 3)
GROUP BY status, agent_id ) t
GROUP BY agent_id
Note that the created_date in the result will just be a randomly selected date in the period for each agent.