我有一张桌子和一些数据。我想为每个帐户分配级别。
DROP TABLE COA;
CREATE TABLE COA
(
ID NUMERIC NOT NULL PRIMARY KEY,
COACode VARCHAR(50),
COATitle VARCHAR(50),
ParentCode NUMERIC,
DebitAmount NUMERIC(30),
CONSTRAINT FK_COAParentCode
FOREIGN KEY (ID) REFERENCES COA(ID)
);
INSERT INTO COA VALUES (1, '01', 'Expenses', NULL, NULL);
INSERT INTO COA VALUES (2, '02', 'Assets', NULL, NULL);
INSERT INTO COA VALUES (3, '03', 'Bills', NULL, NULL);
INSERT INTO COA VALUES (4, '01-01', 'Salary Expenses', 1, NULL);
INSERT INTO COA VALUES (5, '01-02', 'Staff Transport Expenses', 1, NULL);
INSERT INTO COA VALUES (6, '01-01-01', 'Internship Stippends', 4, NULL);
INSERT INTO COA VALUES (7, '01-01-02', 'Non Technical Staff Pay', 4, '1500');
INSERT INTO COA VALUES (8, '01-01-03', 'Technical Staff Pay', 4, '5000');
INSERT INTO COA VALUES (9, '01-02-01', 'Staff Transport Charges', 5, NULL);
INSERT INTO COA VALUES (10, '01-02-02', 'Daily Product Delivery Transport Charges', 5, '10000');
INSERT INTO COA VALUES (11, '02-01', 'East Plant Dues', 2, NULL);
INSERT INTO COA VALUES (12, '02-02', 'West Plant Dues', 2, NULL);
INSERT INTO COA VALUES (13, '03-01', 'Electricity Bills', 3, NULL);
INSERT INTO COA VALUES (14, '03-01-01', 'Head Office Electricity Bills', 13, '1200');
INSERT INTO COA VALUES (15, '03-01-02', 'Plant Electricity Bills', 13, '1300');
INSERT INTO COA VALUES (16, '03-02', 'Phone Bills', 3, NULL);
INSERT INTO COA VALUES (17, '03-02-01', 'Mobile Phone Charges', 16, '4500');
INSERT INTO COA VALUES (18, '03-02-02', 'Landline Charges', 16, NULL);
INSERT INTO COA VALUES (19, '03-02-03', 'Internet Charges', 16, '2400');
对于资产的费用2和对于账单的3应该是1级。我想分组并显示每个帐户的金额总和。
以下是我正在寻找的格式,我相信如果我按级别进行分组但不确定如何在运行时分配级别,则可以执行此操作。另外我不是在sql窗口中寻找这种格式我正在创建一个报告,如果我相应地对它进行分组,我可以实现这种格式。所以我希望有人能帮我分配账户级别。
ID COACode COATitle ParentCode DebitAmount
--------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------- ---------------------------------------
1 01 Expenses NULL NULL
4 01-01 Salary Expenses 1 NULL
6 01-01-01 Internship Stippends 4 NULL
7 01-01-02 Non Technical Staff Pay 4 1500
8 01-01-03 Technical Staff Pay 4 5000
---------------------------------------
6500
5 01-02 Staff Transport Expenses 1 NULL
9 01-02-01 Staff Transport Charges 5 NULL
10 01-02-02 Daily Product Delivery Transport Charges 5 10000
---------------------------------------
10000
2 02 Assets NULL NULL
11 02-01 East Plant Dues 2 NULL
12 02-02 West Plant Dues 2 NULL
3 03 Bills NULL NULL
13 03-01 Electricity Bills 3 NULL
14 03-01-01 Head Office Electricity Bills 13 1200
15 03-01-02 Plant Electricity Bills 13 1300
---------------------------------------
2500
16 03-02 Phone Bills 3 NULL
17 03-02-01 Mobile Phone Charges 16 4500
18 03-02-02 Landline Charges 16 NULL
19 03-02-03 Internet Charges 16 2400
---------------------------------------
6900
答案 0 :(得分:1)
这样的事情会起作用吗?
select top 1000
*,
Level1Sum = sum(DebitAmount) over (partition by left(COACode, 2)),
Level2Sum = sum(DebitAmount) over (partition by left(COACode, 5))
from COA
order by COACode
答案 1 :(得分:1)
更新答案:
SELECT
COACode,
COATitle,
ParentCode,
DebitAmount,
Level# AS Level,
sum(debitamount) OVER (PARTITION BY ParentCode) AS Total
FROM (
SELECT *, dense_rank() OVER (PARTITION BY ParentCode ORDER BY COACode) AS Level#
FROM COA
) Tab
ORDER BY COACode
<强>结果:强>
COACode COATitle ParentCode DebitAmount Level Total
-------------------------------------------------- -------------------------------------------------- --------------------------------------- --------------------------------------- -------------------- ---------------------------------------
01 Expenses NULL NULL 1 NULL
01-01 Salary Expenses 1 NULL 1 NULL
01-01-01 Internship Stippends 4 NULL 1 6500
01-01-02 Non Technical Staff Pay 4 1500 2 6500
01-01-03 Technical Staff Pay 4 5000 3 6500
01-02 Staff Transport Expenses 1 NULL 2 NULL
01-02-01 Staff Transport Charges 5 NULL 1 10000
01-02-02 Daily Product Delivery Transport Charges 5 10000 2 10000
02 Assets NULL NULL 2 NULL
02-01 East Plant Dues 2 NULL 1 NULL
02-02 West Plant Dues 2 NULL 2 NULL
03 Bills NULL NULL 3 NULL
03-01 Electricity Bills 3 NULL 1 NULL
03-01-01 Head Office Electricity Bills 13 1200 1 2500
03-01-02 Plant Electricity Bills 13 1300 2 2500
03-02 Phone Bills 3 NULL 2 NULL
03-02-01 Mobile Phone Charges 16 4500 1 6900
03-02-02 Landline Charges 16 NULL 2 6900
03-02-03 Internet Charges 16 2400 3 6900