SQL 计数和求和
SQL COUNT And SUM
我有一个 table 有这个
tabledone_by
Var_ID| Var_name| Q1_by |Q2_by|Q3_by|Q4_by
1 | abc | me | me |me |you
2 | cba | me | me |you |you
3 | abd | me | you |you |me
我想要得到的结果是得到所有的总和
我和你的价值
我=7
你=5
已完成计数,但我无法计算每列的所有 'me'
您可以像这样进行条件聚合:
SELECT SUM(CASE WHEN Q1_by = 'me' THEN 1 ELSE 0 END +
CASE WHEN Q2_by = 'me' THEN 1 ELSE 0 END +
CASE WHEN Q3_by = 'me' THEN 1 ELSE 0 END +
CASE WHEN Q4_by = 'me' THEN 1 ELSE 0 END) AS me ,
SUM(CASE WHEN Q1_by = 'you' THEN 1 ELSE 0 END +
CASE WHEN Q2_by = 'you' THEN 1 ELSE 0 END +
CASE WHEN Q3_by = 'you' THEN 1 ELSE 0 END +
CASE WHEN Q4_by = 'you' THEN 1 ELSE 0 END) AS you
FROM TableName
使用每个季度的单独计数并将它们相加
SELECT (
(SELECT Count(Q1_by) FROM done_by WHERE Q1_by = 'me')+
(SELECT Count(Q2_by) FROM done_by WHERE Q2_by = 'me')+
(SELECT Count(Q3_by) FROM done_by WHERE Q3_by = 'me')+
(SELECT Count(Q4_by) FROM done_by WHERE Q4_by = 'me')) AS MeCount,
(
(SELECT Count(Q1_by) FROM done_by WHERE Q1_by = 'you')+
(SELECT Count(Q2_by) FROM done_by WHERE Q2_by = 'you')+
(SELECT Count(Q3_by) FROM done_by WHERE Q3_by = 'you')+
(SELECT Count(Q4_by) FROM done_by WHERE Q4_by = 'you')) AS YouCount
FROM done_by
IDEONE link: https://ideone.com/ZAuS5O
条件聚合使用 "Sum" 而不是 "Count" 是可行的方法,如果您不知道不同的值,您可以使用动态 sql 扩展它 -
--Dynamic SQL Extension to count sum of all distinct values
--Extract out distinct values in temporary table
SELECT DISTINCT by_val=val INTO #by_tbl FROM (SELECT val=Q1_by
FROM #TableName
UNION ALL
SELECT val=Q2_by
FROM #TableName
UNION ALL
SELECT val=Q3_by
FROM #TableName
UNION ALL
SELECT val=Q4_by
FROM #TableName) A
--Create a SQL String
DECLARE @sql NVARCHAR(max)
SELECT @sql = ISNULL(@sql+',', 'SELECT ') + '['+by_val+']=SUM(CASE WHEN Q1_by='''+by_val+''' THEN 1 ELSE 0 END
+ CASE WHEN Q2_by='''+by_val+''' THEN 1 ELSE 0 END
+ CASE WHEN Q3_by='''+by_val+''' THEN 1 ELSE 0 END
+ CASE WHEN Q4_by='''+by_val+''' THEN 1 ELSE 0 END) '
FROM #by_tbl
SET @sql = @sql + ' FROM #TableName'
EXEC(@SQL)
DROP TABLE #TableName
DROP TABLE #by_tbl
DECLARE @byMe INT = 0;
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q1_by = 'me';
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q2_by = 'me';
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q3_by = 'me';
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q4_by = 'me';
SET @byMe = @byMe * 7;
我有一个 table 有这个 tabledone_by
Var_ID| Var_name| Q1_by |Q2_by|Q3_by|Q4_by
1 | abc | me | me |me |you
2 | cba | me | me |you |you
3 | abd | me | you |you |me
我想要得到的结果是得到所有的总和 我和你的价值 我=7 你=5
已完成计数,但我无法计算每列的所有 'me'
您可以像这样进行条件聚合:
SELECT SUM(CASE WHEN Q1_by = 'me' THEN 1 ELSE 0 END +
CASE WHEN Q2_by = 'me' THEN 1 ELSE 0 END +
CASE WHEN Q3_by = 'me' THEN 1 ELSE 0 END +
CASE WHEN Q4_by = 'me' THEN 1 ELSE 0 END) AS me ,
SUM(CASE WHEN Q1_by = 'you' THEN 1 ELSE 0 END +
CASE WHEN Q2_by = 'you' THEN 1 ELSE 0 END +
CASE WHEN Q3_by = 'you' THEN 1 ELSE 0 END +
CASE WHEN Q4_by = 'you' THEN 1 ELSE 0 END) AS you
FROM TableName
使用每个季度的单独计数并将它们相加
SELECT (
(SELECT Count(Q1_by) FROM done_by WHERE Q1_by = 'me')+
(SELECT Count(Q2_by) FROM done_by WHERE Q2_by = 'me')+
(SELECT Count(Q3_by) FROM done_by WHERE Q3_by = 'me')+
(SELECT Count(Q4_by) FROM done_by WHERE Q4_by = 'me')) AS MeCount,
(
(SELECT Count(Q1_by) FROM done_by WHERE Q1_by = 'you')+
(SELECT Count(Q2_by) FROM done_by WHERE Q2_by = 'you')+
(SELECT Count(Q3_by) FROM done_by WHERE Q3_by = 'you')+
(SELECT Count(Q4_by) FROM done_by WHERE Q4_by = 'you')) AS YouCount
FROM done_by
IDEONE link: https://ideone.com/ZAuS5O
条件聚合使用 "Sum" 而不是 "Count" 是可行的方法,如果您不知道不同的值,您可以使用动态 sql 扩展它 -
--Dynamic SQL Extension to count sum of all distinct values
--Extract out distinct values in temporary table
SELECT DISTINCT by_val=val INTO #by_tbl FROM (SELECT val=Q1_by
FROM #TableName
UNION ALL
SELECT val=Q2_by
FROM #TableName
UNION ALL
SELECT val=Q3_by
FROM #TableName
UNION ALL
SELECT val=Q4_by
FROM #TableName) A
--Create a SQL String
DECLARE @sql NVARCHAR(max)
SELECT @sql = ISNULL(@sql+',', 'SELECT ') + '['+by_val+']=SUM(CASE WHEN Q1_by='''+by_val+''' THEN 1 ELSE 0 END
+ CASE WHEN Q2_by='''+by_val+''' THEN 1 ELSE 0 END
+ CASE WHEN Q3_by='''+by_val+''' THEN 1 ELSE 0 END
+ CASE WHEN Q4_by='''+by_val+''' THEN 1 ELSE 0 END) '
FROM #by_tbl
SET @sql = @sql + ' FROM #TableName'
EXEC(@SQL)
DROP TABLE #TableName
DROP TABLE #by_tbl
DECLARE @byMe INT = 0;
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q1_by = 'me';
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q2_by = 'me';
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q3_by = 'me';
SELECT @byMe = @byMe + COUNT(*) FROM dbo.tablName
WHERE Q4_by = 'me';
SET @byMe = @byMe * 7;