我有一个表,我想运行一些计算但是这个select语句在我的oracle驾驶室之上。
我想在oracle中尝试(尽可能多)。但最终这将最终通过php运行以获得输出到屏幕。
这适用于Oracle 8i
我有4个重要的数据 - (客户ID,New_Item,Suggested_net和逐年的数量)
customer_id = #
new_item = 0 or 1
suggested_net = #
year_1_n = #
year_2_n = #
year_3_n = #
year_1_e = #
year_2_e = #
year_3_e = #
new_items = 0仅与year_1_e,year_2_e,year_3_e相关
new_items = 1仅与year_1_n,year_2_n,year_3_n
相关以下是一个示例
customer_id, new_item, suggested_net, year_1_n, year_2_n, year_3_n, year_1_e, year_2_e, year_3_e
2, 0, 4.25, null, null, null, 100, 100, 100;
2, 0, 5.25, null, null, null, 100, 100, 100;
3, 0, 2.50, null, null, null, 100, 100, 100;
1, 0, 3.50, null, null, null, 100, 100, 100;
2, 1, 5.99, 100, 200, 300, null, null, null;
3, 1, 4.99, 200, 400, 600, null, null, null;
所以我需要获取此信息: 对于每一行,添加卷(year_x ...)* suggested_net,然后通过new_item = 0或1将该金额添加到分组的customer_id。
所以使用上表
Customer 2, new_item = 0 would have a sum of (1275 + 1575)
Customer 1, new_item = 0 would have a sum of 1050
Customer 2, new_item = 1 would have a sum of 3594
等。等
现在这可能或者可能不会在select语句中有效或高效,但就像我说我想用oracle做的那么多繁重的工作,其余的我可以用php来处理。
答案 0 :(得分:2)
select customer_id
, new_item
, sum((nvl(year_1_n, 0) +
nvl(year_2_n, 0) +
nvl(year_3_n, 0) +
nvl(year_1_e, 0) +
nvl(year_2_e, 0) +
nvl(year_3_e, 0)) *suggested_net) Total
from t1
group by customer_id
, new_item
答案 1 :(得分:1)
SELECT Coustomer_Id,
new_item,
SUM((CASE WHEN new_item = 1 THEN year_1_n + year_2_n + year_3_n
WHEN new_item = 0 THEN year_1_e + year_2_e + year_3_e
END ) * suggested_net) AS TotalSum
FROM Table1
GROUP BY Coustomer_Id,
new_item
OR
SELECT Coustomer_Id,
new_item,
SUM(DECODE(new_item, 1, year_1_n + year_2_n + year_3_n
, 0, year_1_e + year_2_e + year_3_e
, 0) * suggested_net) AS TotalSum
FROM Table1
GROUP BY Coustomer_Id,
new_item
答案 2 :(得分:0)
试试这个:
选择 顾客ID , 新物品 ,sum(案例new_item为0然后suggest_net *(year_1_e + year_2_e + year_3_e) 否则suggested_net *(year_1_n + year_2_n + year_3_n)结束)总计
来自表格
按customer_id分组,new_item;