1

我希望能够从相同的区域、类型和操作系统中减去 ondemand 的保留数量,然后将剩余的数量乘以费率。

这是mysql数据的示例转储

| Zone  | Type  | Qty | OS    | Reservation | Rate  |
| zone1 | type1 | 12  | linux | ondemand    | 0.24  |
| zone1 | type1 | 6   | linux | reserved    | 0.056 |
| zone1 | type2 | 3   | linux | ondemand    | 0.82  |
| zone2 | type1 | 5   | mswin | ondemand    | 0.24  |
| zone2 | type1 | 2   | mswin | reserved    | 0.056 |
| zone2 | type2 | 3   | linux | ondemand    | 0.82  |
| zone3 | type1 | 4   | linux | ondemand    | 0.24  |
| zone3 | type1 | 1   | linux | reserved    | 0.056 |

结果将是

| Zone  | Type  | Qty | OS    | Reservation | Rate  | sum() |
| zone1 | type1 | 6   | linux | ondemand    | 0.24  | 1.44  |
| zone1 | type1 | 6   | linux | reserved    | 0.056 | 0.336 |
| zone1 | type2 | 3   | linux | ondemand    | 0.82  | 0.246 |
| zone2 | type1 | 3   | mswin | ondemand    | 0.24  | 0.72  |
| zone2 | type1 | 2   | mswin | reserved    | 0.056 | 0.112 |
| zone2 | type2 | 3   | linux | ondemand    | 0.82  | 0.246 |
| zone3 | type1 | 3   | linux | ondemand    | 0.24  | 0.72  |
| zone3 | type1 | 1   | linux | reserved    | 0.056 | 0.056 |

我不确定如何获得一个独特的声明来处理这个问题。我不知道这是否可以使用 mysql 或者我是否需要编写脚本然后生成输出。任何帮助表示赞赏。

对于包含按需的类型和操作系统,区域中并不总是有相应的保留。

4

2 回答 2

1

您需要将表连接到自身:

select
    t1.Zone, t1.Type, t1.Qty - ifnull(t2.Qty, 0), t1.OS,
    t1.rate, (t1.Qty - ifnull(t2.Qty, 0)) * t1.rate as total
from mytable t1
left join mytable t2
    on t1.zone = t2.zone
    and t1.type = t2.type
    and t1.os = t2.os
    and t2.reservation = 'reserved'

        和 t1.reservation = 'ondemand'

关键功夫是加入条件中的最后一个条件。它确保只有“ondemand”行的连接 - 对于left join所有其他行类型(以及那些没有相应“reserved”行的“ondenand”行)将获得 null 用于连接 qty 值(因此ifnull()给出他们为零工作)。

请注意,即使“按需”行没有匹配的“保留”行,这也会正确计算总数。

老实说,我以前从未见过这种查询,其中只有来自父端的一些行被连接到,但所有行都被保留。

于 2013-05-29T02:31:08.323 回答
0

这还没有运行,但可能是这样的?

select zone, type, os, ondemand_cnt, reserved_cnt, ondemand_cnt-reserved_cnt as cnt, 
<some_rate>*(ondemand_cnt-reserved_cnt) as calc_rate
from
(
select zone, type, os, sum(qty) as ondemand_cnt 
from table
where reservation = 'ondemand'
group by zone, type, os
) as o
join
(
select zone, type, os, sum(qty) as reserved_cnt
from table
where reservation = 'reserved'
group by zone, type, os
) as r on (o.zone = r.zone and o.type = r.type and o.os = r.os)
于 2013-05-29T02:26:21.933 回答