mishap
mishap

Reputation: 8505

Grouping on multiple columns

I have a table with following columns:

Id(int), StoreId(int), StoreCode(nvarchar), StartDate(date), EndDate(date)

It has some records with same StoreId, StartDate, EndDate, but different StoreCodes. I wold like to select these store codes (for which rest of the data is duplicated in two or more records). How can I group this properly ?

Upvotes: 0

Views: 128

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

SELECT  StoreId, StoreDate, EndDate, COUNT(DISTINCT StoreCode) AS Count
FROM MyTableName
GROUP BY StoreId, StoreDate, EndDate
HAVING COUNT(DISTINCT StoreCode) > 1

Will return (StoreId, StoreDate, EndDate) combination and number of DISTINCT StoreCodes associated with that combination when there are at least 2 occurrences.

Upvotes: 2

Related Questions