Complex query design suggestions

Hi, I should first of all point out I'm new to pl/sql and struggling a bit so any help you can provide is really appreciated.
I have the requirements for a complex query the conditions of which are as follows
Four tables involved
ProductGroups, Products, Stock, StockMovements
Query requirements psudeocode (broken into bits to make things easier)
/* CODE */
Select distinct products
where productgroup.group between 'a' and 'z'
And stock.location = 'x'
And stock.qty > 0
For each product
     _myDate = currentdate - 1 year
     stkMvt= Select * from StockMovements where product = product
     If(select count(*) from stkMvt where code = 'A' and type = 'A' and source = 'A' and stkMvt.date > _myDate) > 0)
          -- exclude row / move next product
     End If
     If (select count(*) from stkMvt where code = 'B' and type = 'B' and source = 'B' and stkMvt.date> _myDate) > 0)
          -- exclude row / move next product
     End If
     If (select count(*) from stkMvt where code = 'C' and type = 'C' and source = 'C' and Qty > 0 and stkMvt.date> _myDate) > 0)
          -- exclude row / move next product
     End If
     If (select max(stkMvt.date) from stkMvt where code = 'D' and type = 'D' and source = 'D' < _myDate)
          If ( select max(stkMvt.date) from stkMvt where ((code = 'E' and type = 'E' and source 'E')
               Or (code = 'F' and type = 'F' and source = 'F')
               Or (code = 'G' and type = 'G' and source = 'G')) > _myDate )
               -- exclude row / move next product
          End If
     End If
     -- Get the fields to return
     Qty1 = Select coalesce(sum(qty),0) from stkMvt where code = 'H' and type = 'H' and source = 'H'
     Qty2 = Select coalesce(sum(qty),0) from stkMvt where code = 'I' and type = 'I' and source = 'I'
     Qty3 = Select coalesce(sum(qty),0) from stkMvt where code = 'J' and type = 'J' and source = 'J' and stkMvt.date between :fromDate and :toDate
     LastDate = Select max(stkMvt.Date) from stkMvt
     StkQty = Select Qty from Stock where product = product
     AllQty = Select AllQty from Stock where product = product
     FreeQty = StkQty - AllQty
     Value = Select value from Stock
     Select group, product , Qty1, Qty2, Qty3, LastDate, StkQty, AllQty, FreeQty, Value INTO table/cursor
Loop
Return table / cursor
/* CODE END */
Any guidance about the best way to achieve the above in an SP or Package would be massively helpful,
Thanks in advance
Scott.

Agreed, it is a mess and I was looking the other way when it was dumped into my lap to deal with unfortunately. Any pointers in the right direction would really be helping me out.
ok first the table structures, I'll include only the relevant fields to make things a bit easier.
CREATE TABLE "TEST"."CMGROUP"
(     "CMGRP_GROUP" CHAR(4 BYTE),
     "CMGRP_DESC" CHAR(30 BYTE)
CREATE TABLE "TEST"."CMPROD"
(     "CMP_PRODUCT" CHAR(20 BYTE),
     "CMP_DESC" CHAR(50 BYTE),
     "CMP_GROUP" CHAR(4 BYTE),
     "CMP_SPECIAL" CHAR(1 BYTE)
CREATE TABLE "TEST"."STOCK"
     "STK_PRODUCT" CHAR(20 BYTE),
     "STK_LOC" CHAR(5 BYTE),
     "STK_STKQTY" NUMBER,
     "STK_VALUE" NUMBER,
     "STK_ALLSTK" NUMBER
CREATE TABLE "TEST"."STMOVE"
     "STMOV_LOC" CHAR(5 BYTE),
     "STMOV_PRODUCT" CHAR(20 BYTE),
     "STMOV_DATE" DATE,
     "STMOV_TYPE" CHAR(1 BYTE),
     "STMOV_SOURCE" CHAR(1 BYTE),
     "STMOV_CODE" CHAR(1 BYTE),
     "STMOV_QTY" NUMBER,
     "STMOV_YEAR" NUMBER(5,0),
     "STMOV_PERIOD" NUMBER(5,0)
Relationships are as follows
CMGROUP.CMGRP = CMPROD.CMP_GROUP (One to Many)
CMPROD.CMP_PRODUCT = STOCK.STK_PRODUCT (One to Many)
STOCK.STK_LOC = STMOVE.STMOV_LOC (Many to Many)
STOCK.STK_PRODUCT = STMOVE.STMOV_PRODUCT (Many to Many)
As for the requirements well they aren't straightforward but the gist of it is to return a ref cursor to a crystal report from an oracle package. The ref cursor is to contain the following
select products that are
1) In stock
2) For a specific location
3) In a range of product groups
4) That either have a manual stock receipt movement within the last 12 months
5) Or do not have the following stock movement within the last 12 months
     a. PO Receipt
     b. WO Receipt
     c. Stock Acquisition Receipt
