Tuesday, September 2, 2014

Query Behavior

Hi,



DECLARE @temp TABLE
(SEQ nvarchar(10), TERR_CD NVARCHAR(10))

INSERT INTO @temp VALUES ('0000005','0203')
INSERT INTO @temp VALUES ('0000114','0561')
INSERT INTO @temp VALUES ('0000119','0561')
INSERT INTO @temp VALUES ('0000131','0279')
INSERT INTO @temp VALUES ('0000157','0279')
INSERT INTO @temp VALUES ('0000172','0561')

SELECT SEQ, COUNT(SEQ) OVER (PARTITION BY TERR_CD) AS TERR_CNT
FROM @temp

SELECT SEQ, COUNT(SEQ) OVER (PARTITION BY TERR_CD) AS TERR_CNT
FROM @temp
WHERE SEQ = '0000131'

Can someone help me understand this behavior? When there is a where clause, why the count is getting changed? (please note: this is just a sample I have posted). What I need is the TERR count under each SEQ. When I use the normal GROUP by (without partition, the count is all 1). So whats the difference between using a GROUP by and OVER partition?


Thanks





No comments:

Post a Comment