【问题标题】:SQl query to get Daily count and amount based on type基于类型获取每日计数和金额的 SQl 查询
【发布时间】:2021-07-17 09:34:00
【问题描述】:

考虑一个表格“客户”,下面是列

CreationDatetime
CustType
Amount

在 CustType 列中只能有 2 个值 1 0r 2。

现在,我想编写 oracle sql 查询,在该查询中,我将根据唯一的客户类型获取每日用户数量和当天添加的数量,

CreationDate Total Count of CustType 1 Total Amount of CustType 1 Total Count of CustType 2 Total Amount of CustType 2
1/1/2021 100 200,000 80 100,000
2/1/2021 20 40,000 10 10,000

我正在使用以下查询,但它提供的每日总计数不基于客户类型。请指教。

SELECT Trunc(CreationDatetime),Count(1),SUM(amount)
FROM Customer
group by Trunc(CreationDatetime)

【问题讨论】:

    标签: sql oracle aggregate


    【解决方案1】:

    你可以试试这个:

    SELECT      TRUNC(CreationDatetime) AS Date, CustType AS CustomerType, COUNT(*) AS DailyCount, SUM(Amount) AS AmountForTheDay
    FROM        Customer
    GROUP BY    TRUNC(CreationDatetime), CustType
    

    【讨论】:

      【解决方案2】:

      看起来像 sum + case 组合:

      select trunc(creationdatetime) datum,
        sum(case when custtype = 1 then 1      else 0 end) count_of_type_1,
        sum(case when custtype = 1 then amount else 0 end) amount_of_type_1,
        --
        sum(case when custtype = 2 then 1      else 0 end) count_of_type_2,
        sum(case when custtype = 2 then amount else 0 end) amount_of_type_2
      from customer
      group by trunc(creationdatetime)
        
      

      【讨论】:

        【解决方案3】:

        这应该会得到你想要的输出:

        SELECT 
           Trunc(CreationDatetime),
           SUM(CASE WHEN CustType=1 THEN 1 END) as "Total Count of CustType 1",
           SUM(CASE WHEN CustTYpe=1 THEN amount END) as "Total Amount of CustType 1",
           SUM(CASE WHEN CustType=2 THEN 1 END) as "Total Count of CustType 2",
           SUM(CASE WHEN CustTYpe=2 THEN amount END) as "Total Amount of CustType 2"
        FROM Customer
        group by Trunc(CreationDatetime)
        

        【讨论】:

          【解决方案4】:

          您可以为此使用pivot

          select
            trunc(creationdatetime) as dt
            , CUST_TYPE1_TOTAL_COUNT
            , CUST_TYPE1_TOTAL_AMOUNT
            , CUST_TYPE2_TOTAL_COUNT
            , CUST_TYPE2_TOTAL_AMOUNT
          from t
          pivot (
            count(*) as total_count
            , sum(amount) as total_amount
            
            for custtype in (
              1 as cust_type1
              , 2 as cust_type2
            )
          )
          
          DT | CUST_TYPE1_TOTAL_COUNT | CUST_TYPE1_TOTAL_AMOUNT | CUST_TYPE2_TOTAL_COUNT | CUST_TYPE2_TOTAL_AMOUNT :-------- | ---------------------: | ----------------------: | ---------------------: | ----------------------: 21 年 7 月 19 日 | 1 | 70 | 0 | 21 年 7 月 17 日 | 2 | 400 | 1 | 200 21 年 7 月 18 日 | 2 | 110 | 2 | 70

          db小提琴here

          【讨论】:

            猜你喜欢
            • 2020-11-23
            • 1970-01-01
            • 2012-11-28
            • 1970-01-01
            • 2019-10-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多