Of the results I need to
     1) Display the group
     2) Display the product
     3) Sum the Qty of WO Receipts from the 1st of the month (13 Months Ago) until today
     4) Sum the Qty of PO Receipts from the 1st of the month (13 Months Ago) until today
     5) Sum the Qty of Stock Transfers from the 1st of the month (13 Months Ago) until today
     6) Display the most recent stock movement date
     7) Display the current stock quantity
     8) Display the allocated stock quantity
     9) Display the available stock quantity
     10) Display the stock value
Movement code definitions as follows     
Manual Stock Receipt
Stmov_type: O
Stmov_source: J
Stmov_code: G
Purchase Order Goods Receipt
Stmov_type: O
Stmov_source: R
Stmov_code: S
Stock Requisition Receipt (Positive stmov_qty value)
Stmov_type: G
Stmov_source: J
Stmov_code: L
Stock Requisition Issue (Negative stmov_qty value)
Stmov_type: G
Stmov_source: J
Stmov_code: L
Manual Stock Issue to GL Account (Issue to workshop)
Stmov_type: F
Stmov_source: J
Stmov_code: G
Works order issue
Stmov_type: F
Stmov_source: W
Stmov_code: G
Works order receipt
Stmov_type: 1
Stmov_source: W
Stmov_code: G
There are a few other conditions and requirements but I think that covers the basics. If there is anything I've left out let me know and I do my best to provide.
Thanks in advance
Scott.

