我有一个问题,我已经工作了几天,感谢这个问题SQL SERVER T-SQL Calculate SubTotal and Total by group我已经能够得到我的小计和我的总数。
当此数据返回到我的自定义应用程序(我无法控制的应用程序)时,它会通过ID列对返回的数据进行排序,忽略我的ORDER BY子句。
要解决这个问题,我想在我的TSQL中添加一个哑数,这样无论找到哪一个行,找不到找到的每一行,计数器都会加1。
我的代码如下。
select ID as ID, EstRefNum as "Estimate No",
(case when GROUPING(Ordered) = 0 and
GROUPING(Ref1) = 0 and
GROUPING(ID) = 1
then
case when Ref1 IS NULL
then 'No BDM Sub Total'
else Ref1 + ' Sub Total'
end
when GROUPING(Ordered) = 0 and
GROUPING(Ref1) = 1 and
GROUPING(ID) = 1
then
CASE
WHEN Ordered = 0 THEN 'Not Ordered Sub Total'
WHEN Ordered = 1 THEN 'Ordered Sub Total'
WHEN Ordered = 2 THEN 'Superseded Sub Total'
WHEN Ordered = 4 THEN 'Won/Convert Sub Total'
WHEN Ordered = 5 THEN 'Lost Sub Total'
WHEN Ordered IS NULL THEN 'None Sub Total'
ELSE 'Unknown status code'
END
when GROUPING(Ordered) = 1 and
GROUPING(Ref1) = 1 and
GROUPING(ID) = 1
then
'Total'
else
case
when Ref1 IS NULL
then 'No BDM'
else Ref1
end
end) as "Sales Rep",
[mo].[EstimateDate] as "Estimate Date",
[mo].[CustomerRef] as "Customer",
[mo].[JobDescription] as "Title",
cast(Format([mo].[OtherTotal], 'N', 'en-us') as varchar(30)) as "Estimate total",
[mo].[TotalTotal] as "Sales",
[mo].[PaperSubTot] + [mo].[OriginMatSubTot] + [mo].[OtherMatSubTotal] + [mo].[OutworkSubtot] as "Direct Costs",
[mo].[TotalTotal] - ([mo].[PaperSubTot] + [mo].[OriginMatSubTot] + [mo].[OtherMatSubTotal] + [mo].[OutworkSubtot]) as "Value Added",
([mo].[OriginLabLabSubTot] + [mo].[PrintingSubTotal] + [mo].[FinishingSubTotal]) as "Overheads",
([mo].[TotalTotal] - ([mo].[PaperSubTot] + [mo].[OriginMatSubTot] + [mo].[OtherMatSubTotal] + [mo].[OutworkSubtot])) - ([mo].[OriginLabLabSubTot] + [mo].[PrintingSubTotal] + [mo].[FinishingSubTotal]) as "Profit",
sum(TotalTotal) as SubTotal,
CASE
WHEN [mo].[OtherTotal] = 0.000000 THEN '0.00'
WHEN [mo].[OtherTotal] = .00 THEN '0.00'
WHEN [mo].[OtherTotal] = 0.00 THEN '0.00'
ELSE LTRIM(Str(CAST([mo].[OtherTotal] as decimal(18,2)), 25, 2))
END AS 'CASE EST TOTAL',
CASE
WHEN Ordered = 0 THEN 'Not Ordered'
WHEN Ordered = 1 THEN 'Ordered'
WHEN Ordered = 2 THEN 'Superseded'
WHEN Ordered = 4 THEN 'Won/Convert'
WHEN Ordered = 5 THEN 'Lost'
WHEN Ordered IS NULL THEN 'None'
ELSE 'Unknown status code'
END AS 'Estimate Status'
From [dbo].[MainEstimateDetails] [mo] WITH (NOLOCK)
WHERE
[mo].[Ref1] LIKE 'STAFF MEMBER NAME'
group by grouping sets((Ordered, Ref1, ID, EstRefNum,EstimateDate, CustomerRef, JobDescription, OtherTotal, TotalTotal, PaperSubTot, OriginMatSubTot, OtherMatSubTotal, OutworkSubtot, OriginLabLabSubTot, PrintingSubTotal, FinishingSubTotal),
(Ordered, Ref1),
()
)
order by 'Estimate Status' Desc;
它返回如下数据。
我想要的只是左侧的计数如下
如何忽略group by命令获取返回的行数?我已成功设法获得每组内的项目数量,这些项目很接近,但不是我想要的。我只是无法弄清楚如何操作变量,以便在每个循环中通过select语句生成的数据库加1。
我发现问题和答案接近我需要的东西,但似乎没有什么似乎是银弹。
由于 迪安
答案 0 :(得分:1)
正如评论所说,您只需添加如下所示的ROW_NUMBER列。它会为每一行增加一个整数。
select ID as ID, EstRefNum as "Estimate No",
(case when GROUPING(Ordered) = 0 and
GROUPING(Ref1) = 0 and
GROUPING(ID) = 1
then
case when Ref1 IS NULL
then 'No BDM Sub Total'
else Ref1 + ' Sub Total'
end
when GROUPING(Ordered) = 0 and
GROUPING(Ref1) = 1 and
GROUPING(ID) = 1
then
CASE
WHEN Ordered = 0 THEN 'Not Ordered Sub Total'
WHEN Ordered = 1 THEN 'Ordered Sub Total'
WHEN Ordered = 2 THEN 'Superseded Sub Total'
WHEN Ordered = 4 THEN 'Won/Convert Sub Total'
WHEN Ordered = 5 THEN 'Lost Sub Total'
WHEN Ordered IS NULL THEN 'None Sub Total'
ELSE 'Unknown status code'
END
when GROUPING(Ordered) = 1 and
GROUPING(Ref1) = 1 and
GROUPING(ID) = 1
then
'Total'
else
case
when Ref1 IS NULL
then 'No BDM'
else Ref1
end
end) as "Sales Rep",
[mo].[EstimateDate] as "Estimate Date",
[mo].[CustomerRef] as "Customer",
[mo].[JobDescription] as "Title",
cast(Format([mo].[OtherTotal], 'N', 'en-us') as varchar(30)) as "Estimate total",
[mo].[TotalTotal] as "Sales",
[mo].[PaperSubTot] + [mo].[OriginMatSubTot] + [mo].[OtherMatSubTotal] + [mo].[OutworkSubtot] as "Direct Costs",
[mo].[TotalTotal] - ([mo].[PaperSubTot] + [mo].[OriginMatSubTot] + [mo].[OtherMatSubTotal] + [mo].[OutworkSubtot]) as "Value Added",
([mo].[OriginLabLabSubTot] + [mo].[PrintingSubTotal] + [mo].[FinishingSubTotal]) as "Overheads",
([mo].[TotalTotal] - ([mo].[PaperSubTot] + [mo].[OriginMatSubTot] + [mo].[OtherMatSubTotal] + [mo].[OutworkSubtot])) - ([mo].[OriginLabLabSubTot] + [mo].[PrintingSubTotal] + [mo].[FinishingSubTotal]) as "Profit",
sum(TotalTotal) as SubTotal,
CASE
WHEN [mo].[OtherTotal] = 0.000000 THEN '0.00'
WHEN [mo].[OtherTotal] = .00 THEN '0.00'
WHEN [mo].[OtherTotal] = 0.00 THEN '0.00'
ELSE LTRIM(Str(CAST([mo].[OtherTotal] as decimal(18,2)), 25, 2))
END AS 'CASE EST TOTAL',
CASE
WHEN Ordered = 0 THEN 'Not Ordered'
WHEN Ordered = 1 THEN 'Ordered'
WHEN Ordered = 2 THEN 'Superseded'
WHEN Ordered = 4 THEN 'Won/Convert'
WHEN Ordered = 5 THEN 'Lost'
WHEN Ordered IS NULL THEN 'None'
ELSE 'Unknown status code'
END AS 'Estimate Status',
ROW_NUMBER() OVER(ORDER BY CASE
WHEN Ordered = 0 THEN 'Not Ordered'
WHEN Ordered = 1 THEN 'Ordered'
WHEN Ordered = 2 THEN 'Superseded'
WHEN Ordered = 4 THEN 'Won/Convert'
WHEN Ordered = 5 THEN 'Lost'
WHEN Ordered IS NULL THEN 'None'
ELSE 'Unknown status code'
END) AS RowID
From [dbo].[MainEstimateDetails] [mo] WITH (NOLOCK)
WHERE
[mo].[Ref1] LIKE 'STAFF MEMBER NAME'
group by grouping sets((Ordered, Ref1, ID, EstRefNum,EstimateDate, CustomerRef, JobDescription, OtherTotal, TotalTotal, PaperSubTot, OriginMatSubTot, OtherMatSubTotal, OutworkSubtot, OriginLabLabSubTot, PrintingSubTotal, FinishingSubTotal),
(Ordered, Ref1),
()
)
order by 'Estimate Status' Desc;