1
我有一個類似下面的查詢。我的目標是添加所有'total_points'並檢索所有行的總和的單個結果。獲取所有行的總和
SELECT
SUM(
COALESCE(
CASE
WHEN COUNT(DISTINCT `table_1`.views) > 50 OR COUNT(DISTINCT `table_1`.views) = 50 AND COUNT(DISTINCT `table_1`.views) < 100 THEN COUNT(DISTINCT `table_1`.views) +5
ELSE COUNT(DISTINCT `table_1`.views)
END,0)
+
CASE
WHEN COUNT(DISTINCT `table_2`.views) > 50 OR COUNT(DISTINCT `table_2`.views) = 50 AND COUNT(DISTINCT `table_2`.views) < 100 THEN COUNT(DISTINCT `table_2`.views) +5
ELSE COUNT(DISTINCT `table_2`.views)
END,0)) AS sum
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.id = `table_2`.id
GROUP BY `table_1`.primary_id
這會給我這樣的事情
rank | total_points
1 321
2 111
3 100
4 90
5 72
6 60
7 45
8 23
9 11
10 5
結果這是我的願望的結果:
sum |
838
我試着但沒有工作。 – user3135626
它給我一個錯誤「無效的使用組功能」 – user3135626
SELECT SUM(Totalpoints)FROM(my query)x; – Strawberry