我正在 SQL 中创建一个存储过程,但我无法使用分组来查找计数。
这是我的数据库架构:
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,
)
数据值:
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','$');
我使用以下 SP:
CREATE PROCEDURE dbo.getCurrencyReportFiels
AS
BEGIN
SELECT InputCurrID,Date,OpCurrencyName,IpCurrencyName,Count=(COUNT(*))
FROM InputCurr
GROUP BY InputCurrID,date,OpCurrencyName,IpCurrencyName
END
我得到这样的 O/p:
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
现在,我想要这样的输出......
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