Avatar of Star79
Star79
Flag for United States of America asked on

Oracle Query -Lead/over

I have below query for a single date.
SELECT nsn,thisdate,  
vendor1_code, vendor1_name, qtysum_1, DELIVERED_QTY_1, split_1,
vendor2_code, vendor2_name, qtysum_2, DELIVERED_QTY_2, split_2
FROM 
(SELECT nsn_code AS nsn, vendor_code AS vendor1_code, vendor_name AS vendor1_name, thisdate,
qtysum AS qtysum_1, DELIVERED_QTY AS DELIVERED_QTY_1,
split AS split_1, LEAD(vendor_code) OVER (ORDER BY split) AS vendor2_code,
               LEAD(vendor_name) OVER (ORDER BY split) AS vendor2_name,
               LEAD(qtysum) OVER (ORDER BY split)      AS qtysum_2,
              LEAD(DELIVERED_QTY)OVER (ORDER BY split) AS DELIVERED_QTY_2,
               LEAD(split) OVER (ORDER BY split)       AS split_2
               ,ROW_NUMBER() OVER (ORDER BY split)      rn
          FROM (  SELECT p.NSN_CODE,
                        dv.vendor_code,
                         dv.vendor_name,d.thisdate,
                         SUM(po.quantity) qtysum,
                         sum(po.QTYDELIVERED) as DELIVERED_QTY,
                         rfp.vendor_share_percent AS split
                    FROM fact_po_lines po
                         INNER JOIN dim_product p
                             ON po.dim_product_id = p.dim_product_id 
                         INNER JOIN dim_date d ON po.ORDERED_DATE_ID = d.dim_date_id
                         INNER JOIN fact_pa_item pai
                             ON     po.dim_product_id = pai.dim_product_id
                                AND po.dim_vendor_id = pai.dim_vendor_id
                         INNER JOIN dim_vendor dv ON po.dim_vendor_id = dv.dim_vendor_id
                         INNER JOIN dim_date deff ON pai.dim_date_effective_id = deff.dim_date_id
                         INNER JOIN dim_date dexp ON pai.dim_date_expiration_id = dexp.dim_date_id
                         INNER JOIN dim_product p ON pai.dim_product_id = p.dim_product_id
                         INNER JOIN fact_rfp_product rfp
                             ON     pai.source_rfp_id = rfp.source_rfp_id
                                AND pai.dim_product_id = rfp.dim_product_id
                                AND pai.dim_date_effective_id = rfp.dim_date_create_id
                   WHERE    d.THISDATE = '12-AUG-14'  
                   AND d.thisdate BETWEEN deff.thisdate AND dexp.thisdate
                         and p.NSN_CODE = '2610-01-214-1344'
                GROUP BY p.nsn_code,
                         dv.vendor_code,
                         dv.vendor_name, 
                         d.thisdate,
                         rfp.vendor_share_percent) t)
                        
 WHERE rn = 1
 order by thisdate;

Open in new window


But if I need to do the same for a date range on the order date and have to extract the total sum for a vendor group for that order date range.As in the resultset capture.ResultsetThe bottom section for vendor1 and vendor 2(total order qty,total delivered qty leaving the split) is needed.
Thanks for your time

Please let me know.
Oracle DatabaseSQL

Avatar of undefined
Last Comment
Star79

8/22/2022 - Mon
PortletPaul

There is already a date range  in the query

        AND d.thisdate BETWEEN deff.thisdate AND dexp.thisdate

i.e. between a "date_effective" and  a "date_expiration"

What happens if the new query date range isn't compatible with the existing date range?
Could the "split" change during the new date range, and if so how do you want to handle that?

Do you need to alter the where clause to something like this perhaps?
            WHERE ( 
                       d.THISDATE >= to_date('2014-08-12','yyyy-mm-dd') 
                   AND d.THISDATE  < to_date('2014-09-12','yyyy-mm-dd') 
                  )
                  AND to_date('2014-08-12','yyyy-mm-dd') BETWEEN deff.thisdate AND dexp.thisdate

                  AND p.NSN_CODE = '2610-01-214-1344'

