SQL Server : max date and inner join - sql

I have two tables, one is a list of tasks. The other containing historical values for those tasks.
I need to generate a list of the latest event (and its description) for each check, as long as long as its Date_Executed is less than the current datetime minus the Timeframe (TimeFrame being hours within the task has to be done, formatted for use in DATEADD). But only if they have an active = 1.
Table: checks
Check_id description TimeFrame active
1 Task One -24 0
2 Task Two -24 0
3 Task Forty -48 1
4 Task Somehin -128 1
Table: events
Event_id Check_id Comment Date_Executed User_Executed
1 1 NULL 2012-09-18 16:10:44.917 admin
2 1 NULL 2012-09-25 11:39:01.000 jeff
3 4 Failed 2012-09-25 13:20:09.930 steve
4 4 Half failed 2012-09-25 13:05:09.953 marsha
5 2 NULL 2012-09-25 14:02:24.000 marsha
6 3 NULL 2012-09-18 16:10:55.023 marsha
The best solutions I have so far is:
SELECT
a.[Date_Executed]
a.[Check_id],
a.[Comments],
b.[frequency],
b.[Check_id],
b.[description]
FROM
[checksdb].[dbo].events as a,
[checksdb].[dbo].checks as b
where
b.active = 1
and a.[Date_Executed] < = dateadd(HOUR,b.[frequency],GETDATE())
and a.Check_id = b.Check_id
order by Check_id, priority
and
select MAX(date_Executed), Task_id from daily_check_events group by Task_id
Neither of which gets me what I need, I could really use some help.

Since you are SQL Server which supports Common Table Expression and Window Function. Try this,
WITH latestEvents
AS
(
SELECT Event_id, Check_id, [Comment], Date_Executed, User_Executed,
ROW_NUMBER() OVER(PARTITION BY Check_ID ORDER BY DATE_Executed DESC)
AS RowNum
FROM events
)
SELECT a.[Check_id], a.[description],
b.[Date_Executed], b.[Comment]
FROM checks a
INNER JOIN latestEvents b
on a.check_ID = b.check_ID
WHERE b.RowNum = 1 AND
a.active = 1
-- other conditions here
SQLFiddle Demo
The above query will only work on RDBMS that supports Window Functions. Alternatively, use the query below that works on most RDBMS
SELECT a.Check_id, a.description,
c.Date_Executed, c.Comment
FROM checks a
INNER JOIN
(
SELECT check_id, MAX(Date_Executed) maxExecuted
FROM events
GROUP BY check_ID
) b ON a.check_ID = b.check_ID
INNER JOIN events c
ON c.check_ID = b.check_ID AND
c.date_executed = b.maxExecuted
WHERE a.active = 1
SQLFiddle Demo

Related

How to use multiple counts in where clause to compare data of a table in sql?

I want to compare data of a table with its other records. The count of rows with a specific condition has to match the count of rows without the where clause but on the same grouping.
Below is the table
-------------
id name time status
1 John 10 C
2 Alex 10 R
3 Dan 10 C
4 Tim 11 C
5 Tom 11 C
Output should be time = 11 as the count for grouping on time column is different when a where clause is added on status = 'C'
SELECT q1.time
FROM (SELECT time,
Count(id)
FROM table
GROUP BY time) AS q1
INNER JOIN (SELECT time,
Count(id)
FROM table
WHERE status = 'C'
GROUP BY time) AS q2
ON q1.time = q2.time
WHERE q1.count = q2.count
This is giving the desired output but is there a better and efficient way to get the desired result?
Are you looking for this :
select t.*
from table t
where not exists (select 1 from table t1 where t1.time = t.time and t1.status <> 'C');
However you can do :
select time
from table t
group by time
having sum (case when status <> 'c' then 1 else 0 end ) = 0;
If you want the times where the rows all satisfy the where clause, then in Postgres, you can express this as:
select time
from t
group by time
having count(*) = count(*) filter (where status = 'C');

Select rows in one table, adding column where MAX(Date) of rows in other, related table

