2
我試圖在關係確定DD(branch
)的比例爲AA,BB,CC其中DD存在於員工,客戶,水果的子集。每位員工的pcnt
增加到1(100%)。先進比例計算在TSQL
其分解,該計算必須滿足以下條件:
- 忽略子集(僱員,帳戶,水果),而不DD
| EMPLOYEE | ACCOUNT | FRUIT | BRANCH | PCNT | --------------------------------------------------- | 123 | Fruit1 | Apple | AA | 0.1 |
- 忽略子集(僱員,帳戶,水果)被完全DD
| EMPLOYEE | ACCOUNT | FRUIT | BRANCH | PCNT | --------------------------------------------------- | 123 | Fruit2 | Apple | DD | 0.05 |
- 按比例分配DD在AA,BB,子集內CC(員工,客戶,水果)
來源:
| EMPLOYEE | ACCOUNT | FRUIT | BRANCH | PCNT | --------------------------------------------------- | 123 | Fruit1 | Strawberry | AA | 0.05 | | 123 | Fruit1 | Strawberry | BB | 0.02 | | 123 | Fruit1 | Strawberry | CC | 0.07 | | 123 | Fruit1 | Strawberry | DD | 0.10 |
分爲:
| EMPLOYEE | ACCOUNT | FRUIT | BRANCH | PCNT | --------------------------------------------------- | 123 | Fruit1 | Strawberry | AA | 0.09 | | 123 | Fruit1 | Strawberry | BB | 0.03 | | 123 | Fruit1 | Strawberry | CC | 0.12 |
的計算對於上述的子集比例將是:
AA + {AA * [DD/(AA + BB + CC)]} 0.05 + {0.05 * [0.1/(0.05 + 0.02 + 0.07)]} = 0.09 0.02 + {0.02 * [0.1/(0.05 + 0.02 + 0.07)]} = 0.03 0.07 + {0.07 * [0.1/(0.05 + 0.02 + 0.07)]} = 0.12
- 這些計算和條件後,
pcnt
總和應爲1 - 可能存在的一些子集,其中不是所有的三(AA,BB,CC)是本
表結構
CREATE TABLE tbl ( employee tinyint NOT NULL, account varchar(10) NOT NULL, fruit varchar(15) NOT NULL, branch char(2) NOT NULL, pcnt decimal(3,2) NULL PRIMARY KEY (employee, account, fruit, branch))
樣本數據
INSERT INTO tbl (employee, account, fruit, branch, pcnt) VALUES (123, 'Fruit1', 'Apple', 'AA', '0.1'), (123, 'Fruit1', 'Mango', 'DD', '0.02'), (123, 'Fruit1', 'Mango', 'CC', '0.1'), (123, 'Fruit1', 'Mango', 'BB', '0.02'), (123, 'Fruit1', 'Mango', 'AA', '0.06'), (123, 'Fruit1', 'Pineapple', 'DD', '0.01'), (123, 'Fruit1', 'Pineapple', 'CC', '0.05'), (123, 'Fruit1', 'Pineapple', 'BB', '0.05'), (123, 'Fruit1', 'Pineapple', 'AA', '0.05'), (123, 'Fruit1', 'Strawberry', 'DD', '0.10'), (123, 'Fruit1', 'Strawberry', 'CC', '0.07'), (123, 'Fruit1', 'Strawberry', 'BB', '0.02'), (123, 'Fruit1', 'Strawberry', 'AA', '0.05'), (123, 'Fruit2', 'Apple', 'DD', '0.05'), (123, 'Fruit2', 'Mango', 'DD', '0.01'), (123, 'Fruit2', 'Mango', 'CC', '0.02'), (123, 'Fruit2', 'Mango', 'BB', '0.01'), (123, 'Fruit2', 'Mango', 'AA', '0.01'), (123, 'Fruit2', 'Pineapple', 'DD', '0.02'), (123, 'Fruit2', 'Pineapple', 'CC', '0.03'), (123, 'Fruit2', 'Pineapple', 'BB', '0.02'), (123, 'Fruit2', 'Pineapple', 'AA', '0.02'), (123, 'Fruit2', 'Strawberry', 'DD', '0.01'), (123, 'Fruit2', 'Strawberry', 'CC', '0.05'), (123, 'Fruit2', 'Strawberry', 'AA', '0.05')
我已經試過
SELECT b.*,
CASE WHEN (b.Sum_AA + b.Sum_BB + b.Sum_CC) > 0 THEN 'hello'
ELSE NULL
END
FROM (
SELECT a.*,
(SELECT SUM(pcnt) AS [pcnt] FROM dbo.tbl
WHERE employee = a.employee AND account = a.account AND fruit = a.fruit
AND branch = 'AA') AS [Sum_AA],
(SELECT SUM(pcnt) AS [pcnt] FROM dbo.tbl
WHERE employee = a.employee AND account = a.account AND fruit = a.fruit
AND branch = 'BB') AS [Sum_BB],
(SELECT SUM(pcnt) AS [pcnt] FROM dbo.tbl
WHERE employee = a.employee AND account = a.account AND fruit = a.fruit
AND branch = 'CC') AS [Sum_CC]
FROM (
SELECT employee, account, fruit, SUM(pcnt) AS [Sum_DD]
FROM dbo.tbl
WHERE employee = 123 AND branch = 'DD'
GROUP BY employee, account, fruit) a) b
截至上述查詢,我已經分組AA,BB,CC。但是,我不知道如何繼續。任何幫助表示讚賞。
我覺得你做到了。謝謝。 – Kermit 2012-07-31 18:13:18