2013-04-06 91 views
0

一個條件字段返回所有記錄我有2列:
[tblCustomer]
ID | DebtNr | CustomerName
1 | 10001 | CustomerNr001
2 | 10002 | CustomerNr002
3 | 10003 | CustomerNr003
4 | 10004 | CustomerNr0ab
5 | 10008 | CustomerNr0yy
6 | 10012 | CustomerSomeName

[tblInquiry]
ID | CustID | IsInquiryReady
1 | 1 | True
2 | 1 | False
3 | 6 | True
4 | 2 | False

我要回所有客戶(所以在這種情況下6個記錄)。另外,如果至少1個是客戶沒有準備好(成品),一個附加的字段與[FALSE]的值

[結果]
ID | DebtNr | CustomerName | IsAtLeastOneNotReady
1 | 10001 | CustomerNr001 | True
2 | 10002 | CustomerNr002 | True
3 | 10003 | CustomerNr003 | False
4 | 10004 | CustomerNr0ab | False
5 | 10008 | CustomerNr0yy | False
6 | 10012 | CustSomeName | False

該查詢返回7個記錄:
SELECT [tblCustomer].ID, [tblCustomer].CustomerName, [tblCustomer].DebtNr, [tblInquiry].ID, [tblInquiry].IsInquiryReady FROM [tblCustomer] LEFT JOIN [tblInquiry] ON [tblInquiry].CustomerID = [tblCustomer].Id;

這是可能?如果是的話,我做錯了什麼?

在此先感謝。左移2個表的連接從第一臺和第二

回答

1

你可以做這樣的事情

select a.id, a.debtnr, a.customername,(select 'true' from dbo.tblinquiry b where b.custid = a.id and isinquiryready = 'false') as IsAtleastonenotready from dbo.tblcustomer a 
+0

謝謝,這個作品! – user2253287 2013-04-07 18:54:44

相關問題