【发布时间】:2015-03-12 21:42:33
【问题描述】:
你好,我已经处理这个查询很多天了,最后除了 1 个问题之外几乎完成了。
它给了我这个输出
N ConductorName Denomination totaltransactions totalamount
1 NULL NULL 1882 41610.00
1 Imran 30.00 199 5970.00
2 NULL Imran total 199 5970.00
1 Shoaib 30.00 99 2970.00
2 NULL Shoaib total 99 2970.00
1 Umair 10.00 792 7920.00
2 Umair 15.00 396 5940.00
3 Umair 30.00 99 2970.00
4 Umair 40.00 99 3960.00
5 Umair 60.00 198 11880.00
6 NULL Umair total 1584 32670.00
很好,但我想消除 ConductorName 列中的名称重复,即
预期:
N ConductorName Denomination totaltransactions totalamount
1 NULL NULL 1882 41610.00
1 Imran 30.00 199 5970.00
2 NULL Imran total 199 5970.00
1 Shoaib 30.00 99 2970.00
2 NULL Shoaib total 99 2970.00
1 Umair 10.00 792 7920.00
2 NULL 15.00 396 5940.00
3 NULL 30.00 99 2970.00
4 NULL 40.00 99 3960.00
5 NULL 60.00 198 11880.00
6 NULL Umair total 1584 32670.00
所以每个名字都有 1 次,但数据完好无损。
SP:
ALTER PROCEDURE [dbo].[ReportConductorPerformance]
@FromDate DATE,
@ToDate DATE
AS
BEGIN
SELECT ROW_NUMBER() OVER (PARTITION BY c.name order by c.name) 'N',CASE WHEN isnull(CAST(T .amount AS varchar(30)), c.name + ' total') LIKE '%total%' THEN NULL ELSE c.name END AS ConductorName,
ISNULL(CAST(T.Amount AS varchar(30)), c.Name + ' total') AS Denomination, COUNT(*) AS totaltransactions, SUM(T.Amount) AS totalamount
FROM dbo.Tickets AS T INNER JOIN
Transport.Conductors AS c ON c.ConductorID = T.Conductor_ID
WHERE CONVERT(DATE,ServerDateTime) BETWEEN @FromDate and @ToDate
GROUP BY c.Name, T.Amount WITH ROLLUP
END
【问题讨论】:
-
@Blam 抱歉,这是什么意思?我应该害怕什么>
标签: sql sql-server tsql stored-procedures sql-server-2012