我正在做一些關於財務賬戶的報告,並且我需要根據初始數字對數值進行求和...... 例如,將所有以0開頭的賬戶值(01,011 ,012。),或從1開始(1,10,111 ...),或與111(111,1112,1113 ...)等PostrgreSQL 8.4:通過賬戶號碼求和
這裏開始被簡化試樣臺:
CREATE TABLE account(id, acctNo, credit) AS (
VALUES
(1, '01', 100)
,(2, '011', 200)
,(3, '0112', 300)
,(4, '014', 400)
,(5, '0144', 500)
,(6, '0148', 600)
,(7, '01120', 100)
,(8, '01121', 100)
,(9, '0140', 50)
,(10,'02', 50)
,(11,'021', 50)
,(12,'1', 50)
,(13,'10', 100)
,(15,'100', 50)
,(14,'1021', 50)
,(16,'202', 50)
,(17,'221', 50)
,(18,'4480', 50)
,(19,'447', 50)
,(20,'5880', 50)
)
我設法做到了,但它有點強大的SQL,有沒有更好的解決方案? 這裏是代碼:
WITH
a AS (SELECT SUBSTRING(acctNo,1,1) AS LEVEL,
SUM(credit) AS sum1 FROM account GROUP BY LEVEL
ORDER BY LEVEL),
b AS
(SELECT SUBSTRING(acctNo,1,2) AS level2,
SUM(credit) FROM account GROUP BY level2
ORDER BY level2),
c AS
(SELECT SUBSTRING(acctNo,1,3) AS level3,
SUM(credit) FROM account GROUP BY level3
ORDER BY level3),
d AS (SELECT SUBSTRING(acctNo,1,4) AS level4,
SUM(credit) FROM account GROUP BY level4
ORDER BY level4),
e AS (SELECT SUBSTRING(acctNo,1,5) AS level5,
SUM(credit) FROM account GROUP BY level5
ORDER BY level5)
SELECT * FROM
(SELECT a.* FROM a
UNION (SELECT b.* FROM b WHERE char_length(level2)>=2)
UNION (SELECT c.* FROM c WHERE char_length(level3)>=3)
UNION (SELECT d.* FROM d WHERE char_length(level4)>=4)
UNION (SELECT e.* FROM e WHERE char_length(level5)>=5)) a
ORDER BY LEVEL
這僅僅是5級(五位數字)......有一些通用的解決方案?如果明天我需要爲6個級別,等什麼...
這裏是SQL Fiddle
感謝。
也許你應該瞭解1NF,第一? – wildplasser
@wildplasser:...也許...謝謝 – lana80