【问题标题】:sql oracle group by subquerysql oracle group by子查询
【发布时间】:2018-11-14 18:00:14
【问题描述】:

我在每个日期都获得了相同的电子商务号码。我正在尝试根据日期获取电子商务价值计数,每个日期都不同,因为整个 10 月的总数只有 105,而不是 391958。 知道如何按子查询的输出进行分组吗? 谢谢!

SELECT   to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
(
    SELECT count(*) 
    FROM   ft_t_wcs1 wcs1,ft_t_stup stup 
    WHERE  stup.modl_id='ECOMMERC'
    AND    stup.CROSS_REF_ID=wcs1.acct_id
    AND    stup.end_tms IS NULL
) AS     ecommerce
FROM     ft_t_wcs1 wcs1, ft_t_stup stup
WHERE    wcs1.scenario='CREATE' 
AND      wcs1.acct_id IS NOT NULL 
AND      wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
GROUP BY to_char(wcs1.start_tms,'DD/MM/YYYY')
ORDER BY to_char(wcs1.start_tms,'DD/MM/YYYY');

输出

【问题讨论】:

    标签: sql oracle correlated-subquery


    【解决方案1】:

    尝试以下修改后的查询

    select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
    ecommerce
    from ft_t_wcs1 wcs1, ft_t_stup stup
    where stup.modl_id='ECOMMERC' and stup.CROSS_REF_ID=wcs1.acct_id and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and 
    wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and 
    LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
    group by to_char(wcs1.start_tms,'DD/MM/YYYY')
    order by to_char(wcs1.start_tms,'DD/MM/YYYY');
    

    -- 使用JOIN子句的另一种方式

    select to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,count(*) AS 
    ecommerce
    from ft_t_wcs1 wcs1 
    join ft_t_stup stup
    ON stup.CROSS_REF_ID=wcs1.acct_id
    where stup.modl_id='ECOMMERC' and stup.end_tms is null wcs1.scenario='CREATE' and wcs1.acct_id is not null and 
    wcs1.start_tms between add_months(TRUNC(SYSDATE,'mm'),-1) and 
    LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
    group by to_char(wcs1.start_tms,'DD/MM/YYYY')
    order by to_char(wcs1.start_tms,'DD/MM/YYYY');
    

    【讨论】:

      【解决方案2】:

      在不了解您的表关系的情况下很难提出答案,但我可以告诉您,您的问题是您的子查询和主查询之间没有关系。您的子查询仅返回 modl_id='ECOMMERC' 的计数,因此该值将始终相同 - 在您的情况下为 105。您需要向子查询添加一个 JOIN 条件,将唯一值与您的主查询联系起来。您还需要对表名进行不同的别名,以确保您正确加入。

      【讨论】:

        【解决方案3】:

        当您只想要一个相关的子查询时,您正在执行不必要的连接:

        SELECT to_char(wcs1.start_tms,'DD/MM/YYYY') as dates,
               (SELECT count(*) 
                FROM ft_t_stup stup 
                WHERE stup.modl_id=  'ECOMMERC' AND
                      stup.CROSS_REF_ID = wcs1.acct_id
                      stup.end_tms IS NULL
               ) AS ecommerce
        FROM ft_t_wcs1 wcs1
        WHERE wcs1.scenario = 'CREATE' AND
              wcs1.acct_id IS NOT NULL AND
              wcs1.start_tms BETWEEN add_months(TRUNC(SYSDATE,'mm'),-1) AND LAST_DAY(add_months(TRUNC(SYSDATE,'mm'),-1))
        GROUP BY to_char(wcs1.start_tms, 'DD/MM/YYYY')
        ORDER BY to_char(wcs1.start_tms, 'DD/MM/YYYY');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-09-22
          • 2016-02-20
          • 2014-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多