2017-05-10 12 views
0

私はmysqlクエリに新しいです、そして、私はクエリを取得することに固執しています。基本的には、以下の画像に示すように、年と月(Jan'17、Feb'17 ...)を水平方向(列)にグループ化する必要があります。クエリを作成するのに役立ちます年と月をグループ分けしてMysqlカウントをクエリする

Data image

Count

データを以下の画像に表示されています。

+0

アプリケーションコード – Strawberry

答えて

0

条件付き集計を行うことができます.1回はlogin_idごとに行い、次に全体的に結果を結合します。

Select login_idindex, 
    Sum(date_format(visitdate, '%Y%m') = '201701') as jan_17, 
    Sum(date_format(visitdate, '%Y%m') = '201702') as feb_17, 
    . . ., 
    Count(*) as total 
From your_table 
Where visitdate between ? and ? 
Group by login_idindex 

Union all 

Select null, 
    Sum(date_format(visitdate, '%Y%m') = '201701') as jan_17, 
    Sum(date_format(visitdate, '%Y%m') = '201702') as feb_17, 
    . . ., 
    Count(*) as total 
From your_table 
Where visitdate between ? and ?; 

あなたはすべての日付を使用している場合、実際の日付と?を交換するか、WHERE句を削除します。

+0

でデータ表示の問題を扱うことを真剣に検討していただきありがとうございます。あなたは私の時間を救った。 – Debaranjan

関連する問題