-12

我有一张像这样的桌子:

id name center1    center2   cnter3
1  abc  baroda     bhopal    chennai
2  pqr  amhedabad  Surat     kolkata
3  nml  bhopal     chennai   Surat
4  fts  baroda     Surat     Baroda

我想要一个结果,比如:

         center1 center2  center3
Baroda     2       0        1   
Ahmedabad  1       0        0
bhopal     1       1        0
chennai    0       1        1
Kolkatta   0       0        1
Surat      0       2        1.
4

2 回答 2

3

This will require a handful of subquery joins. First, you need to get all the distinct values from the three columns, which would be done with a UNION of queries of each column.

That distinct list must then be LEFT JOINed against a trio of subqueries which pulls the counts per value from each of the three columns.

Finally, a COALESCE() replaces NULLs with 0.

SELECT 
  allvalues.value,
  /* LEFT JOIN will result in NULL for non-existent values, so coalesce into 0 */
  COALESCE(c1.counts, 0) AS center1,
  COALESCE(c2.counts, 0) AS center2,
  COALESCE(c3.counts, 0) AS center3
FROM
  (
    /* UNIONs gets distinct vals from all columns */
    SELECT center1 AS value FROM yourtable
    UNION
    SELECT center2 AS value FROM yourtable
    UNION
    SELECT center3 AS value FROM yourtable
  ) AS allvalues
  /* LEFT JOIN the list of distinct possible values against a count per value for each column */
  LEFT JOIN (
    SELECT center1, COUNT(*) AS counts FROM yourtable GROUP BY center1
  ) c1 ON allvalues.value = c1.center1
  LEFT JOIN (
    SELECT center2, COUNT(*) AS counts FROM yourtable GROUP BY center2
  ) c2 ON allvalues.value = c2.center2
  LEFT JOIN (
    SELECT center3, COUNT(*) AS counts FROM yourtable GROUP BY center3
  ) c3 ON allvalues.value = c3.center3 

Here is a working demonstration, because I guess I'm feeling extra charitable tonight.

于 2012-09-11T01:32:13.383 回答
2

我使用sqlfiddle将表的文本表示转换为一系列 CREATE TABLE 和 INSERT INTO 语句:

CREATE TABLE CenterCodes
    (`id` int, `name` varchar(3), `center1` varchar(9), `center2` varchar(7), `center3` varchar(7))
;

INSERT INTO CenterCodes
    (`id`, `name`, `center1`, `center2`, `center3`)
VALUES
    (1, 'abc', 'baroda', 'bhopal', 'chennai'),
    (2, 'pqr', 'amhedabad', 'Surat', 'kolkata'),
    (3, 'nml', 'bhopal', 'chennai', 'Surat'),
    (4, 'fts', 'baroda', 'Surat', 'Baroda')
;

尝试以下查询。内部UNION为每个字符串值生成多行,每组 3 列中的 2 列为 NULL。这些 NULL 然后被SUM()外部查询中的聚合 s 消除,将行集折叠为每个字符串值一行。

SELECT
  centername,
  SUM(CASE WHEN centername = center1 THEN 1 ELSE 0 END) AS center1,
  SUM(CASE WHEN centername = center2 THEN 1 ELSE 0 END) AS center2,
  SUM(CASE WHEN centername = center3 THEN 1 ELSE 0 END) AS center3
FROM (
  SELECT
    center1 AS centername,
    center1,
    NULL AS center2,
    NULL AS center3
  FROM CenterCodes
  UNION ALL
  SELECT
    center2 AS centername,
    NULL AS center1,
    center2,
    NULL AS center3
  FROM CenterCodes
  UNION ALL
  SELECT
    center3 AS centername,
    NULL AS center1,
    NULL AS center2,
    center3
  FROM CenterCodes
) AS Centers
GROUP BY centername;
于 2012-09-11T01:42:08.563 回答