Informix基于查询中返回的静态值进行分组

时间:2015-05-14 03:22:40

标签: sql informix

我有一个目前有效的查询,它返回了一堆产品编号,我想将这些产品编号分组到产品组级别。这些组不存在于数据库中。

我目前有这样的列表:

'Metro 60'  '63782-1-4000','63782-1-5000','63778-1-4000','63778-1-5000','63780-1-5000','63776-1-4000','63776-1-5000','63772-1-4000','63772-1-5000','63774-1-4000','63774-1-5000','63784-1-5000','63786-1-5000'
'Elements WA'   '63782-1-4000','63782-1-5000','63778-1-4000','63778-1-5000','63780-1-5000','63776-1-4000','63776-1-5000','63772-1-4000','63772-1-5000','63774-1-4000','63774-1-5000','63784-1-5000','63786-1-5000','63774-2-1000','63782-2-1000','63778-2-0200','63772-2-1000','63776-2-1000','67744-1-5000','63770-2-1000','63768-2-0200','63772-5-1000','63774-5-1000','63774-5-3000','63760-2-1000','63748-5-4000','66757-5-1000','63774-5-2000','63494-1-5000','63486-1-5000','63476-1-5000','63486-1-6000','63495-1-5000','63487-1-5000','63487-1-6000'
'INDULGENCE'    '63782-1-4000','63782-1-5000','63778-1-4000','63778-1-5000','63780-1-5000','63776-1-4000','63776-1-5000','63772-1-4000','63772-1-5000','63774-1-4000','63774-1-5000','63784-1-5000','63786-1-5000','63774-2-1000','63782-2-1000','63778-2-0200','63772-2-1000','63776-2-1000','67744-1-5000','63770-2-1000','63768-2-0200','63772-5-1000','63774-5-1000','63774-5-3000','63760-2-1000','63748-5-4000','66757-5-1000','63774-5-2000','63494-1-5000','63486-1-5000','63476-1-5000','63486-1-6000','63495-1-5000','63487-1-5000','63487-1-6000','63530-1-5000','63536-1-5000','63534-1-5000','63532-1-4000','63532-1-5000','63528-1-5000','63531-1-5000','63537-1-5000','63535-1-5000','63533-1-5000','63529-1-5000'

如何对查询结果进行分组以返回此结果而不是product.part_code?

查询:

Select  Product.part_code,
    sum(product.cubic_qty*(salesstat.order_qty-salesstat.return_qty)) /1000 Sales_QTY,
    sum(salesstat.net_amt-salesstat.return_net_amt) Gross_Revenue,
    sum(salesstat.net_prod_amt - salesstat.return_nprod_amt) Net_Revenue,
    Decode(sum(product.cubic_qty*(salesstat.order_qty)), 0, Null,
        Sum(salesstat.net_prod_amt)/sum(product.cubic_qty*(salesstat.order_qty))) *1000 Net_ASP,
    Decode(sum(product.cubic_qty*(salesstat.order_qty-salesstat.return_qty)), 0, Null,
        sum(salesstat.cost_amt-salesstat.return_net_amt) / (sum(product.cubic_qty*(salesstat.order_qty-salesstat.return_qty)) /1000)) Cost_Per_Thou,
    sum(salesstat.cost_amt-salesstat.return_cost_amt) Total_Cost,
    sum(salesstat.net_prod_amt - salesstat.return_nprod_amt)  - sum(salesstat.cost_amt-salesstat.return_cost_amt) Gross_Profit,
    Decode(sum(product.cubic_qty*(salesstat.order_qty-salesstat.return_qty)), 0, Null,
        (sum(salesstat.net_prod_amt - salesstat.return_nprod_amt)  - sum(salesstat.cost_amt-salesstat.return_cost_amt)) / (sum(product.cubic_qty*(salesstat.order_qty-salesstat.return_qty)) /1000)) Profit_Per_Thou
from    Customer,
    salesstat,
    warereptdetl,
    Product,
    maingrp
