【问题标题】:How do I retrieve data from multiple aggregation tables?如何从多个聚合表中检索数据?
【发布时间】:2017-07-15 17:00:08
【问题描述】:

我有 3 个付款方式表(信用卡 -SADAD-at_place)。

我从所有这些中检索数据,然后使用 Sum 对所有的价格求和

select Payment_Date, count(Payment_Date) as Transaction_COUNT, sum(OWNER_Amount) as OWNER_Amount  , sum (commission) as commission,sum(Total_Amount) As Sub_total
  from (
select format(PAYMENT_POOL_CREDIT.PAYMENT_POOL_CREDIT_DATE,'dd/MM/yyyy') as Payment_Date,PAYMENT_POOL_CREDIT.PAYMENT_POOL_CREDIT_OWNER_MONEY as OWNER_Amount,PAYMENT_POOL_CREDIT.PAYMENT_POOL_CREDIT_TAX_MONEY as commission, PAYMENT_POOL_CREDIT.PAYMENT_POOL_CREDIT_OWNER_MONEY+PAYMENT_POOL_CREDIT.PAYMENT_POOL_CREDIT_TAX_MONEY as Total_Amount from PAYMENT_POOL_CREDIT
union  all
select format(PAYMENT_POOL_SADAD.PAYMENT_POOL_SADAD_DATE,'dd/MM/yyyy') as Payment_Date,PAYMENT_POOL_SADAD.PAYMENT_POOL_SADAD_OWNER_MONEY as OWNER_Amount,PAYMENT_POOL_SADAD.PAYMENT_POOL_SADAD_TAX_MONEY as commission ,PAYMENT_POOL_SADAD.PAYMENT_POOL_SADAD_OWNER_MONEY+PAYMENT_POOL_SADAD.PAYMENT_POOL_SADAD_TAX_MONEY as Total_Amount  from PAYMENT_POOL_SADAD
union all 
select format(PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_DATE,'dd/MM/yyyy') as Payment_Date,PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_OWNER_MONEY as OWNER_Amount,PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_TAX_MONEY as commission,PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_OWNER_MONEY+PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_TAX_MONEY  as Total_Amount from PAYMENT_POOL_AT_PLACE
 ) as t

group by Payment_Date

现在我想要将另一个聚合列加入到上一个查询中 从这个聚合。

select format(PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_DATE,'dd/MM/yyyy') as Payment_Date, sum(PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_OWNER_MONEY+PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_TAX_MONEY)  as Total_Amount_At_Place from PAYMENT_POOL_AT_PLACE
    group by  format(PAYMENT_POOL_AT_PLACE.PAYMENT_POOL_AT_PLACE_DATE,'dd/MM/yyyy') 

谁能帮忙?

这是示例表

PAYMENT_POOL_CREDIT

Payment_Date | OWNER_Amount  |  commission | Total_Amount 
11/02/2017   |    500.00     |    40.00    |    540.00
15/05/2016   |    242.00     |    10.00    |    252.00
11/02/2017   |    100.00     |    30.00    |    130.00
15/05/2016   |    620.00     |    60.00    |    680.00

PAYMENT_POOL_SADAD

Payment_Date | OWNER_Amount  |  commission | Total_Amount 
05/05/2016   |    5000.00    |  200.00     |    5200.00
11/02/2017   |    242.00     |    10.00    |    252.00
15/05/2016   |    430.00     |    30.00    |    460.00
11/02/2017   |    310.00     |    60.00    |    370.00
15/05/2016   |    220.00     |    60.00    |    280.00

PAYMENT_POOL_AT_PLACE

Payment_Date | OWNER_Amount  |  commission | Total_Amount 
17/06/2016   |    2000.00    |  300.00     |    2300.00
15/05/2016   |    500.00     |   200.00    |    700.00
22/06/2016   |    500        |    300.00   |    800.00
17/06/2016   |    2000.00    |    300.00   |    2300.00
15/05/2016   |    500.00     |    200.00   |    700.00

我要找的结果是这样的

Payment_Date |  Transaction_COUNT  | OWNER_Amount  | Total_commission  |  Total_Amount  |  Total_at_palce
05/05/2016   |      1              |    5000.00    |    200.00         |    5200.00     |  NULL
11/02/2017   |      4              |    1052.00    |    140.00         |    1192.00     |  NULL
15/05/2016   |      6              |    2512.00    |    590.00         |    3102.00     |  1400
22/06/2016   |      1              |    500.00     |    300.00         |    800.00      |  800

