如何在同一个表中使用不同条件两次检索同一列?

时间:2013-05-07 16:18:13

标签: sql sql-server database

这是我的表:

Anganbadi_ID               Food     Month
-------------------------------------------    
1165                       हाँ         1
1165                       हाँ         2
1165                       हाँ         4
1168                       हाँ         4
2032                       नहीं        4
2218                       नहीं        4
2219                       हाँ         4
2358                       नहीं        4
2546                        हाँ        10 

有4列Anganbadi_ID, Food, Month, Year,我希望根据两个不同的月份值对Food列进行两次比较。

e.g。如果我选择Month = 4作为第一个Food(Food-1)列,Month = 10作为第二个Food(Food-2)列,那么它应该如下:

Anganbadi_ID            Food-1     Food-2    
------------------------------------------    
1165                       हाँ          NULL 
1168                       हाँ          NULL 
2032                       नहीं        NULL 
2218                       नहीं        NULL 
2219                       हाँ          NULL 
2358                       नहीं        NULL 
2546                     NULL        हाँ 

当我尝试这段代码时

SELECT     
   Anganbadi_ID, Food,
   (SELECT Food
    FROM Anganbadi AS Anganbadi_2
    WHERE (Anganbadi_1.Anganbadi_ID = Anganbadi_ID) 
      AND (Anganbadi_1.Month = 10)
   ) AS 'Food(2)'
FROM Anganbadi AS Anganbadi_1
WHERE (Month = 4)

它显示以下结果::

Anganbadi_ID              Food-1     Food-2    
--------------------------------------------
1165                       हाँ          NULL 
1168                       हाँ          NULL 
2032                       नहीं        NULL 
2218                       नहीं        NULL 
2219                       हाँ          NULL 
2358                       नहीं        NULL 

请尽快帮助我....

5 个答案:

答案 0 :(得分:11)

SELECT DISTINCT Anganbadi_ID,

(SELECT Food 
 FROM Anganbadi 
 WHERE      (Anganbadi_ID = A.Anganbadi_ID) 
    AND (Month = 4)) AS Food1,

(SELECT Food 
 FROM Anganbadi 
 WHERE      (Anganbadi_ID = A.Anganbadi_ID) 
    AND (Month = 10)) AS Food2

FROM Anganbadi AS A 
WHERE A.Month = 10 OR A.Month = 4

SQL Fiddle

答案 1 :(得分:1)

这里的关键是ID上的完全外连接,每个子查询中的条件可以是任何东西,全外连接将为您提供ID匹配的一行,并且在相反的列中它们不为空。如果您想要所有结果,即使食物在任何一个月都没有满足条件,那么在其中一个子查询中使用AllFoodsTable的选择ID进行左联接

SELECT Food1, Food2, ID
FROM (
  SELECT Food1, ID
  FROM MyTable
  WHERE Month = 4
) Con1
FULL OUTER JOIN (
  SELECT Food2, ID
  FROM MyTable
  WHERE Month = 10
) Con2
ON Con1.ID = Con2.ID

答案 2 :(得分:0)

SELECT DISTINCT Anganbadi_ID
,(SELECT     Food
FROM          Anganbadi AS Anganbadi_2
WHERE      (Anganbadi_1.Anganbadi_ID = Anganbadi_ID) AND (Month = 4)) AS 'Food(4)'
,(SELECT     Food
FROM          Anganbadi AS Anganbadi_2
WHERE      (Anganbadi_1.Anganbadi_ID = Anganbadi_ID) AND (Month = 10)) AS 'Food(10)'

FROM         Anganbadi AS Anganbadi_1

SQL-Fiddle

答案 3 :(得分:0)

SELECT Anganbadi_ID, Food as food1, null as Food2
where Month = 4

UNION ALL

SELECT Anganbadi_ID, null as food1, food as Food2
where Month = 10

答案 4 :(得分:0)

SELECT 
Food
DISTINCT Anganbadi_ID,

(SELECT Food 
 FROM Anganbadi 
 WHERE      (Anganbadi_ID = A.Anganbadi_ID) 
    AND (Month = 4)) AS Food1,

FROM Anganbadi AS A 
WHERE A.Month = 10 OR A.Month = 4