2016-11-10 1 views
-1

は、私はそれが動作するはずのように、このようなクエリ、その作業を得たが、私はそれ1つのクエリで変更サブクエリ

SELECT 
(
Select count(exists) 
from country c 
    join special s ON c.id_special = s.id 
    join raports rap ON c.id = rap.id_raport 
where c.id_document = 7 and exists = 1 
and rap.id_data = 201501 and is_id = 1 
) as number_of_ID_in_table_exists, 
(
Select count(sps) 
from country c 
join special s ON c.id_special = s.id 
join raports rap ON c.id = rap.id_raport 
where c.id_document = 7 and sps = 1 
and rap.id_data = 201501 and is_id2 = 1 
) as number_of_ID2_in_table_exists; 
+1

http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-クエリ – Strawberry

+0

ありがとう、私の心の次回にそれを持っています。 – Buckethead

答えて

1

使用条件カウントを行うためのcase表現作るか疑問:

Select count(case when exists = 1 and is_id = 1 then 1 end) as number_of_ID_in_table_exists, 
     count(case when sps = 1 and is_id2 = 1 then 1 end) as number_of_ID2_in_table_exists 
from country c 
    join special s ON c.id_special = s.id 
    join raports rap ON c.id = rap.id_raport 
where rap.id_data = 201501 and c.id_document = 7 
+0

sps = 1の代わりに1 =存在する必要があります、残念私の悪いです。 Thxの仕事、私はあなたを愛している:)私はこのような彼の何かを試したが、私はハングアウト: – Buckethead

1
SELECT SUM(CASE WHEN is_id = 1 THEN 1 END) number_of_ID_in_table_exists 
     SUM(CASE WHEN is_id2 = 1 THEN 1 END) number_of_ID2_in_table_exists 
FROM country c 
JOIN special s ON c.id_special = s.id 
JOIN raports rap ON c.id = rap.id_raport 
WHERE c.id_document = 7 AND rap.id_data = 201501 AND [exists]= 1 
+0

それはsps = 1の代わりに存在する必要があります= 1、あまりにも申し訳ありませんが、 – Buckethead

関連する問題