SQL: Display associated value based on Min and Max values - sql

I have tblDump which has ~52,000 records. It is a list of IDs with corresponding dosing times. The relevant variables are SubjectNumber, RECDATE, RECTIME, FoodType. Each patient doses twice a day, so there is a Min(RECTIME) and Max(RECTIME) per RECDATE. To display this, I have this so far:
SELECT tblDump.SubjectNumber, tblDump.RECDATE,
Min(RECTIME) As MornDose, Max(RECTIME) As EveDose
FROM tblDump
GROUP BY tblDump.SubjectNumber, tblDump.RECDATE
ORDER BY tblDump.SubjectNumber, tblDump.RECDATE Asc;
For the variable FoodType, there are three possibilities: 1, 2 or 3. I need to print the Subjects whose Min(RECTIME) is associated with a 1 and Max(RECTIME) is associated with a 3, per day.
I was attempting to make the HAVING statement such that the Foodtype = 1 for Min(RecTIME) and Foodtype = 3 for Max(RECTIME), but I can't get the linguistics correct.
Resolved:
Okay, I'm sure there is a more efficient way to do this but this works. Hopefully this will help someone or someone can post a more efficient method. I used 3 queries, one for the Min Dose time, one for the Max dose time, and one to join them together.
qryMin:
SELECT a.SubjectNumber, a.RECDATE, First(a.colm AS MornDose), First(m.FoodType)
FROM (SELECT SubjectNumber, RECDATE, MIN(RECTIME) as colm
FROM tblDump GROUP BY SubjectNumber, RECDATE) AS a
INNER JOIN tblDump AS m
ON (a.colm=m.RECTIME)
AND (a.RECDATE = m.RECDATE)
AND (a.SubjectNumber = m.SubjectNumber)
GROUP BY a.SubjectNumber, a.RECDATE
ORDER BY a.SubjectNumber, a.RECDATE;
qryMax:
SELECT a.SubjectNumber, a.RECDATE, First(a.colm) AS MornDose, First(m.FoodType)
FROM (SELECT SubjectNumber, RECDATE, MAX(RECTIME) AS colm
FROM tblDump GROUP BY SubjectNumber, RECDATE) AS a
INNER JOIN tblDump AS m
ON (a.SubjectNumber = m.SubjectNumber)
AND (a.RECDATE = m.RECDATE)
AND (a.colm = m.RECTIME)
GROUP BY a.SubjectNumber, a.RECDATE
ORDER BY a.SubjectNumber, a.RECDATE;
qryCombo:
SELECT qryMin.*, qryMax.*
FROM qryMin
INNER JOIN qryMax
ON qryMin.SubjectNumber = qryMax.SubjectNumber
WHERE qryMin.RECDATE = qryMax.RECDATE
AND qryMin.FoodType = 1
AND qryMax.FoodType = 3;

Related

SQL Queries Returning Non-equivalent Results and Different Counts Every Run

