按顺序计算每个不同的行

时间:2014-04-07 18:32:11

标签: sql

我正在运行一个显示以下值的SQL查询。

目前结果显示如下:

 AccountNum         FieldName           FieldValue      DisplayOrder
1000    |   SalesYTD    |   0   |   1
1000    |   SalesTTM    |   0   |   1
1000    |   ReturnsYTD  |   0   |   1
1000    |   ReturnsTTM  |   0   |   1
2000    |   SalesYTD    |   0   |   1
2000    |   SalesTTM    |   0   |   1
2000    |   ReturnsYTD  |   0   |   1
2000    |   ReturnsTTM  |   0   |   1

我希望它看起来像这样:

AccountNum         FieldName           FieldValue      DisplayOrder
1000    |   SalesYTD    |   0   |   1
1000    |   SalesTTM    |   0   |   2
1000    |   ReturnsYTD  |   0   |   3
1000    |   ReturnsTTM  |   0   |   4
2000    |   SalesYTD    |   0   |   1
2000    |   SalesTTM    |   0   |   2
2000    |   ReturnsYTD  |   0   |   3
2000    |   ReturnsTTM  |   0   |   4

我做错了什么?示例代码如下。感谢。

Select distinct(accountnum)as AccountID, 
      '|' as bar1,
     'SalesYTD' as FieldName,
      '|' as bar2,
      Isnull(SUB100.YTDGrossSalesTY, 0) as FieldValue,
      '|' as bar3,
      row_number() over(partition by accountnum order by [accountnum]) as DisplayOrder,
      '|' as bar4,
      a.salesgroup as 'RepID'
from Custtable as A 

2 个答案:

答案 0 :(得分:0)

更改PARTITION ORDER BY,我更喜欢使用RANK。同样在SQL SERVER中,DISTINCT不是单列操作。

    Select accountnum as AccountID, 
      '|' as bar1,
     'SalesYTD' as FieldName,
      '|' as bar2,
      Isnull(SUB100.YTDGrossSalesTY, 0) as FieldValue,
      '|' as bar3,
      RANK() over(partition by accountnum order by [Fieldname]) as DisplayOrder,
      '|' as bar4,
      a.salesgroup as 'RepID'
    from Custtable as A 

    Left Join [dbo].[DIRPARTYNAMEPRIMARYADDRESSVIEW] DPA with(Nolock)
    on a.party = dpa.party
    and a.PARTITION = DPA.PARTITION
    and [VALIDTO] > Getdate() 

    Left Join 
    (
    Select 
           cij.ORDERACCOUNT,

       SUM(Case when cij.dataareaid = 'CHE' Then 
                (Case When @currency in (2, 3) Then (cij.salesbalance)
                     Else (cij.salesbalancemst)
                 End)                  
                When cij.dataareaid = 'CHS' Then 
                     (Case When @currency = 1 Then (cij.salesbalancemst * (Case When cij.CURRENCYCODE = 'USD' Then 100 Else Isnull(ex.exchrate,1) End)/100)
                          Else (cij.salesbalancemst)
                      End)
                When cij.dataareaid = 'CHJ' Then 
                     (Case When @currency = 1 Then (cij.salesbalancemst * (Case When cij.CURRENCYCODE = 'USD' Then 100 Else Isnull(ex.exchrate,1) End)/100)
                          Else (cij.salesbalancemst)
                      End) 
           End) as YTDGrossSalesTY

from dbo.CUSTINVOICEJOUR cij With(Nolock)

LEFT OUTER JOIN [dbo].V_Exchrates EX WITH(NOLOCK)
                          ON ex.[Name] = 'CHE'                          
                          and DatePart(mm, Dateadd(mm, -1, Getdate())) = DatePart(mm, ex.fromdate)
                          and DatePart(yy, Dateadd(mm, -1, Getdate())) = DatePart(yy, ex.fromdate)
                          and DatePart(dd, ex.fromdate) = 1
                                           and ex.Tocurrencycode = 'USD'
                          and ex.fromcurrencycode = cij.CURRENCYCODE

where (cij.dataareaid in ('CHE', 'CHS')
and cij.ORDERACCOUNT not in ('CHS', 'CHJ')  -- Exclude CHS sales
and cij.invoicedate > '2011-05-31' 
and Left(cij.invoiceid,2) != 'CM' 
and Left(cij.invoiceid,2) != 'FT' 
and Datepart(yy, cij.INVOICEDATE) = datepart(yy, Getdate())
and cij.INVOICEDATE <= Getdate())
or
(cij.dataareaid = 'CHJ'
and cij.invoicedate > '2011-05-31' 
and Left(cij.invoiceid,2) != 'CM' 
and Left(cij.invoiceid,2) != 'FT' 
and Datepart(yy, cij.INVOICEDATE) = datepart(yy, Getdate())
and cij.INVOICEDATE <= Getdate()
and Left(cij.ORDERACCOUNT, 2) = 'JP') 

Group by cij.ORDERACCOUNT
)
SUB100 
on SUB100.ORDERACCOUNT = accountnum

答案 1 :(得分:0)

这是我提出的解决方案。

Select distinct(accountnum)as AccountID, 
      '|' as bar1,
     'SalesYTD' as FieldName,
      '|' as bar2,
      Isnull(SUB100.YTDGrossSalesTY, 0) as FieldValue,
      '|' as bar3,
      Fieldname = '1',
      '|' as bar4,
      a.salesgroup as 'RepID'
from Custtable as A