[英]COUNT from DISTINCT values in multiple columns
If this has been asked before, I apologize, I wasn't able to find a question/solution like it before breaking down and posting. 抱歉,如果您以前曾提出过此问题,请在分解和发布之前找不到类似的问题/解决方案。 I have the below query (using Oracle SQL) that works fine in a sense, but not fully what I'm looking for.
我有以下查询(使用Oracle SQL)在某种意义上可以正常工作,但不能完全满足我的要求。
SELECT
order_date,
p_category,
CASE
WHEN ( issue_grp = 1 ) THEN '1'
ELSE '2/3 '
END AS issue_group,
srt AS srt_level,
COUNT(*) AS total_orders
FROM
database.t_con
WHERE
order_date IN (
'&Enter_Date_YYYYMM'
)
GROUP BY
p_category,
CASE
WHEN ( issue_grp = 1 ) THEN '1'
ELSE '2/3 '
END,
srt,
order_date
ORDER BY
p_category,
issue_group,
srt_level,
order_date
Current Return (12 rows): 当前返回(12行):
Needed Return (8 rows without the tan rows being shown): 所需的回报(8行,未显示棕褐色的行):
Here is the logic of total_order column that I'm expecting: 这是我期望的total_order列的逻辑:
I'm eventually adding a filled_orders column that will go before the total_orders column, but I'm just not there yet. 我最终要添加一个filled_orders列,该列将位于total_orders列之前,但我还没有。
Sorry I wasn't as descriptive earlier. 抱歉,我之前没有这么描述。 Thanks again!
再次感谢!
You don't appear to need a subquery; 您似乎不需要子查询; if you want the count for each combination of values then group by those, and aggregate at that level;
如果您想要每个值组合的计数,则将其分组,并在该级别进行汇总; something like:
就像是:
SELECT
t1.order_date,
t1.p_category,
CASE
WHEN ( t1.issue_grp = 1 ) THEN '1'
ELSE '2/3 '
END AS issue_group,
t1.srt AS srt_level,
COUNT(*) AS total_orders
FROM
database.t_con t1
WHERE
t1.order_date = TO_DATE ( '&Enter_Date_YYYYMM', 'YYYYMM' )
GROUP BY
t1.p_category,
CASE
WHEN ( t1.issue_grp = 1 ) THEN '1'
ELSE '2/3 '
END,
t1.srt,
t1.order_date
ORDER BY
p_category,
issue_group,
srt_level,
order_date;
You shouldn't be relying on implicit conversion and NLS settings for your date argument (assuming order_date
is actually a date column, not a string), so I've used an explicit TO_DATE()
call, using the format suggested by your substitution variable name and prompt. 您不应该依赖于date参数的隐式转换和NLS设置(假设
order_date
实际上是日期列,而不是字符串),所以我使用了显式的TO_DATE()
调用,使用了替换变量建议的格式名称和提示。
However, that will give you the first day of the supplied month, since a day number isn't being supplied. 但是,这将为您提供所提供月份的第一天,因为没有提供日期编号。 It's more likely that you either want to prompt for a full date, or (possibly) just the year/month but want to include all days in that month - which
IN()
will not do, if that was your intention. 您更有可能想要提示一个完整的日期,或者(可能)仅提示年/月,但想要包括该月的所有天数-如果您打算这样做,则
IN()
不会这样做。 It also implies that stored dates all have their time portions set to midnight, as that is all it will match on. 这也意味着存储的日期都将其时间部分设置为午夜,因为这将与之匹配。 If those values have non-midnight times then you need a range to pick those up too.
如果这些值具有非午夜时间,那么您也需要一个范围来选择这些值。
I got it working to the extent of what my question was. 在我的问题范围内,我得到了它的解决。 Just needed to nest each column where counts/calculations were happening.
只需要嵌套发生计数/计算的每一列。
SELECT
order_date,
p_category,
issue_group,
srt_level,
order_count,
SUM(order_count) OVER(
PARTITION BY order_date, issue_group, p_category
) AS total_orders
FROM
(
SELECT
order_date,
p_category,
CASE
WHEN ( issue_grp = 1 ) THEN '1'
ELSE '2/3 '
END AS issue_group,
srt AS srt_level,
COUNT(*) AS order_count
FROM
database.t_con
WHERE
order_date IN (
'&Enter_Date_YYYYMM'
)
GROUP BY
p_category,
CASE
WHEN ( issue_grp = 1 ) THEN '1'
ELSE '2/3 '
END,
srt,
order_date
)
ORDER BY
order_date,
p_category,
issue_group
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.