简体   繁体   English

sql oracle子查询分组

[英]sql oracle group by subquery

I get the same ecommerce number for each date. 每个日期我都会得到相同的电子商务编号。 I am trying to get ecommerce value count depending on the date, which is different for each date as the total number is only 105 for all October, not 391958. Any idea how to group by the output of a subquery? 我正在尝试根据日期获取电子商务价值计数,每个日期的电子商务日期计数都不同,因为整个十月的总数仅为105,而不是391958。知道如何将子查询的输出分组吗? Thank you! 谢谢!

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');

OUTPUT OUTPUT 在此处输入图片说明

Try below modified queries 在下面尝试修改后的查询

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');

-- Another way using JOIN clause -使用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');

It's hard to suggest an answer without understanding your table relationship, but I can tell that your problem is there is no relationship between your subquery and your main query. 在不了解表关系的情况下很难给出答案,但是我可以告诉您问题在于子查询和主查询之间没有关系。 Your subquery simply returns a count where modl_id='ECOMMERC' , so that value will always be the same - in your case, 105. You need to add a JOIN criteria to the subquery that ties the unique value to your main query. 您的子查询仅返回一个其中modl_id='ECOMMERC'的计数,因此该值将始终相同-在您的情况下为105。您需要向子查询添加JOIN条件,以将唯一值与主查询联系起来。 You'll also want to alias the table names differently to ensure you're joining correctly. 您还需要对表名使用不同的别名,以确保正确连接。

You are doing unnecessary joins when you just want a correlated subquery: 当您只想要相关的子查询时,您正在执行不必要的联接:

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM