-1
我有2個表,「read_barcodetolid」和「serial_barcode」。他們都有ordernum領域的共同點。我要選擇行,不希望有重複的值, 起初我用這個查詢:加入select語句中的額外重複記錄
SELECT distinct(t1.ordernum)
, t1.tarikh,
t2.city,
t2.officeNum,
t2.provience
FROM read_barcodetolid t1
join serial_barcode t2 on t1.ordernum=t2.ordernum
它正常工作與剛剛ORDERNUM領域。但是當我添加另一個像城市的字段時,它會創建一些重複記錄。
然後我寫了這一個:
SELECT DISTINCT (
SELECT DISTINCT TOP (1) ordernum
FROM read_barcodetolid
WHERE (ordernum = seri.ordernum)
ORDER BY ordernum DESC
) AS ordernum,
(
SELECT TOP (1) seri.officeNum
FROM read_barcodetolid AS read_barcodetolid_2
WHERE (ordernum = seri.ordernum)
ORDER BY ordernum DESC
) AS officeNum,
(
SELECT TOP (1) provience
FROM serial_barcode
WHERE (ordernum = seri.ordernum)
) AS provience,
(
SELECT TOP (1) city
FROM serial_barcode AS serial_barcode_1
WHERE (ordernum = seri.ordernum)
) AS city,
(
SELECT TOP (1) tarikh
FROM read_barcodetolid AS read_barcodetolid_1
WHERE (ordernum = seri.ordernum)
) AS tarikh
FROM serial_barcode AS seri
WHERE (ordernum IS NOT NULL)
ORDER BY tarikh DESC
它返回的結果,但在最後給出了一些空的記錄,當我使用「其中ORDERNUM IS NOT NULL」它不工作
我能做些什麼? 是更好的解決方案來擺脫額外的重複記錄?