Open in new window

Personally I don't think I can offer much detail without some "sample data" (per table) and the "expected result" (based on the sample data).

Please also note I'm not sure why you use a string like '12-AUG-14'  as a date.
I always use to_date() or to_timestamp() depending on the data type in the relevant columns.
Star79

ASKER
I have attached the current resultset,
the query I have run is
SELECT nsn,thisdate,  
vendor1_code, vendor1_name, qtysum_1, DELIVERED_QTY_1, split_1,
vendor2_code, vendor2_name, qtysum_2, DELIVERED_QTY_2, split_2
FROM 
(SELECT nsn_code AS nsn, vendor_code AS vendor1_code, vendor_name AS vendor1_name, thisdate,
qtysum AS qtysum_1, DELIVERED_QTY AS DELIVERED_QTY_1,
split AS split_1, LEAD(vendor_code) OVER (ORDER BY split) AS vendor2_code,
               LEAD(vendor_name) OVER (ORDER BY split) AS vendor2_name,
               LEAD(qtysum) OVER (ORDER BY split)      AS qtysum_2,
              LEAD(DELIVERED_QTY)OVER (ORDER BY split) AS DELIVERED_QTY_2,
               LEAD(split) OVER (ORDER BY split)       AS split_2
               ,ROW_NUMBER() OVER (ORDER BY split)      rn
          FROM (  SELECT p.NSN_CODE,
                        dv.vendor_code,
                         dv.vendor_name,d.thisdate,
                         SUM(po.quantity) qtysum,
                         sum(po.QTYDELIVERED) as DELIVERED_QTY,
                         rfp.vendor_share_percent AS split
                    FROM fact_po_lines po
                         INNER JOIN dim_product p
                             ON po.dim_product_id = p.dim_product_id 
                         INNER JOIN dim_date d ON po.ORDERED_DATE_ID = d.dim_date_id
                         INNER JOIN fact_pa_item pai
                             ON     po.dim_product_id = pai.dim_product_id
                                AND po.dim_vendor_id = pai.dim_vendor_id
                         INNER JOIN dim_vendor dv ON po.dim_vendor_id = dv.dim_vendor_id
                         INNER JOIN dim_date deff ON pai.dim_date_effective_id = deff.dim_date_id
                         INNER JOIN dim_date dexp ON pai.dim_date_expiration_id = dexp.dim_date_id
                         INNER JOIN dim_product p ON pai.dim_product_id = p.dim_product_id
                         INNER JOIN fact_rfp_product rfp
                             ON     pai.source_rfp_id = rfp.source_rfp_id
                                AND pai.dim_product_id = rfp.dim_product_id
                                AND pai.dim_date_effective_id = rfp.dim_date_create_id
                   WHERE  
                   d.THISDATE ='12-AUG-14'
                   AND d.thisdate BETWEEN deff.thisdate AND dexp.thisdate
                         and p.NSN_CODE = '2610-01-194-6220'--'2610-01-194-6220'
                GROUP BY p.nsn_code,
                         dv.vendor_code,
                         dv.vendor_name, 
                         d.thisdate,
                         rfp.vendor_share_percent) t)
                        
 WHERE rn = 1
 order by thisdate;

Open in new window


The export data attached is something Iam expecting .
The query above returns each of those lines for a single date entry
 d.THISDATE ='12-AUG-14 pr '07-AUG-14'.
How to return the same dataset by passing
 d.THISDATE BETWEEN '01-AUG-14' AND '13-AUG-14'  
That way I dont have to run the query each time for a single date,I just have to run it for a date range.
Thanks for your time
export.xlsx
ASKER CERTIFIED SOLUTION
Star79

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Star79

ASKER
I was able to get to the solution
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23