一个SQL语句代替两个

时间:2019-02-13 13:45:10

标签: mysql

我正在尝试缩短代码,但被卡住了。当前,我执行两个单独的查询以从两个单独的表中获取所需的数据。前提是要生成一份报告,根据性别和种族比较两个不同的班级考试成绩。

查询1:

SELECT g.ethnicity AS dataPointA, 
count(case when t.fall_spring = 1 then t.studentID end) as countA,
round(avg(case when t.fall_spring = 1 then t.compScore end),0) as scoreA,
round(avg(case when t.fall_spring = 1 then t.compNP end),1) as scoreNPA

from table1 t
JOIN aall_students s
ON t.studentID=s.studentID 
JOIN aall_ethnicities g
ON s.ethnicityID=g.ethnicityID
WHERE t.classof = 2021 and s.gender = 'F'
group by s.ethnicityID

Results query 1:

dataPointA        countA   scoreA    scoreNPA
--------------------------------------------
African American |   18   | 914   |   56.6  |
--------------------------------------------
Asian            |    8   | 998   |   71.4  |
--------------------------------------------
Hispanic/Latino  |    5   | 936   |   63.0  |
--------------------------------------------
Two or More      |   11   | 1005  |   72.3  |
--------------------------------------------
White            |   28   | 1028  |   76.7  |

查询2: 请注意,第二个查询中的WHERE子句不同,并且返回的行数也不同。

SELECT g.ethnicity AS dataPointB, 
count(case when t.fall_spring = 1 then t.studentID end) as countB,
round(avg(case when t.fall_spring = 1 then t.compScore end),0) as scoreB,
round(avg(case when t.fall_spring = 1 then t.compNP end),1) as scoreNPB

from table2 t
JOIN aall_students s
ON t.studentID=s.studentID 
JOIN aall_ethnicities g
ON s.ethnicityID=g.ethnicityID
WHERE t.classof = 2022 and s.gender = 'F'
group by s.ethnicityID

Results query 2:

dataPointB        countB   scoreB    scoreNPB
----------------------------------------------
African American |   12   | 838    |   40.0   |
----------------------------------------------
Asian            |    7   | 957    |   65.9   |
----------------------------------------------
Hispanic/Latino  |    7   | 931    |   59.6   |
----------------------------------------------
Native American  |    1   | 940    |   64.0   |
----------------------------------------------
Two or More      |   18   | 963    |   66.1   |
----------------------------------------------
White            |   32   | 1074   |   84.1   |

请注意,查询返回的行数不同,这意味着第一个查询中没有美洲印第安人,第二个查询中没有美洲印第安人。

如果我可以将两个查询合并为一个查询,将减少查询和处理量,因此,例如,来自两个查询的非裔美国人的所有数据都排成一行。它还可以消除脚本方面的处理和潜在的错误。

我尝试了UNION,各种形式的JOIN和广泛的Google搜索,但无法提出任何解决方案。任何将我指向正确方向的建议将不胜感激。

所需的结果如下:

dataPointA        countA   scoreA    scoreNPA   dataPointB       countB   scoreB   scoreNPB
--------------------------------------------------------------------------------------------
African American |   18   | 914   |   56.6  |  African American |   12   | 838   |   40.0  |
--------------------------------------------------------------------------------------------
Asian            |    8   | 998   |   71.4  |  Asian            |    7   | 957   |   65.9  |
--------------------------------------------------------------------------------------------
Hispanic/Latino  |    5   | 936   |   63.0  |  Hispanic/Latino  |    7   | 931   |   59.6  |
--------------------------------------------------------------------------------------------
null             |  null  | null  |   null  |  Native American  |    1   | 940   |   64.0  |
--------------------------------------------------------------------------------------------
Two or More      |   11   | 1005  |   72.3  |  Two or More      |   18   | 963   |   66.1  |
--------------------------------------------------------------------------------------------
White            |   28   | 1028  |   76.7  |  White            |   32   | 1074  |   84.1  |

使用UNION ALL,我得到了一个可行的数组,需要在代码方面进行一些操作。我还添加了答复中建议的“类”列,以区分表:

dataPoint           class     count    score      scoreNP   
------------------------------------------------------------
African American |  2021   |   18    |  914    |   56.6    |
------------------------------------------------------------
Asian            |  2021   |    8    |  998    |   71.4    |
------------------------------------------------------------
Hispanic/Latino  |  2021   |    5    |  936    |   63.0    |
------------------------------------------------------------
Two or More      |  2021   |   11    | 1005    |   72.3    |
------------------------------------------------------------
White            |  2021   |   28    | 1028    |   76.7    |
------------------------------------------------------------
African American |  2022   |   12    |  838    |   40.0    |
------------------------------------------------------------
Asian            |  2022   |    7    |  957    |   65.9    |
------------------------------------------------------------
Hispanic/Latino  |  2022   |    7    |  931    |   59.6    |
------------------------------------------------------------
Native American  |  2022   |    1    |  940    |   64.0    |
------------------------------------------------------------
Two or More      |  2022   |   18    |  963    |   66.1    |
------------------------------------------------------------
White            |  2022   |   32    | 1074    |   84.1    |

1 个答案:

答案 0 :(得分:1)

This is a job for UNION ALL。为什么全部?一个普通的UNION尝试对行进行重复数据删除,而您则不需要这样做。

尝试这样做:

     SELECT 'A' datatype, whatever, whatever
       FROM table1 ... WHERE whatever
     UNION ALL
     SELECT 'B' datatype, whatever, whatever
       FROM table2 ... WHERE whatever

您将获得一个结果集,其中包含一个名为datatype的额外列(在我的示例中)。您可以使用该列来区分两个不同表的结果。