簡體   English   中英

SQL Server異常報告腳本

[英]SQL Server Exception Reporting Script

看到這是我2016年的第一篇帖子,祝大家新年快樂。

再一次,我陷入了一個棘手的報告,我必須從SQL Server中提取報告。

我有兩個表叫做DocumentsDoctype

Documents欄:

File_Name, Date, Region, DoctypeID

此列中的數據如下。

   00001,2016-01-06,JHB,1d187ecc
   00001,2016-01-06,JHB,bccc05f9
   00001,2016-01-06,JHB,fe697be0
   00001,2016-01-06,JHB,bbae8c73
   00002,2016-01-06,JHB,1d187ecc
   00002,2016-01-06,JHB,bccc05f9
   00002,2016-01-06,JHB,fe697be0

Doctype列:

DoctypeID, Document_Type

此列中的數據如下。

1d187ecc, Collection
bccc05f9, Image
fe697be0, Log
bbae8c73, Sent to warehouse.

我的查詢需要使用上面的數據給我下面的結果。

File_Name,Collection, Image, Log, Sent to Warehouse,Region
00001,         1,       1,    1,       1,            JHB
00002,         1,       1,    1,       0,            JHB

我希望以上所述是有道理的,我將如何去做?

謝謝大家。

您可以嘗試以下方法:

SELECT FILE_NAME, ISNULL([Collection],0) AS [Collection], ISNULL([Image],0) AS [Image],  
ISNULL([Log],0) AS [Log],  ISNULL([Sent to warehouse],0) AS [Sent to warehouse], Region
FROM (  
    SELECT FILE_NAME, Document_Type, COUNT(Document_Type) AS Frequency, Region 
    FROM documents d,doctype dt WHERE d.DoctypeID = dt.DoctypeID 
    GROUP BY Document_Type, FILE_NAME,REGION
) AS s
PIVOT
(
    SUM(Frequency)
    FOR [Document_Type] IN ([Collection], [Image], [Log], [Sent to warehouse])
)AS pvt

這是您可以閱讀的詳細參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM