如何查找组的行数?

时间:2013-01-01 06:12:32

标签: sql sql-server stored-procedures count group-by

我在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,
)

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','$');

我使用以下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

3 个答案:

答案 0 :(得分:3)

你可以这样做:

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;

SQL Fiddle Demo

这会给你:

| 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 |

您可以使用子查询或相关子查询在没有CTE的情况下编写它。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

select [InputCurrID],
   [Date],
   [OpCurrencyName],
   [IpCurrencyName],
   count(*) over (partition by [OpCurrencyName],[IpCurrencyName])
from [InputCurr]
order by [InputCurrID]

SQL Fiddle