A sum and case query help

I need help please with the following query.
I have a table that contains a currency, voucher, start_date and a value.
Data in the tables looks like this:
GB, 31/05/2010, A, 100
GB, 31/05/2010, B, 250
GB, 31/05/2010, A, 72
GB, 12/12/2009, A, 1000
GB, 12/12/2009, B, 72
LX, 12/05/2010, A, 90
This is where it gets complicated, I need to show the total(SUM) value when the start_date < 60 days from sysdate and also when the start_date > 61 days AND only if the voucher = A and partioned by the currency.
So, in other words I need my results like this.
GB, 31/05/2010, A, 100, *172, 0*
GB, 31/05/2010, B, 250, *0, 0*
GB, 31/05/2010, A, 72, *172, 0*
GB, 12/12/2009, A, 1000, *0, 1000*
GB, 12/12/2009, B, 72, *0, 0*
LX, 12/05/2010, A, 90, *90, 0*
The bold columns are what I need, one called less_than and one more_than.
A big big thank you and any advice I appreciate.
S

Please make a habit of posting sample data we can work with right away by using CREATE TABLE and INSERT INTO statements, or use the WITH clause as shown below:
SQL> with t as ( -- generating sample data:
  2  select 'GB' currency, to_date('31/05/2010', 'dd/mm/yyyy') start_date, 'A' voucher, 100 value from dual union
  3  select 'GB', to_date('31/05/2010', 'dd/mm/yyyy'), 'B', 250 from dual union
  4  select 'GB', to_date('31/05/2010', 'dd/mm/yyyy'), 'A', 72 from dual union
  5  select 'GB', to_date('12/12/2009', 'dd/mm/yyyy'), 'A', 1000 from dual union
  6  select 'GB', to_date('12/12/2009', 'dd/mm/yyyy'), 'B', 72 from dual union
  7  select 'LX', to_date('12/05/2010', 'dd/mm/yyyy'), 'A', 90 from dual
  8  )
  9  --
10  -- actual query:
11  --
12  select currency
13  ,      start_date
14  ,      voucher
15  ,      value
16  ,      sum(case
17               when  trunc(sysdate-start_date)< 60
18               and  voucher = 'A'
19               then value
20               else 0
21             end) over (partition by currency, voucher, start_date) sum_val1
22  ,      sum(case
23               when trunc(sysdate-start_date)> 61
24               and  voucher = 'A'
25               then value
26               else 0
27             end) over (partition by currency, voucher, start_date) sum_val2
28  from   t;
CU START_DAT V      VALUE   SUM_VAL1   SUM_VAL2
GB 12-DEC-09 A       1000          0       1000
GB 31-MAY-10 A         72        172          0
GB 31-MAY-10 A        100        172          0
GB 12-DEC-09 B         72          0          0
GB 31-MAY-10 B        250          0          0
LX 12-MAY-10 A         90         90          0
6 rows selected.

