我有这个程序返回三个记录,我需要它只返回具有最大日期的行。我创建了一个传递逗号分隔值的函数。它返回传递的值的多个记录。我需要它只返回最大日期的记录。我想我错过了一些简单的事情。在此先感谢
SELECT DISTINCT
ZITMTSICOD6.LNGDES_0 as variety
,STOLOT.YCROPYR_0 as crop_year
,STOLOT.LOT_0 as lot_number
,ZITMTSICOD2.LNGDES_0 AS product_name
,ISNULL([UV_Alpha], 'NA') as uv_alpha
,ISNULL([UV_Beta], 'NA') as uv_beta
,ISNULL([HSI], 'NA') as hsi
,ISNULL(Moisture, 'NA') as moisture
,ISNULL(Oil_by_Dist,'NA') as oil_by_dist
,ISNULL([HPLC Alpha],'NA') as hplc_alpha
,ISNULL([HPLC Beta],'NA') as hplc_beta
,ISNULL([HPLC Cohumulone],'NA') as hplc_cohumulone
,ISNULL([HPLC Colupulone],'NA') as hplc_colupulone
,ISNULL([Mebak Alpha 7.5],'NA') as mebak_alpha_7_5
,ISNULL([LCV Alpha 7.4],'NA') as lcv_alpha_7_4
,ISNULL(Oil_B_Pinene,'NA') as oil_b_pinene
,ISNULL(Oil_Myrcene,'NA') as oil_myrcene
,ISNULL(Oil_Linalool,'NA') as oil_linalool
,ISNULL(Oil_Caryophyllene,'NA') as oil_caryophyllene
,ISNULL(Oil_Farnesene,'NA') as oil_farnesene
,ISNULL(Oil_Humulene,'NA') as oil_humulene
,ISNULL(Oil_Geraniol,'NA') as oil_geraniol
,STOQLYH.CREDAT_0 as created_at
FROM
LIVE.STOLOT
left outer join
LIVE.STOQLYD on STOLOT.LOT_0 = STOQLYD.LOT_0 and STOLOT.SLO_0 =
STOQLYD.SLO_0 and STOQLYD.ITMREF_0 = STOLOT.ITMREF_0
left outer join
LIVE.STOQLYH on STOQLYH.VCRNUM_0 = STOQLYD.VCRNUM_0 and STOQLYH.ITMREF_0
= STOQLYD.ITMREF_0
left outer join
LIVE.ITMMASTER on STOLOT.ITMREF_0 = ITMMASTER.ITMREF_0
left outer join
LIVE.ZITMTSICOD6 on ITMMASTER.TSICOD_6 = ZITMTSICOD6.ID_0
left outer join
LIVE.ZITMTSICOD2 on ITMMASTER.TSICOD_2 = ZITMTSICOD2.ID_0
left outer join
(
SELECT
QLYCTLDEM_0,
QLYCRDASW.VCRLIN_0,
ITMREF_0,
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'UVALP110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'UV_Alpha',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'UVBET110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'UV_Beta',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'UVHSI110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'HSI',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'OVMOI110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Moisture',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'DIOIL110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_by_Dist',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'HPALP110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'HPLC Alpha',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'HPBET110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'HPLC Beta',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'HPCOH110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'HPLC Cohumulone',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'HPCOL110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'HPLC Colupulone',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'LCALP110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Mebak Alpha 7.5',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'LCALP310' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'LCV Alpha 7.4',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'GCBPI110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_B_Pinene',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'GCMYR110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_Myrcene',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'GCLIN110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_Linalool',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'GCCAR110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_Caryophyllene',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'GCFAR110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_Farnesene',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'GCHUM110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_Humulene',
MAX(CASE WHEN QLYCRDASW.QSTNUM_0 = 'GCGEL110' THEN QLYCRDASW.ASW_0
ELSE NULL END) AS 'Oil_Geraniol'
FROM
LIVE.QLYCRDASW
GROUP BY QLYCTLDEM_0, QLYCRDASW.VCRLIN_0, ITMREF_0
) AS QLYCRDASW
ON (STOQLYD.VCRNUM_0 = QLYCRDASW.QLYCTLDEM_0) AND (STOQLYD.VCRLIN_0 =
QLYCRDASW.VCRLIN_0) AND (STOQLYD.ITMREF_0 = QLYCRDASW.ITMREF_0)
WHERE STOLOT.LOT_0 In(
SELECT Item
--FROM dbo.SplitString('17-OR277-001,PL1-YKUCTZ0012,DZJ25168',','))
FROM dbo.SplitString('PL1-YKUCTZ0012',','))
and (ISNUMERIC(UV_Alpha) = 1 or ISNUMERIC(UV_Beta) = 1 or
ISNUMERIC(HSI) = 1 or ISNUMERIC(Moisture) = 1 and ISNUMERIC(Oil_by_Dist) = 1
or ISNUMERIC([HPLC Alpha]) = 1
or ISNUMERIC([HPLC Beta]) = 1 or ISNUMERIC([HPLC Cohumulone]) = 1 or
ISNUMERIC([HPLC Colupulone]) = 1 or ISNUMERIC([Mebak Alpha 7.5]) = 1 or
ISNUMERIC([LCV Alpha 7.4]) = 1
or ISNUMERIC(Oil_B_Pinene) = 1 or ISNUMERIC(Oil_Myrcene) = 1 or
ISNUMERIC(Oil_Linalool) = 1 or ISNUMERIC(Oil_Caryophyllene) = 1 or
ISNUMERIC(Oil_Farnesene) = 1 or ISNUMERIC(Oil_Humulene) = 1
or ISNUMERIC(Oil_Geraniol) = 1)
提前致谢
答案 0 :(得分:0)
您是否尝试使用RANK形式,可能是DENSE_RANK?这是一个很好的解释它们的网站。你在查询中添加它并将整个事物包装在另一个查询中,然后添加WHERE RankedColumn = 1.有意义吗?
https://www.c-sharpcorner.com/article/rank-denserank-and-rownumber-functions-in-sql-server/