2016-03-31 121 views
-1

[比較兩列ftp_start_time和ftp_stop_time並根據條件返回列數:3列爲1.Tried(count(ftp_start_time))2.成功3.(ftp_start_time當ftp_stop_time不爲空的計數)失敗(ftp_stop_time的數量必須爲null)..請幫助比較兩列ftp_start_time和ftp_stop_time並根據條件返回列數

Screenshot

SELECT 
    a.Branch_code, 
    b.DevCount AS total, 
    COUNT(a.ftp_start_time) AS tried 
FROM 
    ftplogview AS a 
LEFT JOIN 
    palmtecsetup AS b ON a.Branch_code = b.Branch_code 
WHERE 
    ftp_start_date = '2016-03-31' 
GROUP BY 
    a.Branch_code, b.DevCount 
ORDER BY 
    Branch_code 

這是我迄今使用的查詢...誰能幫幫我?

+0

你的圖像沒有鏈接到這個問題。 – JoeFletch

+0

@RizwanaNazeer我可以看到ftp_stop_date和ftp_stop_time爲NULL。那些是你所指的專欄? –

+0

我錯了問題..實際上ftp_start_time和ftp_stop_time比較 –

回答

0

希望這人會幫助你,使用子選擇查詢像下面 -

SELECT 
     q.Branch_code, 
     q.DevCount AS total, 
     COUNT(q.ftp_start_time) AS tried, 
     SUM(Success) AS SuccessCount, 
     SUM(Failed) AS FailedCount, 
     SUM(NOTTRIED) AS NOTTRIEDCount 
    FROM 
    (
     SELECT 
      a.Branch_code, 
      b.DevCount AS total, 
      a.ftp_start_time AS tried, 
      CASE WHEN a.ftp_start_time IS NOT NULL AND a.ftp_stop_time IS NOT NULL THEN 1 ELSE 0 END AS Success, 
      CASE WHEN a.ftp_start_time IS NOT NULL AND a.ftp_stop_time IS NULL THEN 1 ELSE 0 END AS Failed, 
      CASE WHEN a.ftp_start_time IS NULL AND a.ftp_stop_time IS NULL THEN 1 ELSE 0 END AS NOTTRIED 
     FROM 
      ftplogview AS a 
     LEFT JOIN 
      palmtecsetup AS b ON a.Branch_code = b.Branch_code 
     WHERE 
      ftp_start_date = '2016-03-31'  

    ) Q 
    GROUP BY 
     Q.Branch_code, Q.Total 
    ORDER BY 
     Q.Branch_code 
+0

Msg 4104,Level 16,State 1,Line 3 多部分標識符「a.Branch_code」不能界。 Msg 4104,Level 16,State 1,Line 4 無法綁定多部分標識符「b.DevCount」。 Msg 4104,Level 16,State 1,Line 5 無法綁定多部分標識符「a.ftp_start_time」。 嘗試上面的代碼後,我得到這個錯誤....! –

+0

編輯我的查詢,請現在檢查。 – Vinit

+0

非常有幫助...非常感謝!這幫助了我......感謝你耐心地幫助我...... –

0

我會使用公共表表達式

with cte(Branch_code, DevCount, total,ftp_start_time,ftp_stop_date,ftp_stop_time) 
as (
SELECT 
    a.Branch_code, 
    b.DevCount, 
    a.ftp_start_time, 
    a.ftp_stop_date, 
    a.ftp_stop_time 
FROM 
    ftplogview AS a 
LEFT JOIN 
    palmtecsetup AS b ON a.Branch_code = b.Branch_code 
GROUP BY 
    a.Branch_code, b.DevCount 
) 
SELECT Branch_code, 
    DevCount AS total, 
    COUNT(ftp_start_time) AS tried, 
    SUM(
    case 
     when ftp_start_time is not null and ftp_stop_time is not null then 1 
     else 0 end 
     ) as success, 
    SUM(
     case 
      when ftp_stop_time is null then 1 
      else 0 end 
     ) as failed 
FROM CTE 
WHERE ftp_start_time = '2016-03-31' 
+0

其實在前端用戶必須選擇數據取得的日期......這段代碼不適用於那 –

+0

所以,請更好地解釋你的問題,否則難以相應地幫助你。 – alessalessio

+0

我已經添加了一個圖像可以ü請檢查? –

相關問題