Similar Messages

  • BEx query design: defining complex filters?

    Is it possible to define complex filters in a query definition? By complex I mean using logical operators (AND, OR, NOT)
    Example: report giving sales results
    I want to have
    (Customers from Germany <b> AND </b> product type A)
    <b> OR </b>
    (Customers from Romania <b> AND </b> product type B)
    If I define a standard filter:
    Customer country IN (DE,RO)
    Product type IN (A,B)
    I will obtain all combinations while I only want to have the couples {(DE,A),(RO,B)}.
    Thxs.
    LauQ

    Hide is just to remove the "object" from the display. It is used as an intermediate filter for the object that includes the logical combinations that one would like to get in the results.
    In my example:
    In the Query Designer:
    1. Define a structure (rows)
    2. right-click on it and make new selections corresponding with
    a) Country=Germany (:='DE')
    b) Country=Romania (:='RO')
    c) Material=A
    d) Material=B
    3. Right-click on each of them and "hide" them
    4. New formula in the structure:
    TEST:=(( 'DE' AND 'A' ) OR ( 'RO' AND 'B' ) )
    5. You need to edit the Cell definition and make use of the Help Cells (select therein the KF you need to display; e.g. "QUANTITY SOLD").
    6. "Play" with Cell definition and define a final KF such as:
    FINAL:='QUANTITY SOLD' * 'HELP'
    You need to try by yourself...
    Regards
    LauQ

  • Creating a complex query in CR designer

    I'm running Crystal Reports XI Release 2 on Windows NT. I have a very complex query and cannot figure out how to make it work in CR. Basically, I've got a table A with fields containing reference codes that may or may not be present. I created a SQL query that imbeds select statements in the select fields (the field names between SELECT and FROM). So, it would read something like SELECT field1, field2, (select Code_Name from REF_Table where REF_Table.code=TableA.code and TableA.code is not null) FROM TableA. That works great in SQL Developer.
    In CR, I tried a command field but that produces a cartesian set where I get a line for every record that has the field for each and every record in TableA.  No good. I just want the name for the value in the TableA record translated to the name if it is present.  Is there a way to do this in CR? I used to be able to edit the SQL directly in CR but now it won't let me do that. Anyone know a better way to solve this kind of problem?

    I'm not an Oracle guy so I don't know what you can or can't do in PL SQL.
    I'm surprised that you are getting different results between SQL Developer and the CR Command. CR should pass the query, exactly as it's written back to the server, same as SQL Dev.
    IN T-SQL...If  you want to make sure that the sub-query is returning only 1 row, try placing "TOP 1" behind the SELECT.
    (select top 1 loc.location_name
    from ref_location loc
    where loc.location_num=obd.location_num and odb.location_num is not null)
    I'm thinking that Oracle doesn't have the the TOP N option but it does have a rownum feature. So maybe...
    (SELECT x.location_name
    FROM (
        select loc.location_name
        from ref_location loc
        where loc.location_num=obd.location_num and odb.location_num is not null) x
    WHERE rownum = 1
    ORDER BY rownum)
    This is assuming that Oracle allows you to use ORDER BY in a sub-query. T-SQL only allows it if it's used in conjunction with TOP N...
    Just a thought,
    Jason

  • Exceptional Aggregation Option at Query Level in BW 3.5 Query Designer

    Hi all,
    I want to use Exceptional aggregation with reference char 0CALDAY,on Formula keyfigure in the Query designer(BW3.5).
    But I am not getting that option when I went into Properties of that Keyfigure.So please Suggest  me where I will Find that Exceptional Aggregation option & Ref.Char at the Query level.
    Thanks,
    Kiran Manyam

    Hi
    In the Key Figure Properties dialog box, choose Enhance >>. The Aggregation Behavior field is added to the dialog box.
    You can make settings for the aggregation of the calculated key figure and the time that the calculated key figure is calculated here. Depending on the complexity of the formula, you can select various settings in the enhanced properties of the calculated key figure. The following types of calculated key figures with the corresponding formula complexity are available:
    http://help.sap.com/saphelp_nw04/helpdata/en/6f/56853c08c7aa11e10000000a11405a/frameset.htm
    Hope it helps

  • Results based on From and To value in Bex query designer.

    Dear all,
    We are using BI7.00. In one of our report which pertains to PM user wants to get the details of number of breakdowns which falls in the value range 0 to 1 hours.
    I created a condition which has 2 variables, one is From value and the other is for the To value (since user wants to enter both the limits). I have written a condition that if the breakdown hour is greater than or equal to variable1 and less than or equal to variable2.
    Using the above condition if the query is run, i am not getting the desired output. The values avaialble in the report are 0.65,0.78,1.30,7.38,0 etc., when i give 0 to 1 i should get the values 0.65,0.78 and 0 but instead report displays "No valid results".
    I changed the condition and selected "Between" variable 1 and variable 2 - yet no change in the query.
    To my knowledge i foresee that system is not able to recognize the decimal and hence provides details only based on the whole numbers.
    How to get my requirement achieved in the Bex query designer.
    Experts suggestion with steps will be very useful to solve my problem.
    Regards,
    M.M
    Edited by: Magesh Magesh on Dec 26, 2008 11:28 AM

    Hi Magesh,
    I think you could try one more way.
    Create a formula inserting your two formula variables for limits .(FV -  are ready for input ) .
    The formula would return the result as 1 or 0 because it would have boolean condition in it.
    You could then place a condition in the query designer to only show all records where the value of the formula key fig is 1.
    You formula A would look like
    ( KF > FV1) AND (KF < FV2) * 1.
    And in the condition put A EQ 1.
    You could scale the key figures as well and multiply the limits mentioned by the users in FV1 and FV2 if decimals are still not recognized by SAP.(scaling)
    Regards,
    Sunmit.

  • Need Help: Query Designer

    Hi experts,
    In Query designer, is it possible to create an IF statement for "IRRATIONAL NUMBERS"?
    Lets say 10.03, 1.26, 3.1 and so on...
    The statement would be as follow:
    If Val1==N.x then 0 else Val1.
    To summary, if the value is N.x (N for rational numbers 1,2,3,... and .x for decimal values like .23, .44, .01 and so on...),
    then the value N.x will be changed or converted into "0".
    Example: If the value of FieldA on the report is 10.3, it then should be converted into Zero "0".
    another is if value is 1.03, then the report should show 0 instead of 1.03
    I arrived to a solution wherein I have to create three logic expression.
    First is to convert first the Irrational Number into Zero "0".
    Second is to create new selection in KF wherein it will pull the data containing the converted "N.x to Zero".
    And third is to create the IF statement logic that will satisfy my expected value.
    I'm having this problem on the first one as I have no idea how to convert irrational number into zero "0" value.
    the second and the last part would be fine as my main problem here is how can I convert the irrational number in to zero?
    Any suggestions?

    Hi,
    Please try the below.. Hope this will solve your issue
    Eg. of data
    Dist. Channel  -
    Value
    Dom1----100.5
    exp1----200.4
    dom2---50
    exp2---75
    Expected out put
    Dist. Channel  -
    Value
    Dom1----0
    exp1----0
    dom2---50
    exp2---75
    Steps1
    Drag and drop Dist.Channel in row and value in colum
    create a formula1 using ur kf value 
          CEIL(value)    --- which will provide us integior part with out decimal
    Steps2
    create another fomula2
    using boolean operator compare the value
    (formula2==value)*value
    this formula compare our two values and will produce a boolean value ie 1 or 0
        :  Eg 100.5  ceil(100.5) = 100  Now in our formula1 we are comparing these two values are equal or not ie 100.5 - 100 so out come will be 0 then 0100.5 = 0 always if our condition is true then 1100 = 100
    Related Links :
    Boolean Operator
    http://help.sap.com/saphelp_nw04/helpdata/EN/71/0aec3b6e011341e10000000a114084/frameset.htm
    Thanks
    BVR

  • Error while trying to change or save as  query in query designer

    Hi ,
    When we are trying to change or do a save as to a query in the query designer,it is giving us an error stating"'An unexpected object variable or with block variable not set'error occured in wdbrlog 1 error(s) are logged".
    We have searched in SDN for the same error and found some OSS notes(Notes 571831,575434) should be implemnted but those notes seem old.We are using BI 7.0 version.
    Could anyone please suggest??
    Thanks,
    Bhagyarekha.

    What is your front end SP level ?

  • Query Designer restricted key figures using a prompt value

    Hallo Experts,
    I would like to restrict key figures after user has beeen prompted to enter a date. I have 2 key figures:
    a) KF1, Date1
    b) KF2, Date2
    User should be prompted to select a specific date: e.g. 01/2011. After selecting/entering the prompted date value, it should be given to date objects for intance Date1=01/2011 and Date2=01/2011.
    How could I achieve it in Query Designer without using customer_exit function? Reason is that non-experienced users will use this report and they should not be confronted with complex query. Do you have any idea?  
    Many thanks
    Mamadu

    Hi,
    I assume that you need to restrict the value of keyfigures by date based on user input data on variable selection screen.
    First create one user input variable on your date field say for eg calday.
    Now restrict your KF1 with calday and choose that vairable which you created.
    For second KF2 which seems to me need to be restricted with offset of +1 based on the date entered by the user.
    So restrict this KF2 with calday and use the same variable with offset of +1
    I just misunderstood you ned calmonth or day because in requirement you have given month but you are calling it as date.
    If its month then replace calday with calmonth..
    Hope it helps.
    Regards,
    AL:
    Edited by: AL1112 on Dec 21, 2011 9:42 AM

  • Can Power Users or Mgt Create and Design Reports Through Query Designer ?

    Dear BI consultants,
    Iam new to BI.
    My management has requested me to Train Power users from All Modules(FICO,HR,QM,PP,SD,MM) to Create Queries and Design Through Query Designer.
    My question is are we suppose to Give query Designer Access to Power users or Mgt so that they can create Queries and reports by themselves.Please Suggest me wht is the Best Practices.
    is it feasable to train power users and Mgt as to how 2 create queries. will there be any side effect on the System or our end.
    Plz suggest.
    wht are the advantages and disadvantages of this.
    If I train the Power users then is there any risk of my Job.
    since they will be creating all reports by themselves.
    wht else can a BI consultant do since iam in a support project.
    how do i convince my Mgt?
    Plz Suggest.
    Thanks Awaiting

    This is common procedure maintained in most of the company's for end users.
    There is no issue if you train the power users.you will be the first point of contact if they have any doubts.and more over you are supporting for your client right? this can't be handed over to power user or end user. data is imp for the client so there will not be any problem for you, if any issue comes while loading you need to take care of the errors and fix the.
    power users or end user will be familiar with Business requirement not with the Query building logic's not with the BW procedure so no worries for you.
    as vineeth said its better to keep control regarding the global and key things at your end so that you will not loose contol on the reports.

  • Grouping Free Characteristics in Query Designer?

    Hello,
    I have a couple reports that per busniess have many free characteristics. So many that that they do not fit on one page and you must continue scrolling the page down to view all. This makes the report look sloppy but the business owner wants all of these free characteristics. I'm looking for a way to clean up the list of free characteristics without removing any selections. Can they be grouped up or consolidated through Query Designer or WAD?
    Thank You,
    Alex

    Hi Alex,
    Actually use of free characteristic will not slow down the report performance rather free characteristics are used to enable the user for dynamic drilldown and to improve the query performance for initial output display.
    What you can suggest your users is to keep only required free characteristics and then remove the rest of the characteristics and from grouping perspective all the free characteristics in query designer output are shown alphabetically so you can not manipulate the group in designer.
    Regards,
    Durgesh.

  • MTD/YTD reporting design, query designer

    Hi BI folks,
    I have a requirement to build queries through BEx query designer that show MTD and YTD figures.  I also have multiple versions/value type built into the RKFs already.  I am wondering if there is a way to display YTD/MTD figures, without having to physically restrict them into the RKFs??, (i.e. I would like to have that time restriction sitting on top of the structure as a characteristic).
    I am trying to create more flexibility for the end-user, rather that having a huge amount of selections in the structure representing a particular version and time period.
    Is there some sort of hierarchy or variable definition that anyone can suggest where I can solve this need?
    Thanks.
    - BJ

    BJ,
    this depends on the environment that you are using, but it is fairly simple to do for MTD and YTD.
    as there are multiple variables available to be used for this, I consistently use two in the restriction (but you can play around and see what fits your needs the most).  For each structure, you should restrict on the following:
    MTD:
    Period Characteristic - Restrict to variable 0P_PER3
    Fiscal Year Characteristic - Restrict to variable 0P_FYEAR
    QTD:
    Period Characteristic - Restrict to Value range from period 00 to variable 0P_PER3
    Fiscal Year Characteristic - Restrict to variable 0P_FYEAR
    The reason you should have the Fiscal Year there is that you can use the text variable to set the column headers.
    Be careful that you use the right KF for this.  You need the periodic activity for the KF.  The reason Period 00 is included as that is where the balance from the prior year is carried over, so if you want to have the YTD values for the Balance Sheet, you would want to include that.  This scenario is the only option in BCS, and it doesn't have the Cumulative values.
    you can also use the cumulative values, but that depends on how the KF's are set up
    Pavel

  • Problem with Variable in Query Designer.

    hi all,
    we have a variable ZAMREG on characteristic AM Region, where AM region is attribute for two different master data objects mastobj1 and mastobj2, we have different infosets build on these two master data objects along with some other ODS.
    now this variable ZAMREG is visible in one of the infosets only in query designer, whereas it is not visible in the other infoset.
    what could be the problem??
    thanks,
    Rk

    Hi,
    Please check if that variable is created locally or globally. If it is a local variable in any query, it will not appear in any other query.
    In anyways, you can always create one more variable if the older one is not available. This will not make much of difference. I would suggest go ahead with creating a new variable.
    Regards,
    Yogesh

  • Variables defined in Query designer not affecting the web template

    Hello,
    I have some variables defined in a query, through Bex query  designer.
    The selection screen to enter the values for the variables pops up when excuted on the web or in excel .
    But the variable selection screen does not pop up when i execute a web template where in i defined with the same query.
    Please suggest me why it is happening so..

    In WAD check in the General Properties whether you have unchecked the Force Variable Screen On Checkbox
    Other thing to check is whether you have personalized the selection screen??? After Executing the Report go to selection screen by clicking on the Variable Screen option. To the left side of each variable you can see the personalization icon. right click on that and select delete personalization.
    Hope this helps.

  • Quantity is Showing Blank : Bex Analyzer, Source - Bex Query Designer

    Hello Expert SAP BI,
    I Extracting Data from Flat File,
    Till then Cube My Transaction data Loaded Perfectly  ,
    i want to create Report in Bex Analyzer, fetching fields from Bex Query Designer.
    Characteristics are showing in Rows , that showing in Report , but in Columns added Key Figure , its not showing in Report .
    Can any one Suggest , what will be issue and how i have resolve it.
    - Thank you.

    Datasource -> Transformation -> InfoSource
    Infosource -> Transformation -> DSO
    DSO -> Transformation -> Cube

  • In query designer, an hierarchy(structure)  duplicating lines in level 2.

    Here we are at version 7.0 - SP 10, I have created a structure hierarchy in BEX, and when I execute this query the result is ok ... until I tried to closed or open the second level.
    At first, it doesn't close, and the worst if I tried to open  it's
    duplicating the lines of the second level.
    In query designer, creating an hierarchy(structure) something like this:
    > total division 1    
    ...> division 1.1     
    ...> division 1.2     
    > total division 2    
    ...> division 2.1     
    ...> division 2.2     
    and after the execution, if I tried to close or open the level of division 2 the result will duplicates the lines division 2.1 and division  2.2. the result is like this:
    > total division 1    
    ...> division 1.1     
    ...> division 1.2     
    > total division 2    
    ...> division 2.1     
    ...> division 2.2
    <b>...> division 2.1     
    ...> division 2.2</b>
    The stange is in excel it works normaly.
    Best regards,
    Silvio Meurer

    Hi,
    An Heirarchy is always an addition of the below nodes. hence. WBS 1 = WBS 1.1 + WBS 1.2. since youre looking at durations. it will add up 7+9 instead of looking at earliest start and latest end dates.
    dont know if itll work but a suggestion would be to get the start date and end date for WBS 1 and then do the calculation for duration in a formula in query designer.
    regards,
    Dilanke

Maybe you are looking for