Comment find des lignes nombre de groupes?

Je crée une procédure stockée dans SQL et je suis en difficulté pour find le count en utilisant le groupement.

Voici le schéma de ma database:

CREATE TABLE InputCurr ( [InputCurrID] int IDENTITY(1, 1) NOT NULL, [Date] datetime NULL, [OpCurrencyName] decimal(18, 8) NULL, [IpCurrencyName] varchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Count] int NULL, ) 

Datavalues:

 insert into InputCurr ( Date,OpCurrencyName,IpCurrencyName ) values ('1/1/2013','US','$'); insert into InputCurr ( Date,OpCurrencyName,IpCurrencyName ) values ('1/2/2013','US','$'); insert into InputCurr ( Date,OpCurrencyName,IpCurrencyName ) values ('1/3/2013','UK','#'); insert into InputCurr ( Date,OpCurrencyName,IpCurrencyName ) values ('1/4/2013','US','$'); insert into InputCurr ( Date,OpCurrencyName,IpCurrencyName ) values ('1/5/2013','US','$'); 

J'utilise le SP suivant:

 CREATE PROCEDURE dbo.getCurrencyReportFiels AS BEGIN SELECT InputCurrID,Date,OpCurrencyName,IpCurrencyName,Count=(COUNT(*)) FROM InputCurr GROUP BY InputCurrID,date,OpCurrencyName,IpCurrencyName END 

Je reçois O / p comme ceci:

 InputCurrID Date OpCurrencyName IpCurrencyName Count ----------------------------------------------------------------------- 1 1/1/2013 US $ 1 <--here i want count of grouping by OpCurrencyName & IpCurrencyName 2 1/2/2013 US $ 1 <--here i want count of grouping by OpCurrencyName & IpCurrencyName 3 1/3/2013 UK # 1 4 1/4/2013 US $ 1<--here i want count of grouping by OpCurrencyName & IpCurrencyName 5 1/5/2013 US $ 1<--here i want count of grouping by OpCurrencyName & IpCurrencyName 

Maintenant, je veux une sortie comme ça …

 InputCurrID Date OpCurrencyName IpCurrencyName Count ----------------------------------------------------------------------- 1 1/1/2013 US $ 4 2 1/2/2013 US $ 4 3 1/3/2013 UK # 1 4 1/4/2013 US $ 4 5 1/5/2013 US $ 4 

Tu peux le faire:

 WITH Groups AS ( SELECT OpCurrencyName, IpCurrencyName, Count=(COUNT(*)) FROM InputCurr GROUP BY OpCurrencyName,IpCurrencyName ) SELECT i.InputCurrID, i.Date, g.OpCurrencyName, g.IpCurrencyName, g.Count FROM InputCurr i INNER JOIN Groups g ON i.OpCurrencyName = g.OpCurrencyName AND i.IpCurrencyName = g.IpCurrencyName ORDER BY i.InputCurrID; 

Démo de Fiddle SQL

Cela vous donnera:

 | INPUTCURRID | DATE | OPCURRENCYNAME | IPCURRENCYNAME | COUNT | ------------------------------------------------------------------------------------------ | 1 | January, 01 2013 00:00:00+0000 | US | $ | 4 | | 2 | January, 02 2013 00:00:00+0000 | US | $ | 4 | | 3 | January, 03 2013 00:00:00+0000 | UK | # | 1 | | 4 | January, 04 2013 00:00:00+0000 | US | $ | 4 | | 5 | January, 05 2013 00:00:00+0000 | US | $ | 4 | 

Vous pouvez l'écrire sans CTE, en utilisant une sous-requête ou avec une sous-requête corrélée.

 SELECT InputCurrID,Date,OpCurrencyName,IpCurrencyName ,(Select Count(*) from InputCurr c2 where c2.OpCurrencyName=InputCurr.OpCurrencyName) as [Count] FROM InputCurr Order by InputCurrID 
 select [InputCurrID], [Date], [OpCurrencyName], [IpCurrencyName], count(*) over (partition by [OpCurrencyName],[IpCurrencyName]) from [InputCurr] order by [InputCurrID] 

SQL Fiddle