SQL Server组由谁来解决?

时间:2017-11-19 11:24:49

标签: sql sql-server group-by

需要查询帮助来解决这个简单的问题。任务;)

TBL1

Name    TransDate  Credit Debit  
Diaz    01/01/2015  500   200
Diaz    01/05/2015  500   100
Diaz    01/01/2016  500   200
Maya    01/01/2014  500   300
Maya    01/04/2015  500   300
Maya    01/11/2015  500   200
Jane    01/02/2014  500   200
Jane    01/03/2014  500   100
Jane    01/08/2016  500   300

您的查询 - >

结果tbl2:

Name (Credit–Debit)/2014 (Credit–Debit)/2015 (Credit–Debit)/2016
Diaz          0              700                   300
Maya        200              500                     0
Jane        700                0                   200

非常感谢!! d。

3 个答案:

答案 0 :(得分:1)

您可以使用以下

<强>解释

  1. 从日期中提取年份
  2. 查找信用 - 借记
  3. 在2014,2015,2016年应用年度总和
  4. 数据设置

    我使用Credit和Debit的数据类型作为钱,如果你想要你可以使用int。

    declare @data table(
        Name nvarchar(50) not null,
        TransDate date not null,
        Credit money not null,
        Debit money not null
    )
    
    insert into @data(Name,TransDate,Credit,Debit)
    values ( 'Diaz','2015-01-01',500,200),
           ( 'Diaz','2015-05-01',500,100),
           ( 'Diaz','2016-01-01',500,200),
           ( 'Maya','2014-01-01',500,300),
           ( 'Maya','2015-04-01',500,300),
           ( 'Maya','2015-11-01',500,200),
           ( 'Jane','2014-02-01',500,200),
           ( 'Jane','2014-03-01',500,100),
           ( 'Jane','2016-08-01',500,200)
    

    查询

    ;with report as(
        select Name,
        [Year] = datepart(year,TransDate),
        [Credit-Debit] = Credit- Debit
        from @data
    )
    select Name,
           [Credit-Debit/2014]= isnull([2014],0),
           [Credit-Debit/2015]= isnull([2015],0),
           [Credit-Debit/2016]= isnull([2016],0)
    from report
    pivot (
        sum([Credit-Debit])
        for [Year] in([2014],[2015],[2016]
    )) p
    

    结果

    Name    Credit-Debit/2014   Credit-Debit/2015   Credit-Debit/2016
    Diaz    0.00    700.00  300.00
    Jane    700.00  0.00    300.00
    Maya    200.00  500.00  0.00
    

    希望这会对你有所帮助

答案 1 :(得分:0)

假设您只有有限的已知年数,一种方法可能是使用caseaggregation

SELECT
  name,
  COALESCE(SUM(CASE
    WHEN YEAR(TransDate) = '2014' THEN credit - debit
  END), 0) AS credit_debit_2014,
  COALESCE(SUM(CASE
    WHEN YEAR(TransDate) = '2015' THEN credit - debit
  END), 0) AS credit_debit_2015,
  COALESCE(SUM(CASE
    WHEN YEAR(TransDate) = '2016' THEN credit - debit
  END), 0) AS credit_debit_2016
FROM table1
GROUP BY name

<强>结果:

+------+-------------------+-------------------+-------------------+
| name | credit_debit_2014 | credit_debit_2015 | credit_debit_2016 |
+------+-------------------+-------------------+-------------------+
| Diaz |                 0 |               700 |               300 |
| Jane |               700 |                 0 |               200 |
| Maya |               200 |               500 |                 0 |
+------+-------------------+-------------------+-------------------+

<强> DEMO

答案 2 :(得分:0)

我喜欢使用条件聚合来解决这些问题。即使某一年的python3creditdebit,以下内容也应有效:

NULL