SQL:获取链接到一个项目的多个行条目?

时间:2013-08-22 08:43:41

标签: sql postgresql aggregate-functions

我有一张桌子:

ID | ITEMID | STATUS | TYPE
1  | 123    | 5      | 1
2  | 123    | 4      | 2
3  | 123    | 5      | 3
4  | 125    | 3      | 1
5  | 125    | 5      | 3

此表中的任何项目都可以包含0到多个条目。我需要一个查询,告诉我ITEM是否有5或4状态的所有条目。例如,在上面的例子中,我想得到结果:

ITEMID | REQUIREMENTS_MET
123    | TRUE    --> true because all statuses are either 5 or 4
125    | FALSE   --> false because it has a status of 3 and a status of 5. 
                     If the 3 was a 4 or 5, then this would be true

更好的是这样的事情:

ITEMID | MET_REQUIREMENTS | NOT_MET_REQUIREMENTS
123    | 3                | 0
125    | 1                | 1

知道如何为此编写查询吗?

6 个答案:

答案 0 :(得分:2)

快速,简短,简单:

SELECT itemid
      ,count(status = 4 OR status = 5 OR NULL) AS met_requirements
      ,count(status < 4 OR status > 5 OR NULL) AS not_met_requirements
FROM   tbl
GROUP  BY itemid
ORDER  BY itemid;

假设所有列都为integer NOT NULL

basic boolean logic上构建:
TRUE OR NULL收益TRUE
FALSE OR NULL会产生NULL

count()不计算NULL。

->SQLfiddle demo.

答案 1 :(得分:0)

简单的一个:

select
    "ITEMID",
    case
        when min("STATUS") in (4, 5) and max("STATUS") in (4, 5) then 'True'
        else 'False'
    end as requirements_met
from table1
group by "ITEMID"

更好的一个:

select
    "ITEMID",
    sum(case when "STATUS" in (4, 5) then 1 else 0 end) as MET_REQUIREMENTS,
    sum(case when "STATUS" in (4, 5) then 0 else 1 end) as NOT_MET_REQUIREMENTS
from table1
group by "ITEMID";

sql fiddle demo

答案 2 :(得分:0)

SELECT a.ID FROM (SELECT ID, MIN(STATUS) AS MINSTATUS, MAX(STATUS) AS MAXSTATUS FROM TABLE_NAME AS a GROUP BY ID)
WHERE a.MINSTATUS >= 4 AND a.MAXSTATUS <= 5

答案 3 :(得分:0)

这样做的一种方法是

SELECT t1.itemid, NOT EXISTS(SELECT 1
                             FROM mytable t2
                             WHERE itemid=t1.itemid
                             AND status NOT IN (4, 5)) AS requirements_met
FROM mytable t1
GROUP BY t1.itemid

更新:了解您的更新要求,您可以使用以下内容:

SELECT itemid,
       sum(CASE WHEN status IN (4, 5) THEN 1 ELSE 0 END) as met_requirements,
       sum(CASE WHEN status IN (4, 5) THEN 0 ELSE 1 END) as not_met_requirements
FROM mytable
GROUP BY itemid

答案 4 :(得分:0)

WITH dom AS (
        SELECT DISTINCT item_id FROM items
        )
,       yes AS ( SELECT item_id, COUNT(*) AS good_count FROM items WHERE status IN (4,5) GROUP BY item_id
        )
,       no AS ( SELECT item_id, COUNT(*) AS bad_count FROM items WHERE status NOT IN (4,5) GROUP BY item_id
        )
SELECT d.item_id
        , COALESCE(y.good_count,0) AS good_count
        , COALESCE(n.bad_count,0) AS bad_count
FROM dom d
LEFT JOIN yes y ON y.item_id = d.item_id
LEFT JOIN no n ON n.item_id = d.item_id
        ;

也可以使用外部联接:

WITH    yes AS ( SELECT item_id, COUNT(*) AS good_count FROM items WHERE status IN (4,5) GROUP BY item_id)
,       no AS ( SELECT item_id, COUNT(*) AS bad_count FROM items WHERE status NOT IN (4,5) GROUP BY item_id)
SELECT COALESCE(y.item_id, n.item_id) AS item_id
    , COALESCE(y.good_count,0) AS good_count
    , COALESCE(n.bad_count,0) AS bad_count
FROM yes y
FULL JOIN no n ON n.item_id = y.item_id
    ;

答案 5 :(得分:-1)

没关系,实际上很容易做到:

select  ITEM_ID , 
  sum (case when STATUS >= 3 then 1 else 0 end ) as met_requirements, 
  sum (case when STATUS < 3 then 1 else 0 end ) as not_met_requirements
from TABLE as d
group by ITEM_ID