我有一个excel电子表格,它从SQL中提取记录。我想结合重复记录并总结匹配记录的数量字段。 我尝试将select(sum)和group by命令添加到我现有的查询中,但由于没有真正的SQL经验,因此我没有达到目标。 我的表看起来像这样:
item no.| item description | qty | date
1 red onion 5 20110405
2 yellow onion 5 20110406
1 red onion 10 20110405
我希望它看起来像这样:
item no.| item description | qty | date
1 red onion 15 20110405
2 yellow onion 5 20110406
这是我使用的查询:
SELECT
OELINHST_SQL.item_no, OELINHST_SQL.item_desc_1, OELINHST_SQL.qty_ordered, oelinhst_sql.unit_weight, OEHDRHST_SQL.shipping_dt, OEHDRHST_SQL.inv_dt
FROM
OEHDRHST_SQL OEHDRHST_SQL,
OELINHST_SQL OELINHST_SQL
WHERE
OEHDRHST_SQL.ord_type *= OELINHST_SQL.ord_type AND
OEHDRHST_SQL.ord_no *= OELINHST_SQL.ord_no AND
(OELINHST_SQL.prod_cat <> '26' AND
OELINHST_SQL.prod_cat <> '25') AND
OELINHST_SQL.loc = 'fs2' AND
OELINHST_SQL.item_desc_1 IS NOT NULL AND
OEHDRHST_SQL.shipping_dt >= 20110101 AND
OELINHST_SQL.item_no NOT IN ('800-505-00', '800-083-00', '800-506-00', '400-511-39') AND (OELINHST_SQL.item_no NOT BETWEEN '800-000-00' AND '999-999-99')
ORDER BY
OELINHST_SQL.item_no ASC,
OEHDRHST_SQL.inv_dt DESC
答案 0 :(得分:8)
您需要使用GROUP BY
子句和SUM
相关列。
这是一个可以根据您的情况调整的简化示例。
select t1.name, t2.name, t2.date, sum(t2.orders)
from table1 t1
inner join table2 t2 on t1.id = t2.t1_id
group by t1.name, t2.name, t2.date
答案 1 :(得分:1)
看起来你需要添加group by子句,并添加SUM(Qty)和MIN(date)
对列名称不太确定,但可能是这样的:
SELECT
OELINHST_SQL.item_no, OELINHST_SQL.item_desc_1, SUM(OELINHST_SQL.qty_ordered), oelinhst_sql.unit_weight, MIN(OEHDRHST_SQL.shipping_dt), MIN(OEHDRHST_SQL.inv_dt)
FROM
OEHDRHST_SQL OEHDRHST_SQL,
OELINHST_SQL OELINHST_SQL
WHERE
OEHDRHST_SQL.ord_type *= OELINHST_SQL.ord_type AND
OEHDRHST_SQL.ord_no *= OELINHST_SQL.ord_no AND
(OELINHST_SQL.prod_cat <> '26' AND
OELINHST_SQL.prod_cat <> '25') AND
OELINHST_SQL.loc = 'fs2' AND
OELINHST_SQL.item_desc_1 IS NOT NULL AND
OEHDRHST_SQL.shipping_dt >= 20110101 AND
OELINHST_SQL.item_no NOT IN ('800-505-00', '800-083-00', '800-506-00', '400-511-39') AND (OELINHST_SQL.item_no NOT BETWEEN '800-000-00' AND '999-999-99')
GROUP BY OELINHST_SQL.item_no, OELINHST_SQL.item_desc_1, oelinhst_sql.unit_weight
ORDER BY
OELINHST_SQL.item_no ASC,
OEHDRHST_SQL.inv_dt DESC
答案 2 :(得分:0)
我想你想要这样的东西:
SELECT
OELINHST_SQL.item_no,
OELINHST_SQL.item_desc_1,
SUM(OELINHST_SQL.qty_ordered),
SUM(oelinhst_sql.unit_weight),
MIN(OEHDRHST_SQL.shipping_dt),
MIN(OEHDRHST_SQL.inv_dt)
FROM
OEHDRHST_SQL OEHDRHST_SQL,
OELINHST_SQL OELINHST_SQL
WHERE
OEHDRHST_SQL.ord_type *= OELINHST_SQL.ord_type AND
OEHDRHST_SQL.ord_no *= OELINHST_SQL.ord_no AND
(OELINHST_SQL.prod_cat <> '26' AND
OELINHST_SQL.prod_cat <> '25') AND
OELINHST_SQL.loc = 'fs2' AND
OELINHST_SQL.item_desc_1 IS NOT NULL AND
OEHDRHST_SQL.shipping_dt >= 20110101 AND
OELINHST_SQL.item_no NOT IN ('800-505-00', '800-083-00', '800-506-00', '400-511-39') AND (OELINHST_SQL.item_no NOT BETWEEN '800-000-00' AND '999-999-99')
GROUP BY
OELINHST_SQL.item_no,
OELINHST_SQL.item_desc_1
ORDER BY
OELINHST_SQL.item_no ASC,
OEHDRHST_SQL.inv_dt DESC
但是,这假设发货日期和int_date将始终相同或者您希望在两种情况下都是最早的日期。
答案 3 :(得分:0)
我添加了更简单的表别名,以便于阅读。基本上,您需要GROUP BY所有未包含在聚合函数中的列。
SELECT
l.item_no
,l.item_desc_1
,SUM(l.qty_ordered) AS qty_ordered
,l.unit_weight
,h.shipping_dt
,h.inv_dt
FROM OEHDRHST_SQL h
LEFT OUTER JOIN OELINHST_SQL l ON h.ord_type = l.ord_type AND h.ord_no = l.ord_no
WHERE l.prod_cat <> '26'
AND l.prod_cat <> '25'
AND l.loc = 'fs2'
AND l.item_desc_1 IS NOT NULL
AND h.shipping_dt >= 20110101
AND l.item_no NOT IN ('800-505-00', '800-083-00', '800-506-00', '400-511-39')
AND l.item_no NOT BETWEEN '800-000-00' AND '999-999-99'
GROUP BY l.item_no, l.item_desc_1, l.unit_weight, h.shipping_dt, h.inv_dt
ORDER BY l.item_no ASC, h.inv_dt DESC