问题描述
我在表格中有3列每列1(CUT)和第2列(GROUP),第3列(EmployeeID)
有条目如
年龄15-20岁GOOD E_123
年龄15-20岁VGOOD E_128
年龄15-20岁VGOOD E_123
年龄15-20岁BAD E_128
年龄20-25岁BAD E_124
年龄20-25岁好E_126
年龄20-25岁VGOOD E_127
年龄20-25岁坏E_124
年龄20-25岁坏E_126
年龄20-25好E_127
我想出去像
col1 col2 col3 col4 col5
年龄15-20好1 4
年龄15-20岁VGOOD 2 4
年龄15-20岁BAD 1 4
年龄20-25好2 6
年龄20-25 VGOOD 1 6
年龄20-25岁坏3 6
col4是groupby EMPID
col5是col2的分组总和
代码块添加 - OriginalGriff [ /编辑]
我尝试过:
SELECT col1,col2,COUNT(EMPID) AS countempid
FROM table1 GROUP BY col1,col2 order by AgeGroup,EngLevel
SELECT col1,col2,col3,count(col3)AS col4,
(
SELECT count(col2)FROM tablename t2 WHERE t2.col2 = t1.col2
)
AS col5 FROM tablename t1
GROUP BY col1,col2,col3
I have 3 column in a table each column 1 (CUT) and Column 2 (GROUP), column 3 (EmployeeID)
having entries like
age 15-20 GOOD E_123 age 15-20 VGOOD E_128 age 15-20 VGOOD E_123 age 15-20 BAD E_128 age 20-25 BAD E_124 age 20-25 GOOD E_126 age 20-25 VGOOD E_127 age 20-25 BAD E_124 age 20-25 BAD E_126 age 20-25 GOOD E_127
I want out put like
col1 col2 col3 col4 col5 age 15-20 GOOD 1 4 age 15-20 VGOOD 2 4 age 15-20 BAD 1 4 age 20-25 GOOD 2 6 age 20-25 VGOOD 1 6 age 20-25 BAD 3 6
col4 is groupby EMPID
col5 is sum of group by for col2
[edit]Code block added - OriginalGriff[/edit]
What I have tried:
SELECT col1, col2, COUNT(EMPID) AS countempid FROM table1 GROUP BY col1, col2 order by AgeGroup,EngLevel
这篇关于如何在SQL中应用group in sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!