在SQL Server中,我想对具有多个列的distinct
语句中的单个列 [BLC.Container_No] 使用SELECT
关键字。
我的查询:
SELECT BLD.VV_CODE,
V.Vessel_name,
BLD.Arrival_date,
ISNULL(IGM.VIR_NO, 'NULL') AS VIR_NO,
ISNULL(BLD.TERMINAL_CODE, 'NULL') AS TERMINAL_CODE,
BLD.BL_NO,
ISNULL(BLD.Parent_BL, 'NULL') AS Parent_BL,
BLD.Consignee_Description,
DO.DO_Issue_Date,
CA.CAgent_Name,
BLC.Container_No,
CS.Container_Size_Description
FROM Vessel V,
IGM,
BL_DATA BLD,
CAgent CA,
Delivery_Order DO,
BL_Container BLC,
Container_Size CS
WHERE V.Vessel_code = SUBSTRING(BLD.VV_CODE, 1, 3)
AND BLD.VV_CODE = IGM.VV_CODE
AND DO.CAgent_Code = CA.CAgent_Code
AND BLD.BL_NO = BLC.BL_NO
AND BLC.Container_Size_Code = CS.Container_Size_Code;
答案 0 :(得分:1)
您不能那样做。 SQL怎么知道您想要哪个BLC.Container_NO?您必须告诉sql您想要哪个。您可以使用这个。
With cte as(
SELECT BLD.VV_CODE,
V.Vessel_name,
BLD.Arrival_date,
ISNULL(IGM.VIR_NO, 'NULL') AS VIR_NO,
ISNULL(BLD.TERMINAL_CODE, 'NULL') AS TERMINAL_CODE,
BLD.BL_NO,
ISNULL(BLD.Parent_BL, 'NULL') AS Parent_BL,
BLD.Consignee_Description,
DO.DO_Issue_Date,
CA.CAgent_Name,
BLC.Container_No,
ROW_NUMBER() OVER (PARTITION BY BLD.VV_CODE ORDER BY BLC.Container_No asc) AS row_num
,CS.Container_Size_Description
FROM Vessel V,
IGM,
BL_DATA BLD,
CAgent CA,
Delivery_Order DO,
BL_Container BLC,
Container_Size CS
WHERE V.Vessel_code = SUBSTRING(BLD.VV_CODE, 1, 3)
AND BLD.VV_CODE = IGM.VV_CODE
AND DO.CAgent_Code = CA.CAgent_Code
AND BLD.BL_NO = BLC.BL_NO
AND BLC.Container_Size_Code = CS.Container_Size_Code)
SELECT *
FROM cte
WHERE rn = 1
答案 1 :(得分:0)
您不能..除非您对列进行分组..然后您可以指定分组功能,例如count(distinct field1)
或sum(distinct field2)
等。