我想从表中为每个bucket_type获取最快的quotaBasedService行。 packageSpeed值位于带有package_id的AdditionalQuota表中,其他字段位于quotaBased表中。
SELECT
q.ID,
q.SUBSCRIBER_IDENTIFIER,
q.BUCKET_TYPE,
MAX(CASE WHEN ext.PACKAGE_SPEED = 0 THEN 999999 ELSE ext.PACKAGE_SPEED END) AS PACKAGE_SPEED
FROM PIMS_QUOTA_BASED_SERVICE q
INNER JOIN PIMS_ADDITIONAL_QUOTA_PACKAGE ext ON ext.PACKAGE_ID = q.ADDITIONAL_QUOTA_PACKAGE_ID
WHERE q.PERIOD = '201810' AND q.BUCKET_TYPE IS NOT NULL
GROUP BY
q.SUBSCRIBER_IDENTIFIER,
q.BUCKET_TYPE,
q.ID
ORDER BY SUBSCRIBER_IDENTIFIER, BUCKET_TYPE, PACKAGE_SPEED DESC
这是sql结果 quotaBasedId,subs_identifier,bucket_type,速度
108337,subscriber1@domain,Extra,999999
108345,subscriber1@domain,Extra,5120
108334,subscriber1@domain,Extra1,999999
108342,subscriber1@domain,Extra1,5120
108336,subscriber1@domain,Extra2,999999
108344,subscriber1@domain,Extra2,5120
108335,subscriber1@domain,Extra4,999999
108343,subscriber1@domain,Extra4,5120
108253,subscriber2@domain,Extra,999999
108261,subscriber2@domain,Extra,5120
108250,subscriber2@domain,Extra1,999999
108258,subscriber2@domain,Extra1,5120
108252,subscriber2@domain,Extra2,999999
108260,subscriber2@domain,Extra2,5120
108251,subscriber2@domain,Extra4,999999
108259,subscriber2@domain,Extra4,5120
我的期望在这里
108337,subscriber1@domain,Extra,999999
108334,subscriber1@domain,Extra1,999999
108336,subscriber1@domain,Extra2,999999
108335,subscriber1@domain,Extra4,999999
108253,subscriber2@domain,Extra,999999
108250,subscriber2@domain,Extra1,999999
108252,subscriber2@domain,Extra2,999999
108251,subscriber2@domain,Extra4,999999
答案 0 :(得分:0)
我认为您只需要从id
和group by
中删除select
:
SELECT q.SUBSCRIBER_IDENTIFIER, q.BUCKET_TYPE,
MAX(CASE WHEN ext.PACKAGE_SPEED = 0 THEN 999999 ELSE ext.PACKAGE_SPEED END) AS PACKAGE_SPEED
FROM PIMS_QUOTA_BASED_SERVICE q INNER JOIN
PIMS_ADDITIONAL_QUOTA_PACKAGE ext
ON ext.PACKAGE_ID = q.ADDITIONAL_QUOTA_PACKAGE_ID
WHERE q.PERIOD = '201810' AND q.BUCKET_TYPE IS NOT NULL
GROUP BY q.SUBSCRIBER_IDENTIFIER, q.BUCKET_TYPE
ORDER BY SUBSCRIBER_IDENTIFIER, BUCKET_TYPE;
如果要使用id
,则需要一个聚合函数,可能是MIN()
。
答案 1 :(得分:0)
我认为您无需在q.ID
子句中使用GROUP BY
:
SELECT q.SUBSCRIBER_IDENTIFIER, q.BUCKET_TYPE,
MAX(CASE WHEN ext.PACKAGE_SPEED = 0 THEN 999999 ELSE ext.PACKAGE_SPEED END) AS PACKAGE_SPEED
FROM PIMS_QUOTA_BASED_SERVICE q INNER JOIN
PIMS_ADDITIONAL_QUOTA_PACKAGE ext
ON ext.PACKAGE_ID = q.ADDITIONAL_QUOTA_PACKAGE_ID
WHERE q.PERIOD = '201810' AND q.BUCKET_TYPE IS NOT NULL
GROUP BY q.SUBSCRIBER_IDENTIFIER, q.BUCKET_TYPE
ORDER BY SUBSCRIBER_IDENTIFIER, BUCKET_TYPE;
如果要使用q.ID
,请使用min()/max()
将其聚合:
答案 2 :(得分:0)
或设置MAX ID,这样您也将获得ID
SELECT
MAX(q.ID),
q.SUBSCRIBER_IDENTIFIER,
q.BUCKET_TYPE,
MAX(CASE WHEN ext.PACKAGE_SPEED = 0 THEN 999999 ELSE ext.PACKAGE_SPEED END) AS PACKAGE_SPEED
FROM PIMS_QUOTA_BASED_SERVICE q
INNER JOIN PIMS_ADDITIONAL_QUOTA_PACKAGE ext ON ext.PACKAGE_ID = q.ADDITIONAL_QUOTA_PACKAGE_ID
WHERE q.PERIOD = '201810' AND q.BUCKET_TYPE IS NOT NULL
GROUP BY
q.SUBSCRIBER_IDENTIFIER,
q.BUCKET_TYPE,
ORDER BY SUBSCRIBER_IDENTIFIER, BUCKET_TYPE, PACKAGE_SPEED DESC