我無法獲取我想要的記錄。Sql如何結合一個記錄中的兩條記錄,並從表中選擇任意一個字段
SELECT
ROW_NUMBER() OVER(order by CAST(pr.District As INT) ASC) AS Row,
pr.District, pr.Project, pr.Tenure,
CASE pr.CompletionDate
WHEN NULL THEN 'UNKNOWN'
WHEN '' THEN 'UNKNOWN'
ELSE pr.CompletionDate
END AS COMPLETIONYEAR,
pr.AVG_PSF, pr.NoOfTransaction,
p.AVG_PSFRENTAL, p.RentalContract,
ROUND(((p.MAXRENT * 1200)/ pr.MAXPRICE),2,0) AS RENTAL_YIELD,
pr.Latitude, pr.Longitude,
CASE
WHEN ROUND(((p.MAXRENT * 1200)/ pr.MAXPRICE),2,0) < 3.0
THEN 'http://maps.google.com/mapfiles/ms/icons/blue.png'
WHEN ROUND(((p.MAXRENT * 1200)/ pr.MAXPRICE),2,0) > 3.0
AND ROUND(((p.MAXRENT * 1200)/ pr.MAXPRICE),2,0) < 4.0
THEN 'http://maps.google.com/mapfiles/ms/icons/green.png'
WHEN ROUND(((p.MAXRENT * 1200)/ pr.MAXPRICE),2,0) > 4.0
AND ROUND(((p.MAXRENT * 1200)/ pr.MAXPRICE),2,0) < 5.0
THEN 'http://maps.google.com/mapfiles/ms/icons/red.png'
ELSE 'http://maps.google.com/mapfiles/ms/icons/yellow.png'
END AS MARKER
FROM
(SELECT
District, Project,
MAX(Rent) AS MAXRENT,
ROUND(AVG((Rent/MaxArea)), +2, 0) AS AVG_PSFRENTAL,
COUNT(PrivateURARentalID) AS RentalContract
FROM
(SELECT
PrivateURARentalID, District, Project,
Rent, MaxArea, RentalDate
FROM
PrivateURARental
WHERE
RentalDate >= DATEADD(MONTH,DATEDIFF(MONTH, 0, GETDATE()) - 6, 0)
AND Project = 'QUEENS') [temp]
GROUP BY
Project, District
HAVING
COUNT(PrivateURARentalID) > 3) [p]
INNER JOIN
(SELECT
District, Project, Tenure, CompletionDate,
ROUND(AVG(psf),0,0) AS AVG_PSF,
MAX(price) AS MAXPRICE,
COUNT(psf) AS NoOfTransaction,
Latitude, Longitude
FROM
(SELECT
PrivateID, District, Project, Tenure,
CompletionDate, psf, Price,
Latitude, Longitude
FROM
Private
WHERE
[ContractDate] >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 6, 0)
AND Project = 'QUEENS') [temp]
GROUP BY
Project, District, Tenure, CompletionDate,
Latitude, Longitude
HAVING COUNT(PrivateID) > 3) pr ON p.Project = pr.Project
ORDER BY
CAST(pr.District As INT) ASC
現在從該查詢我得到這樣的結果:
1 3 QUEENS 99 Yrs From 16/02/1998 2002 1264 4 3.84 89 4.96 1.2925986 103.8072896 http://maps.google.com/mapfiles/ms/icons/red.png
2 3 QUEENS 99 Yrs From 16/02/1998 2002 1231 5 3.84 89 4.85 1.2936208 103.8063609 http://maps.google.com/mapfiles/ms/icons/red.png
爲同一個項目我有兩個records.I知道this.Its背後的原因是由於不同的經度和latitude.My問題我想從這兩個位置選擇經度中的任何一個。我還希望將這些記錄記錄在該項目的單個記錄中。
請幫我這麼做。
哪個RDBMS是這樣的?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –