2

我正在 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
4

3 回答 3

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 小提琴演示

这会给你:

| 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、使用子查询或相关子查询的情况下编写它。

于 2013-01-01T08:04:21.587 回答
1
SELECT InputCurrID,Date,OpCurrencyName,IpCurrencyName
,(Select Count(*) from InputCurr c2 where c2.OpCurrencyName=InputCurr.OpCurrencyName) as [Count]
FROM InputCurr
Order by InputCurrID
于 2013-01-01T08:08:27.150 回答
1
select [InputCurrID],
   [Date],
   [OpCurrencyName],
   [IpCurrencyName],
   count(*) over (partition by [OpCurrencyName],[IpCurrencyName])
from [InputCurr]
order by [InputCurrID]

SQL小提琴

于 2013-01-01T16:31:20.390 回答