[英]Oracle SQL Group By a String Aggregated Field
I'm having a problem grouping a field in my query. 我在对查询中的字段进行分组时遇到问题。 Here is an example of what I'm talking about:
这是我正在谈论的示例:
Example: 例:
AIR_DT DOL_GAP_TIME MATL_SIZE
15-JAN-15 8:00 AM 30
15-JAN-15 8:00 AM 25
15-JAN-15 9:00 AM 5
15-JAN-15 9:00 AM 10
15-JAN-15 9:00 AM 5
15-JAN-15 9:00 AM 20
Those with same time should be grouped as one, summing up their matl_size 那些具有相同时间的对象应归为一,总结其matl_size
Expected output: 预期产量:
AIR_DT DOL_GAP_TIME MATL_SIZE
15-JAN-15 8:00 AM 55
15-JAN-15 9:00 AM 40
Here is my SQL: 这是我的SQL:
SELECT
a.air_dt,
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),
21, 2)) dol_gap_time, e.matl_size
FROM order_implem_dtl_broadcast a, matl_mstr b, matl_size_mstr e
WHERE a.matl_id = b.matl_id
AND b.matl_size_id = e.matl_size_id
AND a.air_dt LIKE '%15-JAN-15%'
GROUP BY a.air_dt, a.dol_pref_start_time, e.matl_size
ORDER BY a.air_dt, a.dol_pref_start_time;
Thank you for helping in advance! 感谢您的提前帮助!
Based on your sample data, this should do what you want: 根据您的样本数据,这应该做您想要的:
select AIR_DT, DOL_GAP_TIME, sum(MATL_SIZE)
from table t
group by AIR_DT, DOL_GAP_TIME;
However, I have no idea what this has to do with the query in the question. 但是,我不知道这与问题中的查询有什么关系。
OK let's start with your initial query: 好,让我们从您的初始查询开始:
SELECT
a.air_dt,
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'), 21, 2)) dol_gap_time, e.matl_size
FROM order_implem_dtl_broadcast a, matl_mstr b, matl_size_mstr e
WHERE a.matl_id = b.matl_id
AND b.matl_size_id = e.matl_size_id
AND a.air_dt LIKE '%15-JAN-15%'
GROUP BY a.air_dt, a.dol_pref_start_time, e.matl_size
ORDER BY a.air_dt, a.dol_pref_start_time;
I see a couple of problems. 我看到几个问题。 One, you're grouping by
e.matl_size
even though that is the column you want to SUM()
. 一个,即使您要对
SUM()
进行列, e.matl_size
按e.matl_size
分组。 You don't want it in the GROUP BY
. 您不希望在
GROUP BY
。 Second, your manner of getting the time from dol_pref_start_time
is really odd. 其次,您从
dol_pref_start_time
获取时间的方式确实很奇怪。 It looks like you want to round down to the hour, then just get the hour plus whether it is AM or PM. 看起来您想四舍五入到小时,然后获取小时加上小时,无论是上午还是下午。 So this:
所以这:
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'), 21, 2)) dol_gap_time
can simply be this: 可以简单地是这样的:
TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM') AS dol_gap_time
Third, are your dates stored as dates ? 第三,您的日期是否存储为日期 ? If so, why are you doing this?
如果是这样,您为什么要这样做?
AND a.air_dt LIKE '%15-JAN-15%'
It would be far better to do this: 这样做会更好:
AND TRUNC(a.air_dt) = date'2015-01-15'
or, if you have an index on a.air_dt
, this: 或者,如果您在
a.air_dt
上有一个索引,则:
AND a.air_dt >= date'2015-01-15'
AND a.air_dt < date'2015-01-16'
Putting this all together, we get something like this (note that I've also converted your joins to ANSI SQL joins): 将所有这些放在一起,我们得到如下信息(请注意,我还将您的联接转换为ANSI SQL联接):
SELECT TRUNC(a.air_dt) AS air_dt
, TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM') AS dol_gap_time
, SUM(e.matl_size) AS matl_size
FROM order_implem_dtl_broadcast a INNER JOIN matl_mstr b
ON a.matl_id = b.matl_id
INNER JOIN matl_size_mstr e
ON b.matl_size_id = e.matl_size_id
WHERE a.air_dt >= date'2015-01-15'
AND a.air_dt < date'2015-01-16'
GROUP BY TRUNC(a.air_dt), TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM')
ORDER BY air_dt, TO_DATE(dol_gap_time, 'HH:MI AM'); -- using aliases in the ORDER BY, converting dol_gap_time to DATE for sorting
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.