0

以下の列を持つ販売テーブルがあります

  • 名前
  • タイプ(学生、社会人、社会人)
  • 販売 (A、B、C、D)
  • 料金
  • 場所 (1,2,3,4)

名前に関連する種類、売上、場所の数を取得し、名前でグループ化してテーブルに表示できるようにしたい ここに画像の説明を入力

クエリを使用してグループ化されたすべての値を取得することは可能ですか>

どんな助けでも大歓迎です..

4

2 に答える 2

1

GROUP BY次にSUM(condition)、カウントを取得するために使用します。

SELECT   name AS Name,
         SUM(type     = 'worker' ) AS Worker,
         SUM(type     = 'student') AS Student,
         SUM(type     = 'adult'  ) AS Adult,
         SUM(sales    = 'A'      ) AS A,
         SUM(sales    = 'B'      ) AS B,
         SUM(sales    = 'C'      ) AS C,
         SUM(sales    = 'D'      ) AS D,
         SUM(location = 1        ) AS Location1,
         SUM(location = 2        ) AS Location2,
         SUM(location = 3        ) AS Location3,
         SUM(location = 4        ) AS Location4
FROM     sales
GROUP BY Name
于 2012-05-28T11:42:54.257 に答える
0
function getDistributorStat() // One Distributor
{

    $this ->db->select("count(*) ch1 ,
                        sum(c_privi_flag = 'Y') ch2, sum(c_happy1_flag = 'Y') ch3, sum(c_happy2_flag = 'Y') ch4,
                        sum(date(D_JOIN) = CURRENT_DATE) nch1,
                        sum(date(d_privi_activated) = CURRENT_DATE) nch2,
                        sum(date(d_happy1_activated) = CURRENT_DATE) nch3,
                        sum(date(d_happy2_activated) = CURRENT_DATE) nch4"
                        );
    $this->db->from('bc_master');
    $query = $this ->db->get()->row();
    $data['distributors']=$query->ch1;
    $data['privilaged']=$query->ch2;
    $data['happy1']=$query->ch3;
    $data['happy2']=$query->ch4;
    $data['new_distributors']=$query->nch1;
    $data['new_privilaged']=$query->nch1;
    $data['new_happy1']=$query->nch1;
    $data['new_happy2']=$query->nch1;

    return $data;

}   
于 2013-08-13T15:05:08.550 に答える