Similar Messages

  • CASE and JOIN Query Help

    I have two Tables
    Table A
    ID                             VALUE           
    1                         Delhi                
    2                      Mumbai                
    3                      Bangalore
    Table B
    TYPE                        TYPE_A_ID                         TYPE_B_ID             
    A                      1                                 NA                   
    A                      1                                 NA                
    B                      NA                                2           
    A                      3                                 NA                  
    B                      NA                                    1                   
    A                      1                                 NA            I want to get the value from Table A, depending on the Type in Table B
    i.e., when the Type is A, the value in Type_A_ID should be looked up in Table A
    and when the Type is B, the value in Type_B_ID should be looked up in Table A.
    The result how i need is as follows:
    TYPE                        TYPE_A_ID                         TYPE_B_ID                 Value      
    A                         1                                 NA                     Delhi
    A                      1                                 NA                     Delhi
    B                      NA                                2                      Mumbai
    A                      3                                 NA                     Bangalore       
    B                      NA                            1                      Delhi       
    A                      1                                 NA                     Delhi

    Here you go...
    <pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>
    SQL&gt; DROP TABLE A;
    Table dropped.
    SQL&gt; CREATE TABLE A ( ID INTEGER, VALUE VARCHAR2(20));
    Table created.
    SQL&gt; INSERT INTO A VALUES(1,'Delhi');
    1 row created.
    SQL&gt; INSERT INTO A VALUES(2,'Mumbai');
    1 row created.
    SQL&gt; INSERT INTO A VALUES(3,'Bangalore');
    1 row created.
    SQL&gt; DROP TABLE B;
    Table dropped.
    SQL&gt; CREATE TABLE B(TYPE CHAR(1), TYPE_A_ID CHAR(2), TYPE_B_ID CHAR(2));
    Table created.
    SQL&gt; INSERT INTO B VALUES ('A','1','NA');
    1 row created.
    SQL&gt; INSERT INTO B VALUES ('A','1','NA');
    1 row created.
    SQL&gt; INSERT INTO B VALUES ('B','NA','2');
    1 row created.
    SQL&gt; INSERT INTO B VALUES ('A','3','NA');
    1 row created.
    SQL&gt; INSERT INTO B VALUES ('B','NA','1');
    1 row created.
    SQL&gt; INSERT INTO B VALUES ('A','1','NA');
    1 row created.
    SQL&gt; COMMIT;
    Commit complete.
    SQL&gt;
    SQL&gt; SELECT TYPE, TYPE_A_ID, TYPE_B_ID, VALUE
    2 FROM A, (SELECT TYPE, TYPE_A_ID, TYPE_B_ID, DECODE(TYPE_A_ID,'NA',TYPE_B_ID,TYPE_A_ID) ID FROM B) B
    3 WHERE A.ID = B.ID
    4 /
    T TY TY VALUE
    A 1 NA Delhi
    A 1 NA Delhi
    B NA 1 Delhi
    A 1 NA Delhi
    B NA 2 Mumbai
    A 3 NA Bangalore
    6 rows selected.
    </code></pre>
    Karthick
    http://www.karthickarp.blogspot.com/

  • Combine greatest, sum, and case when

    All,
    I have the following view
    Select table1.field1,
    greatest(sum(table1.field3) - case when table1.oneorzerocolumn=1 then table1.field2 else table1.field4 end,0) as NetAbove0
    from table1
    group by table1.field1;
    I seem to be having trouble figuring out the best way to accomplish this, though, as whenever I try to execute this query I get not a valid group by expression. How can I combine the greatest function and the aggregate sum function without creating a separate view?
    Thanks in advance.
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    CORE     11.2.0.2.0     Production
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production
    Edited by: 929933 on Mar 14, 2013 1:32 PM

    929933 wrote:
    @ sb92075 would you care to elaborate on exactly what was missing?If you would care to read the link sb92075 posted you will understand by yourself:
    What is particularly important in that FAQ is:
    6) Tables/Indexes
    7) Sample Data
    8) Expected Output
    9) Formatting with {noformat}{noformat} Tags
    You have posted an unformatted code and did not provide proper input.
    Coming to your problem the issue is quite clear. If you use a GROUP BY clause then all field which are not returned as aggregated function shall be included in the GROUP BY clause.
    And this has a logic.
    Your initial query (formatted in a decent way):SELECT table1.field1
    , GREATEST (SUM (table1.field3)
    - CASE
    WHEN table1.oneorzerocolumn = 1 THEN
    table1.field2
    ELSE table1.field4
    END, 0) AS netabove0
    FROM table1
    GROUP BY table1.field1;
    is not correct.
    The column used in the query table1.oneorzerocolumn, table1.field2 and table1.field4 are not part of the GROUP BY and not used in aggregation functions.
    So how could you determine the value, i.e. table1.oneorzerocolumn, if for the same value of table1.field1 you have different values of table1.oneorzerocolumn?
    You are aggregating by field1 so which value do you want to use for non aggregated columns?
    Everything will be easier to understand if you can post some sample data, explain the logic you want to apply and post the expected output for the sample data you have posted.
    Regards.
    Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Sum and group by using CAML query

    Is there any way to get the SUM of values with group by ID using CAML query?
    I have a custom list, in that I have ID and Value columns as below
          ID         Value1      Value2
          1             10             4
          2              5              3
          1             15             2
          3             20             1
    I want to get result:
         ID          Value1        Value2
         1             25                6
         2             5                  3
         3             20                1
    I used CAML query:
    <View>
    <Query>
    <Where>
    <IsNotNull>
    <FieldRef Name="ID" />
    </IsNotNull>
    </Where>
    <GroupBy Collapse="FALSE">
    <FieldRef Name="ID" />
    </GroupBy>
    </Query>
    <ViewFields>
    </ViewFields>
    <Aggregations Value="On">
    <FieldRef Name="Value1" Type="SUM" />
    <FieldRef Name="Value2" Type="SUM" />
    </Aggregations>
    </View>
    But
    this query returns all the records
    that satisfy the condition <Where>.
    I
    do not correctly?

    You need to work with current view based aggregation. Please check below threads for your reference.
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/dda5735a-fecf-403f-9495-1b63617d2fbf/question-on-a-caml-query?forum=sharepointdevelopmentlegacy
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/24e88d6a-ee15-4d81-a5fe-504c7bd14e46/how-to-sum-a-column-value-using-caml-query?forum=sharepointdevelopment
    Hope this helps.
    My Blog- http://www.sharepoint-journey.com|
    If a post answers your question, please click Mark As Answer on that post and Vote as Helpful
    I've seen these
    issues and articles and do them, but
    does not work. I use object mozhel
    Sharepoint (javascript).

  • Hi! Everyone, I have some problems with JOIN and Sub-query; Could you help me, Please?

    Dear Sir/Madam
    I'm a student who is interested in Oracle Database and
    I have some problems with JOIN and Sub-query.
    I hope so many of you could help me.
    if i use JOIN without sub-query, may it be faster or not?
    SELECT field1, field2 FROM tableA INNER JOIN tableB
    if i use JOIN with sub-query, may it be faster or not?
    SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
    Thanks in advance!

    Hi,
    fac30d8e-74d3-42aa-b643-e30a3780e00f wrote:
    Dear Sir/Madam
    I'm a student who is interested in Oracle Database and
    I have some problems with JOIN and Sub-query.
    I hope so many of you could help me.
    if i use JOIN without sub-query, may it be faster or not?
    SELECT field1, field2 FROM tableA INNER JOIN tableB
    if i use JOIN with sub-query, may it be faster or not?
    SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
    Thanks in advance!
    As the others have said, the execution plan will give you a better idea about which is faster.
    If you're trying to see how using (or not using) a sub-query affects performance, make the rest of the queries as similar as possible.  For example, include field3 in both queries, or ignore field3 in both queries.
    In this particular case, I guess the optimizer would do the same thing either way, but that's just a guess.  I can't see your execution plans.
    In general, simpler code is faster, and better in other ways, too.  In this case
    tableB
    is simpler than
    (SELECT field1, field2  FROM tableB)
    Why do you want a sub-query in this example?

  • Possibly a very basic question but I have set up a spread sheet in Numbers and have viewed help videos but I can't get it to give me sums for the columns I highlight.  Keeps coming up with the figure zero.  I have version 09 2.3(554). Can anyone help

    Possibly a very basic question but I am going nowhere without a solution.  I have set up a spreadsheet in Numbers but it won't give me sums for chosen columns. I  have viewed the run through videos and used the formula but nothing happens apart from giving a figure of zero or a red arrow.  The sum icon in the bottom left hand section of the window does not highlight or show any total as I understand from the video that you can drag whatever total is here onto the relevent position on your spreadsheet.  I have Numbers 09 version 2.3 (554). Can anyone advise. Thanks.

    Hi Sohojools,
    To sum a column, use a formula such as this one in Cell A6
    Type this in a cell below your data:
    =SUM(A2:A4) or whatever range of cells you want to sum. The easy way is to type:
    =SUM(
    and then drag or shift-click to select the range of cells. Close the formula with a final bracket ")".
    The sum icon in the bottom left hand section of the window does not highlight or show any total
    To see the sum (and other simple statistics) in the bottom left, select a range of cells. That tells Numbers which cells you are refering to.
    The videos are good, but the Numbers'09 User guide, and the Formulas and Functions User Guide are better. Download them from the Help Menu in Numbers.
    Regards,
    Ian.

  • CASE vs DECODE - CASE with SUM and All in Page Item is non aggregable

    Hi,
    I'm using Discoverer 9.0.4.
    After switching calculations from DECODE to CASE
    I found out that case gives a non aggregable result when using a Page Item and selecting <All>.
    The calculations
    (SUM x) / (SUM y)
    or
    (x SUM) / (y SUM)
    where x and y are variables, work fine with page item <All>.
    But for example:
    CASE WHEN 1=2 THEN 1 ELSE (SUM x) / (SUM y) END
    gives non-aggregable.
    The same code works with DECODE:
    DECODE(1,2,1,(SUM x) / (SUM y))
    and is aggregable.
    Does anyone know a reason or a way to make it work with CASE?
    Thanks,
    Joao Noronha
    P.S.: I wanted <= comparisons and CASE is the best in simplicity,
    but now I know I can do it with DECODE, still looking ok using LEAST instead of ABS of the difference.

    Hi there
    I think therefore you have answered your own question and determined that using CASE in aggregations is not a good idea. I only threw out the two CASE options as ideas not as solutions, just in case (pardon the pun) one of these worked in your situation.
    Your comment I must say that if it worked it would give a wrong result (the sum of the divisions is not the same as the division of the sums) may give the wrong answer in your case but may be correct in others. It just depends how the items in the folder have been set up. I agree though that SUM(x) / SUM(y) will more often than not give the right answer.
    This discussion about DECODE vs CASE has been going on ever since Oracle introduced CASE as a means of placating a younger breed of user who needed an IF..THEN...ELSE construct and could not get their minds around the intricacies of DECODE. The DECODE is a much more reliable function than CASE because it has been around for a long time allowing Oracle plenty of opportunity to iron the bugs out of it. If I get a chance I will always use a DECODE whenever aggregations are required. However, when no aggregations are in use then I'll use CASE, simply because it's easier for users to work with.
    Unfortunately, users need to work with aggregations and so I don't see any alternative to Plus users having to learn DECODE. Whenever I teach Plus I always teach the users both CASE and DECODE but point out that DECODE has fewer issues that CASE. Oh, and talking of issues, try getting the THEN and ELSE components to return a different datatype. CASE has a fit and will not compile.
    Best wishes and glad you got your issue solved - you did right?
    Regards
    Michael

  • Query that treats "accents insensitive" and "case insensitive"

    I need to do one query that treats "accents insensitive" and "case insensitive" if possible too.
    I need to have the query below ajusted to return both accent or not accent words:
    with t as
    ( select 'xxxELEIÇÕESyyy' text from dual union all
    select 'xxxELEIÇOESyyy' text from dual union all
    select 'xxxELEICÕESyyy' text from dual)
    select text from t
    where text like '%eleicoes%'; --> i need some filter that result true for both 3 lines

    SQL> with t as
      2  ( select 'xxxELEIÇÕESyyy' text from dual union all
      3  select 'xxxELEIÇOESyyy' text from dual union all
      4  select 'xxxeleiÇOESyyy' text from dual union all
      5  select 'xxxELEICÕESyyy' text from dual)
      6  select text from t
      7  where regexp_like(t.text, '(x|X){3}(ELEI|elei){1}([CcÇç]{1})([ÕOõo]{1})(ES|es){1}(y|Y){3}')
      8 
    SQL> /
    TEXT
    xxxELEIÇÕESyyy
    xxxELEIÇOESyyy
    xxxeleiÇOESyyy
    xxxELEICÕESyyy

  • How to query using 3 optional inputs and case insensitive using SQL?

    Hi Folks,
    I am having trouble with the following query:
    select *
    from t1
    where (1=1)
    and lower(fname) like lower ('%mary%')
    and lower(lname) like lower ('%smith%')
    and lower(status) like lower ('%%')
    I need all three statements in the where clause to be completely optional and case insensitve.
    if I just write the following:
    (1=1)
    and lower(fname) like lower ('%mary%')
    and lower(lname) like lower ('%%') <-- Need to ignore this line
    and lower(status) like lower ('%%') <-- need to ignore this line
    nothing is returned. How do I ignore the 2nd and third lines using SQL only. I know about the ask TOM Article using procedures, but I need to do this using SQL only
    thanks in advance
    Edited by: user2184537 on Oct 16, 2009 9:40 AM
    Edited by: user2184537 on Oct 16, 2009 10:10 AM

    Hi,
    Is this query generated dynamically? (That's the only reason I can see for saying "WHERE 1 = 1".)
    If so, test the parameters for NULL, and only add them if a value was given.
    Failing that, you can explicitly test for NULL parameters
    where   (     lower(fname)  = '%' || lower (:p_fname) || '%'
         OR     :p_fname     IS NULL
    and     (     lower(lname)  = '%' || lower (:p_lname) || '%'
         OR     :p_lname     IS NULL
    and     (     lower(status) = '%' || lower (:p_status) || '%'
         OR     :p_status    IS NULL
         )Did you really mean to have all those '%''s? '%' is a wildcard in LIKE operations, but not when using =.
    Perhaps you should be saying:
    where   (     lower(fname)  = lower (:p_fname)
         OR     :p_fname     IS NULL
    and     (     lower(lname)  = lower (:p_lname)
         OR     :p_lname     IS NULL
    and     (     lower(status) = lower (:p_status)
         OR     :p_status    IS NULL
         )You're already handling case-sensitivity, by using LOWER in all the comparisons.
    Unfortunately, you can't just say something like:
    WHERE   LOWER (fname) = LOWER (NVL (:p_fname, fname))because that would discartd rows where fname IS NULL when :p_name is also NULL.
    Edited by: Frank Kulash on Oct 16, 2009 12:54 PM

  • I have used Final Cut Pro 7 for about a year. Two weeks ago when I tried to open it, it opened for 15 seconds and then the icon disappeared. I have tried to reinstall and it didnt help. any one has similar case or can help?

    I have used Final Cut Pro 7 for about a year. Two weeks ago when I tried to open it, it opened for 15 seconds and then the icon disappeared. I have tried to reinstall and it didnt help. any one has similar case or can help?

    Please post your problem in the Final Cut Studio (Final Cut Pro 7) forum:
    https://discussions.apple.com/community/professional_applications/final_cut_stud io?view=discussions
    You'll get the help you need in that forum.

  • Sum but cases

    hi i'm using oracle 10g r2
    i have a data like this
    ITEM-------------LINETYPE--------------DEBIT------------------CREDIT
    A010-------------LIABILITY---------------0---------------------------3000
    A010--------------LIABILITY-------------0----------------------------2000
    A010--------------TAX----------------------0---------------------------200
    A010----------------CHARGE---------------5200-----------------------0I want to show my data like this
    ITEM-------------LINETYPE--------------DEBIT------------------CREDIT
    A010-------------LIABILITY---------------0---------------------------5000
    A010--------------TAX----------------------0---------------------------200
    A010----------------CHARGE---------------5200-----------------------0I WANT to sum but if linetype = 'LIABILITY' how to achieved this by query?

    If some of the linetypes other than 'LIABILITY' did have multiple rows for the same item, and you didn't want to combine them, then you could add a CASE expression to the end of GROUP BY clause, somthing like this:
    YES frank i want to like this
    in case which column i have to set primary key
    if my data like this
    ITEM---------LINE_TYPE-----------DR---------------CR
    B1---------------LIABILITY-----------0-----------------5000
    B1---------------LIABILITY-----------0------------------2000
    B1----------------TAX------------------0------------------1000
    B1---------------TAX------------------0---------------------800
    B1---------------CHARGE----------5000
    B1---------------CHARGE-----------3800i want like this
    ITEM---------LINE_TYPE-----------DR---------------CR
    B1---------------LIABILITY-----------0-----------------7000
    B1----------------TAX------------------0------------------1000
    B1---------------TAX------------------0---------------------800
    B1---------------CHARGE----------5000
    B1---------------CHARGE-----------3800ONLY sum in liability case
    please help
    Edited by: BluShadow on 17-Oct-2011 15:43
    fixed {noformat}{noformat} tags.  the word "code" has to be lower case in the tags.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Query Help Please

    Hi... having problems with a query.  Any assistance would be
    much appreciated.
    Two queries with identical columns: Villages_Query_1 and Villages_Query_2.
    Both have these columns: Village_ID, Village_Name, Player_ID.
    I need to find all records in Villages_Query_2 where the Village_ID's match but the Player_ID's have changed.
    Example Village_Query_1
    Village_ID
    Village_Name
    Player_ID
    1
    Houston
    1
    2
    Dallas
    2
    3
    Chicago
    3
    Example Village_Query_2
    Village_ID
    Village_Name
    Player_ID
    1
    Houston
    1
    2
    Phoenix
    4
    3
    Chicago
    3
    4
    New York
    5
    In this case, Village_ID = 2, has changed names (Dallas to Phoenix) and the Player_ID has changed (2 to 4).  In addition, a new record was added.
    The eventual output I need is to be able to report the following:
    Player 2 village "Dallas" was taken by Player 4 and renamed "Phoenix".
    New York is a new village owned by Player 5.
    How the heck do I do this??  I have been trying query after query... reading about query of queries and JOINS and and and... I am now completely confused.
    Help appreciated.
    Mark

    Well... firstly... you do not use MS Access for that volume of data.  Plain and simple.  MS Access is for DBs like "My CD collection".  It's a desktop application, and is not intended to be used other than as a desktop application.
    Part of the reason for it not being appropriate for the job is that it can't do things like bulk loading data, which is kinda what you're wanting to do here.  That aside, it's a single-user file-based DB which is simply not designed to work as the back-end for a web application (or any sort of serious application).
    Anyway, I would approach this by putting all the data from the CSV files into the DB as is.  Then on the DB run a query which gets all your changes.  You're really going to struggle with the suggestions here to use valueList() to generate a list that is then used for a NOT IN(#list here#), because you're likely to have a mighty long list there.  Even proper DBs like Oracle only allow 2000 entries in a list like that (SQL Server is about the same, from memory), so I doubt QoQ will allow even that.  The reason the DBs put limits on these things is that doing a WHERE IN (#list#) is a really poorly-performing process.
    If you've got all your data in the DB, then your query becomes pretty easy, and I'm sure even Access could cope with it.  it'd be something like this:
    SELECT VB.village_id, VB.village_name AS village_old_name, VB.player_id AS player_old_id,
    VU.village_id AS village_new_id, VU.village_name AS village_new_name, VU.player_id as player_new_id
    FROM villages_base VB
    RIGHT OUTER JOIN villages_updates VU
    ON VB.village_id = VU.village_id
    WHERE VB.village_name != VU.village_name
    (that's untested and I only gave it about 1min thought before typing it in, so don't quote me on that!)
    Where VILLAGE_BASE is your original data, and VILLAGE_UPDATES is the data that indicates the changes.  I'm kinda guessing that this is the sort of thing you want.  Note: the "new" villages will be the ones which have NULLs for the village_id, village_old_name and player_old_id.
    Getting all the data into the DB is going to be a matter of looping over the CSV file and doing an INSERT for each row.  And that will take as long as it takes, so you might need to get some control over your request timeouts.  However doing these inserts will take less time than all the QoQ logic suggested before, so you might be OK.  And the query should be quick.
    What happens to the data once the report is written?  Does the "updated" data become the "live" data?  If so, after you run your report you're gonna want to do something like a TRUNCATE on villages_base, and INSERT all the records from villages_update into it (then TRUNCATE villages_update, ready for the next time you need to run this process).  Although don't take my word for it here, as I'm guessing your requirement here ;-)
    Adam

  • SQL Query Help - Is this possible or impossible????

    Hi guys,
    I need help with an SQL query that I'm trying to develop. It's very easy to explain but when trying to implement it, I'm struggling to achieve the results that I want.....
    For example,
    I have 2 tables
    The first table is:
    1) COMPANY create table company (manufacturer varchar2(25),
                                                          date_established date,
                                                          location varchar2(25) );My sample test date is:
    insert into company values ('Ford', 1902, 'USA');
    insert into company values ('BMW', 1910, 'Germany');
    insert into company values ('Tata', 1922, 'India');The second table is:
    2) MODELS create table models (manufacturer varchar(25),
                                                 model varchar2(25),
                                                 price number(10),
                                                 year date,
                                                 current_production_status varchar2(1) ) ;My sample test data is:
    insert into models values ('Ford', 'Mondeo', 10000, 2010, 0);
    insert into models values ('Ford', 'Galaxy', 12000,   2008, 0);
    insert into models values ('Ford', 'Escort', 10000, 1992, 1);
    insert into models values ('BMW', '318', 17500, 2010, 0);
    insert into models values ('BMW', '535d', 32000,   2006, 0);
    insert into models values ('BMW', 'Z4', 10000, 1992, 0);
    insert into models values ('Tata', 'Safari', 4000, 1999, 0);
    insert into models values ('Tata', 'Sumo', 5500,   1996, 1);
    insert into models values ('Tata', 'Maruti', 3500, 1998, 0);And this is my query:
    SELECT
            com.manufacturer,
            com.date_established,
            com.location,
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
            mod.current_production_status
    FROM
           company com,
           models mod
    WHERE
          mod.manufacturer = com.manufacturer
          and com.manufacturer IN ('Ford', 'BMW', 'Tata')
          and mod.current_production_status IN (1,0)
    ORDER BY
            mod.current_production_status DESCWhat I want the query to output is this:
    com.manufacturer        com.date_established          com.location          mod.model          mod.price             mod.year     mod.current_production_status
    Ford               1902                    USA               Escort               10000               1992          1
    BMW               1910                    Germany               -               -               -          0
    Tata               1922                    India               Sumo               5500               1998          1If current_production_status is 1 it means this particular model has been discontinued
    If current_production_status is 0 it means the manufacturer does not have any discontinued models and all are in procuction.
    The rule is only one record per manufacturer is allowed to have a current_production_status of 1 (so only one model from the selection the manufactuer offers is allowed to be discontinued).
    So the query should output the one row where current_production_status is 1 for each manufacturer.
    If for a given manufacturer there are no discontinued models and all have a current_production_status of 0 then ouput a SINGLE row that only includes the data from the COMPANY table (as above). The rest of the columns from the MODELS table should be populated with a '-' (hyphen).
    My query as it is above will output all the records where current status is 1 or 0 like this
    com.manufacturer        com.date_established          com.location          mod.model          mod.price          mod.year     mod.current_production_status
    Ford               1902                    USA               Escort               10000               1992          1
    Tata               1922                    India               Sumo               5500               1998          1
    Ford               1902                    USA               -               -               -          0                    
    Ford               1902                    USA               -               -               -          0
    BMW               1910                    Germany               -               -               -          0
    BMW               1910                    Germany               -               -               -          0
    BMW               1910                    Germany               -               -               -          0
    Tata               1922                    India               -               -               -          0
    Tata               1922                    India               -               -               -          0However this is not what I want.
    Any ideas how I can achieve the result I need?
    Thanks!
    P.S. Database version is '10.2.0.1.0'

    Hi Vishnu,
    Karthiks query helped...
    But this is the problem I am facing...
    SELECT
            com.manufacturer,
            com.date_established,
            com.location,
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
            DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
            mod.current_production_status
    FROM
           company com,
           models mod
    WHERE
          mod.manufacturer = com.manufacturer
          and com.manufacturer = 'Ford'
          and mod.current_production_status IN (1,0)
    ORDER BY
            mod.current_production_status DESCThe value of:
    and com.manufacturer = 'Ford'will be dependent on front end user input....
    When I run the query above I get all the rows where current_production_status is either 1 or 0.
    I only require the rows where current_production_status is 1.
    So if I amend it to look like this:
         and mod.current_production_status = 1This works....
    BUT if a user now passes in more than one manufacturer EG:
    and com.manufacturer IN ('Ford', 'BMW')The query will only return the one row for Ford where current_production_status is 1. However because BMW has no models where current_production_status is 1 (all 3 are 0), I still want this to be output - as one row....
    So like this:
    com.manufacturer        com.date_established          com.location          mod.model          mod.price             mod.year     mod.current_production_status
    Ford               1902                    USA               Escort               10000               1992          1
    BMW               1910                    Germany               -               -               -          0So (hopefully you understand), I want both cases to be catered for.....whether a user enters one manufacturer or more than one...
    Thanks you so much!
    This is really driving me insane :-(

  • ABAP Reports and SAP Query

    Hi Experts,
    I have question regarding ABAP Reports, SAP Query, and Transaction with variant.  How are we securing one the above reports that we assign them through pfcg.  We can secure custom program by custom transaction or define the auth group in S_PROGRAM auth object but in this case we have to assign SA38 in production. is that correct?
    Please help me understand difference between the ABAP reports and SAP query. Is the ABAP reports same as Program or they are different.
    Thanks in advance
    Faisal
    Edited by: Faisal on Jun 30, 2009 11:06 PM

    Hi,
    1) End user security (role matrix coordinate with process team)
    This role Matrix design is most important where we can put restrictions and use SoD.
    2) Secure Table (by auth group)
    Table TDDAT and use of transaction se54 for security tables to right auth Groups. Secure s_tabu_dis, s_tabu_cli.
    3) Secure program (as you said ABAP reports are referred to Program)
    Use of table TPGP and program RSCSAUTH for assignment of groups to Program. SA38 Running of SA38 requires a minimum SUBMIT in user Action. A user having SA38 is dangerous as he/she is now enabled to run any report. Hence protection in Auth Group is needed. Verify each and every program is having authority check statement and Auth Group or not before assigning sa38. As you mentioned its best to avoid SA38 and create CUSTOM TXN for each report.
    (We should also SECURE S_DEVELOP in Production properly along with ur points. Please Note).
    4) Secure some batch jobs roles for batch job
    Secure by s_btch* objects and less access to se36.
    5) Create support roles for cutover activity during Go-live
    That is always needed. Go ahead.
    6) Emergency roles & IT roles for support
    This is very much needed as a role of Mitigation and Fire Fighting for Temporary access. Ensure to enable ur audit parameters in RZ10 (rsau* sm20,RSLG* for sm21). Give emergency access but enable audit via sm19 and get audit reports from sm20 and sm21 immediately after the use of emergency access.
    There are also other auth objects we need to be care ful which is a long list and hope every body ensures that (s_cts,a_admi,s_trans, tables ssm_cust, prgn,t000) etc and a host of others. Besh wishes. Let us know if any issue.
    Regards
    Aveek.

  • Diff. bet ABAP  and SAP Query

    HI
    Anybody can explain in detail about ABAP  and SAP Query.....
    And tell me what is the main diff. bet ABAP  and SAP Query...
    Regards
    S.Baskaran

    SAP Query
    Purpose
    The SAP Query application is used to create lists not already contained in the SAP standard system. It has been designed for users with little or no knowledge of the SAP programming language ABAP. SAP Query offers users a broad range of ways to define reporting programs and create different types of reports such as basic lists, statistics, and ranked lists.
    Features
    SAP Query's range of functions corresponds to the classical reporting functions available in the system. Requirements in this area such as list, statistic, or ranked list creation can be met using queries.
    All the data required by users for their lists can be selected from any SAP table created by the customer.
    To define a report, you first have to enter individual texts, such as titles, and select the fields and options which determine the report layout. Then you can edit list display in WYSIWYG mode whenever you want using drag and drop and the other toolbox functions available.
    ABAP Query, as far as I Believe, is the use of select statements in the ABAP Programming. This needs a knowledge of Open SQL commands like Select,UPdtae, Modify etc. This has to be done only by someone who has a little bit of ABAP experience.
    To sum up, SAP queries are readymade programs given by SAP, which the user can use making slight modification like the slection texts, the tables from which the data is to be retrieved and the format in which the data is to be displayed.ABAP queries become imperative when there is no such SAP query existing and also when there is a lot of customizing involved to use a SAP Query directly.
    Check out these links.
    http://help.sap.com/saphelp_46c/helpdata/en/35/26b413afab52b9e10000009b38f974/content.htm
    http://www.thespot4sap.com/Articles/SAP_ABAP_Queries_Introduction.asp
    Step-by-step guide for creating ABAP query
    http://www.sappoint.com/abap/ab4query.pdf
    ABAP query is mostly used by functional consultants.
    Kindly reward points by clicking the star on the left of reply,if it helps.Kindly close your previous threads if your problem is solved and reward points for helpful answers.
    Don't forget to reward if useful...

Maybe you are looking for

  • Sharing iTunes throughout a house

    I am starting to wish Steve Jobs would come up with some sort of weird hybrid Time Machine / Mac mini / Apple TV / Drobo thing, at which point, frankly, he could take my credit card details and charge whatever he felt to be appropriate...but while I

  • Removing Sevices from General Information in MSS

    Hi All, I am very new to EP, i want to remove some links like PCR,Photo and etc from General Information in MSS. How we can remove thse links? please need help in this regard. Thanks, Ramesh

  • Problem to generate SRM 7.0 configuration guide into solman

    Hi gurus, We have a problem to generate SRM 7.0 configuration guide related to business scenario plan driven procurement with supplier collaboration into transaction SOLAR02. We are using Solution Manager 7.0 and when we execute the configuration gui

  • Reducing an image dimensions when printing?

    The image I created is much larger than the final size needs to be.  Nominally letter to Bisiness card. How can I reduce the dimensions of the image so it prints at the correct size?

  • Macbook pro touchpad/curser problems.

    macbook pro touchpad curser problems.  typing randomly jumps to wherever the curser is resting at the time.  unbelievably maddening.  multiple devices same all have same problem.  hard to believe we cannot find anything on the apple site about this.