抱歉我的英语不好。
我得到了一些结果。你可以在SS看到。
我的查询:
SELECT TransferOrderDate=Siparisler.datescheduled,
ProductTransferDate=UrunTransfer.daterealized,
FirmName=Bayi.firmname,
PlateOfTruck=UrunTransfer.plate,
DriverOfTruck=Surucu.name,
LatOfTruck=TankerKonum.lat,
LngOfTruck=TankerKonum.lng,
SpeedOfTruck=TankerKonum.speed,
LastUpdate=TankerKonum.readtime,
PrintID=UrunTransfer.printoutid
FROM producttransfer AS UrunTransfer
JOIN transferorder AS Siparisler
ON Siparisler.oid = UrunTransfer.transferorderid
JOIN dealer AS Bayi
ON Bayi.oid = Siparisler.dealerid
JOIN driver AS Surucu
ON Surucu.oid = Siparisler.driverid
JOIN devcocom_admin.tankerlocation AS TankerKonum
ON TankerKonum.tankerid = Siparisler.tankerid
WHERE UrunTransfer.daterealized > Dateadd(hour, -24, Getdate())
AND TankerKonum.oid IN (SELECT Max(TankerKonum.oid)
FROM devcocom_admin.tankerlocation AS
TankerKonum
GROUP BY TankerKonum.tankerid)
输出如下:
但是
此查询结果有3个不同的经销商。但它有相同的truck,lat,lng,speed
等。
在一个truck
工作一个或两个或三个dealer
。
我想在同一行显示所有结果。
如何将查询结果更改为:
TDName1:TransferDate for FirmName1
TDName2:TransferDate for FirmName2
TDName3:TransferDate for FirmName3
PDName1 : ProductDate for FirmaName1
PDName2 : ProductDate for FirmaName2
PDName3 : ProductDate for FirmaName3
答案 0 :(得分:-2)
假设你做了你想做的事。如果有4个或更多经销商出现怎么办?一般解决方案是GROUP BY列是相同的,对于你想要拆分为3列的东西,我会使用一些返回数组的聚合函数。在psql中它是array_agg。所以它看起来像:
SELECT a,b,c
, (array_agg(d order by a,b,c))[1] as d1
, (array_agg(d order by a,b,c))[2] as d2
, (array_agg(d order by a,b,c))[3] as d3
FROM foo
GROUP BY a,b,c
祝你好运!