So I have 2 SQL queries both including the same variable: basically (n_orders and orders_count) should return the same exact results. Problem is:
the 2 columns are not always equivalent for all values
the count of different values changes every run
so first run could be that 20 rows have different (n_orders, orders_count) values then 2nd run says count of different values is 56 for example and so on with changing counts every run.
Query 1:
SELECT product_id,
packing_unit_id,
count(DISTINCT product_sales_order.sales_order_id)
FROM product_sales_order
WHERE product_sales_order.created_at::date BETWEEN '{start}' AND '{end}'
GROUP BY 1, 2
ORDER BY product_id, packing_unit_id
Query 2:
select kpis.*, lr.lr
FROM
(SELECT product_sales_order.product_id,
product_sales_order.packing_unit_id,
count(DISTINCT product_sales_order.sales_order_id) AS orders_count,
count(DISTINCT sales_orders.retailer_id) AS retailers_count,
count(DISTINCT product_sales_order.sales_order_id)*1.0 / count(DISTINCT sales_orders.retailer_id) AS frequency,
(count(DISTINCT sales_orders.retailer_id)*1.0 /(SELECT count(DISTINCT sales_orders.retailer_id) AS month_retailers
FROM sales_orders
JOIN retailers on retailers.id = sales_orders.retailer_id
WHERE sales_orders.created_at::Date BETWEEN '{start}' AND '{end}'
AND sales_orders.sales_order_status_id = 6
AND retailers.is_market_type_private = false)) AS reach,
sum(product_sales_order.total_price) AS nmv,
(sum(product_sales_order.total_price)*1.0 / (SELECT sum(product_sales_order.total_price) AS month_nmv
FROM product_sales_order
WHERE product_sales_order.created_at::Date BETWEEN '{start}' AND '{end}'
AND product_sales_order.purchased_item_count <> 0)) AS contribution,
sum(product_sales_order.purchased_item_count * product_sales_order.basic_unit_count) AS bskt_size,
sum(product_sales_order.total_price)*1.0 / count(DISTINCT product_sales_order.sales_order_id) AS avg_ts,
sum(product_sales_order.total_price)*1.0 / count(DISTINCT sales_orders.retailer_id) AS nmv_p_retailer
FROM product_sales_order
LEFT JOIN sales_orders ON sales_orders.id = product_sales_order.sales_order_id
LEFT JOIN products ON products.id = product_sales_order.product_id
LEFT JOIN retailers on retailers.id = sales_orders.retailer_id
WHERE product_sales_order.created_at::date BETWEEN '{start}' AND '{end}'
GROUP BY 1,2
ORDER BY product_sales_order.product_id, product_sales_order.packing_unit_id, orders_count
) as kpis
LEFT JOIN (
SELECT performance.lost_revenue.product_id,
sum(performance.lost_revenue.lost_revenue) as lr
FROM performance.lost_revenue
WHERE performance.lost_revenue.created_at::Date between '{start}' AND '{end}'
GROUP BY 1
)as lr on lr.product_id = kpis.product_id
What could be corrected regarding the structure of the 2nd SQL query to make it yield the same results for orders_count?
Why does different values count return different results every run?

How to use 1 SQL query related to date and time in order to compare value difference

SELECT c.treatment_category, a.treatment_id, MAX(a.counts - b.counts) AS ReviewDifference
FROM
(SELECT treatment_id, COUNT(treatment_id) AS counts
FROM review
WHERE DATE(review.created) BETWEEN DATE(TIMESTAMP'2016-01-01 00:00:00.0') AND DATE(TIMESTAMP'2016-12-31 23:59:59.999')
GROUP BY treatment_id) a
LEFT JOIN
(SELECT treatment_id, COUNT(treatment_id)
FROM review
WHERE DATE(review.created) BETWEEN DATE(TIMESTAMP'2015-01-01 00:00:00.0') AND DATE(TIMESTAMP'2015-12-31 23:59:59.999')
GROUP BY treatment_id) b
ON a = b
LEFT JOIN
(SELECT t.treatment_category AS category, r.treatment_id AS number
FROM treatment t
LEFT JOIN review r
ON t.treatment_id = r.treatment_id
GROUP BY category, number) c
ON b.treatment_id = c.number
GROUP BY a.treatment_id, c.treatment_category
ORDER BY ReviewDifference DESC
LIMIT 1;
I need some hints or simpler query on how to do this question since it is related to date and time. Thank you.
What treatment category has seen the biggest increase in reviews from 2015 to 2016?
Please see below for the tables.
I have provided my code snippet and I would like to find a simpler and cleaner way on writing the code.
SELECT t.treatment_id, t.treatment_name,
COUNT( CASE WHEN YEAR(created) = 2016 THEN r.review_id END)
- COUNT( CASE WHEN YEAR(created) = 2015 THEN r.review_id END) as review_count
FROM treatments t
JOIN reviews r
ON t.treatment_id = r.treatment_id
GROUP BY t.treatment_id, t.treatment_name,
ORDER BY review_count DESC

Can't order query correctly

