我有两个表,我想分组并获得总价值的总和
select t.originfacilitycode, sum(t.cnt_total),t.soldto,
case when t.product_id in ('81','36','76','384') then 'Expedited'
when t.product_id in ('77','82','83','383') then 'Ground'
when t.product_id in('631') then 'Max' end as parcel_category
from ops_owner.volume_summary_month t,
ops_owner.account_data_pickup p
where t.soldto = p.soldto
and t.product_id in ('81','36','76','384','77','82','83','383','631')
and t.year = '2017'
and t.month in ('11','12')
and t.soldto = '5112087'
group by p.customer_name, t.originfacilitycode,t.soldto,
case when t.product_id in ('81','36','76','384') then 'Expedited'
when t.product_id in ('77','82','83','383') then 'Ground'
when t.product_id in('631') then 'Max' end
having sum(t.cnt_total) > 60
但是,结果并非按组分组唯一。
ORIGINFACILITYCODE SUM(T.CNT_TOTAL) SOLDTO PARCEL_CATEGORY
USEWR1 156864 5112087 Expedited
USEWR1 78432 5112087 Expedited
如果我按ORIGINFACILITYCODE分组,SOLDTO,PARCEL_CATEGORY为什么我们得到多次返回,应该分组,对吧?
ops_owner.account_data_pickup有重复如下,但它应该选择第一个,对吧?
SOLDTO PICKUP CUSTOMER_NAME
5112087 5314711 GOGOTECH
5112087 5320536 GOGOTECH II, LLC
ops_owner.volume_summary_month已将sold_to列为列
更新
我为同一个帐户提取量
select t.originfacilitycode, sum(t.cnt_total)
from ops_owner.volume_summary_month t
where t.soldto = '5112087'
and t.year = '2017'
and t.month in ('11','12')
group by t.originfacilitycode
ORIGINFACILITYCODE SUM(T.CNT_TOTAL)
USATL1 1
USEWR1 78432
USDFW1 1
有人有任何线索吗?为什么我的第一个查询有第一行?
ORIGINFACILITYCODE SUM(T.CNT_TOTAL) SOLDTO PARCEL_CATEGORY
USEWR1 156864 5112087 Expedited
看着这个查询
SELECT
T.originfacilitycode,
T.cnt_total,
T.soldto,
CASE
WHEN T.product_id IN ('81','36','76','384') THEN 'Expedited'
WHEN T.product_id IN ('77','82','83','383') THEN 'Ground'
WHEN T.product_id IN('631') THEN 'Max'
END AS parcel_category
FROM
ops_owner.volume_summary_month T,
ops_owner.account_data_pickup P
WHERE
T.soldto = P.soldto
AND T.product_id IN ('81','36','76','384','77','82','83','383','631')
AND T.YEAR = '2017'
AND T.MONTH IN ('11','12')
AND T.soldto = '5112087'
GROUP BY
P.customer_name,
T.originfacilitycode,
T.soldto,
CASE
WHEN T.product_id IN ('81','36','76','384') THEN 'Expedited'
WHEN T.product_id IN ('77','82','83','383') THEN 'Ground'
WHEN T.product_id IN('631') THEN 'Max'
END AS parcel_category
HAVING SUM(T.cnt_total) > 60
正如您已经评论过的那样,P.customer_name
上有多个不同的值,因此这是重复行的原因。
您必须在此处选择所需的行为:
t.cnt_total
,从所有可能的值中取P.customer_name
,P.customer_name
上有多个值时,定义另一个行为