i want to use Date_add 2 times in a procedure, but it returns 0 rows (while it should return rows)
Here is the procedure
select av.*, ap.*,c.* from tbl_available av
left join tbl_appointment ap on av.avHours = ap.appointmenttime
and ap.calendarid = kalenderId
and ap.appointmentdate = DATE_ADD(dag, INTERVAL 6 DAY)
left join tbl_client c on ap.clientid = c.clientid
where av.avCalendarId = KalenderId
and av.avDays = DayOfweek(DATE_ADD(dag, INTERVAL 6 DAY))
order by avHours;
it works without the date_add
thanks in advance!
//edit
What i have now:
select av.*, ap.*,c.*, ab.absentid from tbl_available av
left join tbl_appointment ap on av.avHours = ap.appointmenttime
and ap.calendarid = kalenderId
and ap.appointmentdate BETWEEN dag AND DATE_ADD(dag, INTERVAL 6 DAY)
and (av.avDays = DayOfweek(ap.appointmentdate) OR ap.appointmentdate IS NULL)
left join tbl_client c on ap.clientid = c.clientid
left join tbl_absent ab on av.avHours = ab.ababsent
and ab.abHoliday = dag
and ab.abCalendarID = kalenderId
where av.avCalendarId = kalenderId
order by avDays,avHours;
But the ab.absentid is not fetched, why is that? :(
I'm not sure exactly what you want, but for the whole week, try something like:
select av.*, ap.*,c.* from tbl_available av
left join tbl_appointment ap on av.avHours = ap.appointmenttime
and ap.calendarid = kalenderId
and ap.appointmentdate BETWEEN dag AND DATE_ADD(dag, INTERVAL 6 DAY)
and (av.avDays = DayOfweek(ap.appointmentdate) OR ap.appointmentdate IS NULL)
left join tbl_client c on ap.clientid = c.clientid
where av.avCalendarId = KalenderId
order by avHours;
I'm using BETWEEN to specify the date range for ap.appointmentdate. The av.avDays is changed to either correlate to ap.appointmentdate or also show rows with no appointment (assumed this behavior because you have a LEFT JOIN on tbl_appointment). I have left off DayOfweek(dag) ... because you are looking at a whole week so this is redundant.
Related
I am facing 1 issue with MySql query:
select sum(h.total_rooms) as total_rooms from reservation_details as rd LEFT JOIN hotels as h ON rd.hotel_id = h.id left join `chains` as `c` on `c`.`id` = `h`.`chain_id` where YEAR(rd.created_at) = 2016 and MONTH(rd.created_at) = 5 and rd.status >= 50 and h.chain_id = 2 GROUP BY rd.hotel_id
Above query returns :
total_rooms
48216
7700
13250
But
I need sum of the query.
Try to use sum() function like this and use ROLLUP with your query.
select sum(h.total_rooms) as total_rooms from reservation_details as rd
LEFT JOIN hotels as h ON rd.hotel_id = h.id
left join `chains` as `c` on `c`.`id` = `h`.`chain_id`
where YEAR(rd.created_at) = 2016 and MONTH(rd.created_at) = 5
and rd.status >= 50 and h.chain_id = 2 GROUP BY rd.hotel_id
WITH ROLLUP
I am including the code I am trying to use. What I want is to get a count of encounter id's for each doctor. I want to capture patients who have had at least 2 visits or who have had one preventive visit. I don't know how to make this work using the having statement. thanks for any assistance.
SELECT e.doctorID, COUNT(DISTINCT e.encounterID) AS VisitCount
FROM enc e
JOIN users u ON e.patientID = u.uid
Left JOIN diagnosis d ON e.encounterID = d.encounterID
LEFT JOIN items it ON d.itemID = it.itemID
LEFT JOIN itemdetail id ON it.itemID = id.itemID
WHERE e.encType = 1 AND e.status = 'CHK' AND e.deleteFlag = 0 AND
e.date BETWEEN DATE_ADD((LAST_DAY(DATE_ADD(CURDATE(),INTERVAL -2 MONTH))), INTERVAL 1 DAY) AND LAST_DAY(DATE_ADD(CURDATE(),INTERVAL -1 MONTH))
AND FLOOR(DATEDIFF(NOW(),u.ptdob)/365.25) >= 18 AND e.doctorID = e.resourceID
GROUP BY e.doctorID, id.value
HAVING
COUNT(e.patientid)>=2 OR
id.value in ('Z00.00', 'Z00.01')
You dont need to include id.value in grp by clause. Try to give valid condition in having clause with id.value
select e.doctorID,
COUNT(distinct e.encounterID) as VisitCount from enc e join users u on e.patientID = u.uid
left join diagnosis d on e.encounterID = d.encounterID left join items it on
d.itemID = it.itemID left join itemdetail id on
it.itemID = id.itemID where e.encType = 1 and e.status ='CHK' and e.deleteFlag = 0 and
e.date between DATE_ADD((LAST_DAY(DATE_ADD(CURDATE(), INTERVAL - 2 MONTH))), INTERVAL 1 DAY)
and LAST_DAY(DATE_ADD(CURDATE(), INTERVAL - 1 MONTH))
and
FLOOR(DATEDIFF(NOW(), u.ptdob) / 365.25) >= 18
and e.doctorID = e.resourceID group by e.doctorID
having COUNT(e.patientid) >= 2
or sum(case when id.value in ('Z00.00','Z00.01') then 1
else 0 end)>=1
How can I return the row count from the query in this stored procedure? I have been trying to implement solutions found on other stack overflow posts that are similar but each result in the output being the columns with the count for each individual record. (ie, one column will output 1,1,1,1,1 for every row...)
CREATE PROCEDURE stored_proc ()
BEGIN
SET #start_date = LAST_DAY(NOW() - INTERVAL 2 MONTH) + INTERVAL 1 DAY;
SET #end_date = LAST_DAY(NOW() - INTERVAL 1 MONTH);
SELECT users.*, count(DISTINCT mr_rev.id) AS prev_reg
FROM users
INNER JOIN gr_user_group
ON gr_user_group.user_id = wp_users.ID
AND gr_user_group.group_id != 22
AND gr_user_group.group_id = 5
INNER JOIN usermeta ON users.ID = usermeta.user_id
INNER JOIN mr_rev
ON users.ID = mr_rev.users_id AND mr_rev.mr_rev_types_id = 2
INNER JOIN mr_rev mrr
ON users.ID = mrr.users_id
AND mrr.mr_rev_types_id = 2
AND mrr.created_on >= #start_date
AND mrr.created_on <= #end_date
INNER JOIN mr_revs_meta
ON mr_rev.id = mr_revs_meta.mr_rev_id
AND mr_revs_meta.meta_value > #end_date
AND mr_revs_meta.created_at < #end_date
WHERE users.ID NOT IN ('101', '102')
GROUP BY users.ID
HAVING prev_reg > 15;
END;
Is there an efficient way to return the total row count?
EDIT: Return count in Stored Procedure one of the posts I unsuccessfully implemented.
How about wrapping the query in an another select:
SELECT COUNT(*) FROM
(SELECT users.*, count(DISTINCT mr_rev.id) AS prev_reg
FROM users
INNER JOIN gr_user_group
ON gr_user_group.user_id = wp_users.ID
AND gr_user_group.group_id != 22
AND gr_user_group.group_id = 5
INNER JOIN usermeta ON users.ID = usermeta.user_id
INNER JOIN mr_rev
ON users.ID = mr_rev.users_id AND mr_rev.mr_rev_types_id = 2
INNER JOIN mr_rev mrr
ON users.ID = mrr.users_id
AND mrr.mr_rev_types_id = 2
AND mrr.created_on >= #start_date
AND mrr.created_on <= #end_date
INNER JOIN mr_revs_meta
ON mr_rev.id = mr_revs_meta.mr_rev_id
AND mr_revs_meta.meta_value > #end_date
AND mr_revs_meta.created_at < #end_date
WHERE users.ID NOT IN ('101', '102')
GROUP BY users.ID
HAVING prev_reg > 15 ) a;
I need to make query where is where condition date between current year and current year + 6 months
SELECT VOZ.prez_vozac,VOZ.ime_vozac,LOD.naz_lok,LDO.naz_lok,V.datum
FROM voznja AS V
INNER JOIN vozac AS VOZ ON V.sif_vozac = VOZ.sif_vozac
INNER JOIN lokacija AS LOD ON V.sif_lok_od = LOD.sif_lok
INNER JOIN lokacija AS LDO ON V.sif_lok_do = LDO.sif_lok
INNER JOIN vozilo AS VOZI ON V.regbr = VOZI.regbr
INNER JOIN teret AS T ON V.sif_teret = T.sif_teret
WHERE T.tezina <= 5 AND V.datum BETWEEN ***CURENT YEAR*** AND ***CURRENT YEAR + 6 MONTHS ***
Assuming you mean current date and six months from now...
SELECT VOZ.prez_vozac,VOZ.ime_vozac,LOD.naz_lok,LDO.naz_lok,V.datum
FROM voznja AS V
INNER JOIN vozac AS VOZ ON V.sif_vozac = VOZ.sif_vozac
INNER JOIN lokacija AS LOD ON V.sif_lok_od = LOD.sif_lok
INNER JOIN lokacija AS LDO ON V.sif_lok_do = LDO.sif_lok
INNER JOIN vozilo AS VOZI ON V.regbr = VOZI.regbr
INNER JOIN teret AS T ON V.sif_teret = T.sif_teret
WHERE T.tezina <= 5 AND V.datum BETWEEN CURDATE()
AND DATE_ADD(CURDATE(), INTERVAL 6 MONTH)
I have to write a query where, I need to fetch records for last week, last month, and for all.
For this problem I wrote 3 diffrent queries (for last week, for last month and for all)
For Weekly Info :-
SELECT bu.brand_name AS 'Brand_Name',COUNT(s.unique) AS '# Item Sold',SUM(s.price) AS 'Total_Price'
FROM item_details s
LEFT JOIN sales_order o ON s.fk_sales_order = o.id_sales_order
LEFT JOIN customer_info AS c ON o.fk_customer_id = c.id_customer
LEFT JOIN simple_details cc ON s.unique = cc.unique
LEFT JOIN config_details cf ON cc.fk_config_id = cf.config_id
LEFT JOIN brand_details cb ON cf.fk_brand_id = cb.brand_id
LEFT JOIN category_details ctc ON cf.fk_category_id = ctc.category_id
LEFT JOIN gender_details g ON cf.fk_gender_id = g.gender_id
LEFT JOIN buyers AS bu ON bu.brand_name = cb.name AND bu.category_name = ctc.name AND bu.gender = g.name
WHERE bu.buyers = 'xyz' AND DATE_FORMAT(o.created_date,'%Y-%m-%d') >= #weekstartdate AND DATE_FORMAT(o.created_date,'%Y-%m-%d') <= #weekenddate
GROUP BY bu.brand_name
For Monthly Info :-
SELECT bu.brand_name AS 'Brand_Name',COUNT(s.unique) AS '# Item Sold',SUM(s.price) AS 'Total_Price'
FROM item_details s
LEFT JOIN sales_order o ON s.fk_sales_order = o.id_sales_order
LEFT JOIN customer_info AS c ON o.fk_customer_id = c.id_customer
LEFT JOIN simple_details cc ON s.unique = cc.unique
LEFT JOIN config_details cf ON cc.fk_config_id = cf.config_id
LEFT JOIN brand_details cb ON cf.fk_brand_id = cb.brand_id
LEFT JOIN category_details ctc ON cf.fk_category_id = ctc.category_id
LEFT JOIN gender_details g ON cf.fk_gender_id = g.gender_id
LEFT JOIN buyers AS bu ON bu.brand_name = cb.name AND bu.category_name = ctc.name AND bu.gender = g.name
WHERE bu.buyers = 'xyz' AND DATE_FORMAT(o.created_date,'%Y-%m-%d') >= #monthstartdate AND DATE_FORMAT(o.created_date,'%Y-%m-%d') <= #monthenddate
GROUP BY bu.brand_name
For All Records :-
SELECT bu.brand_name AS 'Brand_Name',COUNT(s.unique) AS '# Item Sold',SUM(s.price) AS 'Total_Price'
FROM item_details s
LEFT JOIN sales_order o ON s.fk_sales_order = o.id_sales_order
LEFT JOIN customer_info AS c ON o.fk_customer_id = c.id_customer
LEFT JOIN simple_details cc ON s.unique = cc.unique
LEFT JOIN config_details cf ON cc.fk_config_id = cf.config_id
LEFT JOIN brand_details cb ON cf.fk_brand_id = cb.brand_id
LEFT JOIN category_details ctc ON cf.fk_category_id = ctc.category_id
LEFT JOIN gender_details g ON cf.fk_gender_id = g.gender_id
LEFT JOIN buyers AS bu ON bu.brand_name = cb.name AND bu.category_name = ctc.name AND bu.gender = g.name
WHERE bu.buyers = 'xyz'
GROUP BY bu.brand_name
and these are working fine (giving currect output).
But problem is that, I have to merge these three queries in single one.
Where output should be as
Brand name, item_sold(week), total_price(week),item_sold(month), total_price(month),item_sold(all), total_price(all)
How can I write this query?
Without looking deep into your code, the obvious solution would be
SELECT
all.brand_name
pw.items_sold items_sold_week
pw.total_price total_price_week
pm.items_sold items_sold_month
pm.total_price total_price_month
all.items_sold items_sold_all
all.total_price total_price_all
FROM
(your all-time select) all
JOIN (your per-month select) pm ON all.brand_name = pm.brand_name
JOIN (your per-week select) pw ON all.brand_name = pw.brand_name
Though you probably should rethink your entire approach and make sure whether you really want that kind of logic in a DB layer or it is better to be in your application.
You could use case to limit aggregates to a subset of rows:
select bu.brand_name
, count(case when date_format(o.created_date,'%Y-%m-%d') >= #weekstartdate
and date_format(o.created_date,'%Y-%m-%d') <= #weekenddate
then 1 end) as '# Item Sold Week'
, sum(case when date_format(o.created_date,'%Y-%m-%d') >= #weekstartdate
and date_format(o.created_date,'%Y-%m-%d') <= #weekenddate
then s.price end) as 'Total_Price Week'
, count(case when date_format(o.created_date,'%Y-%m-%d') >= #monthstartdate
and date_format(o.created_date,'%Y-%m-%d') <= #monthstartdate
then 1 end) as '# Item Sold Month'
, ...
If all three selects uses the same fields in the results, you can UNION them:
SELECT *
FROM (SELECT 1) AS a
UNION (SELECT 2) AS b
UNION (SELECT 3) AS c
If you need to tell week/mon/all records from each other - just add constant field containing "week" or "mon"
You cam use the UNION.keyword between the queries to bundle them.together BUT tje column types and sequence must be the same in all queries. You could add an identifier to each set