2014-09-12 128 views
0

首先,我將聲稱在SQL中沒有極端的專業知識。TSQL 2中的查詢1

我想要做的是根據幾個不同的標準從SQL Server數據庫中提取一些事件計數,並將它們放入SSRS中顯示每月計數的折線圖上。我最初創建的兩個查詢都會提取正確的數據,但SSRS不允許在一個圖表上使用多個數據集。這是我掛斷的地方。我確信我可以做一些子查詢來將兩個數據集合成一個只是將計數分隔成列,但是我迄今爲止所嘗試的總計了整個計數。

我需要的數據是報告日期,它是日期/時間值和發生的事件計數(無需彙總計數,或不應該,因爲SSRS將允許我按月對它們進行分組,這是報告應該看起來的樣子)匹配2個標準中的每一個。

下面是兩個查詢,我現在用:

SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count 
FROM   v_Incident INNER JOIN 
         v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number 
WHERE  (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND 
         (v_Incident.CustomerLastName = N'Impact Manager') AND (v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task/Action', 
         'Working Log')) AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE())) 
GROUP BY v_Incident.ReportedDate 
FROM v_Incident 

第二個查詢

SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count 
FROM   v_Incident INNER JOIN 
         v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number 
WHERE  (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND 
         (v_Incident.CustomerLastName = N'Impact Manager') AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE())) 
GROUP BY v_Incident.ReportedDate 
FROM v_Incident 

正如你所看到的,我拉下reporteddate和計數。如果我只將這兩個語句放在一個語句中[select(query1)as filtered](query2)as base]並將group by移到外面,我會得到結果,但它是每個值上的每個值的總和單線,約10K線太多。

是否有一個簡單的解決方案,可以讓我獲得每個日期,以及單個基數和使用這兩個或類似查詢進行過濾?非常感謝您提前!

+1

那麼你想結果拆分的查詢結果或結合?如在查詢1返回'1/1/2014,10'和查詢2返回'1/1/2014,20'你想'1/1/2014,30'還是你希望它顯示重複項? – Elias 2014-09-12 15:34:43

+1

它應該是分開的,所以如果查詢1返回1/1/2014,10和查詢2返回1/1/2014,20它應該輸出1/1/2014 BASE:10 FILTERED:20 – billhubb84 2014-09-12 15:37:03

回答

1

您的查詢非常相似。它看起來像有一個總數,然後是具有特定工作日誌類型的某些記錄的部分計數。這是一個返回日期BaseCount的查詢,並且'WorkLogFilteredCount'SSRS應該能夠非常容易地將日期用作軸,將另外兩列作爲單獨的系列用於圖表。

SELECT v_Incident.ReportedDate 
     , COUNT(*) AS BaseCount 
     , SUM(CASE WHEN v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task/Action', 'Working Log') THEN 1 ELSE 0 END) AS WorkLogFilteredCount 

FROM v_Incident 
     INNER JOIN v_IncidentMaxWorkLog_Category 
      ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number 
WHERE (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') 
     AND (v_Incident.CustomerFirstName = N'BMC') 
     AND (v_Incident.CustomerLastName = N'Impact Manager') 
     AND (v_Incident.ReportedDate >= '7/1/2014') 
     AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE())) 
GROUP BY v_Incident.ReportedDate 
FROM v_Incident 
+0

這看起來特別有效包括我是否需要在未來對需求進行更改。但是,當我嘗試執行查詢時,我收到第一個FROM語句的語法錯誤。有什麼想法嗎?基於我對t-sql的理解,這看起來是正確的。管理工作室還報告說SUM不是一個內置函數,但是當我嘗試執行查詢時,它並沒有出現錯誤。 Msg 156,Level 15,State 1,Line 5 關鍵字'FROM'附近的語法不正確。 – billhubb84 2014-09-12 16:02:12

+0

我已經把它放在了錯誤的地方,但是它從case case語句的末尾丟失了,並且它在正確的位置,這個語句的工作效率很高。我需要做的唯一事情就是把這個事情弄到最終狀態,這並不是一件好事。對於每個worklogfiltered條目,我有2個全部數量的條目,它實際上應該是1。 – billhubb84 2014-09-12 16:08:25

1

根據我的想法你想我會使用UNION並將聯合查詢作爲subquery

SELECT * 
FROM 
    ((SELECT DISTINCT 
      v_Incident.ReportedDate, COUNT(*) AS Count, 'BASE' as [Designator] 
     FROM    
      v_Incident 
     INNER JOIN 
      v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number 
     WHERE   
     (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') 
     AND (v_Incident.CustomerFirstName = N'BMC') 
     AND (v_Incident.CustomerLastName = N'Impact Manager') 
     AND (v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task/Action', 'Working Log')) 
     AND (v_Incident.ReportedDate >= '7/1/2014') 
     AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE())) 
    GROUP BY 
     v_Incident.ReportedDate 
FROM v_Incident) 

UNION ALL 

(SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count, 'Filtered' as [Designator] 
FROM   v_Incident INNER JOIN 
         v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number 
WHERE  (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND 
         (v_Incident.CustomerLastName = N'Impact Manager') AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE())) 
GROUP BY v_Incident.ReportedDate 
FROM v_Incident)) ORDER BY ReportedDate, Designator 
1
select isnull(a.ReportedDate, b.ReportedDate) ReportedDate, 
     a.Count ACount, b.Count BCount 
from 
(
    SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count 
    FROM   v_Incident INNER JOIN 
          v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number 
    WHERE  (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND 
          (v_Incident.CustomerLastName = N'Impact Manager') AND (v_IncidentMaxWorkLog_Category.WORK_LOG_TYPE IN ('General Information', 'Incident Task/Action', 
          'Working Log')) AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE())) 
    GROUP BY v_Incident.ReportedDate 
    FROM v_Incident 
) a 
full outer join 
(
    SELECT DISTINCT v_Incident.ReportedDate, COUNT(*) AS Count 
    FROM   v_Incident INNER JOIN 
          v_IncidentMaxWorkLog_Category ON v_Incident.IncidentID = v_IncidentMaxWorkLog_Category.Incident_Number 
    WHERE  (v_Incident.Summary LIKE N'Intelligent Incident for Impacted CI%') AND (v_Incident.CustomerFirstName = N'BMC') AND 
          (v_Incident.CustomerLastName = N'Impact Manager') AND (v_Incident.ReportedDate >= '7/1/2014') AND (v_Incident.ReportedDate >= DATEADD(year, - 1, GETDATE())) 
    GROUP BY v_Incident.ReportedDate 
    FROM v_Incident 
) b on b.ReportedDate = a.ReportedDate 

這把每個你原來的查詢,並把它們作爲一個「表」到其中加入他們的ReportedDate第三查詢。它使用完全外部聯接,因此如果缺少b,仍會檢索到a,如果缺少a,則仍會報告b。這種方式有3個組合:

  • 一個是存在的,b爲null
  • a和b是有
  • 一個爲空,B是有 因此,爲了得到結果使用ISNULL的ReportedDate顯示a.ReportedDate並且它是空的(上面的情況3)使用b.ReportedDate。

請記住,在任何SQL Select中,您都可以用Select查詢替換表名。