Tuesday, September 2, 2014

Query Behavior

Essentially, you are requesting a running total of unique terr_cd.


I think what you actually want is something like this:



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 *, (SELECT COUNT(*) FROM @temp WHERE t.terr_cd = terr_cd) AS cnt
FROM @temp t


No comments:

Post a Comment