4

このようなものを選択して実行したいと思います。

SELECT
        COUNT(*) WHERE switch=0 AND detail=1 AS zeroone
    AND
        COUNT(*) WHERE switch=0 AND detail=2 AS zerotwo
    AND
        COUNT(*) WHERE switch=1 AND detail=1 AS oneone
    AND 
        COUNT(*) WHERE swithc=1 AND detail=2 AS onetwo
  FROM tablename

これを行う方法はありますか?

4

5 に答える 5

4

CASEでステートメントを使用しSELECTて、結果を取得できます。

SELECT SUM(case when switch=0 AND detail=1 then 1 else 0 end) as zeroone
    , SUM(case when switch=0 AND detail=2 then 1 else 0 end) as zerotwo
    , SUM(case when switch=1 AND detail=1 then 1 else 0 end) as oneone
    , SUM(case when switch=1 AND detail=2 then 1 else 0 end) as onetw
FROM tablename
于 2012-07-31T17:42:37.930 に答える
4
SELECT
    sum(switch=0 AND detail=1) AS zeroone,
    sum(switch=0 AND detail=2) AS zerotwo,
    sum(switch=1 AND detail=1) AS oneone,
    sum(switch=1 AND detail=2) AS onetwo
FROM tablename
于 2012-07-31T17:41:49.237 に答える
1
SELECT SUM(CASE WHEN switch = 0 and detail = 1 THEN 1 ELSE NULL END) AS zeroone
     , ...
FROM TableName
于 2012-07-31T17:39:54.263 に答える
0

何が欲しいのかはっきりしていません。

4つの列を持つ1つの行を返すselectが必要だと思います。

SELECT
COUNT(*) WHERE switch=0 AND detail=1 AS zeroone,
COUNT(*) WHERE switch=0 AND detail=2 AS zerotwo,
COUNT(*) WHERE switch=1 AND detail=1 AS oneone,
COUNT(*) WHERE swithc=1 AND detail=2 AS onetwo
FROM tablename
于 2012-07-31T17:43:38.570 に答える
0

なぜこれを使わないのですか?

SELECT COUNT(*) 
  FROM tablename
 GROUP BY switch, detail
于 2012-07-31T17:46:49.537 に答える