Get latest date before date value in row - sql

I am using MSSQL 2008 R2. I am attempting to get the latest data date up to a date stored in another column. I am able to pull the latest data date overall using max(ProcedureDate) in a subquery; however, i need the latest date before the date that's stored in the column.
Here's an example:
Current Table: Procedures
ID Patient ProcedureType ProcedureDate
1 George ExamA 1/1/2013
2 George TreatmentA 1/3/2013
2 George TreatmentB 1/5/2003
4 George ExamB 2/1/2013
5 George TreatmentA 2/5/2013
Desired Table: ProceduresWithLastExam
ID Patient ProcedureType ProcedureDate LastExamDate DaysSinceLastExam LastExamType
1 George ExamA 1/1/2013 1/1/2013 0 ExamA
2 George TreatmentA 1/3/2013 1/1/2013 2 ExamA
3 George TreatmentB 1/5/2013 1/1/2013 4 ExamA
4 George ExamB 2/1/2013 2/1/2013 0 ExamB
5 George TreatmentA 2/5/2013 2/1/2013 4 ExamB
I have attempted using the following, but it only pulls back the latest data date for that Patient.
select p.*, a.LastExamDate, a.ProcedureType as LastExamType from Procedures p
left join (
select exams.Patient, exams.ProcedureType, MAX(exams.ProcedureDate) as LastExamDate from Procedures exams
where ProcedureType like 'Exam%'
group by exams.Patient, exams.ProcedureType
)a
on p.Patient = a.Patient
The results for all rows is 2/1/13 as LastExamDate and ExamB as LastExamType.
I have attempted including some additional date parameters in my left join, in my where clause, and in my subquery to no success.
Please note I have omitted datediff logic until I can get the correct dates to return.
Thanks in advance for the help.

You can use OUTER APPLY. It is like a correlated subquery but allows multiple columns:
SELECT p.ID,
p.Patient,
p.ProcedureType,
p.ProcedureDate,
[LastExamDate] = exam.ProcedureDate,
[DaysSinceLastExam] = DATEDIFF(DAY, exam.ProcedureDate, p.ProcedureDate),
[LastExamType] = exam.ProcedureType
FROM Procedures p
OUTER APPLY
( SELECT TOP 1 exams.ProcedureType, exams.ProcedureDate
FROM Procedures exams
WHERE Exams.ProcedureType LIKE '%Exam%'
AND Exams.Patient = p.Patient
AND Exams.ProcedureDate <= p.ProcedureDate
ORDER BY Exams.ProcedureDate DESC
) exam;
Example On SQL Fiddle

This can be done with OUTER APPLY:
SELECT A.*,
B.ProcedureDate LastExamDate,
DATEDIFF(DAY,B.ProcedureDate,A.ProcedureDate) DaysSinceLastExam,
B.ProcedureType
FROM Procedures A
OUTER APPLY ( SELECT TOP 1 *
FROM Procedures
WHERE Patient = A.Patient
AND ProcedureDate <= A.ProcedureDate
AND ProcedureType LIKE 'Exam%'
ORDER BY ProcedureDate DESC) B
Here is a demo for you to try.

Related

SQL for joining two tables and grouping by shared column

I want to join two tables and use the column they both share to group the results, including a null result for those accountIds which only appear in one table.
Table a
AccountId
productApurchases
Steve
1
Jane
5
Bill
10
Abed
2
Table b
AccountId
productApurchases
Allan
1
Jane
10
Bill
2
Abed
1
Mike
2
Desired output
AccountId
productApurchases
productBpurchases
Steve
1
0
Jane
5
10
Bill
10
2
Abed
2
1
Mike
0
2
I've been trying with various joins but cannot figure out how to group by all the account ids.
Any advice much appreciated, thanks.
Use full join:
select accountid,
coalesce(productApurchases, 0) as productApurchases,
coalesce(productBpurchases, 0) as productBpurchases
from a full join
b
using (accountid);

Filter DB2 Group based on criteria

I have a data set for multiple versions of a record. In the structure below:
ID Version Date ActionedBy
1 1 20/08/2018 James
1 2 20/08/2018 Samuel
2 1 20/08/2018 Tom
3 1 20/08/2018 Tom
3 2 20/08/2018 Tom
3 1 20/08/2018 Dave
4 1 20/08/2018 Tom
4 2 20/08/2018 Mike
5 1 20/08/2018 Dave
I need a query to return all records that Tom has actioned but not include the ones actioned by Dave. So my result should be:
ID
2
4
I've grouped on ID but am struggling with the Having clause or sub query to not include records with Dave.
My query is:
Select * from events where ActionedBy='Tom' Group By ID Having "NOT SURE WHAT GOES HERE"
One solution could be
SELECT ID FROM events e
WHERE ActionedBy = 'Tom'
AND NOT EXISTS (SELECT 1 FROM events WHERE ActionedBy = 'Dave' AND ID = e.ID)
You do not necessarily need the GROUP BY if Tom occurs only once in a single ID. If not just add it....
I would use group by with a having clause:
select id
from t
where actionedby in ('Tom', 'Dave')
having min(actionedby) = 'Tom' and max(actionedby) = 'Tom';
Actually min() is sufficient in this case, because 'Tom' > 'Dave'.

Concatenating data from one row into the results from another

