2013-08-22 46 views
0

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

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到这个表中的许多条目。我需要一个查询,会告诉我,如果项目已在任何一个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 

任何想法如何编写一个查询呢?

+0

缺失:表定义,Postgres版本。任何列可以是NULL吗? –

回答

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

和NULL不受count()计数。

->SQLfiddle demo.

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

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 
+1

这是行不通的 - 它不会显示任何带有“FALSE”的行 –

+0

你是对的Aleks G,我只想显示一个可能的方法嵌套选择 – xrodas

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 
-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 
+1

这是不正确的 –

+0

那么它是正确的?它给了我想要的结果? – coderama

+0

检查sql小提琴 - http://sqlfiddle.com/#!12/1f0a7/10 –

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 
    ;