除了标志组之外的所有SQL联盟?

时间:2015-09-29 07:36:44

标签: sql sql-server

我对SQL很新手,但这就是我要做的事情:

我有3个sql查询返回3组不同的结果(学生,员工,客户),列都是相同的

id first name last name address zip group 1 group 2 group 3

每个组的每个select语句用类型(学生,员工,客户)填充其中一个组列,select语句之间的联合使我在1个表中获得所有3个组...但我想要重复...合并并让小组反映所有3个查询?

id first name last name address zip group 1 group 2 group 3
1  Bob        Smith     bkr st  33  STUDENT
2  Zoe        Apple     trk av  44          STAFF   CUSTOMER
2  Zoe        Apple     trk av  44  STUDENT

变为:

id first name last name address zip group 1 group 2 group 3
1  Bob        Smith     bkr st  33  STUDENT
2  Zoe        Apple     trk av  44  STUDENT STAFF   CUSTOMER

这里或多或少是带有union的原型select语句:

SELECT DISTINCT ID, student_FN AS'FIRST NAME', student_LN AS 'LAST NAME', ADDRESS, ZIP. 'STUDENT' AS 'GROUP 1'. '' AS 'GROUP 2','' AS 'GROUP 3' 
WHERE STUDENT_STATUS IN ('ENROLLED')
UNION ALL
SELECT DISTINCT ID, STAFF_FN AS 'FIRST NAME', STAFF_LN AS 'LAST NAME', ADDRESS, ZIP. '' AS 'GROUP 1'. 'STAFF' AS 'GROUP 2','' AS 'GROUP 3' 
WHERE STAFF_RECORD IN ('ACTIVE')
UNION ALL
SELECT DISTINCT ID, CUS_FN AS 'FIRST NAME', CUS_LN AS 'LAST NAME', ADDRESS, ZIP. '' AS 'GROUP 1'. '' AS 'GROUP 2','CUSTOMER' AS 'GROUP 3' 
WHERE CUSTOMER_SHOPPED IN ('STORE')

每个选择表从稍微不同的位置拉出列。 非常感谢!

3 个答案:

答案 0 :(得分:1)

尝试:

SELECT ID, 
       "FIRST NAME", 
       "LAST NAME", 
       ADDRESS, 
       ZIP, 
       MAX(CASE "GROUP TYPE" WHEN 'STUDENT' THEN 'STUDENT' END) "GROUP 1", 
       MAX(CASE "GROUP TYPE" WHEN 'STAFF' THEN 'STAFF' END) "GROUP 2", 
       MAX(CASE "GROUP TYPE" WHEN 'CUSTOMER' THEN 'CUSTOMER' END) "GROUP 3"
FROM
(SELECT ID, student_FN AS "FIRST NAME", student_LN AS "LAST NAME", ADDRESS, ZIP, 'STUDENT' AS "GROUP TYPE"
 FROM STUDENTS WHERE STUDENT_STATUS IN ('ENROLLED')
 UNION ALL
 SELECT ID, STAFF_FN AS "FIRST NAME", STAFF_LN AS "LAST NAME", ADDRESS, ZIP, 'STAFF' AS "GROUP TYPE"
 FROM STAFF WHERE STAFF_RECORD IN ("ACTIVE")
 UNION ALL
 SELECT ID, CUS_FN AS "FIRST NAME", CUS_LN AS "LAST NAME", ADDRESS, ZIP, 'CUSTOMER' AS "GROUP TYPE" 
 FROM CUSTOMERS WHERE CUSTOMER_SHOPPED IN ('STORE')
) AS ILQ
GROUP BY ID, "FIRST NAME", "LAST NAME", ADDRESS, ZIP

请注意,如果任何名称或地址详细信息不同,这将为同一ID返回多行。

答案 1 :(得分:0)

你可以试试这个: -

SELECT id, first_name, last_name, address, zip, MAX(group1), MAX(group2), MAX(group3)
FROM YOUR_TABLES
GROUP BY id, first_name, last_name, address, zip

答案 2 :(得分:0)

这是一种避免Union的方法:

;WITH 
S as (SELECT * FROM STUDENTS WHERE STUDENT_STATUS IN ('ENROLLED') ),
F as (SELECT * FROM STAFFS WHERE STAFF_RECORD IN ('ACTIVE') ),
C as (SELECT * FROM CUSTOMERS WHERE CUSTOMER_SHOPPED IN ('STORE') )

SELECT 
   DISTINCT COALESCE(S.ID, F.id, C.id ) as ID, 
            COALESCE(S.'FIRST NAME', F.'FIRST NAME', C.'FIRST NAME') 
            as 'FIRST NAME', 
            COALESCE(S.'LAST NAME', F.'LAST NAME', C.'LAST NAME' ) as 'LAST NAME',
            COALESCE(S.ADDRESS, F.ADDRESS, C.ADDRESS ) as ADDRESS,
            COALESCE(S.ZIP, F.ZIP, C.ZIP ) as ZIP,
            'STUDENT' AS 'GROUP 1',
            'STAFF' AS 'GROUP 2',
            'CUSTOMER' AS 'GROUP 3' 
FROM 
     S
FULL OUTER JOIN 
     F on  F.id = S.id
FULL OUTER JOIN 
     C on C.id = COALESCE( S.id, F.id )