1

おはようございます。mysql/phpを使用してレポートを作成しようとして失敗しました。AccessCrosstabまたはExcelPivotと同様の列として行を作成したいと思います。営業スタッフと月間売上高があり、月を列ヘッダーとしてページ全体に表示したいと思います。

以下の私のクエリはデータを出力しますが、すべての営業担当者には毎月の行があり、あまりよく読めません。

      Jan | Feb | Mar | April

Sales1
Sales2
Sales3
Sales4

Select
  tblcontacts.ContactFullName,
  Count(tblcases.CaseID) As cases,
  MonthName(tblcases.CaseDate) As Monthly
From
  tblcases Inner Join
  tblcontacts On tblcases.ContactAssignedTo =
  tblcontacts.ContactID
Group By
  tblcontacts.ContactFullName,
  MonthName(tblcases.CaseDate)  
  with rollup

Antのアドバイスやポインタを高く評価しました。調査しましたが、ほとんどが頭に浮かびました。

敬具

4

2 に答える 2

1
Select 
    tblcontacts.ContactFullName, 
    sum(case when MonthName(tblcases.CaseDate)='January' then 1 else 0 end) as January,
    sum(case when MonthName(tblcases.CaseDate)='February' then 1 else 0 end) as February,
    .
    .
    sum(case when MonthName(tblcases.CaseDate)='December' then 1 else 0 end) as December,
From 
  tblcases Inner Join 
  tblcontacts On tblcases.ContactAssignedTo = 
  tblcontacts.ContactID 
  Group By 
  tblcontacts.ContactFullName
于 2012-07-19T09:24:48.880 に答える
1

連絡先ごとにグループ化し、条件付き集計を使用して各月に基づいて行をカウントするだけです。

SELECT
    a.ContactFullName,
    SUM(MONTH(b.CaseDate) = 1) AS Jan,
    SUM(MONTH(b.CaseDate) = 2) AS Feb,
    SUM(MONTH(b.CaseDate) = 3) AS Mar,
    SUM(MONTH(b.CaseDate) = 4) AS Apr,
    SUM(MONTH(b.CaseDate) = 5) AS May,
    SUM(MONTH(b.CaseDate) = 6) AS Jun,
    SUM(MONTH(b.CaseDate) = 7) AS Jul,
    SUM(MONTH(b.CaseDate) = 8) AS Aug,
    SUM(MONTH(b.CaseDate) = 9) AS Sep,
    SUM(MONTH(b.CaseDate) = 10) AS Oct,
    SUM(MONTH(b.CaseDate) = 11) AS Nov,
    SUM(MONTH(b.CaseDate) = 12) AS Dec
FROM
    tblcontacts a
INNER JOIN
    tblcases b ON a.ContactID = b.ContactAssignedTo
GROUP BY
    a.ContactFullName

編集:この回答へのコメントに従って:毎月の価格の合計を取得するには、次のようなことを行うことができます:

SELECT
    a.ContactFullName,
    SUM(IF(MONTH(b.CaseDate) = 1, b.price, 0)) AS Jan,
    SUM(IF(MONTH(b.CaseDate) = 2, b.price, 0)) AS Feb,
    SUM(IF(MONTH(b.CaseDate) = 3, b.price, 0)) AS Mar,
    SUM(IF(MONTH(b.CaseDate) = 4, b.price, 0)) AS Apr,
    SUM(IF(MONTH(b.CaseDate) = 5, b.price, 0)) AS May,
    SUM(IF(MONTH(b.CaseDate) = 6, b.price, 0)) AS Jun,
    SUM(IF(MONTH(b.CaseDate) = 7, b.price, 0)) AS Jul,
    SUM(IF(MONTH(b.CaseDate) = 8, b.price, 0)) AS Aug,
    SUM(IF(MONTH(b.CaseDate) = 9, b.price, 0)) AS Sep,
    SUM(IF(MONTH(b.CaseDate) = 10, b.price, 0)) AS Oct,
    SUM(IF(MONTH(b.CaseDate) = 11, b.price, 0)) AS Nov,
    SUM(IF(MONTH(b.CaseDate) = 12, b.price, 0)) AS Dec
FROM
    tblcontacts a
INNER JOIN
    tblcases b ON a.ContactID = b.ContactAssignedTo
GROUP BY
    a.ContactFullName

基本的に、各行について、casedateが特定の月にある場合は、price列の値をSUM集計に渡します。それ以外の場合は、単に渡します0

于 2012-07-19T09:29:35.853 に答える