两个查询合并(SQL)

时间:2016-07-07 08:32:57

标签: sql tsql

我有两个查询,但我不知道很热合并他们。我正在努力实现将最新价格插入清单......

第一个是:

SELECT     
     ITM.CODE
    ,itm.NAME
    ,min(isnull(price,0)) [min price] 
    ,max(isnull(price,0)) [max]
    ,SUM(total)/SUM(amount) as [avr] 
FROM LG_092_ITEMS ITM 
INNER JOIN LG_092_01_STLINE STL 
    ON ITM.LOGICALREF = STL.STOCKREF
WHERE PRICE > 0 
    AND TRCODE = 1 
    AND CANCELLED = 0 
    AND INVOICEREF > 0
GROUP BY ITM.name , itm.code
ORDER BY code

第二个是:

SELECT
    ITM.CODE, itm.NAME, max (stl.date_) [latest date] , price as [latest price]

FROM LG_092_ITEMS ITM 
INNER JOIN LG_092_01_STLINE STL 
    ON ITM.LOGICALREF = STL.STOCKREF
WHERE PRICE > 0 
    AND TRCODE = 1 
    AND CANCELLED = 0 
    AND INVOICEREF > 0 


    group by code, name, stl.DATE_ , price

5 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

不是最优雅的,但我认为它会起作用:)

with CTE AS (
    SELECT ITM.CODE
        ,itm.NAME
        ,STL.price AS [latestprice]
        ,ROW_NUMBER() OVER (PARTITION BY ITM.CODE, itm.NAME ORDER BY stl.date_ desc) AS RowN
    FROM LG_092_ITEMS AS ITM
    JOIN LG_092_01_STLINE AS STL ON ITM.LOGICALREF = STL.STOCKREF
    WHERE STL.price > 0
        AND TRCODE = 1
        AND CANCELLED = 0
        AND INVOICEREF > 0
)
SELECT ITM.CODE
    ,itm.NAME
    ,min(isnull(STL.price, 0)) [min price]
    ,max(isnull(STL.price, 0)) [max]
    ,SUM(total) / SUM(amount) AS [avr]
    ,max(CTE.latestprice) AS latestprice
FROM LG_092_ITEMS AS ITM
JOIN LG_092_01_STLINE AS STL ON ITM.LOGICALREF = STL.STOCKREF
JOIN CTE ON CTE.CODE = itm.code AND CTE.name = ITM.name AND CTE.RowN = 1
WHERE STL.price > 0
    AND TRCODE = 1
    AND CANCELLED = 0
    AND INVOICEREF > 0
GROUP BY ITM.NAME,itm.code
ORDER BY itm.code 

有关CTE的更多信息,请参阅:https://msdn.microsoft.com/en-us/library/ms175972.aspx

答案 2 :(得分:0)

 select * 
 from 
 (
 SELECT     
     ITM.CODE
    ,itm.NAME
    ,isnull(price,0) as [latest price]
    ,min(isnull(price,0))   over (partition by ITM.name, itm.code) as [min price] 
    ,max(isnull(price,0))   over (partition by ITM.name, itm.code) as [max]
    ,SUM(total)/SUM(amount) over (partition by ITM.name, itm.code) as [avr]   
    ,row_number()           over (partition by ITM.name, itm.code order by stl.date_ desc) as rn
FROM LG_092_ITEMS ITM 
INNER JOIN LG_092_01_STLINE STL 
    ON ITM.LOGICALREF = STL.STOCKREF
WHERE PRICE > 0 
    AND TRCODE = 1 
    AND CANCELLED = 0 
    AND INVOICEREF > 0    
) tt
where tt.rn = 1
ORDER BY code

价格> 0你不需要isnull

答案 3 :(得分:0)

我们在朋友的帮助下找到了另一种方式......希望它对其他人也有帮助......

SELECT ITM.CODE
    ,itm.NAME
    ,min(isnull(STL.price, 0)) [min price]
    ,max(isnull(STL.price, 0)) [max]
    ,SUM(total) / SUM(amount) AS [avr]
    --SON SATINALMA FIYATI
    ,ISNULL((SELECT TOP 1 PRC.PRICE FROM LG_092_01_STLINE AS PRC
    WHERE PRC.STOCKREF = ITM.LOGICALREF
        AND PRC.PRICE > 0
        AND PRC.TRCODE = 1
        AND PRC.CANCELLED = 0
        AND PRC.INVOICEREF > 0
    ORDER BY PRC.DATE_ DESC), 0) AS latestprice  
FROM LG_092_ITEMS AS ITM
INNER JOIN LG_092_01_STLINE AS STL ON ITM.LOGICALREF = STL.STOCKREF
WHERE STL.PRICE > 0
    AND TRCODE = 1
    AND CANCELLED = 0
    AND INVOICEREF > 0
GROUP BY ITM.LOGICALREF, ITM.CODE, ITM.NAME
ORDER BY ITM.CODE

答案 4 :(得分:-3)

    SELECT     ITM.CODE, itm.NAME, min(isnull(price,0)) [min price] , max(isnull(price,0)) [max], SUM(total)/SUM(amount) as [avr] , 
    FROM         LG_092_ITEMS ITM inner join
    LG_092_01_STLINE STL ON ITM.LOGICALREF = STL.STOCKREF
    WHERE     PRICE > 0 AND TRCODE = 1 AND CANCELLED = 0 AND INVOICEREF > 0
    GROUP BY ITM.name , itm.code
    order by code
    UNION
    SELECT     price as [latest price]
    FROM         LG_092_ITEMS ITM inner join
    LG_092_01_STLINE STL ON ITM.LOGICALREF = STL.STOCKREF
    WHERE     PRICE > 0 AND TRCODE = 1 AND CANCELLED = 0 AND INVOICEREF > 0 and (date_ = max(date_)
    GROUP BY ITM.name , itm.code
    order by code

UNION子句将合并两个查询,只保留唯一值。