2016-04-20 11 views
0

表のClientServicesをTrueまたはFalseを返しますするには、左側には、表の顧客に、各サービスIDが特定に対応SQLクエリは、左joingテーブルから

Table Customers

SysRef FullName 
1  ABC 
2  RST 
3  XYZ 

Table Client Services

ClientSysRef ServiceID 
1    10 
2    14 
2    7 
3    8 

を入社されますサービスが提供されています。たとえば、ServiceID 7はIT、ServiceID 8はAccounts、Service ID 10はMarketing、ServiceID 14はHRです。特定のクライアントに対してServiceIDが見つかった場合はTrueを返し、そうでない場合はFalseを返す必要があります。必要な結果;

SysRef FullName IT Accounts Marketing HR 
1  ABC  False False  True   False 
2  RST  True False  False  True 
3  XYZ  False True  False  False 

これはSQLで可能ですか?誰かが私を案内してくれますか?おかげで

現在の結果;

SysRef FullName IT Accounts Marketing HR 
1  ABC  False False  True   False 
2  RST  True False  False  False 
2  RST  False False  False  True 
3  XYZ  False True  False  False 
+0

CASE X = '' ELSE THEN '真' '偽の' END ITを使用することができます... – Strawberry

答えて

0
select c.sysref,c.fullname,case when d.serviceID = 7 then 'TRUE' else 'FALSE' end as IT, 
case when d.serviceID = 8 then 'TRUE' else 'FALSE' end as Accounts, 
case when d.serviceID = 10 then 'TRUE' else 'FALSE' end as Marketing, 
case when d.serviceID = 14 then 'TRUE' else 'FALSE' end as HR from customers c join client_services d on c.sysref = d.clientsysref; 
+0

ありがとう@Priyanshu。条件が複数のサービスを満たしている場合は、別々の行に表示されます。一列にすべてを表示するには離れていますか?たとえば、SysRef 2 – Mario

+0

のクライアントのために必要な結果からここに結果表を表示できますか? – Priyanshu

+0

結果表が 'Current Result'の上に追加されました – Mario

1

はい、あなたはCASEのx = 'B'、Control Flow Functions:

SELECT 
    SysRef, 
    IF(ServiceID = 7, 'TRUE', 'FALSE') as IT, 
    ... #The same for the others 
関連する問題