需要SQL查询| SQL 10g | Orace

时间:2017-09-12 06:03:22

标签: sql oracle oracle10g

我的父表如下:

SELECT opr_date,contract,price
                 FROM   table
                 WHERE  date = '07-jun-2017' 
                        AND product = 'XYZ'
                        ANd contract_year = 2017
                        AND contract_month between 1 and 12

执行以下查询:

Date        Month   PRICE
7-Jun-17    17-Sep  -79
7-Jun-17    17-Oct  -75
7-Jun-17    17-Jul  -92
7-Jun-17    17-Aug  -90

我的查询正在提取此结果:

var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);  
var list = clientContext.get_web().get_lists().getByTitle('TestList');  
var itemCreateInfo = new SP.ListItemCreationInformation();  
var listItem = list.addItem(itemCreateInfo);

var singleLookupColumn = new SP.FieldLookupValue();  
singleLookupColumn.set_lookupId(2);  
listItem.set_item('CustomLookup', singleLookupColumn); 

var field = list.get_fields().getByInternalNameOrTitle("TestTaxonomy");  
var taxField = clientContext.castTo(field, SP.Taxonomy.TaxonomyField);  
var taxonomyCol = new SP.Taxonomy.TaxonomyFieldValue();  
taxonomyCol.set_label("Test");  
taxonomyCol.set_termGuid("23d03b66-5be6-512b-9fe3-ff13b9b4757c");  
taxonomyCol.set_wssId(-1);  
taxField.setFieldValueByValue(listItem, taxonomyCol);


listItem.update();  
clientContext.load(listItem);  
clientContext.executeQueryAsync(function(){
    console.log("success");
},function(){
    console.log("error");
});  

但是,当且仅当本季度的所有三个月都存在时,它应该填补价格。 在上面的例子中:我们有7月和8月的价格。因此第3季度的所有3个月都有价格。所以应该填写相应的价格。 但是对于第四季度,即10月,11月和12月(contract_month = 10,11& 12),仅存在oct价格。所以我的查询不应该填充这个价格,或者它应该填充为null(因为nov中的价格不存在,而父表中的dec不存在)。

1 个答案:

答案 0 :(得分:1)

逐年确定季度并计算结果(分组,计数= 3):

select opr_date,contract,price
  from your_table
 where /* your conditions ...   */
     date = '07-jun-2017'
 and product = 'XYZ'
 and contract_year = 2017
 and contract_month between 1 and 12
/* only rows, where exist 3 rows per quarter */
 and case
   when contract_month between 1 and 3 then
    contract_year * 10 + 1
   when contract_month between 4 and 6 then
    contract_year * 10 + 2
   when contract_month between 7 and 9 then
    contract_year * 10 + 3
   else
    contract_year * 10 + 4
 end in (select quarter
           from (select case
                          when contract_month between 1 and 3 then
                           contract_year * 10 + 1
                          when contract_month between 4 and 6 then
                           contract_year * 10 + 2
                          when contract_month between 7 and 9 then
                           contract_year * 10 + 3
                          else
                           contract_year * 10 + 4
                        end as quarter
                   from your_table)
          group by quarter
         having count(*) = 3);