mysql between current year and current year + 6 months - mysql

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)

Related

MYSQL use GROUP BY to SUM timediff to get a total open time

I have multiple tables that I am having to join together in order to work out how long tickets have been open, I am using the following query (convoluted I know!):
SELECT DISTINCT u_db.environments.name AS Env_name, TIMEDIFF(u_db.tickets.close_date, u_db.tickets.created_date) AS Total_open_time
FROM u_db.tickets
INNER JOIN u_db.ticket_units
ON u_db.tickets.id = u_db.ticket_units.ticket_id
INNER JOIN u_db.units
ON u_db.ticket_units.unit_id = u_db.units.id
INNER JOIN u_db.locations
ON u_db.units.location_id = u_db.locations.id
INNER JOIN u_db.location_groups
ON u_db.locations.locations_group_id = u_db.location_groups.id
INNER JOIN u_db.environments
ON u_db.location_groups.environment = u_db.environments.id
WHERE u_db.tickets.created_date >= '2021-09-01 00:00:00'
AND u_db.tickets.created_date < '2021-10-01 00:00:00'
AND u_db.location_groups.id IN (50,17,46,45,48,49)
AND u_db.tickets.id IN (132357,132361,132372,132473);
Note: the close_date and created_date are stored as TIMESTAMP.
This generates the following output:
Env_name Total_open_time
GA 27:38:59
GA 01:43:51
GR 04:32:58
GR 49:39:19
However, I would like to group by Env_name and SUM the Total_open_times for each group, so my desired output is:
Env_name Total_open_time
GA 29:22:50
GR 54:12:17
I cannot seem to get the times to totals to sum when I group by Env_name, any suggestions on how to achieve this would be greatly appreciated!
Guess you can sum with difference in seconds, then convert seconds to time
SELECT u_db.environments.name AS Env_name, SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND, u_db.tickets.created_date, u_db.tickets.close_date))) AS Total_open_time
FROM u_db.tickets
INNER JOIN u_db.ticket_units
ON u_db.tickets.id = u_db.ticket_units.ticket_id
INNER JOIN u_db.units
ON u_db.ticket_units.unit_id = u_db.units.id
INNER JOIN u_db.locations
ON u_db.units.location_id = u_db.locations.id
INNER JOIN u_db.location_groups
ON u_db.locations.locations_group_id = u_db.location_groups.id
INNER JOIN u_db.environments
ON u_db.location_groups.environment = u_db.environments.id
WHERE u_db.tickets.created_date >= '2021-09-01 00:00:00'
AND u_db.tickets.created_date < '2021-10-01 00:00:00'
AND u_db.location_groups.id IN (50,17,46,45,48,49)
AND u_db.tickets.id IN (132357,132361,132372,132473)
GROUP BY Env_name

get sum of multiple values by months in one table

I have table with query :
SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName, TM.terminal_name,
CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) /
TOPUP.currency_rate, 2) AS decimal(18, 2)) AS
Top_Up_Value
FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP
INNER JOIN dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_currency AS CR ON TOPUP.currency_id = CR.currency_id
INNER JOIN dbo.cdf_cuid AS CU ON TOPUP.cu_id = CU.cu_id
INNER JOIN dbo.cdf_card_role AS CO ON CO.id = CU.card_role_id
INNER JOIN dbo.cdf_terminal_user_account AS UA ON UA.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_terminal AS TM ON TM.terminal_id = UA.terminal_id
INNER JOIN dbfastshosted.dbo.fh_sales_map AS MA ON MA.tu_log_id =
TOPUP.tu_log_id
LEFT OUTER JOIN dbfastshosted.dbo.fh_mf_new_card_logs AS NC ON
MA.nc_log_id = NC.nc_log_id
WHERE (ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) > 0)
and YEAR(TOPUP.tu_timestamp) = '2017'
AND month(TOPUP.tu_timestamp) = 1
AND TM.terminal_id = 7
GROUP BY TOPUP.tu_log_id,DATENAME(Month,TOPUP.tu_timestamp),
TM.terminal_name,
TOPUP.tu_credit, NC.initial_bal, TOPUP.currency_rate, CU.card_type_id;
MonthName Terminal name Top Up Value
------------------------------------------------------
January Terminal 1 100
January Terminal 1 200
January Terminal 3 150
Feb Terminal 1 250
Feb Terminal 1 160
March Terminal 2 120
March Terminal 3 100
and i would like to have total sums of top up value according to months which look like this:
MonthName Top Up Value
-----------------------------------
January 450
February 410
March 220
-----
Dec
as i am beginner in sql , i dont have idea how to do it. Really need help on these. Thanks!
SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName,
SUM(CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) /
TOPUP.currency_rate, 2) AS decimal(18, 2))) AS
Top_Up_Value
FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP
INNER JOIN dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id =
TU.terminal_user_id
look at your group by statement, the result you posted is displaying lesser number of columns then what you have in your query. Tweak it around and you will get your results
Try this
SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName,
SUM(CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) /
TOPUP.currency_rate, 2) AS decimal(18, 2))) AS
Top_Up_Value
FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP
INNER JOIN dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_currency AS CR ON TOPUP.currency_id = CR.currency_id
INNER JOIN dbo.cdf_cuid AS CU ON TOPUP.cu_id = CU.cu_id
INNER JOIN dbo.cdf_card_role AS CO ON CO.id = CU.card_role_id
INNER JOIN dbo.cdf_terminal_user_account AS UA ON UA.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_terminal AS TM ON TM.terminal_id = UA.terminal_id
INNER JOIN dbfastshosted.dbo.fh_sales_map AS MA ON MA.tu_log_id =
TOPUP.tu_log_id
LEFT OUTER JOIN dbfastshosted.dbo.fh_mf_new_card_logs AS NC ON
MA.nc_log_id = NC.nc_log_id
WHERE (ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) > 0)
and YEAR(TOPUP.tu_timestamp) = '2017'
AND month(TOPUP.tu_timestamp) = 1
AND TM.terminal_id = 7
GROUP BY DATENAME(Month,TOPUP.tu_timestamp)

Using two different measures in a having statement

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

mysql date_add 2 times in procedure

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.

How can I make more than one select queries in single select query.

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