超值达到最大降压sql

时间:2016-11-21 19:49:54

标签: mysql netezza

我正在尝试根据Max Hours将一个事件分成多个桶。在我的示例中,此处的引脚最大小时数为1938.因此,仅使用我的查询将其分配到2000范围。 但理想情况下,由于引脚已经达到1938小时,它在过去的某个时间必须有500,1000和1500小时。所以我正在寻找一个查询,不仅将此引脚分配给< = 2000桶,而且分配给500,1000,1500和2000桶。所以基本上逻辑应该在所有桶中分配引脚,直到它达到其最大的小时桶。请帮忙。

我的查询



SELECT *,
CASE WHEN "MAX HOURS" <= 500 THEN '500' 
 WHEN "MAX HOURS" <=1000  THEN '1000'
 WHEN "MAX HOURS" <=1500 THEN '1500'
 WHEN "MAX_HOURS" <=2000 THEN '2000'
 WHEN "MAX_HOURS" <=2500 THEN '2500'
 WHEN "MAX_HOURS" <=3000 THEN '3000' 
 WHEN "MAX_HOURS" <=3500 THEN '3500' 
 WHEN "MAX_HOURS" <=4000 THEN '4000' 
 WHEN "MAX_HOURS" <=4500 THEN '4500' 
 WHEN "MAX_HOURS" <=5000 THEN '5000'
ELSE 'OVER 5000'
END AS Hours_Bucket
FROM PA_BTC
&#13;
&#13;
&#13;

[当前输出]

&#13;
&#13;
PIN   Max hours  Hours_Bucket
XYZ   1938.10       2000
&#13;
&#13;
&#13;

以下是我所期待的:对于所有引脚群。我不确定是否以及如何创建一个可以创建以下所需输出的循环。 [预期结果]

&#13;
&#13;
PIN  Max hours  Hours_Bucket
XYZ  1938            500
XYZ  1938           1000
XYZ  1938           1500
XYZ  1938           2000
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我讨厌这个查询的样子,但我不确定另一种方法呢

SELECT PIN, `MAX HOURS`, 500 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 0
UNION ALL
SELECT PIN, `MAX HOURS`, 1000 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 500
UNION ALL
SELECT PIN, `MAX HOURS`, 1500 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 1000
UNION ALL
SELECT PIN, `MAX HOURS`, 2000 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 1500
UNION ALL
SELECT PIN, `MAX HOURS`, 2500 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 2000
UNION ALL
SELECT PIN, `MAX HOURS`, 3000 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 2500
UNION ALL
SELECT PIN, `MAX HOURS`, 3500 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 3000
UNION ALL
SELECT PIN, `MAX HOURS`, 4000 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 3500
UNION ALL
SELECT PIN, `MAX HOURS`, 4500 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 4000
UNION ALL
SELECT PIN, `MAX HOURS`, 5000 AS Hours_Bucket
FROM PA_BTC
WHERE `MAX HOURS` > 4500

答案 1 :(得分:0)

如果您可以使用小桌子(可以是临时的或常规的),这是可能的。

首先,创建一个表来存储合格到存储桶所需的最短小时数。在你的例子中,1938年大于0会变成500,大于500会变成1000,等等。

要创建的表的示例:

$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_CUSTOM);
$objValidation->setFormula1('=OR(AND('.$currCell.$ii.'>='.$min.','.$currCell.$ii.'<='.$max.'),'.$currCell.$ii.'="canceled",'.$currCell.$ii.'="missed")');

然后使用&gt; =基于先前的桶数加入以强制结果的半笛卡尔。例如:

 create temp table bucket_max (
   max_hours_val int
 , max_hours character varying(10)
 );

 insert into bucket_max values (0,'500');
 insert into bucket_max values (500,'1000');
 insert into bucket_max values (1000,'1500');
 insert into bucket_max values (1500,'2000');
 insert into bucket_max values (2000,'2500');
 insert into bucket_max values (2500,'3000');
 insert into bucket_max values (3000,'3500');
 insert into bucket_max values (3500,'4000');
 insert into bucket_max values (4000,'4500');
 insert into bucket_max values (4500,'5000');
 insert into bucket_max values (5000,'Over 5000');

这应该给出最大小时数达到桶最小值的所有值。