Merging two queries?

I have this query:
WITH  weeks_vw AS
    ( SELECT  DISTINCT TO_CHAR(fill_year_day,'IYYYIW') AS fill_year_week
            FROM
           ( SELECT  to_date('2012-05-01','yyyy-mm-dd')  + (ROWNUM-1) AS fill_year_day
                    FROM    DUAL
                    CONNECT BY      LEVEL <= TRUNC(to_date('2012-06-30','yyyy-mm-dd') - to_date('2012-05-01','yyyy-mm-dd') ) + 1)
,   got_sales_qty_sum     AS
    SELECT  a.material, a.plant, sum(NVL(quantity,0)) AS sales_qty_sum
    ,         AVG    (sum(NVL(quantity,0))) OVER (PARTITION BY  material)    AS material_avg
    ,         STDDEV (sum(NVL(quantity,0))) OVER (PARTITION BY  material)    AS material_stddev
    FROM
         SELECT  t.material, t.plant, t.quantity, TO_CHAR(t.posting_date,'IYYYIW') AS posting_week
         FROM    tblCon t
         Where     t.plant = '1900'
         AND     t.Posting_date >= to_date('2012-05-01','yyyy-mm-dd')
         AND     t.Posting_date <= to_date('2012-06-30','yyyy-mm-dd')
         AND     t.material = 'Label1'
       ) A
                         PARTITION BY (a.material,a.plant)
       RIGHT OUTER JOIN weeks_vw  ON weeks_vw.fill_year_week = A.posting_week
       GROUP BY  a.material, a.plant, fill_year_week
SELECT     *
FROM     got_sales_qty_sum
WHERE     ABS (sales_qty_sum - material_avg)     <= 2.0 * material_stddev
;Now I want to add this query as I have since previously. The query does not take relese and revisons changes into account . It sums the total quantity from earlier release and revisoner. But how do I get this functionality on first query?
WITH got_base_material AS
SELECT material, Plant, quantity, to_char(Posting_Date, 'IYYYIW') AS year_week
, REGEXP_REPLACE ( material
       , 'R..$'
       ) AS base_material
FROM tblcon
SELECT   FIRST_VALUE (MAX (material)) OVER ( PARTITION BY  base_material
      ORDER BY     year_week  DESC
    ) AS last_material
,   SUM (quantity) AS total_quantity
,   year_week
,   Plant
FROM   got_base_material
GROUP BY  base_material
,   year_week
,   Plant
;I used REGEXP_REPLACE to extract material from release and revision.
Example on result from abowe query is. Here is data from the table before running the query. This shows per month, but i want it per day.
Material----|---Quantity---|--Period
LABEL10R1A--|-----10-------|---201009
LABEL10R1B--|-----20-------|---201019
LABEL10R1C--|-----30-------|---201010
LABEL13R1A--|-----10-------|---201006
LABEL13R2A--|-----10-------|---201008
LABEL11-----|-----15-------|---201005
LABEL12-----|-----25-------|---201006It presents the latest revision material like this:
Material----|----Quantity--|--Period
LABEL10R1C--|-----30-------|---201006
LABEL10R1C--|-----30-------|---201010
LABEL13R2A--|-----10-------|---201006
LABEL13R2A--|-----10-------|---201008
LABEL11-----|-----15-------|---201005
LABEL12-----|-----25-------|---201006

