?
Solved

SQL Server 2005 Distinct On One Column

Posted on 2010-09-02
5
Medium Priority
?
181 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 1000 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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