我在StackOverflow和Internet上搜索過,但沒有找到任何解決此問題的方法。 我有一個像SQL Server 2008 R2中的myTable這樣的表格: person_id
,city
,addressType
,line_num
,textarea
和一些其他列,但這些都不是必需的。 所有字段的類型爲int,但textarea的類型爲varchar(10)。 一個人可以在一個城市中擁有多種類型的地址。地址可以很大,因此每個地址類型可以有多個行號。地址類型1是家庭地址,地址類型2是辦公地址,地址類型3是其他地址。 示例行此處給出SQL查詢顯示多個連接在同一個表上的錯誤
person_id|city|addressType|line_num|textarea
1 |1 |1 |0 |House no.
---------------------------------------------
1 |1 |1 |1 |10
---------------------------------------------
1 |1 |2 |0 |Building5
---------------------------------------------
1 |1 |2 |1 |Floor 1
---------------------------------------------
1 |1 |3 |0 |Factory
---------------------------------------------
1 |1 |3 |1 |no. 30
我需要表現出一個人的所有類型的地址在一個row.Like這樣的:
person_id|city|homeAddress |officeAddress |otherAddress
1 |1 |House no.,10|Building5,Floor 1|Factory,no.30
我曾經加入。 加入地址類型1和地址類型2
這裏工作得很好是單一加入的是工作的罰款查詢:
SET ANSI_PADDING ON;
SELECT DISTINCT homeAddress_person_id, homeAddress_city, homeAddress, officeAddress
FROM
/*Sub query for home address*/
(SELECT person_id AS homeAddress_person_id, city AS homeAddress_city,
/*Concatinating home address*/
STUFF((SELECT ',' + textarea FROM myTable WHERE person_id=ResultsFrom_myTable_For_homeAddress.person_id AND city=ResultsFrom_myTable_For_homeAddress.city AND addressType=ResultsFrom_myTable_For_homeAddress.addressType FOR XML PATH(''),TYPE).value('.','VARCHAR(4000)'),1,1,'') AS homeAddress
FROM myTable ResultsFrom_myTable_For_homeAddress WHERE person_id=1 AND addressType=1
GROUP BY person_id,city,addressType) ResultsFrom_myTable_For_homeAddress_outer
FULL OUTER JOIN
/*Sub query for office address*/
(SELECT person_id AS officeAddress_person_id, city AS officeAddress_city,
/*Concatinating office address*/
STUFF((SELECT ',' + textarea FROM myTable WHERE person_id=ResultsFrom_myTable_For_officeAddress.person_id AND city=ResultsFrom_myTable_For_officeAddress.city AND addressType=ResultsFrom_myTable_For_officeAddress.addressType FOR XML PATH(''),TYPE).value('.','VARCHAR(4000)')
,1
,1
,'') AS officeAddress
FROM myTable ResultsFrom_myTable_For_officeAddress
WHERE person_id=1 AND addressType=2
GROUP BY person_id,city,addressType) ResultsFrom_myTable_For_officeAddress_outer
ON ResultsFrom_myTable_For_homeAddress_outer.homeAddress_person_id=ResultsFrom_myTable_For_officeAddress_outer.officeAddress_person_id AND ResultsFrom_myTable_For_homeAddress_outer.homeAddress_city=ResultsFrom_myTable_For_officeAddress_outer.officeAddress_city
SET ANSI_PADDING OFF;
但當我再添加入到顯示其它地址。它顯示錯誤了條件,預計 這裏有兩個聯接的查詢
SET ANSI_PADDING ON;
SELECT DISTINCT homeAddress_person_id, homeAddress_city, homeAddress, officeAddress, otherAddress
FROM
/*Sub query for home address*/
(SELECT person_id AS homeAddress_person_id, city AS homeAddress_city,
/*Concatinating home address*/
STUFF((SELECT ',' + textarea FROM myTable WHERE person_id=ResultsFrom_myTable_For_homeAddress.person_id AND city=ResultsFrom_myTable_For_homeAddress.city AND addressType=ResultsFrom_myTable_For_homeAddress.addressType FOR XML PATH(''),TYPE).value('.','VARCHAR(4000)'),1,1,'') AS homeAddress
FROM myTable ResultsFrom_myTable_For_homeAddress WHERE person_id=1 AND addressType=1
GROUP BY person_id,city,addressType) ResultsFrom_myTable_For_homeAddress_outer
FULL OUTER JOIN
/*Sub query for office address*/
(SELECT person_id AS officeAddress_person_id, city AS officeAddress_city,
/*Concatinating office address*/
STUFF((SELECT ',' + textarea FROM myTable WHERE person_id=ResultsFrom_myTable_For_officeAddress.person_id AND city=ResultsFrom_myTable_For_officeAddress.city AND addressType=ResultsFrom_myTable_For_officeAddress.addressType FOR XML PATH(''),TYPE).value('.','VARCHAR(4000)')
,1
,1
,'') AS officeAddress
FROM myTable ResultsFrom_myTable_For_officeAddress
WHERE person_id=1 AND addressType=2
GROUP BY person_id,city,addressType) ResultsFrom_myTable_For_officeAddress_outer
ON ResultsFrom_myTable_For_homeAddress_outer.homeAddress_person_id=ResultsFrom_myTable_For_officeAddress_outer.officeAddress_person_id AND ResultsFrom_myTable_For_homeAddress_outer.homeAddress_city=ResultsFrom_myTable_For_officeAddress_outer.officeAddress_city
FULL OUTER JOIN
/*Sub query for other address*/
(SELECT person_id AS otherAddress_person_id,city AS otherAddress_city,
/*Concatinating office address*/
STUFF((SELECT ',' + textarea FROM myTable WHERE person_id=ResultsFrom_myTable_For_otherAddress.person_id AND city=ResultsFrom_myTable_For_otherAddress.city AND addressType=ResultsFrom_myTable_For_otherAddress.addressType FOR XML PATH(''),TYPE).value('.','VARCHAR(4000)'),1,1,'') AS otherAddress
FROM myTable ResultsFrom_myTable_For_otherAddress WHERE person_id=1 AND addressType=3
GROUP BY person_id,city,addressType) ResultsFrom_myTable_For_otherAddress_outer
ON ResultsFrom_myTable_For_homeAddress_outer.homeAddress_person_id=ResultsFrom_myTable_For_otherAddress_outer.otherAddress_person_id AND ResultsFrom_myTable_For_homeAddress_outer.homeAddress_city=ResultsFrom_myTable_For_otherAddress_outer.otherAddress_city;
SET ANSI_PADDING OFF;
什麼是錯在上面的查詢中指定的上下文非布爾類型的表達式?
格式化您的查詢,因此它會更好的可讀性 – Jens
@Jens查詢格式化。現在這更易讀嗎? – Varun