If Frank can work out what you're after, then I'll take off my hat to him! *{
{noformat};-){noformat}
So, I've taken your data and the first query from your original post, and I don't get anywhere near the output that you claim to be expecting:
with        tblcon as (select 'Label1R1A' material, 1900 plant, 1000 quantity, to_date('2012-05-07','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R1B' material, 1900 plant, 184 quantity, to_date('2012-05-09','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R1B' material, 1900 plant, 570 quantity, to_date('2012-05-10','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R1B' material, 1900 plant, 770 quantity, to_date('2012-05-11','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R1C' material, 1900 plant, 888 quantity, to_date('2012-05-16','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R1C' material, 1900 plant, 651 quantity, to_date('2012-05-17','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R1C' material, 1900 plant, 1081 quantity, to_date('2012-05-18','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2A' material, 1900 plant, 1085 quantity, to_date('2012-05-19','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2A' material, 1900 plant, 3240 quantity, to_date('2012-05-20','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2A' material, 1900 plant, 9 quantity, to_date('2012-05-24','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2A' material, 1900 plant, 1165 quantity, to_date('2012-05-26','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2A' material, 1900 plant, 105 quantity, to_date('2012-05-27','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2A' material, 1900 plant, 1165 quantity, to_date('2012-05-28','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 2125 quantity, to_date('2012-05-29','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 53211 quantity, to_date('2012-05-30','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 3240 quantity, to_date('2012-06-01','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 9 quantity, to_date('2012-06-03','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 1165 quantity, to_date('2012-06-16','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 10 quantity, to_date('2012-06-26','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 1165 quantity, to_date('2012-06-28','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2B' material, 1900 plant, 2125 quantity, to_date('2012-06-29','yyyy-mm-dd') posting_date from dual union all
                       select 'Label1R2C' material, 1900 plant, 8334 quantity, to_date('2012-06-30','yyyy-mm-dd') posting_date from dual union all
                       select 'Label2R1A' material, 1900 plant, 1165 quantity, to_date('2012-06-16','yyyy-mm-dd') posting_date from dual union all
                       select 'Label2R1A' material, 1900 plant, 10 quantity, to_date('2012-06-26','yyyy-mm-dd') posting_date from dual union all
                       select 'Label2R1A' material, 1900 plant, 1165 quantity, to_date('2012-06-28','yyyy-mm-dd') posting_date from dual union all
                       select 'Label2R1A' material, 1900 plant, 2125 quantity, to_date('2012-06-29','yyyy-mm-dd') posting_date from dual union all
                       select 'Label2R1B' material, 1900 plant, 8334 quantity, to_date('2012-06-30','yyyy-mm-dd') posting_date from dual),
          weeks_vw AS (SELECT TO_CHAR(TO_DATE('2012-05-01', 'yyyy-mm-dd') + (level - 1)*7, 'iyyyiw') fill_year_week
                              FROM   DUAL
                              CONNECT BY LEVEL <= (trunc(TO_DATE('2012-06-30', 'yyyy-mm-dd'), 'iw') - trunc(TO_DATE('2012-05-01', 'yyyy-mm-dd'), 'iw'))/7 + 1),
got_sales_qty_sum  AS
    SELECT  a.material, a.plant, sum(NVL(quantity,0)) AS sales_qty_sum, weeks_vw.fill_year_week
    ,      AVG    (sum(NVL(quantity,0))) OVER (PARTITION BY  material)    AS material_avg
    ,      STDDEV (sum(NVL(quantity,0))) OVER (PARTITION BY  material)    AS material_stddev
    FROM
         SELECT  t.material, t.plant, t.quantity, TO_CHAR(t.posting_date,'IYYYIW') AS posting_week
         FROM    tblCon t
         Where     t.plant = '1900'
         AND     t.Posting_date >= to_date('2012-05-01','yyyy-mm-dd')
         AND     t.Posting_date <= to_date('2012-06-30','yyyy-mm-dd')
--         AND     t.material = 'Label1'
       ) A
                PARTITION BY (a.material,a.plant)
       RIGHT OUTER JOIN weeks_vw  ON weeks_vw.fill_year_week = A.posting_week
       GROUP BY  a.material, a.plant, fill_year_week
SELECT  *
FROM    got_sales_qty_sum
WHERE   ABS (sales_qty_sum - material_avg)  <= 2.0 * material_stddev
and sales_qty_sum != 0
order by material, fill_year_week;
MATERIAL       PLANT SALES_QTY_SUM FILL_YEAR_WEEK MATERIAL_AVG MATERIAL_STDDEV
Label1R2A       1900          1279 201221           752.111111      1440.81795
Label1R2A       1900          1165 201222           752.111111      1440.81795
Label1R2B       1900          1165 201224           7005.55556      19373.7876
Label1R2B       1900          3300 201226           7005.55556      19373.7876
Label2R1A       1900          1165 201224           496.111111      1119.82638Perhaps you could explain further? Are you expecting to keep the rows with 0 sales_qty_sum?

Similar Messages

  • Merging two Queries in single crystal report lauout

    Hi All,
    I need to show two reports from two different BEx queries in a single crystal report layout. The queries are independent of each onther and having their selection variables. I have implemented two subreports for two queries in a main report. Now the problem i am facing is, though both the queries are having same slection(Profit Centre and Fiscal Per/Year), they are getting repeated in crystal report parameter screen. I have to enter the values for selections for each report.
    Is there any method to merge the selections for both reports if they are same, so that i can enter the selections only once?
    Thanks in Adv,
    Shubhramukta.

    Hi,
    I have two queries in BEx having Selections. Query 1 is having selections (Profit centre and Fiscal Per/Yr) and query 2 is having same selections as well(i.e. Profit centre and Fiscal Per/Yr). I have created two reports corresponding to these queries have used as subreports in a main report using Crystal report 2008. When i run the main Crystal report, it automatically prompts for values to be entered, as variables are set at BEx query level.
    Though both the reports are having same selection variables, crystal report asks to enter values two times , once for each report, and it is obvious.
    Parameters are Crystal parameters.
    Is there any method to merge the parameters,is selection variables are same, so that it will ask only once?
    Thanks

  • Merging the output of two queries in to one

    Dear experts,
    There are two reports generated with sap queries.
    I want some of the fields of first query to be displayed in to second.
    Is there any option for merging two queries.
    Where is the request of the query saved.
    How to do the changes in the query and how to transport it after changes.
    How to find the user group and infoset of the query.
    Please help me.

    Please serach in this forum, u can find lot of threads
    /people/shafiq.rehman3/blog/2008/06/16/sap-adhoc-query-sq01-sq02-sq03

  • Drill down in report based on two queries

    Hi,
    I have a problem with drilling down in report which is based on two queries.
    Queries are based on different universes.
    Both queries contains almost the same dimensions but different measures.
    In my report is a calculated measure based on measures from both queries.
    In both universes are the same hierachies.
    When I drill down in report for the first time I have to chose hierarchy  but then data are filtered to the choosen  value only from one query , data from second query are not filtered and the values of calculated measure are incorrect
    How can I solve this issue without adding dimensions belonging to the hierarchies to queries.
    Please help.
    Regards.
    MG

    Hi MG,
    First of all, what do you mean by "Both queries contains almost the same dimensions but different measures"
    "Almost" is not a good word in the IT world, especially when trying to merge/join tables. You need to be exact.
    That sounds like a possible reason for the problem.
    I am also not sure about your question:
    "How can I solve this issue without adding dimensions belonging to the hierarchies to queries."
    You may have to add those dimensions to the queries. Why would you not want to?
    Thanks

  • Merging two rpds in OBIEE 11g

    Hi,
    To merge two RPDs in OBIEE 11g, do I need to have both RPDs online? What are the pre requecities? Do I need to modify ini file to mention those RPDs.? Please tell me the steps involved.
    Thanks and Regards
    Santosh

    Q1. If I create a merge RPD from two independent sources, then, how does the physical layer is built?
    A1.
    Q2. Does it creates separete schema in physcial layer for tables from first source and another schema for other source?
    A2.
    Q3. If two separate schemas are built then does those schema pull data from there respective sources independently in Answer?
    A3.
    Q4. If it create combined schema, then hw is data fetched in Answer? Is there any link created by default or developer need to create a common link between two schemas?
    Hw will it fetch data from two seperate data sources during runtime?
    A4.
    Please provide some conceptual understanding for above queries.
    As far as I understand with my knowledge in OBIE, separate connection pool is required for each source when you create merged RPD since tables are from two separate DB sources even if being in the single rpd after merge. If my conceptual understanding is wrong then, guys, please help me to understand.
    Thanks
    Santosh

  • Merge Two Reports in Excel Analyzer - One To Many Relationship

    I want to show AR Data with CO Data - e.g. a bill + Income and it's associated account ssignemnt on the FI/CO side. I want the report to be able to show a cost centre manager to be wary of the income credited to their cost centre when a debtor has been raisedaas they may not fully pay the bill. To do this I'm using the standard DSO for AR & GL line items. Fisrtly I run a query on GL line item data where the user can select from a cost centre variable to look at credits to their cost centre. This produces a list of source documents that are used (via replacement path variable) in a second query. The second GL Query gives me a list of the allocation numbers for those line items. Had to be two queres as there are two GL postings that needed linking (this is because the cost assignment comes from a ceratin acc type and the debtor information comes from another acc type). I then use the allocation number (again via a replacement path variable) to run a third query on the AR data that shows all bills and payments linked to that allocation number. It seems to work ok.
    However I have put these three queries in an Excel Analyzer workbook. All the user does is enter a cost centre when running the report. Each query is on a different sheet but what I'd like to do is merge the data on to the one sheet. I want to see the bill and it's payments from the AR data matched up with the cost assignment from the FI/CO data. This would be a one to many relationship e.g. one bill may have it's cost assignment split over more than one cost centre. Is there anyway to do this using simple excel functionality?
    So I'd like to see:
    Allocation No. 12345 Bill £100 Revenue £50 Cost Centre 1234 £25
                                                                           Cost Centre 2345 £25
    Anybody any ideas?

    Hi,
    Thanks for your answers.
    I did explore using infosets but really struggled with joining the data and getting a sensible output. For example where a bill was done in installments I was getting 12 lines (e.g. one for each month) rather than just one. THis meant it looked liked they'd paid twleve times as much etc.
    I did also try report to report. Which to my mind worked ok. Except the customer wanted it all in "One" report rather than having to drill down to see data.
    I'll try a multiprovider but I'm worried that I'll end up with similar issues as I had when using the infoset.
    Cheers
    Joel

  • Please help me to merge two places.sqlite to get my old and New history at the same time, every time i rename my two places.sqlite to see my old and new history

    every time i rename my new places.sqlite to see my old history and come back rename old places.sqlite to see my new history, i tired and i found No Way to merge two places.sqlite :( but it's must be found this way for The PPL to see their old and new history :(
    Thank You all in Advance

    You can't merge history otherwise then using Sync to store the history and bookmarks of one places.sqlite on the Sync server and then disconnect.<br />
    Copy the second places.sqlite file to your Firefox profile folder with Firefox closed.
    Then setup Sync once again using that account and merge the content on the Sync server with your computer.
    * Merge this device's data with my Sync data

  • TS3988 how do i merge two icloud accounts - on on my computer and ipad, a different icloud id on my iphone?

    How do I merge two iCloud accounts into one?  One account is on my mac and ipad, the other on my iphone.

    Welcome to the Apple community.
    You cannot merge accounts, you will need to choose one and use it.

  • How do I merge two valid, purchased iTunes accounts into one so all my music is in same account?

    I have two valid, purchasd iTunes accounts.  Older iPod has some great music, I just got a new iPad and set up second iTunes account, bought some more iTunes items for that account.  Just discovered iCloud.  Now I want to put all my music from both accounts onto the cloud so I can access it on all my apple devices.   Can't seem to add from one account to the other.  Can sign onto the new account wiht my old iPod, but it will not let me sync without erasing all the music on the device.  How can I merge these two accounts into one?
    PS  Makes you wonder how helpful support is when the usernames "Frustrated," "really frustrated," and "Help!!!!" are all taken...lol

    HeyStupid wrote:
    how do I merge two valid, purchased iTunes accounts into one so all my music is in same account?
    You cannot. iTunes pruchases remian tied to the account they were purchased with.
    I just got a new iPad and set up second iTunes account,
    Why?
    Remove your info from new account, update old account as needed and use that.

  • How can I merge two iPhoto libraries stored on an external drive?

    Today I was in a One-to-One at the Apple Store.  I want to merge two i-Photo libraries into one.  One library is on my Air, the other is on my iMac.  Both libraries have been moved to an external drive.  When I try to move one onto the other, they won't go.  Yet I am certain that while at the store today, the person there started to merge them.  He did not need the library manager software in order to do so.  We stopped the merge in order to move on to another issue.  Now that I am home, I can't get the libraries to merge.  What am I failing to do?  Thanks!

    The only way to merge two libraryies is to use  iPhoto Library Manager which will merge two libraries and keep keywords, titles, faces, places, and other metadata intact.
    If one imports one library into another library every image file in the first library, originals, thumbnails, face files, etc,  get imported as an original photo. You will end up with a total mess.  DO NOT IMPORT ONE LIBRARY INTO ANOTHER LIBRARY!
    If you have backup copies of your libraries get them back and use iPhoto Library Manager to merge them.
    OT

  • Can you merge two user accounts on macbook? my wife has created a user on her new macbook , then inadvertently created a second one when using the migration tool. 1st ac has her office 365 install, yet 2nd has her itunes database, docs and contacts.

    Can you merge two user accounts on a macbook? my wife has created a new user on her new macbook air then, inadvertently, created a second one when using the migration tool. 1st a/c has her office 365 install, while 2nd has her itunes database, docs and contacts. What is the best way forward to get everything into the one account? Not sure if the office 365 will allow another installation into the second account, otherwise would just do that and delete the first, if that is possible?

    There is no merge but you can move data from one account to another via the Shared folder. Data is copied from Shared. Watch your free space when copying. These are large files.  Do one at a time if you are on a small drive. After making copy, delete from other users before you start next copy.
    Office365 installs in the main Applications folder and is available for all users on the computer. Activation is tied to the drive not the User.

  • Whats the difference between these two queries ? - for tuning purpose

    Whats the difference between these two queries ?
    I have huge amount of data for each table. its takeing such a long time (>5-6hrs).
    here whice one is fast / do we have any other option there apart from listed here....
    QUERY 1: 
      SELECT  --<< USING INDEX >>
          field1, field2, field3, sum( case when field4 in (1,2) then 1 when field4 in (3,4) then -1 else 0 end)
        FROM
          tab1 inner join tab2 on condition1 inner join tab3 on condition2 inner join tab4 on conditon3
        WHERE
         condition4..10 and
        GROUP BY
          field1, field2,field3
        HAVING
          sum( case when field4 in (1,2) then 1 when field4 in (3,4) then -1 else 0 end) <> 0;
    QUERY 2:
       SELECT  --<< USING INDEX >>
          field1, field2, field3, sum( decode(field4, 1, 1, 2, 1, 3, -1, 4, -1 ,0))
        FROM
          tab1, tab2, tab3, tab4
        WHERE
         condition1 and
         condition2 and
         condition3 and
         condition4..10
        GROUP BY
          field1, field2,field3
        HAVING
          sum( decode(field4, 1, 1, 2, 1, 3, -1, 4, -1 ,0)) <> 0;
    [pre]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    My feeling here is that simply changing join syntax and case vs decode issues is not going to give any significant improvement in performance, and as Tubby points out, there is not a lot to go on. I think you are going to have to investigate things along the line of parallel query and index vs full table scans as well any number of performance tuning methods before you will see any significant gains. I would start with the Performance Manual as a start and then follow that up with the hard yards of query plans and stats.
    Alternatively, you could just set the gofast parameter to TRUE and everything will be all right.
    Andre

  • How do I merge two different libraries, linked to one account? They're on two different computers but I want them to be on one

    How do I merge two different libraries, linked to one account? They're on two different computers but I want them to be on one

    This should do the trick
    Home Sharing Learn More
    http://support.apple.com/kb/HT201976
    Best of Luck

  • HT204053 Can you merge two Apple IDs into one?  It was a common issue caused by the iPhone 4 and has been corrected, but transferring purchases in iTunes is a pain with two IDs.

    For a brief time when iPhone 4, iOS 4 came out, many of us ended up with two Apple IDs.  I want to know if anyone can please provide me with simple step-by-step instructions on merging two Apple IDs into one?  It would make transferring purchases on iTunes much easier and reliable.  I realize i desperately need an iOS update, but i had no laptop for over two years!  I'm posting a cry for help on that in another community.  Thank you all.

    No,
    Apple IDs cannot be merged. You should use your preferred Apple ID from now on, but you can still access your purchased items such as music, movies, or software using your other Apple IDs.

  • Can i merge two apple ids into one

    i have two apple IDs that i need to merge.  money is both accouts, paid apps on both accounts.
    What are my options?
    thanks

    http://appletoolbox.com/2012/10/can-i-merge-two-or-more-apple-ids-using-multiple -apple-ids/
     Cheers, Tom

Maybe you are looking for

  • How to do this in Query Generator?

    Dear Experts, Please check my following query I've written in Query Generator. /* select from dbo.OCRD t0 */ declare @BP nvarchar(20) set @BP=/* t0.CardName */ '[%0]' declare @Dt1 datetime declare @Dt2 datetime set @Dt1=/* Start Date */ [%1] set Dt2=

  • Visual Studio 6 versus Java Advantages?

    Hi, I am very new on Java and SWING. Can anyone tell me what (if any) advantages to switching from GUI developement with Visual C++ Version 6.0 OR .NET Studio to rewriting the project in JAVA SWING. I know that 1 disadvantage would be the slowness fr

  • How can I change the search engine in the new tab page?

    When I click on the + to open a new tab, I get the new tab page, which displays the previous sites I visit the most. No problem. But in the search window, if I do a search it uses Yahoo. I don't like Yahoo. I would like to change to another search en

  • JEXP Not Triggering

    Dear Experts, I have maintained JEXP with combination of country/plant/control code, but the value is not triggering in my sales order. In analysis of price i see control code has an error.chapter ID and combination of chapter id and material is prop

  • Exit Code: 34 for windows7

    -------------------------------------- Summary -------------------------------------- - 1 fatal error(s), 0 error(s), 0 warning(s) FATAL: Payload '{3F023875-4A52-4605-9DB6-A88D4A813E8D} Camera Profiles Installer 6.0.70.0' information not found in Med