Selecting Historical Record Outside of Date Range - sql

I have a situation where I need to select an address that was current for a particular date and time from an address history table. Some sample records might be as follows:
Address/Client JOIN Table (Address_Client_JOIN):
-------------------------
|AddressId | ClientId |
-------------------------
|5 | 8888887 |
-------------------------
|6 | 8888887 |
-------------------------
History Table (Address_History):
-------------------------------------------------------------------------------------------
|HistoryId | AddressId | AddTypeId | StreetAddress | CreatedDate | ModifiedDate |
-------------------------------------------------------------------------------------------
|1 | 5 | 1 | 123 Home Street| 2013-03-11 21:08 | 2013-04-02 13:18|
-------------------------------------------------------------------------------------------
|2 | 5 | 2 | 456 My Avenue | 2013-03-11 21:08 | 2013-04-08 15:00|
-------------------------------------------------------------------------------------------
|3 | 6 | 1 | 789 Cat Road | 2013-05-17 12:00 | 2013-05-17 12:00|
-------------------------------------------------------------------------------------------
The requirements for this query are that I have to grab the earliest record where #dateOfService falls between the CreatedDate and the ModifiedDate and where the AddTypeId is "1", if there is one, otherwise any other AddTypeId. The query I've thus far created is:
SELECT TOP 1 ah.HistoryId, ah.AddTypeId, ah.AddressId, ah.StreetAddress,
ah.CreatedDate, ah.ModifiedDate
FROM Address_Client_JOIN acj WITH (NOLOCK)
INNER JOIN Address_History ah WITH (NOLOCK) ON ah.AddressId = acj.AddressId
WHERE apj.ClientId = #clientId
AND (ah.CreatedDate <= #dateOfService
AND (#dateOfService <= ah.ModifiedDate ))
ORDER BY
ah.HistoryId ASC, CASE WHEN ah.AddTypeId = 1 THEN 0 ELSE 1 END
This works fine as long as the #dateOfService falls between the CreatedDate and ModifiedDate. However, when I've got a #dateOfService that occurs after the ModifiedDate, I get nothing, obviously. I need to be able to account for a situation where (using the above data) #dateOfService is after the ModifiedDate of 5/17/2013. For example, where #dateOfService = '2013-08-01 12:30'.
Thanks in advance.

You are only selecting the top row. That means that you can move the where filter into the order by clause. Then, it becomes a priority rather than a filter.
So, if nothing matches the filter, you will still be able to get a row. I think the query you want is something like:
SELECT TOP 1 ah.HistoryId, ah.AddTypeId, ah.AddressId, ah.StreetAddress,
ah.CreatedDate, ah.ModifiedDate
FROM Address_Client_JOIN acj WITH (NOLOCK) INNER JOIN
Address_History ah WITH (NOLOCK)
ON ah.AddressId = acj.AddressId
WHERE apj.ClientId = #clientId
ORDER BY ah.HistoryId ASC,
(CASE WHEN ah.AddTypeId = 1 THEN 0 ELSE 1 END),
(case when ah.CreatedDate <= #dateOfService AND #dateOfService <= ah.ModifiedDate then 1
when #dateOfService > ah.ModifiedDate then 2
else 3
end)

Related

SQL: Selecting data from multiple tables with multiple records to select and join

I have three tables: VolunteerRelationships, Organizations, and CampaignDates. I'm trying to write a query that will give me the organization id and name, and the org's start and end campaign dates for the current campaign year <#CampaignYear>, based on the selected volunteer <#SelectedInd>.
Dates are stored as separate column values for day, month and year which I'm trying to cast into a more an formatted date value. If I can get this, I'd also like to use a case statement to get the status of the campaign based on whether the date campaign dates are upcoming, currently running, or already closed, but need to get the first part of the query first.
Sorry if I'm leaving a lot of needed info out, this is my first time posting a question to this forumn. Thank you!
VolunteerRelationships
id | name | managesId |expiryDate
1 | john | 1 |
2 | jack | 2 |6/30/2020
3 | jerry| 3 |12/31/2021
Organizations
id | name1
1 | ACME
CampaignDates
orgId | dateDay | dateMonth | dateYear | dateType | Campaign Year
1 | 5 | 11 | 2020 | Start | 2020
1 | 15 | 11 | 2020 | End | 2020
Result
orgId | orgName | startDate | endDate | Status
1 | ACME | 2020-01-01| 2020-01-15 | Closed
select
v.MANAGEDACCOUNT,
o.Name1,
select * from
(select cast(cast dateyear*1000 + datemonth*100 + dateday as varchar(255)) as date as date1 from <#Schema>.CampaignDates where datetype = 'Start' and campaignyear = <#CampaignYear> and orgaccountnumber = v.MANAGEDACCOUNT) d1,
(select cast(cast dateyear*1000 + datemonth*100 + dateday as varchar(255)) as date as date2 from <#Schema>.CampaignDates where datetype = 'End' and campaignyear = <#CampaignYear> and orgaccountnumber = v.MANAGEDACCOUNT) d2
from <#Schema>.VolunteerRelationships v
inner join <#Schema>.organizations o
on o.accountnumber=v.MANAGEDACCOUNT
where v.VOLUNTEERACCOUNT = <#SelectedInd> and ( v.EXPIRYDATE IS NULL OR v.EXPIRYDATE > <#Today> )

Correlate Sequences of Independent Events - Calculate Time Intersection

We are building a PowerBI reporting solution and I (well Stack) solved one problem and the business came up with a new reporting idea. Not sure of the best way to approach it as I know very little about PowerBI and the business seems to want quite complex reports.
We have two sequences of events from separate data sources. They both contain independent events occurring to vehicles. One describes what location a vehicle is within - the other describes incident events which have a reason code for the incident. The business wants to report on time spent in each location for each reason. Vehicles can change location totally independent of the incident events occurring - and events actually are datetime and occur at random points throughtout day. Each type of event has a startime/endtime and a vehicleID.
Vehicle Location Events
+------------------+-----------+------------+-----------------+----------------+
| LocationDetailID | VehicleID | LocationID | StartDateTime | EndDateTime |
+------------------+-----------+------------+-----------------+----------------+
| 1 | 1 | 1 | 2012-1-1 | 2016-1-1 |
| 2 | 1 | 2 | 2016-1-1 | 2016-4-1 |
| 3 | 1 | 1 | 2016-4-1 | 2016-11-1 |
| 4 | 2 | 1 | 2011-1-1 | 2016-11-1 |
+------------------+-----------+------------+-----------------+----------------+
Vehicle Status Events
+---------+---------------+-------------+-----------+--------------+
| EventID | StartDateTime | EndDateTime | VehicleID | ReasonCodeID |
+---------+---------------+-------------+-----------+--------------+
| 1 | 2012-1-1 | 2013-1-1 | 1 | 1 |
| 2 | 2013-1-1 | 2015-1-1 | 1 | 3 |
| 3 | 2015-1-1 | 2016-5-1 | 1 | 4 |
| 4 | 2016-5-1 | 2016-11-1 | 1 | 2 |
| 5 | 2015-9-1 | 2016-2-1 | 2 | 1 |
+---------+---------------+-------------+-----------+--------------+
Is there anyway I can correlate the two streams together and calculate total time per Vehicle per ReasonCode per location? This would seem to require me to be able to relate the two events - so a change of location may occur part way through a given ReasonCode.
Calculation Example ReasonCodeID 4
VehicleID 1 is in location ID 1 from 2012-1-1 to 2016-1-1 and
2016-4-1 to 2016-11-1
VehicleID 1 is in location ID 2 from 2016-1-1
to 2016-4-1
VehcileID 1 has ReasonCodeID 4 from 2015-1-1 to
2016-5-1
Therefore first Period in location 1 intersects with 365 days of ReasonCodeID 4 (2015-1-1 to 2016-1-1). 2nd period in location 1 intersects with 30 days (2016-4-1 to 2016-5-1).
In location 2 intersects with 91 days of ReasonCodeID 4(2016-1-1 to 2016-4-1
Desired output would be the below.
+-----------+--------------+------------+------------+
| VehicleID | ReasonCodeID | LocationID | Total Days |
+-----------+--------------+------------+------------+
| 1 | 1 | 1 | 366 |
| 1 | 3 | 1 | 730 |
| 1 | 4 | 1 | 395 |
| 1 | 4 | 2 | 91 |
| 1 | 2 | 1 | 184 |
| 2 | 1 | 1 | 154 |
+-----------+--------------+------------+------------+
I have created a SQL fiddle that shows the structure here
Vehicles have related tables and I'm sure the business will want them grouped by vehicle class etc but if I can understand how to calculate the intersection points in this case that would give me the basis for rest of reporting.
I think this solution requires a CROSS JOIN implementation. The relationship between both tables is Many to Many which implies the creation of a third table that bridges LocationEvents and VehicleStatusEvents tables so I think specifying the relationship in the expression could be easier.
I use a CROSS JOIN between both tables, then filter the results only to get those rows which VehicleID columns are the same in both tables. I am also filtering the rows that VehicleStatusEvents range dates intersects LocationEvents range dates.
Once the filtering is done I am adding a column to calculate the count of days between each intersection. Finally, the measure sums up the days for each VehicleID, ReasonCodeID and LocationID.
In order to implement the CROSS JOIN you will have to rename the VehicleID, StartDateTime and EndDateTime on any of both tables. It is necessary for avoiding ambigous column names errors.
I rename the columns as follows:
VehicleID : LocationVehicleID and StatusVehicleID
StartDateTime : LocationStartDateTime and StatusStartDateTime
EndDateTime : LocationEndDateTime and StatusEndDateTime
After this you can use CROSSJOIN in the Total Days measure:
Total Days =
SUMX (
FILTER (
ADDCOLUMNS (
FILTER (
CROSSJOIN ( LocationEvents, VehicleStatusEvents ),
LocationEvents[LocationVehicleID] = VehicleStatusEvents[StatusVehicleID]
&& LocationEvents[LocationStartDateTime] <= VehicleStatusEvents[StatusEndDateTime]
&& LocationEvents[LocationEndDateTime] >= VehicleStatusEvents[StatusStartDateTime]
),
"CountOfDays", IF (
[LocationStartDateTime] <= [StatusStartDateTime]
&& [LocationEndDateTime] >= [StatusEndDateTime],
DATEDIFF ( [StatusStartDateTime], [StatusEndDateTime], DAY ),
IF (
[LocationStartDateTime] > [StatusStartDateTime]
&& [LocationEndDateTime] >= [StatusEndDateTime],
DATEDIFF ( [LocationStartDateTime], [StatusEndDateTime], DAY ),
IF (
[LocationStartDateTime] <= [StatusStartDateTime]
&& [LocationEndDateTime] <= [StatusEndDateTime],
DATEDIFF ( [StatusStartDateTime], [LocationEndDateTime], DAY ),
IF (
[LocationStartDateTime] >= [StatusStartDateTime]
&& [LocationEndDateTime] <= [StatusEndDateTime],
DATEDIFF ( [LocationStartDateTime], [LocationEndDateTime], DAY ),
BLANK ()
)
)
)
)
),
LocationEvents[LocationID] = [LocationID]
&& VehicleStatusEvents[ReasonCodeID] = [ReasonCodeID]
),
[CountOfDays]
)
Then in Power BI you can build a matrix (or any other visualization) using this measure:
If you don't understand completely the measure expression, here is the T-SQL translation:
SELECT
dt.VehicleID,
dt.ReasonCodeID,
dt.LocationID,
SUM(dt.Diff) [Total Days]
FROM
(
SELECT
CASE
WHEN a.StartDateTime <= b.StartDateTime AND a.EndDateTime >= b.EndDateTime -- Inside range
THEN DATEDIFF(DAY, b.StartDateTime, b.EndDateTime)
WHEN a.StartDateTime > b.StartDateTime AND a.EndDateTime >= b.EndDateTime -- |-----|*****|....|
THEN DATEDIFF(DAY, a.StartDateTime, b.EndDateTime)
WHEN a.StartDateTime <= b.StartDateTime AND a.EndDateTime <= b.EndDateTime -- |...|****|-----|
THEN DATEDIFF(DAY, b.StartDateTime, a.EndDateTime)
WHEN a.StartDateTime >= b.StartDateTime AND a.EndDateTime <= b.EndDateTime -- |---|****|-----
THEN DATEDIFF(DAY, a.StartDateTime, a.EndDateTime)
END Diff,
a.VehicleID,
b.ReasonCodeID,
a.LocationID --a.StartDateTime, a.EndDateTime, b.StartDateTime, b.EndDateTime
FROM LocationEvents a
CROSS JOIN VehicleStatusEvents b
WHERE a.VehicleID = b.VehicleID
AND
(
(a.StartDateTime <= b.EndDateTime)
AND (a.EndDateTime >= b.StartDateTime)
)
) dt
GROUP BY dt.VehicleID,
dt.ReasonCodeID,
dt.LocationID
Note in T-SQL you could use an INNER JOIN operator too.
Let me know if this helps.
select coalesce(l.VehicleID,s.VehicleID) as VehicleID
,s.ReasonCodeID
,l.LocationID
,sum
(
datediff
(
day
,case when s.StartDateTime > l.StartDateTime then s.StartDateTime else l.StartDateTime end
,case when s.EndDateTime < l.EndDateTime then s.EndDateTime else l.EndDateTime end
)
) as TotalDays
from VehicleLocationEvents as l
full join VehicleStatusEvents as s
on s.VehicleID =
l.VehicleID
and case when s.StartDateTime > l.StartDateTime then s.StartDateTime else l.StartDateTime end <=
case when s.EndDateTime < l.EndDateTime then s.EndDateTime else l.EndDateTime end
group by coalesce(l.VehicleID,s.VehicleID)
,s.ReasonCodeID
,l.LocationID
or
select VehicleID
,ReasonCodeID
,LocationID
,sum (datediff (day,max_StartDateTime,min_EndDateTime)) as TotalDays
from (select coalesce(l.VehicleID,s.VehicleID) as VehicleID
,s.ReasonCodeID
,l.LocationID
,case when s.StartDateTime > l.StartDateTime then s.StartDateTime else l.StartDateTime end as max_StartDateTime
,case when s.EndDateTime < l.EndDateTime then s.EndDateTime else l.EndDateTime end as min_EndDateTime
from VehicleLocationEvents as l
full join VehicleStatusEvents as s
on s.VehicleID =
l.VehicleID
) ls
where max_StartDateTime <= min_EndDateTime
group by VehicleID
,ReasonCodeID
,LocationID

SQL Query Pivot Data 2 rows into 1

I am try to write a query which pivots activity data into summary rows. For example the input data is activity date, description and status of either; Start, Stop or null if it is an informative row.
This is an example of the data format:
ID | ActivityID | ActivityType | ActivityDate | Status | Activity
----------------------------------------------------------------------
701 | 26 | Start | 02/07/13 15:16 | 10 | Run Job
728 | 26 | No Change | 05/07/13 09:30 | 20 | Running
859 | 26 | Stop | 22/07/13 12:45 | 30 | Error
1064 | 26 | Start | 10/08/13 13:26 | 11 | Restarted
1524 | 26 | Stop | 28/08/13 10:19 | 31 | Error
1785 | 26 | Stop | 07/09/13 11:48 | 31 | Error
2205 | 26 | Start | 17/09/13 09:05 | 10 | Restarted
2528 | 26 | Start | 14/10/13 17:56 | 11 | Restarted
2528 | 26 | Stop | 25/10/13 20:47 | 32 | Completed
And this is the expected result:
ActivityID | Start_Type | Start_Date | Start_Status | Start_Activity | Stop_Type | Stop_Date | Stop_Status | Stop_Activity
---------------------------------------------------------------------------------
26 | Start | 02/07/13 15:16 | 10 | Run Job | Stop | 22/07/13 12:45 | 30 | Error
26 | Start | 10/08/13 13:26 | 11 | Restarted | Stop | 28/08/13 10:19 | 31 | Error
26 | Start | 17/09/13 09:05 | 10 | Restarted | Stop | 25/10/13 20:47 | 32 | Done
I want to get all the starts and put the corrosponding stop in the same row as activity_start_date etc. and activity_stop_date.
My problem is I want the first stop after each start which I have done, but I want to also ignore a start that has a start before it. For instants Start, Stop, Stop, Start, Start, Stop, Start would return; Start Stop, Start Stop, Start null.
I have tried a left join to join the 1st stop after a start but this also includes the second start matching the same stop twice.
I thought I needed a temp table but I think this is unnecessary. Would a date variable work where the first start sets the variable then the stop is the first stop after the variable which sets the variable and the next start is the first start after the new set variable?
Your help is appreciated!
This is what I have so far:
SELECT
activity.ID,
activity.ActivityType AS StartActivityType,
activity.ActivityDate AS StartActivityDate,
activity.Status AS StartStatus,
activity.Activity AS StartActivity,
activity2.ActivityType AS StopActivityType,
activity2.ActivityDate AS StopActivityDate,
activity2.Status AS StopStatus,
activity2.Activity AS StopActivity
FROM tempdb..#TempTable activity
FULL OUTER JOIN #TempTable activity2
ON activity.ID = activity2.ID
AND activity2.ActivityType = 'Stop'
AND (activity2.ActivityDate > activity.ActivityDate)
AND (activity2.ActivityDate = ( SELECT MIN(activity3.ActivityDate)
FROM tempdb..#TempTable activity3
WHERE activity.ID = activity2.ID
AND activity3.ActivityType = 'Stop'
AND activity3.ActivityDate > activity.ActivityDate))
WHERE activity.ActivityType = 'Start'
--does not have a start before
AND activity.ActivityDate > ( SELECT MAX(activity3.ActivityDate)
FROM tempdb..#TempTable activity2
WHERE activity2.PathwayID = activity.ID
AND activity2.ActivityType IN ('Stop','Start')
AND activity2.ActivityDate > activity.ActivityDate))
--AND activity2.ActivityDate != LAG(activity2.ActivityDate) OVER (ORDER BY activity.ActivityDate),
--AND activity.ActivityDate IS NULL
ORDER BY activity.ID ASC
select * from #TempTable a1
cross apply
(
select top 1 *
from
#TempTable a2
where
a2.ActivityType = 'Stop'
and a2.ActivityID = a1.ActivityID
and a2.ActivityDate > a1.ActivityDate
order by
a2.ActivityDate
) a2
where
a1.ActivityType = 'Start'
order by
a1.ActivityDate;
You have some oddities in your data that I do not understand, but this should get you 90% there and it's much cleaner than trying to join back on ActivityDate.
The query of shriop is almost correct, it fails where there are following rows with ActivityType = 'Start' like 2205 and 2528. For those the duplicated rows (the rows with the same ActivityType of the precedent) need to be dropped.
If the OP uses SQLServer 2012 or better this can be done using LAG
WITH DATA AS (
SELECT ActivityID
, ActivityType
, ActivityDate
, Status
, Activity
, LastActivity = LAG(ActivityType, 1, 'Stop') OVER (ORDER BY ActivityDate)
FROM Table1
WHERE ActivityType IN ('Start', 'Stop')
)
SELECT a1.ActivityID
, Start_Type = a1.ActivityType
, Start_Date = a1.ActivityDate
, Start_Status = a1.Status
, Start_Activity = a1.Activity
, Stop_Type = a2.ActivityType
, Stop_Date = a2.ActivityDate
, Stop_Status = a2.Status
, Stop_Activity = a2.Activity
FROM DATA a1
CROSS apply (SELECT top 1 *
FROM Table1 a2
WHERE a2.ActivityType = 'Stop'
AND a2.ActivityID = a1.ActivityID
AND a2.ActivityDate > a1.ActivityDate
ORDER BY a2.ActivityDate) a2
WHERE a1.ActivityType = 'Start'
AND a1.LastActivity <> a1.ActivityType
ORDER BY a1.ActivityDate;
Otherwise LAG can be simulated by row numbering and self join

Multiple select or distinct

I am new to sql so looking for a little help - got the first part down however I am having issues with the second part.
I got the three tables tied together. First I needed to tie tblPatient.ID = tblPatientVisit.PatientID together to eliminate dups which works
Now I need to take those results and eliminate dups in the MRN but my query is only returning one result which is WRONG - LOL
Query
select
tblPatient.id,
tblPatient.firstname,
tblPatient.lastname,
tblPatient.dob,
tblPatient.mrn,
tblPatientSmokingScreenOrder.SmokeStatus,
tblPatientVisit.VisitNo
from
tblPatient,
tblPatientSmokingScreenOrder,
tblPatientVisit
Where
tblPatient.ID = tblPatientVisit.PatientID
and tblPatientVisit.ID = tblPatientSmokingScreenOrder.VisitID
and tblPatient.ID in(
Select Distinct
tblPatient.mrn
From
tblPatient
where
isdate(DOB) = 1
and Convert(date,DOB) <'12/10/2000'
and tblPatientVisit.PatientType = 'I')
Actual Results:
ID | firstName | LastName | DOB | MRN | SmokeStatus | VisitNO
12 | Test Guy | Today | 12/12/1023 | 0015396 | Never Smoker | 0013957431
Desired Results:
90 | BOB | BUILDER | 02/24/1974 | 0015476 | Former Smoker | 0015476001
77 | DORA | EXPLORER | 06/04/1929 | 0015463 | Never Smoker | 0015463001
76 | MELODY | VALENTINE | 09/17/1954 | 0015461 | Current | 0015461001
32 | STRAWBERRY | SHORTCAKE | 07/06/1945 | 0015415 | Current | 0015415001
32 | STRAWBERRY | SHORTCAKE | 07/06/1945 | 0015415 | Never Smoker | 0015415001
32 | STRAWBERRY | SHORTCAKE | 07/06/1945 | 0015415 | Former Smoker | 0015415001
12 | Test Guy | Today | 12/12/1023 | 0015345 | Never Smoker | 0013957431
Anyone have any suggestions on how I go down to the next level and get all the rows with one unique MRN. From the data above I should have 5 in my list. Any help would be appreciated.
Thanks
If I had to guess -- The only thing that looks odd (but maybe it's OK) is that you're comparing patient.ID from your parent query to patient.mrn in the subquery.
Beyond that -- things to check:
(1)
Do you get all your patients with the inner query?
Select Distinct
tblPatient.mrn
From
tblPatient
where
isdate(DOB) = 1
and Convert(date,DOB) <'12/10/2000'
and tblPatientVisit.PatientType = 'I'
(2)
What is your patient type for the missing records? (Your filtering it to tblPatientVisit.PatientType = 'I' -- do the missing records have that patient type as well?)
Perhaps you need to invert the logic here. As in,
Select Distinct
patients.mrn1
From (select
tblPatient.id as id1,
tblPatient.firstname as firstname1,
tblPatient.lastname as lastname1,
tblPatient.dob as DOB1,
tblPatient.mrn as mrn1,
tblPatientSmokingScreenOrder.SmokeStatus as SmokeStatus1,
tblPatientVisit.VisitNo as VisitNo1,
tblPatientVisit.PatientType as PatientType1,
from
tblPatient,
tblPatientSmokingScreenOrder,
tblPatientVisit
Where
tblPatient.ID = tblPatientVisit.PatientID
and tblPatientVisit.ID = tblPatientSmokingScreenOrder.VisitID
) as patients
where
isdate(patients.DOB1) = 1
and Convert(date,patients.DOB1) <'12/10/2000'
and patients.PatientType1 = 'I');
Cleaned it up a bit and I think they were right. MRN wont match patient id, at least not from your example data. You should not need an inner query. This query should give you what you want.
SELECT DISTINCT
p.id,
p.firstname,
p.lastname,
p.dob,
p.mrn,
s.SmokeStatus,
v.VisitNo
FROM
tblPatient p
JOIN tblPatientVisit v ON p.id = v.patientId
JOIN tblPatientSmokingScreenOrder s ON v.id = s.visitId
WHERE
isdate(p.DOB) = 1
AND CONVERT(date,p.DOB) <'12/10/2000'
AND v.PatientType = 'I'

Restrict number of listings in SQL

We have a top ten video list that is displayed on our dashboard. Sometimes we have a situation where one user has posted 6 videos and all of them have made it into the top ten (Most Views). My boss only wants to display the top 2 from any one member in the top ten.
How does one do that. Here is the script I am using to extract the data:
SELECT a.MediaID,
COUNT(a.MediaID) AS TimesViewed
FROM MediaViewLog a
INNER JOIN MemberVideo b ON b.MemberVideoID = a.MediaID
INNER JOIN Member c ON c.MemberID = b.MemberID
WHERE a.ViewDate BETWEEN '5/25/2013 10:04:23 AM' AND '12/12/2050 11:59:59 PM'
AND a.MediaType = 'V'
AND b.CreateDate > '5/25/2013 10:04:23 AM'
AND c.SBIcon = 'N'
GROUP BY a.MediaID
MediaViewLog contains the following data:
+----------------+---------+-----------+-------------------------+
| MediaViewLogID | MediaID | MediaType | ViewDate |
+----------------+---------+-----------+-------------------------+
| 3336 | 7033 | V | 2013-05-26 03:36:52.573 |
+----------------+---------+-----------+-------------------------+
| 3337 | 7037 | V | 2013-05-26 04:22:16.682 |
+----------------+---------+-----------+-------------------------+
| 3338 | 12356 | A | 2013-05-26 03:36:52.573 |
+----------------+---------+-----------+-------------------------+
| 3339 | 7102 | V | 2013-05-26 07:12:25.428 |
+----------------+---------+-----------+-------------------------+
| 3340 | 7058 | V | 2013-05-26 08:02:27.003 |
+----------------+---------+-----------+-------------------------+
| 3341 | 7033 | B | 2013-05-26 03:36:52.573 |
+----------------+---------+-----------+-------------------------+
Each Media is associated with a Member, in the "Member" Table. "MemberID" is the Primary Key
Any ideas how I can only extract the top 2 if there is more than one for any member, as part of the top 10.
Any help would greatly appreciated.
Thanks!
For this, use the row_number() function:
select t.MediaId, t.TimesViewed
from (SELECT a.MediaID, c.MemberId, COUNT(a.MediaID) AS TimesViewed,
row_number() over (partition by c.MemberId order by count(a.MediaId) desc
) as seqnum
FROM MediaViewLog a
INNER JOIN MemberVideo b ON b.MemberVideoID = a.MediaID
INNER JOIN Member c ON c.MemberID = b.MemberID
WHERE a.ViewDate BETWEEN '5/25/2013 10:04:23 AM' AND '12/12/2050 11:59:59 PM'
AND a.MediaType = 'V'
AND b.CreateDate > '5/25/2013 10:04:23 AM'
AND c.SBIcon = 'N'
GROUP BY a.MediaID, c.MemberId
) t
where seqnum <= 2;
This is assuming that each video is posted by only one member. That is, that grouping by MediaId and by a.MediaId, c.MemberId is really the same thing. Otherwise, you are mixing counts from different users (on the "posting" side of the video, not the viewing), and extracting only two for each user is much harder.
I also think there should be a top 10 in there somewhere.