A while ago I requested help to code a LEFT JOIN filtering in a particular way that the result postition the desired value in the first row.
Need to retrieve table's last inserted/updated record with some exclusions
The thing now is that there are many cases which are mixing data. The scenario is that on the same table we have 2 values that we need to organize on different columns. The PO_ID is unique, but can have 1 or more values on the other tables, and for this particular case 1 PO_ID has 3 SHIP_ID_CUS values. We only need 1 PO_ID per row (no duplicates) that is way we used the MAX() and GROUP BY.
Here is a piece of the code that I think cause issues.
select
z.po_id,
max(scdc.ship_id) as ship_id_cdc,
max(lscdc.ship_evnt_cd) as last_event_cdc,
max(lscdc.ship_evnt_tms) as event_tms_cdc,
max(scus.SHIP_ID) as ship_id_cus,
max(lscus.ship_evnt_cd) as last_event_cus,
max(lscus.ship_evnt_tms) as event_tms_cus
from TABLE.A z
left join (select distinct po_id, iltc.ship_id, s.ship_to_loc_code from TABLE.B iltc inner join TABLE.C s on iltc.ship_id=s.ship_id and iltc.ship_to_loc_code=s.ship_to_loc_code and s.ship_to_ctry<>' ') AS A ON z.po_id = a.po_id
left JOIN TABLE.C scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)
left JOIN TABLE.C scdc ON A.SHIP_ID = scdc.SHIP_ID AND A.SHIP_TO_LOC_CODE = scdc.SHIP_TO_LOC_CODE and scdc.loc_type = 'CDC' AND DAYS(scdc.shipment_tms)+10 >= DAYS(z.ship_tms)
left join
( select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
, row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM TABLE.D
WHERE LEFT(ship_evnt_cd, 1) <> '9') lscus
ON lscus.ship_id_856=scus.ship_id and scus.ship_to_loc_code=lscus.ship_to_loc_cd856 and lscus.rn = 1
left join
( select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
, row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM TABLE.D
WHERE LEFT(ship_evnt_cd, 1) <> '9') lscdc
ON lscdc.ship_id_856=scdc.ship_id and lscdc.ship_to_loc_cd856=scdc.ship_to_loc_code and lscdc.rn = 1
WHERE
z.po_id = 'T1DLDC'
GROUP BY z.po_id
By searching that condition we get the following result
The problem is that if we search directly on the TABLE.D, the last event that we need (with last update record tms) is another one (X1) and somehow the date is incorrect.
What is even more weird, is that if we search for the ship_id_cus on the original query, we get the correct code but still with a wrong date...
WHERE
--z.po_id = 'T1DLDC'
scus.ship_id = 'D30980'
GROUP BY z.po_id
I tried other logic changes like modifying the left joins to search on a subquery.
left JOIN ( select * from TABLE.C order by updt_job_tms desc) scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)
But this is also giving the same exact results by searching either by po_id or ship_id_cus
Any ideas or comment will be much appreciated.
Thanks
------------------------------------UPDATE-----------------------------------
Adding the result of the LEFT JOIN with the row_partition() including all the ship_id_cus for that po_id, and all the codes with the tms. None match here.
Based on all these, it should be the last ship_id_cus with X1 event/tms. If we exclude also the ones starting with 9, we would get the following result.
(I am not applying here ordering by ship_id_cus, which already described before that did not work either the way I implemented)
If you have a table: TBL1
ID APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC Y JOE 2019-01-13
ABC N ZACK 2018-12-23
ABC N SUE 2019-02-23
And you do SQL:
SELECT ID, MAX(APPROVED) AS APPROVAL
,MAX(APPROVER) AS APPROVED_BY , MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID
you will get result:
ID APPROVAL APPROVED_BY APPROVED_ON
====== ======== =========== =============
ABC Y ZACK 2019-02-23
which is correct to the code but is NOT what you want
Try the following:
SELECT T1.ID, T1.APPROVED, T1.APPROVER, T1.DATE_APPROVED
FROM TBL1 AS T1
INNER JOIN (SELECT ID, MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID
) AS T2
ON T1.ID =T2.ID
AND T1.DATE_APPROVED = T2.APPROVED_ON
Result:
ID APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC N SUE 2019-02-23

Combining results with DateDiff included

I have a list of places where people have been discharged and I need to stratify them by age. The query that I have currently below works, but it lists every discharge type count, by every age.
You can see how for age 0-1-2-3 there are all separate counts of discharge type based on what age. How can I make it so that it's only the dischargeType and the counts that are listed (as long as they are 18 and under)
SELECT DATEDIFF(yyyy, tblVisits.dob, tblVisits.admitdate) AS Age, tblDischarge.dischargeType, COUNT(tblDischarge.dischargeType) AS COUNTS
FROM tblVisits INNER JOIN
tblDischarge ON tblVisitsDischargeStatus = tblDischarge.dis_statID
GROUP BY DATEDIFF(yyyy,tblVisits.dob, tblVisits.Admitdate), tblDischarge.dischargeType
HAVING (DATEDIFF(yyyy,tblVisits.DOB, tblVisits.AdmitDate) <=18)
If you just want people aged under 18, but only one row per discharge-type...
SELECT
tblDischarge.dischargeType,
COUNT(tblDischarge.dischargeType) AS COUNTS
FROM
tblVisits
INNER JOIN
tblDischarge
ON tblVisitsDischargeStatus = tblDischarge.dis_statID
WHERE
(DATEDIFF(yyyy,tblVisits.DOB, tblVisits.AdmitDate) <=18)
GROUP BY
tblDischarge.dischargeType
If you want to group the ages into strata...
SELECT
tblDischarge.dischargeType,
CASE DATEDIFF(yyyy,tblVisits.DOB, tblVisits.AdmitDate)
WHEN <= 3 THEN '0..3'
WHEN <= 8 THEN '4..8'
WHEN <= 12 THEN '9..12'
ELSE '13..18'
END AS ageBand,
COUNT(tblDischarge.dischargeType) AS COUNTS
FROM
tblVisits
INNER JOIN
tblDischarge
ON tblVisitsDischargeStatus = tblDischarge.dis_statID
WHERE
(DATEDIFF(yyyy,tblVisits.DOB, tblVisits.AdmitDate) <=18)
GROUP BY
tblDischarge.dischargeType,
CASE DATEDIFF(yyyy,tblVisits.DOB, tblVisits.AdmitDate)
WHEN <= 3 THEN '0..3'
WHEN <= 8 THEN '4..8'
WHEN <= 12 THEN '9..12'
ELSE '13..18'
END
Remove the AGE in the selection and in the Group by part:
SELECT tblDischarge.dischargeType, COUNT(tblDischarge.dischargeType) AS COUNTS
FROM tblVisits INNER JOIN
tblDischarge ON tblVisitsDischargeStatus = tblDischarge.dis_statID
GROUP BY tblDischarge.dischargeType
HAVING (DATEDIFF(yyyy,tblVisits.DOB, tblVisits.AdmitDate) <=18)
I am not sure of your problem, but if you just want to count by dischargeType, for all people under 18, the code follows:
SELECT dis.dischargeType, COUNT(dis.dischargeType) AS COUNTS
FROM tblDischarge dis
JOIN tblVisits vst ON vst.DischargeStatus = dis.dis_statID
WHERE (DATEDIFF(yyyy,vst.DOB, vst.AdmitDate) <= 18)
GROUP BY dis.dischargeType;
Or you can use sub-query, like:
SELECT dischargeType, COUNT(dischargeType) AS COUNTS
FROM tblDischarge
WHERE dis_statID IN (
SELECT vst.DischargeStatus
FROM tblVisits vst
WHERE (DATEDIFF(yyyy, vst.DOB, vst.AdmitDate) <= 18)
)
GROUP BY dis.dischargeType;
Simply remove the calculation for age from the SELECT and the GROUP BY, and change the HAVING to a WHERE:
SELECT tblDischarge.dischargeType,
COUNT(tblDischarge.dischargeType) as COUNTS
FROM tblVisits
INNER JOIN tblDischarge
ON tblVisitsDischargeStatus = tblDischarge.dis_statID
WHERE (DATEDIFF(yyyy,tblVisits.DOB, tblVisits.AdmitDate) <=18)
GROUP BY tblDischarge.dischargeType

How can I select only one record per “person”, per date with an inner join in an MS Access query?