I have a SQL Server database of orders I'm struggling with. For a normal order a single table provides the following results:
Orders:
ID Customer Shipdate Order ID
-----------------------------------------------------------------
1 Tom 2015-01-01 100
2 Bob 2014-03-20 200
At some point they needed orders that were placed by more than one customer. So they created a row for each customer and split the record over multiple rows.
Orders:
ID Customer Shipdate Order ID
-----------------------------------------------------------------
1 Tom 2015-01-01 100
2 Bob 2014-03-20 200
3 John
4 Dan
5 2014-05-10 300
So there is another table I can join on to make sense of this which relates the three rows which are actually one order.
Joint.Orders:
ID Related ID
-----------------------------------------------------------------
5 3
5 4
I'm a little new to SQL and while I can join on the other table and filter to only get the data relating to Order ID 300, but what I'd really like is to concatenate the customers, but after searching for a while I can't see how to do this. What'd I'd really like to achieve is this as an output:
ID Customer Shipdate Order ID
----------------------------------------------------------------
1 Tom 2015-01-01 100
2 Bob 2014-03-20 200
5 John, Dan 2014-05-10 300
You should consider changing the schema first. The below query might help you get a feel of how it can be done with your current design.
Select * From Orders Where IsNull(Customer, '') <> ''
Union All
Select ID,
Customer = (Select Customer + ',' From Orders OI Where OI.ID (Select RelatedID from JointOrders JO Where JO.ID = O.ID)
,ShipDate, OrderID
From Orders O Where IsNull(O.Customer, '') = ''

Select info from table where row has max date

My table looks something like this:
group date cash checks
1 1/1/2013 0 0
2 1/1/2013 0 800
1 1/3/2013 0 700
3 1/1/2013 0 600
1 1/2/2013 0 400
3 1/5/2013 0 200
-- Do not need cash just demonstrating that table has more information in it
I want to get the each unique group where date is max and checks is greater than 0. So the return would look something like:
group date checks
2 1/1/2013 800
1 1/3/2013 700
3 1/5/2013 200
attempted code:
SELECT group,MAX(date),checks
FROM table
WHERE checks>0
GROUP BY group
ORDER BY group DESC
problem with that though is it gives me all the dates and checks rather than just the max date row.
using ms sql server 2005
SELECT group,MAX(date) as max_date
FROM table
WHERE checks>0
GROUP BY group
That works to get the max date..join it back to your data to get the other columns:
Select group,max_date,checks
from table t
inner join
(SELECT group,MAX(date) as max_date
FROM table
WHERE checks>0
GROUP BY group)a
on a.group = t.group and a.max_date = date
Inner join functions as the filter to get the max record only.
FYI, your column names are horrid, don't use reserved words for columns (group, date, table).
You can use a window MAX() like this:
SELECT
*,
max_date = MAX(date) OVER (PARTITION BY group)
FROM table
to get max dates per group alongside other data:
group date cash checks max_date
----- -------- ---- ------ --------
1 1/1/2013 0 0 1/3/2013
2 1/1/2013 0 800 1/1/2013
1 1/3/2013 0 700 1/3/2013
3 1/1/2013 0 600 1/5/2013
1 1/2/2013 0 400 1/3/2013
3 1/5/2013 0 200 1/5/2013
Using the above output as a derived table, you can then get only rows where date matches max_date:
SELECT
group,
date,
checks
FROM (
SELECT
*,
max_date = MAX(date) OVER (PARTITION BY group)
FROM table
) AS s
WHERE date = max_date
;
to get the desired result.
Basically, this is similar to #Twelfth's suggestion but avoids a join and may thus be more efficient.
You can try the method at SQL Fiddle.
Using an in can have a performance impact. Joining two subqueries will not have the same performance impact and can be accomplished like this:
SELECT *
FROM (SELECT msisdn
,callid
,Change_color
,play_file_name
,date_played
FROM insert_log
WHERE play_file_name NOT IN('Prompt1','Conclusion_Prompt_1','silent')
ORDER BY callid ASC) t1
JOIN (SELECT MAX(date_played) AS date_played
FROM insert_log GROUP BY callid) t2
ON t1.date_played = t2.date_played
SELECT distinct
group,
max_date = MAX(date) OVER (PARTITION BY group), checks
FROM table
Should work.

SQL: Select all from column A and add a value from column B if present

I'm having quite an easy problem with SQL, I just can't word it properly (therefore I didn't find anything in google and my title probably is misleading)
The problem is: I have a big table containing transaction informations in the form (ID, EmployeeID, Date, Value) (and some more, but only those matter currently) and a list of all EmployeeIDs. What I want is a result table showing all employee IDs with their aggregated value of transactions in a given timespan.
The problem is: How do I get those employees into the result table that don't have an entry for the given time period?
e.g.
ID EMPLID DATE VALUE
1 1 2013-01-01 1000
2 2 2013-02-02 2000
3 1 2013-01-03 3000
4 2 2013-04-01 2000
5 2 2013-03-01 2000
6 1 2013-02-01 4000
EMPLID NAME
1 bob
2 alice
And now I want the aggregated value of all transactions after 2013-03-01 like this
EMPLID VALUE
1 0 <- how to get this based on the employee table?
2 4000
The SQL Server in use is Firebird and I connect to it through JDBC (if that matters)
SELECT a.EmpID, a.Name,
COALESCE(SUM(b.Value), 0) TotalValue
FROM Employee a
LEFT JOIN Transactions b
ON a.EmpID = b.EmpID AND
b.Date >= '2013-03-01'
GROUP BY a.EmpID, a.Name
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins