2013-07-09 102 views
3

我寫了這個查詢,當我從下拉列表中選擇一年時,這個查詢給出了所有12個月中所有365天的明智數據。每月數據的sql查詢

SELECT 
     YEAR(Date) AS [Year] 
    , MONTH(Date) AS [Month] 
    , COUNT(*) AS [Total] 
FROM EnquiryMaster 
WHERE EnquiryOwner = '" + enqowner + "' 
    AND DATEPART(yyyy, Date) = '" + year + "' 
    AND orgid = '" + orgid + "' 
GROUP BY 
     YEAR(Date) 
    , MONTH(Date) 

UNION ALL 

SELECT 
     YEAR(Date) AS [Year] 
    , NULL 
    , COUNT(*) 
FROM EnquiryMaster 
WHERE EnquiryOwner = '" + enqowner + "' 
    AND DATEPART(yyyy, Date) = '" + year + "' 
    AND orgid = '" + orgid + "' 
GROUP BY YEAR(Date) 

我想寫一個查詢,將只用於每月

+0

哪些數據您用於列日期的類型? – Ahmed

回答

1

我覺得這個選擇月份也隨着年份和顯示數據是你想要什麼,假設我的理解這個問題

SELECT 
    DATEPART(yyyy, Date), 
    DATEPART(mm, Date), 
    COUNT(*) AS [Total] 
FROM EnquiryMaster 
GROUP BY 
    DATEPART(yyyy, Date), 
    DATEPART(mm, Date) 
ORDER BY 
    DATEPART(yyyy, Date), DATEPART(mm, Date) 
+0

DATEPART(mm,Date)],在選擇語句 – bvr

+0

中缺少逗號,它總共給出了月份。我想明智地和明智的日子.......我想檢索所有30或31天的數據 – user2147357

0

如果你想導致只有一個月,然後把它放在第(1月,2月)

SELECT 
     YEAR(Date) AS [Year] 
    , MONTH(Date) AS [Month] 
    , COUNT(*) AS [Total] 
FROM EnquiryMaster 
WHERE EnquiryOwner = '" + enqowner + "' 
    AND DATEPART(yyyy, Date) = '" + year + "' 
    AND orgid = '" + orgid + "' 
    AND MONTH(Date) = '" + month + "' 
GROUP BY 
     YEAR(Date) 
    , MONTH(Date) 

UNION ALL 

SELECT 
     YEAR(Date) AS [Year] 
    , NULL 
    , COUNT(*) 
FROM EnquiryMaster 
WHERE EnquiryOwner = '" + enqowner + "' 
    AND DATEPART(yyyy, Date) = '" + year + "' 
    AND orgid = '" + orgid + "' 
     AND MONTH(Date) = '" + month + "' 
GROUP BY YEAR(Date)