Where   Product.part_code = salesstat.part_code
and product.part_code in ('63782-1-4000','63782-1-5000','63778-1-4000','63778-1-5000','63780-1-5000','63776-1-4000','63776-1-5000','63772-1-4000','63772-1-5000','63774-1-4000','63774-1-5000','63784-1-5000','63786-1-5000','63774-2-1000','63782-2-1000','63778-2-0200','63772-2-1000','63776-2-1000','67744-1-5000','63770-2-1000','63768-2-0200','63772-5-1000','63774-5-1000','63774-5-3000','63760-2-1000','63748-5-4000','66757-5-1000','63774-5-2000','63494-1-5000','63486-1-5000','63476-1-5000','63486-1-6000','63495-1-5000','63487-1-5000','63487-1-6000','63530-1-5000','63536-1-5000','63534-1-5000','63532-1-4000','63532-1-5000','63528-1-5000','63531-1-5000','63537-1-5000','63535-1-5000','63533-1-5000','63529-1-5000','63559-1-5000','63440-1-5000','63510-1-4000','63510-1-5000','63510-1-6000','63516-1-5000','63518-1-5000','63540-1-5000','63540-1-6000','63430-1-4000','63430-1-5000','63430-1-6000','63526-1-4000','63526-1-5000','63526-1-6000','63559-1-6000','63516-1-6000','63560-1-5000','63441-1-5000','63511-1-5000','63511-1-6000','63517-1-5000','63519-1-5000','63541-1-5000','63431-1-4000','63431-1-5000','63431-1-6000','63527-1-5000','63560-1-6000','63322-1-5003','63332-1-5003','63330-1-5003','63334-1-5003','63323-1-5003','63333-1-5003','63335-1-5003','63326-1-4000','63326-1-5000','63326-1-6000','63334-1-4000','63334-1-5000','63334-1-6000','63334-1-5004','63327-1-4000','63327-1-5000','63327-1-6000','63335-1-5000','63320-1-5000','63320-1-6000','63321-1-5000','63321-1-6000','63314-1-5000','63422-1-5000','63312-1-5000','63322-1-5000','63332-1-5000','63330-1-5000','63338-1-4000','63338-1-5000','63338-1-6000','63422-1-4000','63315-1-5000','63423-1-5000','63313-1-5000','63323-1-5000','63333-1-5000','63339-1-4000','63339-1-5000','63339-1-6000','63318-1-6000','63318-1-5000','63319-1-6000','63319-1-5000')
and customer.cust_code = salesstat.cust_code
and salesstat.rept_code = '0'
and warereptdetl.ware_code = salesstat.ware_code
and customer.cust_code in ('91826','22978','91247','48972','91386','91520','101472','98214','122586','108675','130703','120847','101155','96360','12005','34263','101517')
and salesstat.year_num = 2015
and salesstat.stat_type_code = 'MLY'
and salesstat.int_num = 9
and salesstat.ord_ind in ('7','8')
and maingrp.maingrp_code = salesstat.maingrp_code
group by Product.part_code

提前致谢

编辑:

我现在已经得到了这个工作,我已经设法转动(交叉表)结果,但如果有更好的方法,我仍然会喜欢帮助,当然我可以在某处使用“In”而不是每行产品

Select  CASE trim(Product.part_code)
    WHEN '63782-1-4000' THEN 'Metro 60'
    WHEN '63782-1-5000' THEN 'Metro 60'
    WHEN '63778-1-4000' THEN 'Metro 60'
    WHEN '63778-1-5000' THEN 'Metro 60'
    WHEN '63780-1-5000' THEN 'Metro 60'
    WHEN '63776-1-4000' THEN 'Metro 60'
    WHEN '63776-1-5000' THEN 'Metro 60'
    WHEN '63772-1-4000' THEN 'Metro 60'
    WHEN '63772-1-5000' THEN 'Metro 60'
    WHEN '63774-1-4000' THEN 'Metro 60'
    WHEN '63774-1-5000' THEN 'Metro 60'
    WHEN '63784-1-5000' THEN 'Metro 60'
    WHEN '63786-1-5000' THEN 'Metro 60'
    WHEN '63774-2-1000' THEN 'Non-Standard pavers'
    WHEN '63782-2-1000' THEN 'Non-Standard pavers'
    WHEN '63778-2-0200' THEN 'Non-Standard pavers'
    WHEN '63772-2-1000' THEN 'Non-Standard pavers'
    WHEN '63776-2-1000' THEN 'Non-Standard pavers'
    WHEN '67744-1-5000' THEN 'Non-Standard pavers'
    WHEN '63770-2-1000' THEN 'Non-Standard pavers'
    WHEN '63768-2-0200' THEN 'Non-Standard pavers'
    WHEN '63772-5-1000' THEN 'Non-Standard pavers'
    WHEN '63774-5-1000' THEN 'Non-Standard pavers'
    WHEN '63774-5-3000' THEN 'Non-Standard pavers'
    WHEN '63760-2-1000' THEN 'Non-Standard pavers'
    WHEN '63748-5-4000' THEN 'Non-Standard pavers'
    WHEN '66757-5-1000' THEN 'Non-Standard pavers'
    WHEN '63774-5-2000' THEN 'Non-Standard pavers'
    WHEN '63494-1-5000' THEN 'Elements WA'
    WHEN '63486-1-5000' THEN 'Elements WA'
    WHEN '63476-1-5000' THEN 'Elements WA'
    WHEN '63486-1-6000' THEN 'Elements WA'
    WHEN '63495-1-5000' THEN 'Elements WA'
    WHEN '63487-1-5000' THEN 'Elements WA'
    WHEN '63487-1-6000' THEN 'Elements WA'
    WHEN '63530-1-5000' THEN 'INDULGENCE'
    WHEN '63536-1-5000' THEN 'INDULGENCE'
    WHEN '63534-1-5000' THEN 'INDULGENCE'
    WHEN '63532-1-4000' THEN 'INDULGENCE'
    WHEN '63532-1-5000' THEN 'INDULGENCE'
    WHEN '63528-1-5000' THEN 'INDULGENCE'
    WHEN '63531-1-5000' THEN 'INDULGENCE'
    WHEN '63537-1-5000' THEN 'INDULGENCE'
    WHEN '63535-1-5000' THEN 'INDULGENCE'
    WHEN '63533-1-5000' THEN 'INDULGENCE'
    WHEN '63529-1-5000' THEN 'INDULGENCE'
    WHEN '63559-1-5000' THEN 'Ocean Sands'
    WHEN '63440-1-5000' THEN 'Ocean Sands'
    WHEN '63510-1-4000' THEN 'Ocean Sands'
    WHEN '63510-1-5000' THEN 'Ocean Sands'
    WHEN '63510-1-6000' THEN 'Ocean Sands'
    WHEN '63516-1-5000' THEN 'Ocean Sands'
    WHEN '63518-1-5000' THEN 'Ocean Sands'
    WHEN '63540-1-5000' THEN 'Ocean Sands'
    WHEN '63540-1-6000' THEN 'Ocean Sands'
    WHEN '63430-1-4000' THEN 'Ocean Sands'
    WHEN '63430-1-5000' THEN 'Ocean Sands'
    WHEN '63430-1-6000' THEN 'Ocean Sands'
    WHEN '63526-1-4000' THEN 'Ocean Sands'
    WHEN '63526-1-5000' THEN 'Ocean Sands'
    WHEN '63526-1-6000' THEN 'Ocean Sands'
    WHEN '63559-1-6000' THEN 'Ocean Sands'
    WHEN '63516-1-6000' THEN 'Ocean Sands'
    WHEN '63560-1-5000' THEN 'Ocean Sands'
    WHEN '63441-1-5000' THEN 'Ocean Sands'
    WHEN '63511-1-5000' THEN 'Ocean Sands'
    WHEN '63511-1-6000' THEN 'Ocean Sands'
    WHEN '63517-1-5000' THEN 'Ocean Sands'
    WHEN '63519-1-5000' THEN 'Ocean Sands'
    WHEN '63541-1-5000' THEN 'Ocean Sands'
    WHEN '63431-1-4000' THEN 'Ocean Sands'
    WHEN '63431-1-5000' THEN 'Ocean Sands'
    WHEN '63431-1-6000' THEN 'Ocean Sands'
    WHEN '63527-1-5000' THEN 'Ocean Sands'
    WHEN '63560-1-6000' THEN 'Ocean Sands'
    WHEN '63322-1-5003' THEN 'Pioneer'
    WHEN '63332-1-5003' THEN 'Pioneer'
    WHEN '63330-1-5003' THEN 'Pioneer'
    WHEN '63334-1-5003' THEN 'Pioneer'
    WHEN '63323-1-5003' THEN 'Pioneer'
    WHEN '63333-1-5003' THEN 'Pioneer'
    WHEN '63335-1-5003' THEN 'Pioneer'
    WHEN '63326-1-4000' THEN 'Symmetry'
    WHEN '63326-1-5000' THEN 'Symmetry'
    WHEN '63326-1-6000' THEN 'Symmetry'
    WHEN '63334-1-4000' THEN 'Symmetry'
    WHEN '63334-1-5000' THEN 'Symmetry'
    WHEN '63334-1-6000' THEN 'Symmetry'
    WHEN '63334-1-5004' THEN 'Symmetry'
    WHEN '63327-1-4000' THEN 'Symmetry'
    WHEN '63327-1-5000' THEN 'Symmetry'
    WHEN '63327-1-6000' THEN 'Symmetry'
    WHEN '63335-1-5000' THEN 'Symmetry'
    WHEN '63320-1-5000' THEN 'Symmetry'
    WHEN '63320-1-6000' THEN 'Symmetry'
    WHEN '63321-1-5000' THEN 'Symmetry'
    WHEN '63321-1-6000' THEN 'Symmetry'
    WHEN '63314-1-5000' THEN 'Western Earth'
    WHEN '63422-1-5000' THEN 'Western Earth'
    WHEN '63312-1-5000' THEN 'Western Earth'
    WHEN '63322-1-5000' THEN 'Western Earth'
    WHEN '63332-1-5000' THEN 'Western Earth'
    WHEN '63330-1-5000' THEN 'Western Earth'
    WHEN '63338-1-4000' THEN 'Western Earth'
    WHEN '63338-1-5000' THEN 'Western Earth'
    WHEN '63338-1-6000' THEN 'Western Earth'
    WHEN '63422-1-4000' THEN 'Western Earth'
    WHEN '63315-1-5000' THEN 'Western Earth'
    WHEN '63423-1-5000' THEN 'Western Earth'
    WHEN '63313-1-5000' THEN 'Western Earth'
    WHEN '63323-1-5000' THEN 'Western Earth'
    WHEN '63333-1-5000' THEN 'Western Earth'
    WHEN '63339-1-4000' THEN 'Western Earth'
    WHEN '63339-1-5000' THEN 'Western Earth'
    WHEN '63339-1-6000' THEN 'Western Earth'
    WHEN '63318-1-6000' THEN 'Western Earth'
    WHEN '63318-1-5000' THEN 'Western Earth'
    WHEN '63319-1-6000' THEN 'Western Earth'
    WHEN '63319-1-5000' THEN 'Western Earth'
    ELSE '0'
    END as ProdGrp,
    sum(CASE WHEN salesstat.int_num = 32 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre11,
    sum(CASE WHEN salesstat.int_num = 33 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre10,
    sum(CASE WHEN salesstat.int_num = 34 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre09,
    sum(CASE WHEN salesstat.int_num = 35 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre08,
    sum(CASE WHEN salesstat.int_num = 36 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre07,
    sum(CASE WHEN salesstat.int_num = 37 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre06,
    sum(CASE WHEN salesstat.int_num = 38 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre05,
    sum(CASE WHEN salesstat.int_num = 39 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre04,
    sum(CASE WHEN salesstat.int_num = 40 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre03,
    sum(CASE WHEN salesstat.int_num = 41 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre02,
    sum(CASE WHEN salesstat.int_num = 42 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Pre01,
    sum(CASE WHEN salesstat.int_num = 43 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week01,
    sum(CASE WHEN salesstat.int_num = 44 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week02,
    sum(CASE WHEN salesstat.int_num = 45 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week03,
    sum(CASE WHEN salesstat.int_num = 46 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week04,
    sum(CASE WHEN salesstat.int_num = 47 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week05,
    sum(CASE WHEN salesstat.int_num = 48 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week06,
    sum(CASE WHEN salesstat.int_num = 49 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week07,
    sum(CASE WHEN salesstat.int_num = 50 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week08,
    sum(CASE WHEN salesstat.int_num = 51 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week09,
    sum(CASE WHEN salesstat.int_num = 52 THEN product.cubic_qty*(salesstat.order_qty-salesstat.return_qty) /1000 ELSE null END) As Week10
from    Customer,
    salesstat,
    warereptdetl,
    Product,
    maingrp
Where   Product.part_code = salesstat.part_code
and product.part_code in ('63782-1-4000','63782-1-5000','63778-1-4000','63778-1-5000','63780-1-5000','63776-1-4000','63776-1-5000','63772-1-4000','63772-1-5000','63774-1-4000','63774-1-5000','63784-1-5000','63786-1-5000','63774-2-1000','63782-2-1000','63778-2-0200','63772-2-1000','63776-2-1000','67744-1-5000','63770-2-1000','63768-2-0200','63772-5-1000','63774-5-1000','63774-5-3000','63760-2-1000','63748-5-4000','66757-5-1000','63774-5-2000','63494-1-5000','63486-1-5000','63476-1-5000','63486-1-6000','63495-1-5000','63487-1-5000','63487-1-6000','63530-1-5000','63536-1-5000','63534-1-5000','63532-1-4000','63532-1-5000','63528-1-5000','63531-1-5000','63537-1-5000','63535-1-5000','63533-1-5000','63529-1-5000','63559-1-5000','63440-1-5000','63510-1-4000','63510-1-5000','63510-1-6000','63516-1-5000','63518-1-5000','63540-1-5000','63540-1-6000','63430-1-4000','63430-1-5000','63430-1-6000','63526-1-4000','63526-1-5000','63526-1-6000','63559-1-6000','63516-1-6000','63560-1-5000','63441-1-5000','63511-1-5000','63511-1-6000','63517-1-5000','63519-1-5000','63541-1-5000','63431-1-4000','63431-1-5000','63431-1-6000','63527-1-5000','63560-1-6000','63322-1-5003','63332-1-5003','63330-1-5003','63334-1-5003','63323-1-5003','63333-1-5003','63335-1-5003','63326-1-4000','63326-1-5000','63326-1-6000','63334-1-4000','63334-1-5000','63334-1-6000','63334-1-5004','63327-1-4000','63327-1-5000','63327-1-6000','63335-1-5000','63320-1-5000','63320-1-6000','63321-1-5000','63321-1-6000','63314-1-5000','63422-1-5000','63312-1-5000','63322-1-5000','63332-1-5000','63330-1-5000','63338-1-4000','63338-1-5000','63338-1-6000','63422-1-4000','63315-1-5000','63423-1-5000','63313-1-5000','63323-1-5000','63333-1-5000','63339-1-4000','63339-1-5000','63339-1-6000','63318-1-6000','63318-1-5000','63319-1-6000','63319-1-5000')
and customer.cust_code = salesstat.cust_code
and salesstat.rept_code = '0'
and warereptdetl.ware_code = salesstat.ware_code
and customer.cust_code in ('91826','22978','91247','48972','91386','91520','101472','98214','122586','108675','130703','120847','101155','96360','12005','34263','101517')
and salesstat.year_num = 2015
and salesstat.stat_type_code = 'WLY'
and salesstat.int_num > 31
and salesstat.ord_ind in ('7','8')
and maingrp.maingrp_code = salesstat.maingrp_code
group by ProdGrp

1 个答案:

答案 0 :(得分:2)

不确定我是否理解你想要的东西......

无论如何,考虑到你只阅读补助金,这里可以选择工作。

  • 此示例适用于COLLECTION数据类型
  • 您无法使用标准SQL添加/减去集合中的值 因此,您只需通知一次所有产品。
create temp table tp01 ( prod char(10) , childs set( varchar(10) not null) ) with no log ; 
insert into tp01 values ( 'p1' , set{"xyz", "abc", "ababa"});
insert into tp01 values ( 'p3' , set{"aaa", "123", "999"});

create temp table tp02 ( subprod char(10) ) with no log ; 
insert into tp02 values ("xyz");
insert into tp02 values ("abc");
insert into tp02 values ("123");
insert into tp02 values ("999");
insert into tp02 values ("xxx");
insert into tp02 values ("aba");

select subprod
     , tp01.prod
from tp02 , outer tp01
where tp02.subprod in tp01.childs



subprod   prod
xyz           p1        
abc           p1        
123           p3        
999           p3        
xxx           <null>
aba           <null>