2017-02-20 64 views
-1

我試圖創建一個有3列的查詢:1列代表列表作業數(不是空值),2°和第3列分別代表特定日期之前和之後的值之和具有多個值錯誤的子查詢。多列的案例

的SQL Server提供了一個錯誤,我真的不知道如何圍繞它的工作(除了不是創建2個查詢。)

SELECT 
    (SELECT 
     PiIndex 
    FROM 
     EleGcImpianti 
    WHERE 
     PiIndex IS NOT NULL) AS Commessa, 
    (SELECT 
     SUM(PrImportoDaFatturare) 
    FROM 
     EleGcPianificazioneRate 
    RIGHT OUTER JOIN 
     EleGcImpianti ON EleGcPianificazioneRate.PrIdImpianto = EleGcImpianti.PiId 
    WHERE 
     (EleGcPianificazioneRate.PrDataFattura <= CONVERT(datetime, '2014-01-01', 102))) AS [Somma Rate], 
    (SELECT 
     SUM(PrPercentualeDaFatturare) 
    FROM 
     EleGcPianificazioneRate 
    RIGHT OUTER JOIN 
     EleGcImpianti ON EleGcPianificazioneRate.PrIdImpianto = EleGcImpianti.PiId 
    WHERE 
     (PrDataFattura > CONVERT(datetime, '2014-01-01', 102))) AS [Somma Percentuale Rate] 
+0

錯誤說的是什麼? – MPelletier

+0

您的sql語法無效 - 您只能使用子查詢指定select子句,但不指定表/視圖; – collapsar

+0

此外,我懷疑你的第一個子查詢返回多個記錄,這也是不允許的 – Kostis

回答

1

看來你只是想選擇EleGcImpianti記錄,並希望相關來自EleGcPianificazioneRate:

SELECT 
    i.PiIndex, 
    COALESCE(pr.sum_after, 0) AS sum_after_date, 
    COALESCE(pr.sum_before, 0) AS sum_before_date 
FROM EleGcImpianti i 
LEFT JOIN 
(
    SELECT 
    PrIdImpianto, 
    SUM(CASE WHEN PrDataFattura <= '20140101' THEN PrImportoDaFatturare END) AS sum_after, 
    SUM(CASE WHEN PrDataFattura > '20140101' THEN PrImportoDaFatturare END) AS sum_before 
    FROM EleGcPianificazioneRate 
    GROUP BY PrIdImpianto 
) pr ON pr.PrIdImpianto = i.PiId 
WHERE i.PiIndex IS NOT NULL 
+0

是的。這是,謝謝!一件事:當我嘗試使用你的查詢時,SQL說**「相關名'i'在FROM子句中被多次指定。」**什麼是錯的? – Etater

+0

明白了!我用'我'改變了'pr'。謝謝 !!!!! SELECT i.PiIndex, COALESCE(pr.sum_after,0)AS sum_after_date, COALESCE(pr。sum_before,0)AS sum_before_date FROM EleGcImpianti我 LEFT JOIN ( SELECT PrIdImpianto,SUM(CASE WHEN PrDataFattura <= '20140101' THEN PrImportoDaFatturare END)AS sum_after, SUM(CASE WHEN PrDataFattura> '20140101' THEN PrImportoDaFatturare END )AS sum_before FROM EleGcPianificazioneRate GROUP BY PrIdImpianto)AS pr ON pr.PrIdImpianto = i.PiId WHERE i.PiIndex IS NOT NULL – Etater

+0

糟糕,是的,對此抱歉。 –

0

您的查詢語法無效SQL:沒有從中收集結果的表/視圖(除子查詢外)。

以下(另)查詢...

  • ...限定從截止日期條件
  • 導出的指標變量after_cutoff ...從相應微不足道刪除用於summarions的基礎值時間
  • 執行全局求和
  • 忽略PiIndex列 - 使用此信息時聚合數據只有在它構成數據分區的一部分時纔有意義。

這應該產生一個記錄和總結果。

請注意,有選擇地總和值的常用SQL慣用法是group by子句。但是,結果將包括各個「無趣」日期時間間隔的總和結果。

導致你可能要檢查你是否真的希望兩個求和被應用到互補日期時間的建議 - 列名建議大家要總結所有值有絕對的價值觀和所有日期(或至少同一時期)的百分比。

select sum(sq2.base_sum_rate)  AS [Somma Rate] 
     , sum(sq2.base_sum_pct_rate) AS [Somma Percentuale Rate] 
     from ( 
       select sq1.after_cutoff 
        , sq1.PrImportoDaFatturare  * (1 - sq1.after_cutoff) base_sum_rate 
        , sq1.PrPercentualeDaFatturare * sq1.after_cutoff  base_sum_pct_rate 
       from (
           select * 
            , CASE WHEN EleGcPianificazioneRate.PrDataFattura <= CONVERT(DATETIME, '2014-01-01', 102) THEN 0 ELSE 1 END after_cutoff 
           from EleGcPianificazioneRate 
         RIGHT OUTER JOIN EleGcImpianti ON EleGcPianificazioneRate.PrIdImpianto = EleGcImpianti.PiId 
           where EleGcImpianti.PiIndex IS NOT NULL 
        ) sq1 
      ) sq2 
     ;