Add and Integrate  3 fields to a Pivot query

I kept asking questions about this query even after I had marked the thread as answered, and may not have been fair to those who assisted.
Given the following code, how do I add 2 calculated fields:
1. Sales - SalesLy as Difference
2. ((Sales - SalesLY) / SalesLY) * 100 as SalesPercent
This percent should be 2 digits after decimal point and also check for divide by zero.
select oracle_key  
-- "pivot" results
,      max(businessdate)   businessdate
,      max(sales)          sales
,      max(sales_ly)       sales_ly
,      max(guests)         guests
,      max(guests_ly)      guests_ly
,      max(dollarvariance) dollarvariance
,      max(wtd_sales)      wtd_sales
,      max(wtd_sales_ly)   wtd_sales_ly
,      max(wtd_guests)     wtd_guests
,      max(wtd_guests_ly)  wtd_guests_ly
from ( with -- just touching both tables once instead of four times here (subquery factoring)
           totals as ( select d.busi_date
                       ,      d.oracle_key
                       ,      s.storeid
                       ,      d.field1
                       ,      d.field2
                       ,      d.field3
                       -- ,      d.field4 (Cars) doesnt't seem to be part of your final result set?
                       from   mydailytotals d
                       ,      mystores s
                       where  d.oracle_key = s.storeid(+) --<< forgive my 'old school' outer-join ;)
       select oracle_key  
--       ,      storeid
--       ,      row_number() over (partition by oracle_key order by busi_date desc) rn      
       -- note: assuming dates are/will become parameters/bind variables in real code...
       -- businesdate
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then busi_date
              end as businessdate
       -- sales             
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then field1
              end as sales
       -- saleslastyear             
       ,      case
                when busi_date = to_date('6/5/2008' , 'mm/dd/yyyy')
                then field1
              end as sales_ly
       -- guestcount             
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then field2
              end as guests
       -- guestcountlastyear             
       ,      case      
                when busi_date = to_date('6/5/2008', 'mm/dd/yyyy')
                then field2              
              end as guests_ly
       -- dollarvariance
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then field3
              end as dollarvariance
       -- weektodatesales             
       ,      sum( case
                     when busi_date between to_date('6/1/2009', 'mm/dd/yyyy') and to_date('6/4/2009', 'mm/dd/yyyy')
                     then field1 else 0
                   end
              as wtd_sales
       -- weektodatesaleslastyear             
       ,      sum( case
                     when busi_date between to_date('6/2/2008', 'mm/dd/yyyy') and to_date('6/5/2008', 'mm/dd/yyyy')
                     then field1 else 0
                   end
              ) as wtd_sales_ly
       -- weektodateguestcount             
       ,      sum( case
                     when busi_date between to_date('6/1/2009', 'mm/dd/yyyy') and to_date('6/4/2009', 'mm/dd/yyyy')
                     then field2 else 0  
                   end
              ) as wtd_guests
       -- weektodateguestcountlastyear             
       ,      sum( case
                     when busi_date between to_date('6/2/2008', 'mm/dd/yyyy') and to_date('6/5/2008', 'mm/dd/yyyy')
                    then field2 else 0
                   end
              ) as wtd_guests_ly
       from totals
       group by oracle_key
       ,        busi_date      
       ,        field1
       ,        field2
       ,        field3      
group by  oracle_key
order  by oracle_keySee this post for background:
Complex Inner Join and subquery on outer where clause on inner join?
Another question regarding the same. The following query successfully eliminates duplicates in the DollarVariance field.
How do I integrate into previous query.
Select StoreId,
           DollarVariance
    from   (select store.storeid,
                    case
                          when inv.fk_str_main_id is null then 'None'
                          when inv.is_posted = 0 then 'NP'
                          else chr(39)||inv.total_dol_var||chr(39)
                     end as DollarVariance ,
               row_number() over (partition by store.storeid order by inv.is_posted desc) rn 
               from  valenti_wendys.inv_main inv
               right outer join valenti_wendys.str_main_mapped_to_storedb store
                              on store.storeid = inv.fk_str_main_id
                              and FK_LK_CNT_FREQ_ID = 1
                              and busi_date = PrevDay)
      where rn = 1;If more information needed, refer to this post:
URLhttp://forums.oracle.com/forums/thread.jspa?threadID=912671&tstart=60 [URL]
Thanks in advance

Yeah, I know, I'm a bit later than planned, about to log off, but I know ;)
How about:
select oracle_key  
-- "pivot" results
,      max(businessdate)   businessdate
,      max(sales)          sales
,      max(sales_ly)       sales_ly
,      max(sales) - max(sales_ly) difference
,      round(((max(sales) - max(sales_ly))/max(sales_ly)*100), 2) salespercent
,      max(guests)         guests
,      max(guests_ly)      guests_ly
,      max(dollarvariance) dollarvariance
,      max(wtd_sales)      wtd_sales
,      max(wtd_sales_ly)   wtd_sales_ly
,      max(wtd_guests)     wtd_guests
,      max(wtd_guests_ly)  wtd_guests_ly
from ( with -- just touching both tables once instead of four times here (subquery factoring)
           totals as ( select d.busi_date
                       ,      d.oracle_key
                       ,      s.storeid
                       ,      d.field1
                       ,      d.field2
                       ,      d.field3
                       -- ,      d.field4 (Cars) doesnt't seem to be part of your final result set?
                       from   mydailytotals d
                       ,      mystores s
                       where  d.oracle_key = s.storeid(+) --<< forgive my 'old school' outer-join ;)
       select oracle_key  
--       ,      storeid
--       ,      row_number() over (partition by oracle_key order by busi_date desc) rn      
       -- note: assuming dates are/will become parameters/bind variables in real code...
       -- businesdate
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then busi_date
              end as businessdate
       -- sales             
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then field1
              end as sales
       -- saleslastyear             
       ,      case
                when busi_date = to_date('6/5/2008' , 'mm/dd/yyyy')
                then field1
              end as sales_ly
       -- guestcount             
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then field2
              end as guests
       -- guestcountlastyear             
       ,      case      
                when busi_date = to_date('6/5/2008', 'mm/dd/yyyy')
                then field2              
              end as guests_ly
       -- dollarvariance
       ,      case
                when busi_date = to_date('6/4/2009', 'mm/dd/yyyy')
                then field3
              end as dollarvariance
       -- weektodatesales             
       ,      sum( case
                     when busi_date between to_date('6/1/2009', 'mm/dd/yyyy') and to_date('6/4/2009', 'mm/dd/yyyy')
                     then field1 else 0
                   end
              as wtd_sales
       -- weektodatesaleslastyear             
       ,      sum( case
                     when busi_date between to_date('6/2/2008', 'mm/dd/yyyy') and to_date('6/5/2008', 'mm/dd/yyyy')
                     then field1 else 0
                   end
              ) as wtd_sales_ly
       -- weektodateguestcount             
       ,      sum( case
                     when busi_date between to_date('6/1/2009', 'mm/dd/yyyy') and to_date('6/4/2009', 'mm/dd/yyyy')
                     then field2 else 0  
                   end
              ) as wtd_guests
       -- weektodateguestcountlastyear             
       ,      sum( case
                     when busi_date between to_date('6/2/2008', 'mm/dd/yyyy') and to_date('6/5/2008', 'mm/dd/yyyy')
                    then field2 else 0
                   end
              ) as wtd_guests_ly
       from totals
       group by oracle_key
       ,        busi_date      
       ,        field1
       ,        field2
       ,        field3      
group by  oracle_key
order  by oracle_key
notez bien
+(still) using the data from your original post here+
Is it not possible, that since you have the fields available in the inner query, you could avoid the need for another? Querying a result set containing all the data you need is less resource intensive than requerying the same data/tables over and over.
A common way to create reports is to base it on a (smart/dedicated) view, the view would deliver the required output already JAPYR.
I have a program that produces report already, but in the business logic.Please elaborate on that, so we can understand better what's goin' on.
Another question regarding the same. The following query successfully eliminates duplicates in the DollarVariance field.
How do I integrate into previous query. I'll get back on that one tomorrow
Edited by: hoek on Jun 21, 2009 11:06 PM

Similar Messages

  • How to add and hide more fields in PPM?

    Hello,
    Im new to this tool and I would like to know how to add and/or hide customized fields. I've only know that Its somewhere under SPRO > Global Enhancements to Project Elements but Im a little bit lost there. Can somebody explain to me with details how to do this and also, does this need a specific access of developer to do this customization?
    As this is a basic need for all, anybody that is new on the tool and search for this will have a good reference on this thread.
    Thanks in advance.
    Many regards!

    The path is SPRO > Global Enhancements to Project Elements > Setup field control.Herein,once you select the field control you may proceed towards defining the conditions that influence the same. A step by step approach is explained in the below article  that will aid you to change properties of custom fields.
    http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/3053cee2-4bca-2d10-38ae-efff39ad248e
    Regards,
    Pradeepkumar Haragoldavar

  • Add $ and % to fields in ALV report

    Hi,
    How to add $ and % sybmbols to field values in ALV report. I cannot use a Char as I need to do totals and subtotals for those fields.
    Best Regards
    Suresh

    Hi,
    You cannot use % or $ or any special characters if you want to use the standard SAP totaling ans subtotaling.. you need to create another field adjacent to that field and show the currency or %.
    Otherwise you need to do the totaling logic yourself and then you can use $ or %
    Cheers:)
    Kothand

  • How to add a new metadata field to iPhoto where new field is calculated as age in years and month based on a specific date and the date photo was taken ? I want to calculate and display the age of my two kids on every photo.

    Hi
    How can I add 2 new metadata-fields to every photo in iPhoto ?
    The new fields should state the age of my kids in years and months based on the date that they were born and the date that photo is taken.
    Exampel:
    My son is born 01.01.2010
    My daughter is born 01.01.2012
    Photo taken by data
    Aage of son
    Aage of daughter
    01.07.2011
    1 year 6 month
    not born yet
    01.01.2014
    4 year 0 month
    2 year 0 month
    I would like to be able to search by kids age and get the info displayed when doing slideshows.
    How to do this in iPhoto ?
    Any alternatives to accomplish the same ?
    Kind regards

    It can't be done with iPhoto.  There are some DAM (digital asset management) applications that can write to other IPTC fields that iPhoto can't read. One such app is Media Pro 1.
    However you would have to calculate the age for each date and add it to one of the fields. There are online age calculators that can do that for you: Age Calculators
    If you go thru that much trouble then use iPhoto, make the calculations and add the age to the Description field.  Then you can use Smart Albums to search for 1year 6 month text.
    OT

  • How do I add and remove text in a text field with a checkbox?

    How do I add and/or remove text in a text field with a checkbox?
    this is my script in the text box......
    event.value="Hello, this is my narrative. \r\n\nFamily: \r\n\n" + this get.Field("FirstName").value + " " + this.getField("LastName").value + was born on"=this.getField("DOB.value + "\r\n\n + this.getField("drpField").value + "\r\n\n" + this getField("Father").value
    The text box looks like this...
    Hello, this is my narrative.
    FAMILY:
    John Smith was born on 08/02/2000
    Boby Lou
    Jack Smith
    I need to add/or remove the father field (Jack Smith) & r/n/n with a check box.
    Does anyone know how to do this?

    There are multiple errors in your code...
    Use this code instead (adjust the name of the check-box):
    var msg = "Hello, this is my narrative. \r\n\nFamily: \r\n\n" + this get.Field("FirstName").value + " " + this.getField("LastName").value + was born on" + this.getField("DOB.value) + "\r\n\n" + this.getField("drpField").value;
    if (this.getField("FatherCheckBox").value!="Off")
         msg += "\r\n\n" + this getField("Father").value;
    event.value = msg;

  • How do I add a yahoo search field to my toolbar so I can always see it?  It used to be there until a month ago and now is gone.

    how do I add a yahoo search field to my toolbar so I can always see it?  It used to be there until a month ago and now is gone.

    If you either updated to Mountain Lion or updated to Safari 6 under Lion then there is a unified URL address bar with the search. I personally don't like it either.

  • The recent upgrade for i-tunes prevents me from adding and using the Field, "Show". How can I add this field in i-tunes?

    The recent upgrade for i-tunes prevents me from adding and using the Field, "Show". How can I add this field in i-tunes?

    Hold down shift as you right-click > Get Info to get the old style dialog box.
    tt2

  • How to ADD reference table and make a field as currency field in dictionary

    pls render some info on how to add refernce table and ref field if i want to make an added field as a currency or quantity field...

    Hi Kiran,
    It sounds like you are creating a "Z" table or structure and have defined a quantity (eg MENGE). But when you run the syntax check, the system is saying you need to define a reference table / field.
    Well when you are in SE11, click on the "Currency / Quantity Fields" tab. You will see 2 columns called "Reference Table" and "Reference Field". These 2 columns define the unit of measure for the currency / qty.
    If you have defined in your table MENGE and MEINS and the MEINS field is the unit of measure for the MENGE field you should define your fields as such (inthe Currency/Quantity Fields" tab:
    Table - ZVBAP
    MENGE MENGE_D QUAN ZVBAP MEINS
    MEINS MEINS   UNIT
    Hope this makes sense.
    Cheers,
    Pat.
    PS. Kindly assign Reward Points to the posts you find helpful.

  • Add new selection fields and ALV output  fields in VA05

    Hi,
    I want to add two new selection fields in VA05 'Further selection criteria' screen, also want to add some more fields in VA05 ALV output.
    Please help me how i can do it using user exit ?
    Thanks ,
    Archana

    hi,
    You have to copy the standard program SAPMV75A to ZSAPMV75A, change them accordingly.

  • Can I add a text form field in Photoshop that can be edited in Acrobat reader?

    I design coffee labels with Photoshop then send them off to the printers. The printers then open up a PDF and put in the best before date before printing. At the moment I have to add the text form fields using Acrobat Pro after i've saved the photoshop document as a Photoshop PDF. The problem is if I want to edit the labels at a later date, I have to re-add the text form fields with Acroabat again. 
    Can I add a text form field in Photoshop that can be edited in Acrobat reader? Is it better to use another program for this task?
    Thanks in advance
    Ian

    As A1's Calculate script, under "Simplified field notation", enter:
    A * 0.8
    The same goes for B1 and B, resp.

  • How to add search help for field in ALV object

    Hello,
    In a program, we use ALV object ( container) to create a liste like : field1, field2 .. but when display we do not have search help for this . Could you please help me how to add match code in this case for field 1 and field2, We use set_table_for_first_display
    Thanks,

    Hi,
    when you define your field catalogue you can create data elements with search help in se11 and use them for field 1 and field 2.
    But maybe it is enough to use data elements belonging to a domain with a value help and to set field F$AVAILABL in the field catalogue or to fill the name of the field CHECKTABLE.
    Regards,
    Klaus

  • Dunning Procedure and Accounting Clerk fields in FBL5N

    Hi Gurus
    Plaese help me to add the field Dunning Procedure and Accounting Clerk in the hidden field option for changing layout in FBL5N
    I have checked for the option of  Define Special fields for finding and sorting data  but it does not allow the fileds from table other than BSEG , BKPF.
    Thanks
    Jhanvi

    hi Jhanvi,
    Execute the FBL5N and create your own Layout and from there you select the fields of Dunning and Accounting Clerk and then save that Layout.
    If the above fields are not available in that option then go to Tcode O7R3 it is for the Special Fields of finding and sorting DATA.
    Click on New Entries and enter the Table name and select the field from the F4 slection and then save it.
    Once again go to Tcode Fbl5n and select your layout and now you can see that fields added to that.
    Hope this will solve your problem
    Cheers
    Ranjit Kumar

  • I need to add check whether 21 fields of a internal table are empty or not.

    Hello,
    I need to add check whether 21 fields of a internal table are empty or not.How can we write a code for the same wand what would be the correct syntax for it.
    I tried entering all the fields in the IF loop with AND condition but its giving syntax error.Perhaps this is because the lenght of the IF condition would be more than the allowed one.

    Hi,
    After the select quiery.
    If not itab is initial.
    Message 'Table is not empty'    type 'I'.
    Endif.
    Regards,
    Jagadish.

  • Parameter driven query and SSRS Dataset fields

    I have a Parameter in my SSRS Report, @PatientSelection. I use this Parameter within my SQL Stored Procedure to execute one version of the report and get specific columns, or another version of the report with different columns.
    -- @PatientSelection is set to 1 if the Business Report Requestor chooses "New Patients"
    IF @PatientSelection = 1...
    -- @PatientSelection is set to 2 if the Business Report Requestor chooses "Established Patients"
    IF @PatientSelection = 2
    When I do this, none of my fields are showing up within my SSRS Dataset.
    Am I fundamentally doing this the wrong way concerning SSRS and Report Creation???
    Any feedback is greatly appreciated. Thanks for your review and am hopeful for a response.
    PSULionRP

    You can add manually  the missing fields, drag and drop all needed fields to the tablix and see how was it  going. Doo you use one or two tables(based on the parameters) to display the report?
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Is there a way to simultaneously add text to Comments field for multiple files?

    Is there a way to simultaneously add text to Comments field for multiple files? I know I can use Option+Comman+i to bring up the Inspector window but it doesn't have a Comments field. For example, in iTunes you can select multiple files and change metadata for any field simultaneously and easily yet when it comes to the Comments field in the Finder, this seems to be missing.
    In our production environment, Comments field is important because we place notes about what drive the file originated then when it gets transferred to LTO-6 tape via LTFS, we can quickly trace it in the event we needed to recreate the hard drive. Because tape archiving is linear, we can't store all of the files on the same tape.
    We've tried using Apple's Automator but it too seems to not have a way to batch change text in the Comments field. Any help is appreciated.

    What is "order data"?

Maybe you are looking for

  • Nokia N8 Symbian Belle HTML EMAIL stopped working

    About a month ago my Hotmail e-mail stopped working properly, to fix it I deleted the mail box and reset it up however as a result of the delete and reset I now have lost HTML formatted e-mail. I am on the latest Nokia N8 Belle release for Orange. Th

  • Create return sales order

    Hi, I met some issue needs your help! I use BAPI to create return sales order, Return an error message "Unpermitted combination of business object bus2032 and sales doc.category H". Can anyone suggest me any solution for this problem? Thanks is advan

  • SQL Developer Extensions in Java

    Hallo, I am trying to find some tutorial/sample/example about extending the SQL Developer in Java. The documentation about the XML-Extension seems pretty good, but the only usefull [site |http://wiki.oracle.com/page/SQL+Dev+SDK+How+To+Create+a+Java+D

  • Steps to create substitution rule for field BSEG-RECID

    MIRO generates FI document. FI document has BSEG-RECID field. I need to apply substitution for BSEG-RECID depending on the value of the RSEG-GEBER field in MIRO. The substitution must be activated only if account to be booked is PL account in FI and

  • IDE NWDS Debug error - Webdynrpo NWDS 2.0.6

    Hello!: This is what I get: <b>At Memory Log view: </b> 11/09/2006 10:46:11 org.eclipse.debug.ui [Thread[main,5,main]] ERROR: STATUSLOG   !MESSAGE Error logged from Debug UI: org.eclipse.core.runtime.CoreException[113]: java.net.ConnectException: Con