3

ドロップダウン リストから年を選択すると、12 か月すべての 365 日すべての日単位のデータを提供するこのクエリを作成しました。

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)

年とともに月を選択し、その月のデータのみを表示するクエリを作成したい

4

2 に答える 2

0

1 か月だけの結果が必要な場合は、where 句に入力します (1 月は 1、2 月は 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)
于 2013-07-09T06:16:47.310 に答える