sql-server - 如何编写此 SQL 代码并在同一张表上输出?
问题描述
我知道在 SQL Server 中有一种方法可以做到这一点,但是经过数小时的故障排除后,我仍然没有弄清楚。
我想要同一张表中的性别和 2 年生存输出。这是我用于按性别分开输出的代码:
-- Count # of patients by gender and count the number of patients that are alive - Females
SELECT twoYrSurv AS WOMEN_2_YR_SURVIVAL, COUNT('gender') AS GENDER_TOTALS
FROM dataLungCancer1$
WHERE gender = 'Female'
GROUP BY twoYrSurv
-- Count # of patients by gender and count the number of patients that are alive - Males
SELECT twoYrSurv AS MEN_2_YR_SURVIVAL, COUNT('gender') AS GENDER_TOTALS
FROM dataLungCancer1$
WHERE gender = 'Male'
GROUP BY twoYrSurv
-- Results after changing group by clause
SELECT twoYrSurv AS Two_YR_SURVIVAL, COUNT('gender') AS GENDER_TOTALS
FROM dataLungCancer1$
GROUP BY gender, twoYrSurv
输出
Two_YR_SURVIVAL GENDER_TOTALS
-----------------------------
Alive 86
Alive 201
Deceased 60
Deceased 103
解决方案
推荐阅读
- java - 您如何在 firebase 数据库中一次修改所有经过身份验证的用户的数据
- c++ - 当覆盖地图中的复杂对象键时,旧对象会被删除(C++)
- python - 在没有 IDE 的情况下运行 Python 脚本
- android - 无法解析 com.google.code.gson:gson:2.8.2。由于防火墙
- java - 如何命名在 Java 中返回 HashMap 的函数?
- python - 散景悬停工具显示“???” 而不是 ColumnDataSource 值
- android - 如何在最后一个片段回压上从最后一个片段重定向到特定片段
- three.js - 由于非常量表达式初始化导致的 for 循环错误
- angular - 表单 ViewChild 未在组件中获取输入字段值
- android - 应用 android:theme="@style/Theme.AppCompat" 时
在 AndroidManifest.xml 中删除 ActionBar