2016-09-20 91 views
4

我有一個表,看起來像這樣,MySQL的 - 基於其他值

+-----+--------+-------+ 
| num | amount | value | 
+-----+--------+-------+ 
| 1 |  12 |  1 | 
| 1 |  12 |  1 | 
| 2 |  13 |  1 | 
| 4 |  15 |  0 | 
| 2 |  13 |  1 | 
| 3 |  14 |  1 | 
| 3 |  14 |  1 | 
| 1 |  12 |  1 | 
+-----+--------+-------+ 

我想根據不同的「NUM」列總結了「量」的列,其中「價值獲取所有不同行的總和'等於1, 例如,運行下面的查詢後,

select DISTINCT num, amount, value from test where value =1 ; 

不同 'NUM' 基於表是

+-----+--------+-------+ 
| num | amount | value | 
+-----+--------+-------+ 
| 1 |  12 |  1 | 
| 2 |  13 |  1 | 
| 3 |  14 |  1 | 
+-----+--------+-------+ 

所以我要的最終結果是

12 + 13 + 14 = 39。

一件事是,

我不能使用子查詢。

因爲它已經是另一個查詢的一部分了。

這裏是我的表的腳本是

SET FOREIGN_KEY_CHECKS=0; 

-- ---------------------------- 
-- Table structure for `test` 
-- ---------------------------- 
DROP TABLE IF EXISTS `test`; 
CREATE TABLE `test` (
    `num` int(11) DEFAULT NULL, 
    `amount` int(11) DEFAULT NULL, 
    `value` int(11) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

-- ---------------------------- 
-- Records of test 
-- ---------------------------- 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('4', '15', '0'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 
+1

只使用和'選擇總和(金額)從測試下value = 1;' –

+0

你期待只有1行INT結果組? –

+0

@P.Salmon完全是1行結果。最後的答案是39 –

回答

0
select num,sum(amount)/Count(*) 
from test 
where value = 1 
group by num 
order by num 
+0

沒有幫助:( –

0

我認爲每一個NUM具有相同的值(如果沒有,你應該如何挑選呢?)

所以,這將工作:

SELECT SUM(DISTINCT amount) 
FROM test 
WHERE value = 1 
+0

'量'無法區分。獨特必須'num'列。它沒有解決問題:( –

+0

爲什麼?我試過這個,它的工作原理。如果金額不能清晰,你要計算哪一個,假設有'num'設置爲'1'的兩行,以及不同'amount'的值如'12'和'13'? –

+1

@MattiaNocerino OP的意思是,金額可以是相同的不同'num'因此'DISTINCT'金額不能用 – Shaharyar

0
SELECT SUM(amount) FROM (SELECT DISTINCT num, amount, VALUE FROM test WHERE VALUE =1) AS temp ; 
+0

不能使用子查詢人:( –

+0

它有幫助嗎? –

0

您可以創建一個臨時表包含不同的值並使用SUM()來計算金額。您的查詢就會像:

SELECT SUM(amount) 
FROM (
     SELECT DISTINCT t.`num`, t.`amount` 
     FROM test t 
     WHERE t.`value`=1 
    ) temp 
+0

@AmeerHamza我已經添加了一個sol ution。檢查這是否有幫助。 –

+0

他說他不能使用子查詢 –

+0

正確..我不能使用子查詢中:(這是限制 –

0

請檢查該解決方案

SET FOREIGN_KEY_CHECKS=0; 

-- ---------------------------- 
-- Table structure for `test` 
-- ---------------------------- 
DROP TABLE IF EXISTS `test`; 
CREATE TABLE `test` (
    `num` int(11) DEFAULT NULL, 
    `amount` int(11) DEFAULT NULL, 
    `value` int(11) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

-- ---------------------------- 
-- Records of test 
-- ---------------------------- 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('4', '15', '0'); 
INSERT INTO `test` VALUES ('2', '13', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('3', '14', '1'); 
INSERT INTO `test` VALUES ('1', '12', '1'); 

-- ---------------------------- 
-- Insert the result set into temp table and then use sum(). 
-- ---------------------------- 


SELECT DISTINCT `num` , `amount` 
INTO #tmp 
FROM test 
WHERE `value` =1 

SELECT * FROM #tmp 

SELECT sum(`amount`) as amount FROM #tmp