Sql 按顺序在每个不同的行中计数
我正在运行一个显示以下值的SQL查询 目前,结果如下所示:Sql 按顺序在每个不同的行中计数,sql,Sql,我正在运行一个显示以下值的SQL查询 目前,结果如下所示: AccountNum FieldName FieldValue DisplayOrder 1000 | SalesYTD | 0 | 1 1000 | SalesTTM | 0 | 1 1000 | ReturnsYTD | 0 | 1 1000 | ReturnsTTM | 0 | 1
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
通过更改分区的顺序,我更喜欢使用秩。此外,在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
这是我想出的解决办法
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
您不能在“选择列表”中对一列使用distinct,也不必这样做。按accountnum订购毫无意义。感谢您的回复。尝试此操作时,未找到字段名。这可能是因为SalesYTD和不是数据库上的实际数据,而只是该fieldname列的某种填充数据吗?现在我看一下查询:IsnullSUB100.YTDGrossSalesTY,0a,无效,因为查询中没有引用SUB100表。我添加了更多代码,以表示SUB100表的计算。这将重复3次以表示字段名。希望我说的没错。谢谢你的帮助。好吧,FieldName是一个计算列,除了ORDER BY之外,它不能在任何地方使用。