2013-02-04 203 views
4

我尋覓了很多的每一行,但無法找到一個有用的答案:MySQL查詢使用另一個查詢

我想有總計列表一段時間內的用戶定義了給我一個開始和結束日期。總計應該從開始日期到開始日期開始,並且每一行添加1天。所以最後一行給出了從開始到結束日期的總計。 例如: - 給定時間=起始2013-01-01,結束= 2013年1月31日

total day 1 = 100 
total day 2 = 0 (not listed in my totalsperday query, but should have a row in my final query) 
total day 3 = 140 
total day 4 = 20 
... 

final table should look like: 
end day 1: 100 
end day 2: 100 
end day 3: 240 
end day 4: 260 
... 

,所以我有一個查詢誰計算所有天:

SELECT '2013-01-01' as startdate, w.endDate 
FROM 
(
    SELECT date('2013-01-01' + INTERVAL u.i*100 + v.i*10 + w.i DAY) AS endDate 
    FROM sysints AS u 
    JOIN sysints AS v 
    JOIN sysints AS w 
    WHERE (u.i*100 + v.i*10 + w.i) <= 
    (
     SELECT DATEDIFF('2013-01-31','2013-01-01') as ddff 
    ) 
) w 
ORDER BY w.endDate ASC 

和我有一個查詢誰計算每天總計

SELECT p.selldate, SUM(p.price) as totalPerDay 
FROM products p 
WHERE '2013-01-01' >= p.selldate <= '2013-01-31' 
GROUP BY p.selldate 
ORDER BY p.selldate ASC 

現在結合這兩個來得到我的最終結果是困難的。

基本上就是最終的查詢應該是這樣的:

- make the sum of sumperday from day 1 to day 1 
- make the sum of sumperday from day 1 to day 2 
- make the sum of sumperday from day 1 to day 3 
... 

任何幫助嗎? thx。 這是我最終查詢的簡化示例。

+0

你使用的是什麼mysql版本?一種選擇是設置一個存儲過程並使用遊標 - 我可以爲你創建一個樣本 –

+0

我認爲這可以做到,但具體而言,我需要表格的描述來做事情。如果可能的話,請提供表格 – Vineet1982

回答

0

下面是示例。這個想法是獲得按日期排序的初始數據集,並具有總計總計,隱式日期範圍記錄。然後使用光標,可以通過每行通過彙總以前的記錄來獲取最終的總列(樣本中的amountCalc列) - 將工作,因爲您已經有按日期排序的列。

程序可以有其他輸入/輸出參數。不用從表格獲取信息,您可以從一個視圖獲取數據,其中視圖可以按照日期順序排序。只是一個樣本,所以可以根據需要進行定製。

祝你好運。

-- drop table `Balance`; 
CREATE TABLE `Balance` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `date` DATE NOT NULL, 
    `account` varchar(30) NOT NULL, 
    `amount` DECIMAL(10,2) NOT NULL, 
    PRIMARY KEY (`id`) 
); 

INSERT INTO `Balance` (`date`, `account`, `amount`) VALUES 
('2013-01-02', 'T355176', 8700), 
('2013-01-03', 'T355176', 8900), 
('2013-01-04', 'T355215', 33308), 
('2013-01-03', 'T355215', 116581), 
('2013-01-06', 'T812022', 275000), 
('2013-01-02', 'T812063', 136500), 
('2013-01-05', 'T812063', 11682), 
('2013-01-06', 'T812064', 615100), 
('2013-01-03', 'T812064', 25000), 
('2013-01-02', 'T812085', 82500); 


SELECT * FROM Balance WHERE date >= '2013-01-01' AND date <= '2013-01-06' ORDER BY date ASC; 
CALL sp_getTotals('2013-01-01', '2013-01-06'); 


-- -------------------------------------------------------------------------------- 
-- Routine DDL 
-- Note: comments before and after the routine body will not be stored by the server 
-- -------------------------------------------------------------------------------- 
DELIMITER $$ 

CREATE DEFINER=`root`@`%` PROCEDURE `sp_getTotals`(IN startDate DATE, IN endDate DATE) 
BEGIN 


DECLARE dt DATE; 
DECLARE amt DECIMAL(10,2); 
DECLARE amtCalcPart DECIMAL(10,2); 
DECLARE done INT DEFAULT 0; 

DECLARE dtStart DATE; 
DECLARE dtEnd DATE; 

DECLARE cur1 CURSOR FOR SELECT date, amount FROM `TempMB`; 
DECLARE cur2 CURSOR FOR SELECT startDate, endDate; 
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; 


DROP TEMPORARY TABLE IF EXISTS `TempMB`; 
CREATE TEMPORARY TABLE IF NOT EXISTS `TempMB` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
    `date` DATE NOT NULL, 
    `amount` DECIMAL(10,2) NULL DEFAULT 0.00, 
    `amountCalc` DECIMAL(10,2) NULL DEFAULT 0.00, 
    PRIMARY KEY (`id`) 
); 


SET dtStart = DATE(startDate); 
SET dtEnd = DATE(endDate); 

WHILE dtStart <= dtEnd DO 
    INSERT INTO `TempMB` (`date`) SELECT dtStart; 
    SET dtStart = DATE_ADD(dtStart, INTERVAL 1 DAY); 
END WHILE; 


SELECT * FROM TempMB; 

-- Fill temp table with info needed 
UPDATE `TempMB` t 
INNER JOIN 
(
    SELECT date, SUM(amount) AS amount 
    FROM Balance 
    WHERE 
     date >= startDate AND date <= endDate 
    GROUP BY date 
    ORDER BY date ASC 
) b ON b.date = t.date 
SET 
    t.amount = b.amount; 
/*INSERT INTO `TempMB` (`date`, `amount`) 
SELECT date, SUM(amount) AS amount 
FROM Balance 
WHERE 
    date >= startDate AND date <= endDate 
GROUP BY date 
ORDER BY date ASC; 
*/ 


SET amtCalcPart = 0.00; 
-- Initialise cursor 
OPEN cur1; 
-- USE BEGIN-END handler for cursor-control within own BEGIN-END block 
BEGIN 
DECLARE EXIT HANDLER FOR NOT FOUND BEGIN END; 
-- Loop cursor throu temp records 
LOOP 
    -- Get next value 
    FETCH cur1 INTO dt, amt; 

    -- Calculate amountCalc 
    SET amtCalcPart = (SELECT SUM(amount) as amt FROM `TempMB` WHERE Date <= dt); 
    UPDATE `TempMB` SET amountCalc = amtCalcPart WHERE date = dt; 

END LOOP; 
END; 

-- Release cursor 
CLOSE cur1; 

SELECT * FROM TempMB; 

END 
+0

THx的描述,就像一個魅力! – kvl

相關問題