2015-05-14 50 views
0

我有一個當前正在運行的查詢,它返回了一堆產品編號,我想將這些產品編號分組到產品組級別。這些組不存在於數據庫中。基於查詢返回的靜態值的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 
+2

使產品組的定義存在於數據庫中。沒有理智的選擇。 CASE是可笑的;使用數據庫來存儲重要信息,如產品組。任何人都會認爲你正在使用電子表格或其他東西。 –

+0

由於我無法控制的原因,我無法以任何方式寫入這個數據庫(相信我會喜歡按照您的建議來完成)。這是我能拿出一份清晰的報告的唯一方法,我可以放入VBA ADO調用並計劃在我的虛擬機上運行,​​每天我都考慮在Excel中分組,但我認爲數據庫比我的虛擬現實,所以要做到這一點。 –

+0

那我建議找一份新工作。如果規則很愚蠢,那麼可能是時候去找一個更適合工作的地方了。無論如何你可以創建一個臨時表 - 或者其中的幾個。這樣做。然後針對永久和臨時表運行報告,當您的會話結束時,臨時對象將消失。這些分組應該在一個臨時表中;客戶代碼可能在另一個。我對pre/week列表達式也有所保留。 –

回答

2

不知道如果我明白你想要什麼...

無論如何,在這裏不用的選項工作,考慮你只讀補助。

  • 本例將收集的數據類型
  • 您不能添加/使用標準的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> 
+1

這是使用我建議的臨時表的想法。我可能不會使用SET類型,但它可以正常工作。似乎對創建臨時表的概念有一些抵觸,但AFAIK無法阻止某人創建臨時表。如果它們沒有被明確地放棄,它們會在會話結束時自動清除,因此數據庫中沒有持久的變化。 –

+0

感謝ceinmart,這就是我一直在尋找的東西。 @JonathanLeffler對任何不涉及修改數據庫的潛在解決方案都沒有任何抵觸,我不記得特別反駁你對臨時表的想法,但是沒有給出實際的例子,很多你說過的話會迷失在哪裏你開始告訴我找一份新工作!!!感謝您的意見和協助,我將應用上面顯示的邏輯。 –