2017-10-01 12 views
1

こんにちは、where Date between ..SalesPercentage > 50のような条件をここに追加したいのですが、salespercentageはdbの列ではなく、以下のコードのようにキャストされています。Where句が計算された列

どうすれば使用できますか?前もって感謝します。

 var sql = @"SELECT TOP 10 
        sp.ProductName, 
        SUM(sp.QtySold) AS QtySold, 
        SUM(r.QtyinPieces) AS StockLimit,     
        CAST( (CAST (SUM(sp.QtySold) AS FLOAT) / CAST(SUM(r.QtyinPieces) AS FLOAT) ) * 100 AS DECIMAL(8,2)  ) [SalesPercentage] 
       FROM 
        Sales_productholder sp 
        JOIN Orders_productholder r ON (sp.ProductID = r.ProductID) 

       GROUP BY 
        sp.ProductName, r.ProductID, r.QtyinPieces 
       ORDER BY 
        SUM(sp.QtySold) DESC"; 

アップデート:私は5,10,20などのアイテムを持っているコンボボックスを持っていると私はTop 5ためのパラメータとしてそれを使用したいです。しかし、それは常にここでエラーにIncorrect syntax near TOP

を引き起こす私が試したものです:

SELECT TOP @top

command.Parameters.AddWithValue("@Top", cboTop.SelectedItem.ToString()); 

string topp = cboTop.SelectedItem.ToString(); 
command.Parameters.AddWithValue("@Top", topp); 

答えて

0

あなたはこのようなHAVINGを追加することができます。

HAVING 
    CAST((CAST (SUM(sp.QtySold) AS FLOAT)/CAST(SUM(r.QtyinPieces) AS FLOAT)) * 100 AS DECIMAL(8,2)) > 50 

すべてのクエリ:

SELECT TOP 10 
    sp.ProductName, 
    SUM(sp.QtySold) AS QtySold, 
    SUM(r.QtyinPieces) AS StockLimit,     
    CAST((CAST (SUM(sp.QtySold) AS FLOAT)/CAST(SUM(r.QtyinPieces) AS FLOAT)) * 100 AS DECIMAL(8,2)) [SalesPercentage] 
FROM 
    Sales_productholder sp 
    JOIN Orders_productholder r ON (sp.ProductID = r.ProductID) 
WHERE [Date] between @DateBegin and @DateAdd 
GROUP BY 
    sp.ProductName, r.ProductID, r.QtyinPieces 
HAVING 
    CAST((CAST (SUM(sp.QtySold) AS FLOAT)/CAST(SUM(r.QtyinPieces) AS FLOAT)) * 100 AS DECIMAL(8,2)) > 50 
ORDER BY 
    SUM(sp.QtySold) DESC 
+0

それは働いていたがありがとうございました! – StudentDev

+0

あなたはまだそこにいますか?私は質問を追加したい。 – StudentDev

+0

はい、私はここにいます、何が質問ですか? –

関連する問題