2015-12-16 38 views
4

選擇查詢不工作時MSSQL2014 我的架構使用的變量是: -使用可變Select查詢MSSQL中沒有運行

CREATE TABLE product 
    (idproduct int, name varchar(50), description varchar(50), tax decimal(18,0)) 


INSERT INTO product 
    (idproduct, name, description,tax) 
VALUES 
    (1, 'abc', 'This is abc',10), 
    (2, 'xyz', 'This is xyz',20), 
    (3, 'pqr', 'This is pqr',15) 


CREATE TABLE product_storage 
    (idstorage int,idproduct int,added datetime, quantity int, price decimal(18,0)) 


INSERT INTO product_storage 
    (idstorage,idproduct, added, quantity,price) 
VALUES 
    (1, 1, 2010-01-01,0,10.0), 
    (2, 1, 2010-01-02,0,11.0), 
    (3, 1, 2010-01-03,10,12.0), 
    (4, 2, 2010-01-04,0,12.0), 
    (5, 2, 2010-01-05,10,11.0), 
(6, 2, 2010-01-06,10,13.0), 
(7, 3, 2010-01-07,10,14.0), 
(8, 3, 2010-01-07,10,16.0), 
(9, 3, 2010-01-09,10,13.0) 

,我下面的命令執行: -

declare @price1 varchar(10) 


SELECT p.idproduct, p.name, p.tax, 
[@price1]=(SELECT top 1 s.price 
     FROM product_storage s 
     WHERE s.idproduct=p.idproduct AND s.quantity > 0 
     ORDER BY s.added ASC), 
(@price1 * (1 + tax/100)) AS [price_with_tax] 
FROM product p 

; 

這不在MSSQL中工作,請幫助我。 詳細檢查http://sqlfiddle.com/#!6/91ec2/296

和我的查詢工作在MYSQL 檢查細節: - http://sqlfiddle.com/#!9/a71b8/1

+1

一個很好的例子爲一個很好的問題,感謝小提琴 – Shnugo

回答

1

試試這樣說:

Explanantion:不能使用變量「在飛行中」,但你可以在APPLY中逐行計算...

SELECT p.idproduct, p.name, p.tax, 
     Price.price1, 
     (price1 * (1 + tax/100)) AS [price_with_tax] 
FROM product p 
CROSS APPLY (SELECT top 1 s.price 
      FROM product_storage s 
      WHERE s.idproduct=p.idproduct AND s.quantity > 0 
      ORDER BY s.added ASC) AS Price(price1) 

; 

編輯:你的小提琴採用的是壞的字面日期格式,試試這個:

INSERT INTO product_storage 
    (idstorage,idproduct, added, quantity,price) 
VALUES 
    (1, 1, '20100101',0,10.0), 
    (2, 1, '20100102',0,11.0), 
    (3, 1, '20100103',10,12.0), 
    (4, 2, '20100104',0,12.0), 
    (5, 2, '20100105',10,11.0), 
    (6, 2, '20100106',10,13.0), 
    (7, 3, '20100107',10,14.0), 
    (8, 3, '20100108',10,16.0), 
    (9, 3, '20100109',10,13.0) 
+0

這個查詢也沒有給出最後一行的正確結果,請與MYSQL比較http://sqlfiddle.com/#!9/a71b8/1 –

+1

AS TFroes指出,原因在於你對排序的思考。 SQL Server不保留您可以依賴的隱式行順序。如果有多個具有相同值的行進行排序,則它將爲**完全隨機**,這將首先發生......嘗試使用唯一日期和/或包含時間的兩個查詢。 – Shnugo

+0

好吧,但我已經改變了相同的值,然後它也顯示了相同的結果 檢查此:-http://sqlfiddle.com/#!6/c896a/2 –

3

嘗試此查詢

SELECT 
    p.idproduct 
    , p.name 
    , p.tax 
    , (t1.price * (1 + tax/100)) AS [price_with_tax] 
FROM product p 
inner join 
(
    SELECT ROW_NUMBER() over (PARTITION by s.idproduct order by s.added ASC) as linha, s.idproduct, s.price 
    FROM product_storage s 
    WHERE s.quantity > 0  
) as t1 
    on t1.idproduct = p.idproduct and t1.linha = 1 
+0

這個查詢也不會放棄最後一排的正確結果,請與MySQL查詢 –

+1

最後一行檢查(產品3)具有兩個相同的日期(2010-01-07)。您需要在查詢中添加其他條件(按順序排列)或添加日期。例如2010-01-07 10:00:00和2010-01-07 15:32:00 – TFroes

+0

但是,它具有相同的架構,它在MYSQL中工作 檢查此: - http://sqlfiddle.com/#!9/a71b8/ 1 –

0

這是SQL Server的正確的模式和查詢運行完美的Shnugo回答。

VALUES 
     (1, 1, convert(datetime,'2010-01-01'),0,10.0), 
     (2, 1, convert(datetime,'2010-01-02'),0,11.0), 
     (3, 1, convert(datetime,'2010-01-03'),10,12.0), 
     (4, 2, convert(datetime,'2010-01-04'),0,12.0), 
     (5, 2, convert(datetime,'2010-01-05'),10,11.0), 
     (6, 2, convert(datetime,'2010-01-06'),10,13.0), 
     (7, 3, convert(datetime,'2010-01-07'),10,14.0), 
     (8, 3, convert(datetime,'2010-01-07'),10,16.0), 
     (9, 3, convert(datetime,'2010-01-09'),10,13.0)