SQL将两行结果合并为一行

时间:2016-01-21 06:16:51

标签: sql sql-server

enter image description here我的DDL

create table Employee
(employeeID varchar(10) not null primary key,
fName varchar(20) not null,
mName varchar(20) not null,
lName varchar(20) not null,
DOB date,
position varchar(15)not null,
addres varchar(40)not null,
warehouseID varchar(10)foreign key references Warehouse(warehouseID),
retailshopID varchar(10)foreign key references Retailshop(retailshopID),
customerID varchar(10)foreign key references Customer(customerID),
);

create table Warehouse_Province
(provinceID varchar(10) not null primary key,
province varchar (25) not null,
city varchar (25) not null,
addresss varchar (40) not null,
);


create table Warehouse
(warehouseID varchar(10) not null primary key,
qtyofStocks int not null,
reorderQty int not null,
provinceID varchar(10)foreign key references     Warehouse_Province(provinceID)
);

create table Retailshop_Provice
(retailprovinceID varchar(10) not null primary key,
province varchar (25) not null,
city varchar (25) not null,
addresss varchar (40) not null,
);

create table Retailshop
(retailshopID varchar(10) not null primary key,
retailprovinceID varchar(10)foreign key references Retailshop_Provice(retailprovinceID));

我需要查询列出在零售店和仓库中工作的所有员工以及他们工作的城市。 为此我做了一个SQL语句

SELECT  e.employeeID,e.fName, e.mName, e.lName,whp.city,rsp.city
FROM Employee e,Warehouse w ,Warehouse_Province whp,Retailshop   r,Retailshop_Provice rsp 
WHERE (e.warehouseID=w.warehouseID AND w.provinceID = whp.provinceID)OR(e.retailshopID=r.retailshopID AND r.retailprovinceID = rsp.retailprovinceID);

然而,我得到的结果有两行为城市,我怎么能将它们组合为下面的sql结果但我希望它只显示一行为City而不包括它在两个单独的SQL中我想将它组合成一个SQL结果显示员工姓名和他们所在的城市。

SELECT  e.employeeID,e.fName, e.mName, e.lName,whp.city
FROM Employee e,Warehouse w ,Warehouse_Province whp 
WHERE (e.warehouseID=w.warehouseID AND w.provinceID = whp.provinceID)

SELECT e.employeeID, e.fName, e.mName, e.lName,rsp.city
FROM Employee e,Retailshop r,Retailshop_Provice rsp 
WHERE e.retailshopID=r.retailshopID  AND  r.retailprovinceID=rsp.retailprovinceID;

示例表应如下所示

employee  fname lname  whpcity rspcity 

a         mr    x      NULL  city2

b         mr    y      city1   NULL

a         mr    x      NULL  city1

b         mr    y      city2   NULL

2 个答案:

答案 0 :(得分:0)

试试这个。将Employee表与Warehouse表以及Retailshop表连接起来应该可以正常工作。

SELECT  e.employeeID,e.fName, e.mName, e.lName,
whp.city as warehouse_city,
rsp.city as retail_city
FROM Employee e,
Warehouse w ,
Warehouse_Province whp,
Retailshop r,
Retailshop_Provice rsp
WHERE (e.warehouseID=w.warehouseID AND w.provinceID = whp.provinceID
AND e.retailshopID=r.retailshopID  AND  r.retailprovinceID=rsp.retailprovinceID);

否则,请尝试根据员工ID加入您提到的两个查询的结果。独立的外壳连接导致初始级别。

A & B & C(A & B) & (A & C)

答案 1 :(得分:0)

我不清楚您的预期输出。我假设你得到的输出如下

employee  fname lname  whpcity rspcity 

a         mr    x      city1   city2

b         mr    y      city1   city2

a         mr    x      city2   city1

b         mr    y      city2   city1

在上面你需要使用Stuff和xmlpath连接城市属于每个员工

SELECT employee, fname, lname, STUFF ((SELECT ','+ whpcity 
                                from #emp e1
                                where e1.employee = e2.employee

                                FOR XML PATH (''))
,1,1,'') as whpcity
from #emp e2
group by employee, fname, lname

您将获得的输出如下

employee    fname       lname       whpcity
a           mr          x           city1,city2
B           mr          Y           city2,city1

另外,我建议您在加入两个表时使用ANSI语法。

SELECT  e.employeeID,e.fName, e.mName, e.lName,whp.city as whpcity,rsp.city as rspcity
FROM Employee e
INNER JOIN 
Warehouse w 
ON ( e.warehouseID=w.warehouseID)
INNER JOIN Warehouse_Province whp
on (w.provinceID = whp.provinceID)
inner join Retailshop r
on (e.retailshopID=r.retailshopID)
inner join Retailshop_Provice rsp
on (r.retailprovinceID = rsp.retailprovinceID)