【问题讨论】:

    标签: sql sql-server database join aggregate-functions


    【解决方案1】:

    您可以向union all 派生表添加一列来区分来源,而不是加入这些查询,因此您可以使用条件聚合来分别从payment_pool_at_place 获取total_amount,如下所示:

    select Payment_Date
      , count(Payment_Date) as Transaction_count
      , sum(owner_Amount) as owner_Amount
      , sum(commission) as commission
      , sum(Total_Amount) as Sub_total
      , sum(case when src = 'ppap' then Total_Amount_At_Place end) as Total_Amount_At_Place
    from (
       select format(ppc.payment_pool_credit_date, 'dd/mm/yyyy') as Payment_Date
        , ppc.payment_pool_credit_owner_money as owner_Amount
        , ppc.payment_pool_credit_tax_money as commission
        , ppc.payment_pool_credit_owner_money 
          + ppc.payment_pool_credit_tax_money as Total_Amount
        , src = convert(varchar(4),'ppc')
       from payment_pool_credit as ppc
      union all
      select format(pps.payment_pool_sadad_date, 'dd/mm/yyyy') as Payment_Date
        , pps.payment_pool_sadad_owner_money as owner_Amount
        , pps.payment_pool_sadad_tax_money as commission
        , pps.payment_pool_sadad_owner_money 
          + pps.payment_pool_sadad_tax_money as Total_Amount
        , src = convert(varchar(4),'pps')
      from payment_pool_sadad as pps
      union all
      select format(ppap.payment_pool_at_place_date, 'dd/mm/yyyy') as Payment_Date
        , ppap.payment_pool_at_place_owner_money as owner_Amount
        , ppap.payment_pool_at_place_tax_money as commission
        , ppap.payment_pool_at_place_owner_money 
          + ppap.payment_pool_at_place_tax_money as Total_Amount
        , src = convert(varchar(4),'ppap')
      from payment_pool_at_place as ppap
     ) as t
    group by Payment_Date
    

    针对新样本更新:

    select PaymentDate
      , count(Payment_Date) as Transaction_count
      , sum(owner_Amount) as owner_amount
      , sum(commission) as commission
      , sum(Total_Amount) as Sub_total
      , sum(case when src = 'ppap' then Total_Amount end) as Total_Amount_At_Place
    from (
       select Payment_Date
        , ppc.owner_amount as owner_Amount
        , ppc.tax_amount as commission
        , ppc.owner_amount 
          + ppc.tax_amount as Total_Amount
        , src = convert(varchar(4),'ppc')
       from payment_pool_credit as ppc
      union all
      select Payment_Date
        , pps.owner_amount as owner_Amount
        , pps.tax_amount as commission
        , pps.owner_amount 
          + pps.tax_amount as Total_Amount
        , src = convert(varchar(4),'pps')
      from payment_pool_sadad as pps
      union all
      select Payment_Date
        , ppap.owner_amount as owner_Amount
        , ppap.tax_amount as commission
        , ppap.owner_amount 
          + ppap.tax_amount as Total_Amount
        , src = convert(varchar(4),'ppap')
      from payment_pool_at_place as ppap
     ) as t
     where Payment_Date != '20160617' /* this is missing from your desired results */
    group by Payment_Date
    order by format(payment_date,'dd/MM/yyyy') /* to match desired results order */
    

    返回:

    +-------------+-------------------+--------------+------------+-----------+-----------------------+
    | PaymentDate | Transaction_count | owner_amount | commission | Sub_total | Total_Amount_At_Place |
    +-------------+-------------------+--------------+------------+-----------+-----------------------+
    | 2016-05-05  |                 1 | 5000.00      | 200.00     | 5200.00   | NULL                  |
    | 2017-02-11  |                 4 | 1152.00      | 140.00     | 1292.00   | NULL                  |
    | 2016-05-15  |                 6 | 2512.00      | 560.00     | 3072.00   | 1400.00               |
    | 2016-06-22  |                 1 | 500.00       | 300.00     | 800.00    | 800.00                |
    +-------------+-------------------+--------------+------------+-----------+-----------------------+
    

    【讨论】:

    • 结果假设它看起来像这样 Payment_Date |交易计数 | owner_Amount |佣金 |小计 | Total_Amount_At_Place 15/5/2016 | 3 | 2000 | 300 | 2300 | 500
    • @FaisalFahad 如果您可以编辑您的问题以提供示例数据和预期结果,这可能有助于澄清情况。
    • @FaisalFahad 已更新以针对样本进行测试
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多