在后续行中跳过相同值的输出

时间:2012-02-29 08:22:25

标签: sql-server-2005

朋友

我希望通过tbale tblABC

的sql查询输出以下类型的输出
  OUTPUT
  ======

  SrNo.         Date               BillNo            Amount
  =================================================
     1         10-02-2012            VAT-1            1200.00
     2                               RET-4              50.00
     3                               RET-5             150.50
     4         11-02-2012            VAT-2            1500.00
     5                               RET-6            2505.00
     6         12-02-2012            VAT-3              05.00

来自具有以下数据的表

     tblABC
     ======

     SrNo.         Date               BillNo            Amount
     =================================================
     1         10-02-2012            VAT-1            1200.00
     2         10-02-2012            RET-4              50.00
     3         10-02-2012            RET-5             150.50
     4         11-02-2012            VAT-2            1500.00
     5         11-02-2012            RET-6            2505.00
     6         12-02-2012            VAT-3              05.00

我想要的OUTPUT和表之间的差异在于 输出我不希望日期重复,因为它在表中。

请帮助我怎么能拥有它。

2 个答案:

答案 0 :(得分:1)

DECLARE @tblABC TABLE
(
    SrNo INT, [Date] DATETIME, BillNo CHAR(5), Amount DECIMAL(10,2)
);

INSERT @tblABC SELECT 1,'20120210','VAT-1',1200.00
UNION ALL SELECT 2,'20120210','RET-4',50.00
UNION ALL SELECT 3,'20120210','RET-5',150.50
UNION ALL SELECT 4,'20120211','VAT-2',1500.00
UNION ALL SELECT 5,'20120211','RET-6',2505.00
UNION ALL SELECT 6,'20120212','VAT-3',05.00;

;WITH x AS 
(
  SELECT SrNo, [Date] = REPLACE(CONVERT(CHAR(10), [Date], 103), '/', '-'), 
    BillNo, Amount, rn = ROW_NUMBER() OVER (PARTITION BY [Date] ORDER BY SrNo)
    FROM @tblAbC
)
SELECT SrNo, [Date] = CASE rn WHEN 1 THEN [Date] ELSE '' END, BillNo, Amount
  FROM x 
  ORDER BY SrNo;

答案 1 :(得分:-1)

在select语句中使用Case关键字。 http://msdn.microsoft.com/en-us/library/ms181765.aspx