Solved

What to compare two table items upto a certain date

Posted on 2010-11-11
4
703 Views
Last Modified: 2012-05-10
Hi, I am creating a Crystal report and I need to select information between two tables. The second table tableB is actually audit trial information which I need to compare against the first tableA and from that work out the differences for Quater1, Quarter 2 etc information.

At moment I have a problem after I use an inner join I have more than one row of data from the same row item from tableA. This is working correctly as it is showing is showing all the different times and data each item in tableA was updated. So if one item was updated 5 times in a year it will show 5 rows of data with timestamps for each time. But I need to only show one unique row depending on which date I need.

So for one of my query’s I need to know only the latest updated item. And then I need to know what was the last item update up to a certain date for Quater1, Quater2 etc.

There is something else I should mention. To get the information I require from tableB i have to use 2 PK and FK to join the tables. So I join with both ReviewID and then a RecordID from tableA and link those to table 2 (AUDIT)  ReviewID and RecordID.

One table holds all the data items (TableA) that I want to report on. I then need to retrieve Audit history from another table (AUDIT).

I would like to create a select statement that would choose the latest time stamped item, so that only one row show for each of the tableA items. The problem is that the TableA may be null or  it could have 1 to many values linking to the TableA reviewID and recoredID row.
Please see attached example data:
There are three tabs, TableA, Audit table, and a Inner join of the two tables. On the third tab, you will see 2 of the table values are coloured in blue were the rows of information are the same however the timestamp and also the scores of the data are different. This is only a small sample of the data, so the problem only shows once. However I want to be able to report only the latest instance of that data up to a certain date. For example here I would like to compare the Current scoreA with last score which would be OLDSCOREA. This is current score of 12 and old score of 3. But trying to get the data to only show once is important. How do I restrict the other duplicate Audit table items appearing from other date periods?
 TABLEA-AND-AUDIT-DATA-EXAMPLE.xls
0
Comment
Question by:mumbles22
  • 2
4 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 34117250
you should use

select * from (
select *, row_number() over (partition by ReviewName order by TimeStamp desc) rn
where TimeStamp > getdate()-30
) x where rn=1

this will give you only latest (in last 30 days) records per ReviewName

the rest you can find it out I guess...
0
 

Author Comment

by:mumbles22
ID: 34119849
This is brillent, just what I was looking for and almost 95% there. However I seem to be a couple of steps short of my desired output.
1. I don't seem to be able to filter using the rn column? When I enter rn=1 into the where clause I get this message: Msg 207, Level 16, State 1, Line 37
Invalid column name 'RN'.
At moment this is the biggest issue because I don't seem to be able to retrieve just the number 1 item.

2. The second problems is that I get the RowNumber of all the record items, however this also gives me row numbers for the same recordID's which appear in other reviews. So I am unable to just retrieve the top 1 record because it would exclude items from other reviews which for example might have a rn of 3 for the top record. Is there a further step to restart the rn on different reviews as well for the same recordid?

This is my code so far:
Select
RV.REVIEWNAME,
RV.[Name(name)],
RV.ScoreAresult
 AT.FIELDNAME,
 AT.TS,
 AT.RECORDID,
 AT.[CHANGEDFROM],
 RV.[number(number)],
row_number() over (partition by RV.RECORDID order by AT.TS desc) as RN
from TABLEA RV inner join tableB AT on RV.RECORDID = AT.RECORDID
where RV.REVIEWID = AT.REVIEWID
--and RV.REVIEWNAME='London'
and AT.FIELDNAME='scoreA'
--and RN=1

works really well except for when I remove the filter for the london review the same record that would also appear in other reviews has row counts continued rather than starting at 1 for each review?
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34123379
you should wrap the query in another select

select * from (
  select *, row_number() over (partition by ReviewName order by TimeStamp desc) rn
  where TimeStamp > getdate()-30
) x where rn=1

try again...
0
 
LVL 41

Expert Comment

by:ralmada
ID: 34123410
you need to add another clause to your partition by there.
select * from 

(

	Select 

		RV.REVIEWNAME, 

		RV.[Name(name)], 

		RV.ScoreAresult

		AT.FIELDNAME, 

		AT.TS, 

		AT.RECORDID, 

		AT.[CHANGEDFROM], 

		RV.[number(number)], 

		row_number() over (partition by RV.REVIEWNAME, RV.[Name(name)] order by AT.TS desc) as RN

	from TABLEA RV 

	inner join tableB AT on RV.RECORDID = AT.RECORDID

	where RV.REVIEWID = AT.REVIEWID

	and AT.FIELDNAME='scoreA'

	and RV.REVIEWNAME='London'

) x

where RN=1

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now