I have a table containing a set of tasks to perform:
Task
ID Name
1 Washing Up
2 Hoovering
3 Dusting
The user can add one or more Notes to a Note table. Each note is associated with a task:
Note
ID ID_Task Completed(%) Date
11 1 25 05/07/2013 14:00
12 1 50 05/07/2013 14:30
13 1 75 05/07/2013 15:00
14 3 20 05/07/2013 16:00
15 3 60 05/07/2013 17:30
I want a query that will select the Task ID, Name and it's % complete, which should be zero if there aren't any notes for it. The query should return:
ID Name Completed (%)
1 Washing Up 75
2 Hoovering 0
3 Dusting 60
I've really been struggling with the query for this, which I've read is a "greatest n per group" type problem, of which there are many examples on SO, none of which I can apply to my case (or at least fully understand). My intuition was to start by finding the MAX(Date) for each task in the note table:
SELECT ID_Task,
MAX(Date) AS Date
FROM
Note
GROUP BY
ID_Task
Annoyingly, I can't just add "Complete %" to the above query unless it's contained in a GROUP clause. Argh! I'm not sure how to jump through this hoop in order to somehow get the task table rows with the column appended to it. Here is my pathetic attempt, which fails as it only returns tasks with notes and then duplicates task records at that (one for each note, so it's a complete fail).
SELECT Task.ID,
Task.Name,
Note.Complete
FROM
Task
JOIN
(SELECT ID_Task,
MAX(Date) AS Date
FROM
Note
GROUP BY
ID_Task) AS InnerNote
ON
Task.ID = InnerNote.ID_Task
JOIN
Note
ON
Task.ID = Note.ID_Task
Can anyone help me please?
If we assume that tasks only become more complete, you can do this with a left outer join and aggregation:
select t.ID, t.Name, coalesce(max(n.complete), 0)
from tasks t left outer join
notes n
on t.id = n.id_task
group by t.id, t.name
If tasks can become "less complete" then you want the one with the last date. For this, you can use row_number():
select t.ID, t.Name, coalesce(n.complete, 0)
from tasks t left outer join
(select n.*, row_number() over (partition by id_task order by date desc) as seqnum
from notes n
) n
on t.id = n.id_task and n.seqnum = 1;
In this case, you don't need a group by, because the seqnum = 1 performs the same role.
How about this just get the max of completed and group by taskid
SELECT t.ID_Task as ID,n.`name`,MAX(t.completed) AS completed
FROM `task` t RIGHT JOIN `note` n on ( t.ID_Task=n.ID )
GROUP BY t. ID_Task
OR
SELECT t.ID_Task as ID,n.`name`,
(CASE when MAX(t.completed) IS NULL THEN '0' ELSE MAX(t.completed))AS completed
FROM `task` t RIGHT JOIN `note` n on ( t.ID_Task=n.ID )
GROUP BY t. ID_Task
select a.ID,
a.Name,
isnull((select completed
from Note
where ID_Task = b.ID_Task
and Date = b.date),0)
from Task a
LEFT OUTER JOIN (select ID_Task,
max(date) date
from Note
group by ID_Task) b
ON a.ID = b.ID_Task;
See DEMO here

Find duplicates within a specific period

I have a table with the following structure
ID Person LOG_TIME
-----------------------------------
1 1 2012-05-21 13:03:11.550
2 1 2012-05-22 13:09:37.050 <--- this is duplicate
3 1 2012-05-28 13:09:37.183
4 2 2012-05-20 15:09:37.230
5 2 2012-05-22 13:03:11.990 <--- this is duplicate
6 2 2012-05-24 04:04:13.222 <--- this is duplicate
7 2 2012-05-29 11:09:37.240
I have some application job that fills this table with data.
There is a business rule that each person should have only 1 record in every 7 days.
From the above example, records # 2,5 and 6 are considered duplicates while 1,3,4 and 7 are OK.
I want to have a SQL query that checks if there are records for the same person in less than 7 days.
;WITH cte AS
(
SELECT ID, Person, LOG_TIME,
DATEDIFF(d, MIN(LOG_TIME) OVER (PARTITION BY Person), LOG_TIME) AS diff_date
FROM dbo.Log_time
)
SELECT *
FROM cte
WHERE diff_date BETWEEN 1 AND 6
Demo on SQLFiddle
Please see my attempt on SQLFiddle here.
You can use a join based on DATEDIFF() to find records which are logged less than 7 days apart:
WITH TooClose
AS
(
SELECT
a.ID AS BeforeID,
b.ID AS AfterID
FROM
Log a
INNER JOIN Log b ON a.Person = b.Person
AND a.LOG_TIME < b.LOG_TIME
AND DATEDIFF(DAY, a.LOG_TIME, b.LOG_TIME) < 7
)
However, this will include records which you don't consider "duplicates" (for instance, ID 3, because it is too close to ID 2). From what you've said, I'm inferring that a record isn't a "duplicate" if the record it is too close to is itself a "duplicate".
So to apply this rule and get the final list of duplicates:
SELECT
AfterID AS ID
FROM
TooClose
WHERE
BeforeID NOT IN (SELECT AfterID FROM TooClose)
Please take a look at this sample.
Reference: SQLFIDDLE
Query:
select person,
datediff(max(log_time),min(log_time)) as diff,
count(log_time)
from pers
group by person
;
select y.person, y.ct
from (
select person,
datediff(max(log_time),min(log_time)) as diff,
count(log_time) as ct
from pers
group by person) as y
where y.ct > 1
and y.diff <= 7
;
PERSON DIFF COUNT(LOG_TIME)
1 1 3
2 8 3
PERSON CT
1 3
declare #Count int
set #count=(
select COUNT(*)
from timeslot
where (( (TimeFrom<#Timefrom and TimeTo >#Timefrom)
or (TimeFrom<#Timeto and TimeTo >#Timeto))
or (TimeFrom=#Timefrom or TimeTo=#Timeto)))

SQL: determine that an activity occurs with a given frequency

A common problem in electronic-medical-record (EMR) reporting in determining that an activity occurs with a specific frequency. In this situation, I need to determine that a note was written every 72-hours after admission.
Given:
A D
|-0-|-1-|-2-|-3-|-4-|-5-|-6-|-7-|-8-|-9-|
|---- 1 ----|---- 2 ----|---- 3 ----|-4-|
There would need to be at least one note during periods 1, 2, and 3. Because 4 isn't a full 72-hour period, it doesn't require a note. Failure to find a note in periods 1, 2, and 3 would be a FAIL.
Data:
(ENC):
ENC_ID ADMITTED DISCHARGED PERIODS PASS_FAIL
4114221 06/15/09 18:30 06/24/09 15:40 3 ?
PERIODS: TRUNC(CEIL((DISCHARGED - ADMITTED)/3))
The 'PASS_FAIL' column would indicate if the encounter had an adequate number and timing of notes.
(NOTE):
ENC_ID NOTE_ID NOTE_TIME PERIOD
4114221 1833764 06/17/09 08:42 1
4114221 1843613 06/18/09 08:14 1
4114221 1858159 06/18/09 20:15 2
4114221 1850948 06/18/09 20:15 2
4114221 1850912 06/18/09 20:18 2
4114221 1859315 06/19/09 18:35 2
4114221 1863982 06/20/09 10:29 2
4114221 1868895 06/21/09 22:00 3
4114221 1873539 06/22/09 15:42 3
PERIOD: CEIL((NOTE_TIME - ADMITTED)/3)
Is there an efficient way to solve this problem?
SELECT e.*,
CASE WHEN cnt = TRUNC(CEIL((discharged / admitted) / 3)) THEN 'pass' ELSE 'fail' END AS pass_fail
FROM (
SELECT COUNT(*) AS cnt
FROM enc ei
CROSS JOIN
(
SELECT level AS period
FROM dual
CONNECT BY
level <=
(
SELECT TRUNC(CEIL((discharged / admitted) / 3))
FROM enc
WHERE enc_id = :enc_id
)
) p
WHERE ei.enc_id = :enc_id
AND EXISTS
(
SELECT NULL
FROM note
WHERE enc_id = ei.enc_id
AND note_time >= ei.admitted + (p - 1) * 3
AND note_time < ei.admitted + p * 3
)
) c
JOIN enc e
ON e.enc_id = :enc_id
If I'm reading your question correctly NOTE is a table with the data indicated.
All you really care about is whether the periods 1, 2 & 3 exist in the notes table for each enc_id.
If this is the case it indicates that an analytic function should be used:
select e.enc_id, e.admitted, e.discharged, e.periods
, decode( n.ct
, 'pass'
, 'fail' ) as pass_fail
from enc e
left outer join ( select distinct enc_id
, count(n.period) over ( partition by n.enc_id ) as ct
from note
where period in (1,2,3)
) n
on e.enc_id = n.enc_id
This selects all period's per enc_id from note, which are the ones you want to examine. Then counts them per enc_id. The distinct is there to ensure you only get one row per enc_id in the final result.
If you only want those enc_ids that have a value in note then turn the left outer join into an inner join.
If period is not, as indicated, in the note query, you have to do a distinct on the full query rather than the sub-query and check which period each note_id is in.
I'm sorry about the horrible formatting but I wanted to try to fit it on the page.
select distinct e.enc_id, e.admitted, e.discharged, e.periods
, decode( count( distinct -- number of distinct periods
case when n.note_time between e.admitted
and e.admitted + 3 then 1
when n.note_time between e.admitted
and e.admitted + 6 then 2
when n.note_time between e.admitted
and e.admitted + 9 then 3
end ) -- per enc_id from note
over ( partition by n.enc_id )
-- if it-s 3 then pass
, 3, 'pass'
-- else fail.
, 'fail' ) as pass_fail
from enc e
left outer join note n
on e.enc_id = n.enc_id
Whatever your data-structure the benefits of both ways are that they are simple joins, one index unique scan ( I'm assuming enc.end_id is unique ) and one index range scan ( on note ).

Grouping in SQL Statement

I have the following SQL statement:
SELECT TOP 30
a.ClassAdID, -- 0
a.AdTitle, -- 1
a.ClassAdCatID, -- 2
b.ClassAdCat, -- 3
a.Img1, -- 4
e.Domain, -- 5
a.AdText, -- 6
a.RegionID, -- 7
a.IsEvent, -- 8
a.IsCoupon, -- 9
b.ParentID, -- 10
a.MemberID, -- 11
a.AdURL, -- 12
a.Location, -- 13
a.GroupID -- 14
FROM ClassAd a
INNER JOIN ClassAdCat b ON b.ClassAdCatID = a.ClassAdCatID
INNER JOIN Member d ON d.MemberID = a.MemberID
INNER JOIN Region e ON e.RegionID = a.RegionID
WHERE DATEDIFF(d, GETDATE(), a.ExpirationDate) >= 0
AND PostType <> 'CPN'
ORDER BY a.CreateDate DESC
I want to only show one from each GROUPID... How can I adjust the statement to achieve this as I am lost with DISTINCT, GROUP BY etc..
Any help would be appreciated.
Many thanks,
Paul
You can use ROW_NUMBER function to partition data set based on GroupId values thus: for every new GroupId values the counter is restarted from 1 and the first row (with ROW_NUMBER = 1) is the newest record (a.CreateDate DESC). Then, we filter all records having ROW_NUMBER = 1 .
SELECT TOP 30 *
FROM
(
SELECT
a.ClassAdID, -- 0
a.AdTitle, -- 1
a.ClassAdCatID, -- 2
b.ClassAdCat, -- 3
a.Img1, -- 4
e.Domain, -- 5
a.AdText, -- 6
a.RegionID, -- 7
a.IsEvent, -- 8
a.IsCoupon, -- 9
b.ParentID, -- 10
a.MemberID, -- 11
a.AdURL, -- 12
a.Location, -- 13
a.GroupID, -- 14
ROW_NUMBER() OVER(PARTITION BY a.GroupId ORDER BY a.CreateDate DESC) AS PseudoId
FROM ClassAd a
INNER JOIN ClassAdCat b ON b.ClassAdCatID = a.ClassAdCatID
INNER JOIN Member d ON d.MemberID = a.MemberID
INNER JOIN Region e ON e.RegionID = a.RegionID
WHERE DATEDIFF(d, GETDATE(), a.ExpirationDate) >= 0
AND PostType <> 'CPN'
) q
WHERE q.PseudoId = 1;
GROUP BY goes with an AGGREGATE function... meaning you want to add up the values in the group, or find the biggest, or smallest in the group etc.
DISTINCT will remove duplicate rows.
in your query, you may be getting a bunch of not-so-similar rows that all happen to have the same group_id... if this is so, then you need to decide which one of those rows you really want to see.
maybe you want the newest one, or the one with the longest name, or something like that.
for grouping, you would pick a column like createdon and say something like MAX( createdon ) in the select list, then group on every other column in the select list to find the rows that match each other (except for created on), and return that only once with the largest value for created on... hope that makes sense.
edit:
very simple example for group id and create date. ( you can keep adding more columns as needed - one in the group by list for every one in the select list :
SELECT groupid, max( createdate )
FROM ClassAd
GROUP BY groupId
If I understand correctly you want to get one row from each group (like groupid)
I used sql server 2005 (Nothwind)
SELECT TOP 30 Customers.CompanyName, Orders.ShipCity, Orders.Freight
FROM Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CompanyName, Orders.ShipCity, Orders.Freight