?
Solved

Help with Over Partition  in a CTE

Posted on 2013-11-08
5
Medium Priority
?
219 Views
Last Modified: 2013-11-08
Below is a simplified CTE and the result set.

With MstrTable(IDNo, RelNo)
as (Select IDNo, RelNo From TableA where xxx
Union
Select IDNo,RelNo from TableA where yyyy)
Select IDNo,RelNo From MstrTable Order by IDNo, RelNo

Results
IDNo          RelNo
1                   1
2                   1
3                   1
3                   2
4                   1

Desired result Set
IDNo          RelNo
1                   1
2                   1
3                   2
4                   1

I want the result set to contain only one entry for the IDNo and the MAX RelNo (see IDNo = 3 above).
With a single query in the CTE, without the UNION, Over Partition works great. But I do not think that will work as anticipated with the UNION. How do I get the result set to display a single value for IDNo and the Maximum value for RelNo?
Thanks,
pat
0
Comment
Question by:mpdillon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 39633511
Without any understanding of what the where clauses are doing, it's hard to see why you would even need a union with both select statements querying the same table. However, given that it's necessary, would the MAX RelNO come from the first or second select statement or from either? I see no reason why an analytical query can't be used with a common table expression. Perhaps you can provide some sample data, with a little more explanation of the where clauses, and your expected results.
0
 

Author Comment

by:mpdillon
ID: 39634230
Thanks.
I have attached a file which contains create table statements plus populates the tables with data.

The Raw data is:

IDNo   RelNo   LastModDt
 1             1        '10/01/2013'
 1             2        '11/01/2013'
 2             1        '10/07/2013'
3              2        '11/06/2013'
4              1        '11/04/2013'
3              1        '10/21/2013'
2              2        '10/22/2013'

Desired Result
 1             2        '11/01/2013'
 2             2        '10/22/2013'
 3             2        '11/06/2013'
 4             1        '11/04/2013'

My initial CTE looks like:

with mstrTable (IDNo, RelNo, LastModDt)
as
(
Select IDNo, relNo, LastModDt from TableA
union
Select IDNo, Relno, LastModDt from TableB
)
Select IDNo, RelNo, LastModDt from mstrTable

But I cannot figure out how to get the result set down to the Desired result above.

Thanks,
pat
frmProducitonLookup-query.txt
0
 
LVL 32

Expert Comment

by:awking00
ID: 39634344
with mstrTable (IDNo, RelNo, LastModDt)
as
(
Select IDNo, relNo, LastModDt from TableA
union
Select IDNo, Relno, LastModDt from TableB
)
Select IDNo, RelNo, LastModDt from
(Select IDNo, RelNo, LastModDt,
 row_number() over (partition by IDNo order by LastModDt desc) rn
 from mstrTable) as x
where x.rn = 1;
0
 
LVL 32

Accepted Solution

by:
awking00 earned 2000 total points
ID: 39634345
with mstrTable (IDNo, RelNo, LastModDt)
as
(
Select IDNo, relNo, LastModDt from TableA
union
Select IDNo, Relno, LastModDt from TableB
)
Select IDNo, RelNo, LastModDt from
(Select IDNo, RelNo, LastModDt,
 row_number() over (partition by IDNo order by LastModDt desc) rn
 from mstrTable) as x
where x.rn = 1;
0
 

Author Closing Comment

by:mpdillon
ID: 39634440
That was it. Thank you. I replaced the LastModDt in the Partition with RelNo otherwise it worked perfectly.
thanks.
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

765 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