Solved

SQL Server 2005 Distinct On One Column

Posted on 2010-09-02
5
180 Views
Last Modified: 2012-10-24
I need to write a query to display distinct values. Here is the scenario:

I have a Store table and a StoreSpecials table. In the Store table I have the following columns StoreID, StoreName, ParentID. In the StoreSpecials table I have the following columns StoreID and SpecialID. The ParentID equals 0 if the store is a prent and equals the ID StoreID of the parent for child stores. In the StoreSpecials table there can be many SpecialIDs for a group of stores. here is some sample data:

Store table
ID      Name         ParentID
287   Safeway   0
343   Safeway   287
526   Safeway   287

StoreSpecials table
StoreID   SpecialID
287         1
287         2
343         1
343         2
343         3
526         1
526         2
526         4

I need a query that displays the distinct SpecialIDs where the distinct SpecialIDs display for the parent instead of the children in the case where the same SpecialID is spread across multiple StoreIDs.

Thanks, Erich Lemke

0
Comment
Question by:elemke
[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
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33590600
can you show a sample of your expected output?
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33590685
Try This

SELECT distinct Id,SpecialID
FROM Store S INNER JOIN StoreSpecials SS
ON S.Id = SS.StoreId
WHERE S.ParentId = 0
0
 

Author Comment

by:elemke
ID: 33590869
Based on the sample data above the expected out would be:

StoreID   SpecialID   ParentID
287         1               0
287         2               0
343         3               287
526         4               287
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 33591968
Try with row_number() windowing function and over() analytical clause.
;WITH specialsCTE
AS
(
SELECT sp.StoreID, sp.SpecialID, st.ParentID
     , ROW_NUMBER()
          OVER(PARTITION BY sp.SpecialID, 
               COALESCE(NULLIF(st.ParentID, 0), sp.StoreID)
               ORDER BY st.ParentID, sp.StoreID) rn
FROM Store st
JOIN StoreSpecials sp ON sp.StoreID = st.ID
)
SELECT StoreID, SpecialID, ParentID
FROM specialsCTE
WHERE rn = 1
;

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33591975
For an explanation of the above, please read this article: Analytical SQL : Where do you rank?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

627 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