I need to pull data from two tables: Neptune_FN_Analysis and Neptune_prem
There will be 3 fields called readings_miu_id (comparable to a persons name or item #), ReadDate, ReadTime (all of which are in Neptune_FN_Analysis). Some readings_miu_ids have multiple ReadTimes for multiple days but I only want to pull the "last time" entered per readings_miu_id, per day.
I need all readings_miu_ids that have an entry date for the selected range but only the last ReadTime entered for each record I am pulling.
My solution so far, based on one table is:
SELECT readings_miu_id, Reading, ReadDate, ReadTime, MIUwindow, SN, Noise, RSSI, OriginCol, ColID, Ownage
FROM analyzed AS A
WHERE ReadDate Between #4/21/2009# and #4/29/2009#
AND ReadTime=
(SELECT TOP 1 analyzed.ReadTime FROM analyzed
where analyzed.readings_miu_id = A.readings_miu_id
AND analyzed.ReadDate = A.ReadDate
ORDER BY analyzed.ReadTime DESC);
When I try to adapt this solution, I can't do the FROM [tableName] as A, INNER JOIN because it gives me an error. The original code that my predecessor made (which is what I am trying to adapt/fix) is as follows:
SELECT readings_miu_id, Reading, ReadDate,Format([MIUtime],'hh:mm:ss') AS
ReadTime, MIUwindow, SN, Noise, RSSI, ColRSSI, MIURSSI, Firmware, CFGDate, FreqCorr,
Active, MeterType, OriginCol, ColID, Ownage, SiteID, PremID, Neptune_prem.prem_group1,
Neptune_prem.prem_group2, ReadID
INTO analyzed
FROM Neptune_FN_Analysis INNER JOIN
Neptune_prem ON Neptune_FN_Analysis.PremID = Neptune_prem.premice_id
WHERE SiteID = 36801 and ReadDate BETWEEN #04/21/09# AND #04/27/09#
and OriginCol = 'US 29' and ColID = 1 and ColID <> 0 and Active = 'Y'
I don't quite get all of what you're trying to do, but if you inner join on a subquery which gets the MAX of date, it could eliminate all the records where the date was not the max
SELECT readings_miu_id, Reading, ReadDate, ReadTime, MIUwindow, SN,
Noise, RSSI, OriginCol, ColID, Ownage
FROM analyzed
INNER JOIN
(SELECT [whatever the id common to all like records is] as likeID, MAX(analyzed.ReadTime) as latestDate
FROM analyzed
GROUP BY likeID) AS maxDate ON analyzed.likeID=maxDate.likeID AND analyzed.latestDate = maxDate.latestDate
WHERE ReadDate Between #4/21/2009# and #4/29/2009#
modify as needed
I would try something like this:
SELECT a.readings_miu_id, a.Reading, a.ReadDate, a.ReadTime, a.MIUwindow, a.SN, a.Noise, a.RSSI, a.OriginCol, a.ColID, a.Ownage
FROM analyzed AS A INNER JOIN
(SELECT max(ReadTime) as MaxReadTime,readings_miu_id FROM analyzed
WHERE ReadDate Between #4/21/2009# and #4/29/2009#
GROUP BY readings_miu_id) as B
on a.readings_miu_id = b.readings_miu_id and a.MaxReadTime = b.ReadTime
SELECT
<your columns>
FROM
Neptune_FN_Analysis A1
INNER JOIN Neptune_prem ON
P.premice_id = A1.PremID
LEFT OUTER JOIN Neptune_FN_Analysis A2 ON
A2.readings_miu_id = A1.readings_miu_id AND
A2.ReadDate = A1.ReadDate AND
A2.ReadTime > A1.ReadTime
WHERE
A2.readings_miu_id IS NULL AND
<add any additional criteria here>
I'm not sure what you imply by specifying "INNER JOIN" this time around. Other answers use a subquery, so here's a an example using two INNER JOINs and no subquery. Rather than getting my head around your schema :) I'm using Northwind to return customers and the date of their most recent order:
SELECT C1.CustomerID, C1.CompanyName,
O1.OrderID, O1.OrderDate
FROM (Customers AS C1
INNER JOIN Orders AS O1
ON C1.CustomerID = O1.CustomerID)
INNER JOIN Orders AS O2
ON C1.CustomerID = O2.CustomerID
GROUP
BY C1.CustomerID, C1.CompanyName,
O1.OrderID, O1.OrderDate
HAVING O1.OrderDate = MAX(O2.OrderDate);