我有5行4列的单表,我希望得到8列的结果集,在同一个表上尝试了内连接,但没有得到预期的结果,有没有办法从中获取选定的记录以下查询结果集?
Declare @CustomerData Table (CustomerNum int,
VehicleName Char(20),
Vehiclepurchaseyear char(4),
Vehicleprice char(10))
INSERT INTO @CustomerData(CustomerNum,VehicleName,Vehiclepurchaseyear,Vehicleprice)
VALUES (1000,'Toyoto Camry','2012',' 14500'),
(1000,'Nissan Ultima','2014','12000'),
(1500,'Honda Accord','2012','15000'),
(1500,'Honda Civic','2012','13000'),
(1800,'Toyoto RAV4','2012','16500')
SELECT X.customerNum as CustomerNum
,X.VehicleName as Vehiclename1
,X.Vehiclepurchaseyear as vehiclepurchaseyear1
,X.Vehicleprice as vehicleprice1
,Y.VehicleName as Vehiclename2
,Y.Vehiclepurchaseyear as vehiclepurchaseyear2
,Y.Vehicleprice as vehicleprice2
FROM
(SELECT CustomerNum,
VehicleName,
Vehiclepurchaseyear,
Vehicleprice
FROM @Customerdata ) X
INNER JOIN
(SELECT CustomerNum,
VehicleName,
Vehiclepurchaseyear,
Vehicleprice
FROM @CustomerData ) Y
on X.CustomerNum = Y.customernum
结果没有预料到:
1000 Toyoto Camry 2012 14500 Toyoto Camry 2012 14500
1000 Toyoto Camry 2012 14500 Nissan Ultima 2014 12000
1000 Nissan Ultima 2014 12000 Toyoto Camry 2012 14500
1000 Nissan Ultima 2014 12000 Nissan Ultima 2014 12000
1500 Honda Accord 2012 15000 Honda Accord 2012 15000
1500 Honda Accord 2012 15000 Honda Civic 2012 13000
1500 Honda Civic 2012 13000 Honda Accord 2012 15000
1500 Honda Civic 2012 13000 Honda Civic 2012 13000
1800 Toyoto RAV4 2012 16500 Toyoto RAV4 2012 16500
预期结果:
1000 Toyoto Camry 2012 14500 Nissan Ultima 2014 12000
1500 Honda Civic 2012 13000 Honda Accord 2012 15000
1800 Toyoto RAV4 2012
答案 0 :(得分:0)
这会在这个小表上给你想要的结果,但你应该让每一行都有一个唯一的id,而不是说x.vehiclename<> y.vehiclename你会使用UID。
Declare @CustomerData Table (CustomerNum int,
VehicleName Char(20),
Vehiclepurchaseyear char(4),
Vehicleprice char(10))
INSERT INTO @CustomerData(CustomerNum,VehicleName,Vehiclepurchaseyear,Vehicleprice)
VALUES (1000,'Toyoto Camry','2012',' 14500'),
(1000,'Nissan Ultima','2014','12000'),
(1500,'Honda Accord','2012','15000'),
(1500,'Honda Civic','2012','13000'),
(1800,'Toyoto RAV4','2012','16500')
SELECT X.customerNum as CustomerNum
,X.VehicleName as Vehiclename1
,X.Vehiclepurchaseyear as vehiclepurchaseyear1
,X.Vehicleprice as vehicleprice1
,Y.VehicleName as Vehiclename2
,Y.Vehiclepurchaseyear as vehiclepurchaseyear2
,Y.Vehicleprice as vehicleprice2
FROM
(SELECT CustomerNum,VehicleName,Vehiclepurchaseyear,Vehicleprice
from @Customerdata ) X
INNER JOIN
(SELECT CustomerNum,VehicleName,Vehiclepurchaseyear,Vehicleprice
from @CustomerData ) Y
on X.CustomerNum = Y.customernum
and X.VehicleName<>Y.VehicleName
答案 1 :(得分:0)
猜猜,但这似乎是你想要的
SELECT cd.CustomerNum,
MAX( CASE WHEN Rn = 1 THEN cd.VehicleName END) AS VehicleName1,
MAX( CASE WHEN Rn = 1 THEN cd.VehiclePurchaseYear END) AS VehiclePurchaseYear1,
MAX( CASE WHEN Rn = 1 THEN cd.VehiclePrice END) AS VehiclePrice1,
MAX( CASE WHEN Rn = 2 THEN cd.VehicleName END) AS VehicleName2,
MAX( CASE WHEN Rn = 2 THEN cd.VehiclePurchaseYear END) AS VehiclePurchaseYear2,
MAX( CASE WHEN Rn = 2 THEN cd.VehiclePrice END) AS VehiclePrice2
FROM ( SELECT *,
ROW_NUMBER() OVER (PARTITION BY CustomerNum ORDER BY VehiclePurchaseYear) Rn
FROM @Customerdata) cd
GROUP BY cd.CustomerNum
这将按年份对每个客户的购买进行排序,并将第一个结果作为Rn 1,第二个作为Rn 2。
如果在同一年购买了2辆汽车,那么它是不可靠的,所以你可能想要包括一个DateTime标记来订购它。