Solved

SQL Server 2005 Distinct On One Column

Posted on 2010-09-02
5
173 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
5 Comments
 
LVL 69

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 59

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 59

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Authentication and Win NT Authentication Issues 20 39
STDEVP in SQL 2 36
View Sql in stored procedure 13 20
Row-Level Security 2 19
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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