Extrapolating records using SQL

Hi,
Need some help with SQL:
I have to explode an incoming feed to generate records for every day.
Let me explain -
Incoming feed is an incremental feed - there is a record only if balance_usd is
updated.
act     date_key     balance_usd
101     20090201     230
101     20090210     3400
101     20090221     2000
Desired fact table - one row for every day, previous balance repeated for
the account forevery day unless there is a new record in the incoming feed
(marked with * below)
act     date_key     balance_usd
101     20090201     230     *
101     20090202     230
101     20090203     230
101     20090204     230
101     20090205     230
101     20090206     230
101     20090207     230
101     20090208     230
101     20090209     230
101     20090210     3400     *
101     20090211     3400
101     20090212     3400
101     20090213     3400
101     20090214     3400
101     20090215     3400
101     20090216     3400
101     20090217     3400
101     20090218     3400
101     20090219     3400
101     20090220     3400
101     20090221     2000     *
101     20090222     2000
101     20090223     2000
101     20090224     2000
101     20090225     2000
101     20090226     2000
101     20090227     2000
101     20090228     2000
Reason for storing it in such an "exploded" form is to be able
to compute average balance for a given time period (month).
Is there a way to create this with a single sql statement or do I need to write
a procedure?
Thanks!

Hi,
Solution with 10g
SQL> WITH t AS
  2       (SELECT 101 act, DATE '2009-02-01' date_key, 230 balance_usd
  3          FROM DUAL
  4        UNION ALL
  5        SELECT 101 act, DATE '2009-02-10' date_key, 3400 balance_usd
  6          FROM DUAL
  7        UNION ALL
  8        SELECT 101 act, DATE '2009-02-21' date_key, 2000 balance_usd
  9          FROM DUAL),
10       data_test AS
11       (SELECT act, date_key,
12                 NVL (LEAD (date_key) OVER (PARTITION BY act ORDER BY date_key),
13                      LAST_DAY (date_key)+1
14                     )
15               - date_key diff,
16               balance_usd
17          FROM t)
18  SELECT act, date_key, balance_usd
19    FROM data_test
20   MODEL
21   PARTITION BY (ACT,date_key dk )
22   DIMENSION BY (1 RN)
23   MEASURES(DATE_KEY,   DIFF,BALANCE_USD )
24   ( DATE_KEY[FOR RN FROM 1 TO DIFF[1] INCREMENT 1]=CV(RN)+DATE_KEY[1]-1,
25   BALANCE_USD[FOR RN FROM 1 TO DIFF[1] INCREMENT 1]=BALANCE_USD[1])
26   ORDER BY ACT,DATE_KEY;
       ACT DATE_KEY   BALANCE_USD
       101 2009-02-01         230
       101 2009-02-02         230
       101 2009-02-03         230
       101 2009-02-04         230
       101 2009-02-05         230
       101 2009-02-06         230
       101 2009-02-07         230
       101 2009-02-08         230
       101 2009-02-09         230
       101 2009-02-10        3400
       101 2009-02-11        3400
       101 2009-02-12        3400
       101 2009-02-13        3400
       101 2009-02-14        3400
       101 2009-02-15        3400
       101 2009-02-16        3400
       101 2009-02-17        3400
       101 2009-02-18        3400
       101 2009-02-19        3400
       101 2009-02-20        3400
       101 2009-02-21        2000
       101 2009-02-22        2000
       101 2009-02-23        2000
       101 2009-02-24        2000
       101 2009-02-25        2000
       101 2009-02-26        2000
       101 2009-02-27        2000
       101 2009-02-28        2000
28 ligne(s) sélectionnée(s).
SQL>  Regards Salim.

Similar Messages

  • How to insert the records using sql query

    hi,
    i insert the record uisng sql query and DOTNET programme successfully and increase the doc num .ubut when i try to  add record using SAP B1 the old Doc no show.It means its not consider the docnums which are not inserted by sql query.
    how can i solve this problem?
    Regards,
    M.Thippa Reddy

    You are not support use Insert / Update / Delete on SAP Databases directly.
    SAP will not support any database, which is inconsistent, due to SQL-Queries, which modify datasets or the datastructure of the SAP Business One Database. This includes any update-, delete- or drop-statements executed via SQL-Server Tools or via the the query interface of SAP Business One
    Check SAP Note: 896891 Support Scope for SAP Business One - DB Integrity
    [https://websmp130.sap-ag.de/sap(bD1lbiZjPTAwMQ==)/bc/bsp/spn/smb_searchnotes/display.htm?note_langu=E&note_numm=896891]

  • Update record using SQL statement

    I have VB6.0 and Oracle 10G Express Edition in Windows 2000 Server. My procedure in VB 6.0 can't update record in the table using SQL statement, and the Error Message is " Missing SET keyword ".
    The SQL statement in VB6.0 look like this :
    General Declaration
    Dim conn as New ADODB.Connection
    Dim rs as New ADODB.Recordset
    Private Sub Command1_Click()
    dim sql as string
    sql = " UPDATE my_table " & _
    " SET Name = ' " & Text3.Text & " ' " & _
    " AND Unit = ' " & Text2.Text & " ' " & _
    " WHERE ID = ' " & Text1.Text & " ' "
    conn.Execute (sql)
    Private Sub Form Load()
    Set conn = New ADODB.Connection
    conn.Open "Provider=MSDASQL;" & "Data Source=my_table;"& "User ID =marketing;" & "Password=pass123;"
    I'm sorry about my language.
    What's wrong in my SQL statement, I need help ........ asap
    Best Regards,
    /Harso Adjie

    The syntax should be
    UPDATE TABLE XX
    SET FLD_1 = 'xxxx',
    FLD_2 = 'YYYY'
    WHERE ...
    'AND' is improperly placed in the SET.

  • Retrieving records using SQL Date Queries

    Hello,
    I am using Lab Windows CVI 8.5 and also SQL TOolkit for my project. I am using DBMapColumnToChar function for mapping Date to the database. And for retrieving records I am using SQL Query as follows:
    SELECT * FROM DBTable WHERE DATE_TABLE BETWEEN '%s' AND '%s'",cCurrentDate,cCurrentDate
    I have given a drop down for selecting dates.
    When I execute the query I am not getting the selected date values an instead I am getting some records for which the dates aren't selected...
    Quick help needed for this..Thanks in advance

    Depending on the database, the date/time structure can be completely different from the structure in CVI.  CVI uses the Windows format for time(), that is the number of seconds since 1900.  The tm struct can break this apart into a date and time structure that is usable.
    I am using MS Access for the databases and SQL toolkit.  I finally ended up replace the date field with an INT field format, and then store the CVI time (gotten with time()) as an INT value.
    Easy then to get a search range, store it a tm struct and then convert with mktime() to a calendar time to search with.
    Hope this helps.  The date/time thing is a never ending struggle.
    Regards, David E.

  • Insert a record using sql broker service

    Hi Guys,
    I need some samples to use sql broker service for creation of a record in other database in order to reduce the delay in creating number of records at the same time.

    Hello,
    In SQL Server Service Broker you can use the "Internal Activation" to start a stored procedure when a message comes in; in such a stored procedure you can do what ever you want to do, also inserting data into a table.
    See MSDN
    Service Broker Tutorials
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • How to combine records using SQL?

    If I have the following data:
    Table A
    order_no company_code
    O001 C001
    Table B
    order_no po_no
    O001 P001
    O001 P002
    O001 P003
    Now, I want to combine the data as:
    View A
    order_no company_code po_no
    O001 C001 P001, P002, P003
    If can it be realized using SQL?
    Thanks and Best Regards,
    Su Qian

    Hi Su Qian,
    To get the result like this you should write a function and call that in the select query.
    The function could be like this:
    CREATE OR REPLACE FUNCTION TEST(p_order_no IN B.order_no%TYPE ) RETURN VARCHAR2 IS
    v_po_no VARCHAR2(100);
    BEGIN
    FOR c IN (SELECT po_no FROM B WHERE order_no = p_order_no)
    LOOP
    IF v_po_no IS NULL THEN
    v_po_no := c.po_no;
    ELSE
    v_po_no := v_po_no||','||c.po_no;
    END IF;
    END LOOP;
    RETURN v_po_no;
    END;
    Now you issue the query like this:
    select order_no, company_code, test(order_no) po_no from a;
    This will return you what you want.
    Hope this will help you.
    Best Regards,
    Ramesh Rathi.
    OCP - DBA.
    Zensar Tech Ltd.
    [email protected]
    If I have the following data:
    Table A
    order_no company_code
    O001 C001
    Table B
    order_no po_no
    O001 P001
    O001 P002
    O001 P003
    Now, I want to combine the data as:
    View A
    order_no company_code po_no
    O001 C001 P001, P002, P003
    If can it be realized using SQL?
    Thanks and Best Regards,
    Su Qian

  • How to Get Top 10 records using SQL select statement.

    :-) Hiee E'body,
    I want to generate a sql report in which I only want the top 10 records based on the numeric value of a column.
    For Example,
    To generate a Location Wise, Employee Wise Years of Service Report.
    Here I need the Top 10 Employees according to their No. of Years of Service for all the existing locations using a single query.
    eg.
    Location Emp No. YOS
    India - 22 30
    212 28
    819 24 ...
    US 123 40
    312 33
    90 33
    144 30 ...
    UK 77 20
    79 20
    331 18
    109 16 ...
    Every Location should display their respective Top 10 Employees
    regarding their No. of Years of Service.
    Please let me the know the solution to this.
    Have a nice day.
    Thanking You,
    Vivek Kapoor.

    For example if the table contained (India rows only shown) :
    India 202 30
    India 212 28
    India 819 24
    India 820 24
    India 900 20
    India 920 18
    India 922 17
    India 925 16
    India 926 15
    India 927 14
    India 928 13
    India 929 13
    India 930 12
    do you want to see
    India 202 30
    India 212 28
    India 819 24
    India 820 24
    India 900 20
    India 920 18
    India 922 17
    India 925 16
    India 926 15
    India 927 14
    or
    India 202 30
    India 212 28
    India 819 24
    India 820 24
    India 900 20
    India 920 18
    India 922 17
    India 925 16
    India 926 15
    India 927 14
    India 928 13
    India 929 13
    Also if the India rows were
    India 202 30
    India 212 30
    India 819 30
    India 820 30
    India 900 30
    India 920 30
    India 922 30
    India 925 30
    India 926 30
    India 927 30
    India 928 30
    India 929 30
    do you want to see
    India 202 30
    India 212 30
    India 819 30
    India 820 30
    India 900 30
    India 920 30
    India 922 30
    India 925 30
    India 926 30
    India 927 30
    or
    India 202 30
    India 212 30
    India 819 30
    India 820 30
    India 900 30
    India 920 30
    India 922 30
    India 925 30
    India 926 30
    India 927 30
    India 928 30
    India 929 30
    Please clarify.
    Thanks,
    Partha

  • Need help, Trouble in uploading records using sql loader in Forms 6i

    Hi,
    I am trying to develop a screen for uploading records to a table by using a ctl file, batch file and sql loader.
    Env: Forms 6i, Oracle 8
    Table to be updated is: shy_upload_table
    My TSN entry looks similar to this,
    TEST_AXA.CNB.COM =
    (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 11.23.11.123)(PORT = 1234))
    (CONNECT_DATA =
    (SID = axdabc)
    My intention is whenever i press the upload_button, I should truncate the table and upload it with the contents of the file.
    In the when-button-pressed event of the upload_button I have the following code. always I am able to truncate the table but am not able to upload it with the contents of the file. Can any of you help me fix this problem ?
    declare
         var_control varchar2(256);
         VAR_DATA VARCHAR2(256);
         VAR_OUTPUT VARCHAR2(500);
         var_filename varchar2(256);
         str varchar2(50);
         cnt number;
    begin
         FORMS_DDL('TRUNCATE TABLE shy_upload_table ');
         select count(*) into cnt from shy_upload_table;
         message('count '||cnt);
         MESSAGE('');
    If NOT form_success Then
         MESSAGE('Upload Failed');
         MESSAGE('Upload Failed');           
    else
         set_item_property('DISPLAY_PB',enabled,property_true);
    --when ever i run, i am able to see the display_pb enabled. it means form_success is true.
    end if;
         var_filename := :txt_filename;
    --I have tried with each of the below option,
    --sqlldr userid/[email protected] control=F:\ERP\file_upload.ctl
    --sqlldr userid/password@axdabc control=F:\ERP\file_upload.ctl
    --sqlldr userid/password@TEST_AXA.CNB.COM control=F:\ERP\file_upload.ctl
         VAR_DATA :='data=' || var_filename ;
         VAR_OUTPUT := var_control|| ' ' ||VAR_DATA;
         host('F:\a.bat');
    end;
    batch file contents...
    # I have tried with each of the below options
    sqlldr userid/[email protected] control=F:\ERP\file_upload.ctl data=F:\ERP\sample.txt log=F:\ERP\x.log bad=F:\ERP\x.bad
    #sqlldr userid/password@axdabc control=F:\ERP\file_upload.ctl data=F:\ERP\sample.txt log=F:\ERP\x.log bad=F:\ERP\x.bad
    #sqlldr userid/password@TEST_AXA.CNB.COM control=F:\ERP\file_upload.ctl data=F:\ERP\sample.txt log=F:\ERP\x.log bad=F:\ERP\x.bad
    pause
    Thanks
    vish

    Hi Francois,
    Thanks for responding, I am not very sure of what you want me to try out.
    When I double click the batch file containing the below, the record gets inserted in the table. Only when using my form and trying to upload, it fails to insert the record.
    batch file contents...
    #sqlldr userid/password@TEST_AXA.CNB.COM control=F:\ERP\file_upload.ctl data=F:\ERP\sample.txt log=F:\ERP\x.log bad=F:\ERP\x.bad
    pause
    Thanks
    Vish

  • Split a ";" delimited record using SQL

    Post Author: GraemeG
    CA Forum: Data Connectivity and SQL
    I receive a string of semi-colon delimited data output by a third-party system. The idea is to drag this record into Crystal and match it up to some reference information in our database and then output it to a Crystal report. We have no programming resources available and don't want the users to have to drag it into Excel (or something similar) first as it would allow them to amend the data before it is reported on.
    So, our plan is to break the record up in a SQL statement, embed that into the report as a command in Database Expert and then do the joins to our database. I'm hoping some clever person on here can tell me how!
    The fields in the data string are variable length and looks something like this:
    W01.COMSTD;3503794;5003711;F01.ANNR;;0:00;;;0;280607;1103;280607;1103;280607;2359;0:00;...etc.
    I would really appreciate any assistance. I have looked at just using the split() command in Crystal but this makes my joins a nightmare.

    Post Author: GraemeG
    CA Forum: Data Connectivity and SQL
    Thanks Jagan. The portal we use (a Lawson embedded version of Infoview) has issues running subreports and Business Objects have acknowledged its a bug... which kinda means I won't have a fix for a while yet. With our new system going live 1 August, I can't afford to wait.
    I was hoping to find a clever SQL statement that would do the same as the split function that I could use to split my data in a DE Command. If there isn't one, I may be forced to take the old RPG book out of mothballs!

  • Fixed Length records using SQL

    hi , using the following code in a file that generates an output file with a fixed length 300 character lines.
    SPOOL C:\PHONE_SAMPLE.OUT;
    SET HEADING OFF;
    SET PAGES 0;
    SET ECHO OFF;
    SELECT rpad(CONTACT_ID,15),rpad(PHONE1,15),rpad(PHONE2,15),rpad(PHONE3,15),rpad(PHONE4,15),rpad(PHONE5,15),rpad(PHONE6,15)
    ,rpad(CALLSEQ,6),rpad(NDCALLSEQ,6),rpad(LANG,3),rpad(FIRST,25),rpad(LAST,25),rpad(CONCODE,10),rpad(EMAIL,30),rpad(TXTMSG,20),rpad(WVEND,20),
    rpad(APPTSTART,14),rpad(APPTEND,14),rpad(APPTTYPE,10),rpad(APPTLOCTION,10),rpad(FILLER,1)||'\n'
    FROM PHONE_SAMPLE;
    SPOOL OFF;
    After I run this I get my data and the following below - my end of Character line, and then "22 rows selected" - How do I remove the 22 rows selected. and stop at \n. Any help appreciated.
    Is it a set command I am missing ?
    \n
    22 rows selected.

    Hi,
    SET FEEDBACK OFF
    And you don't need SET HEADING OFF when you have SET PAGES 0
    And you don't need to end SET commands with ';' since they are SQL*Plus commands
    Regards
    Peter

  • Split records T-SQL

    Hi Friends,
    I have a full record which does have single and return information in the same record so I want to split into two records using SQL
    have a look attached below screen shot this is the Input I have
     The below output I am expecting using SQL code
    Can anyone guide me how to do this?
    below is my SQL Query.
    SELECT
    [BookID]
    ,[DepartureDate]
    ,[DepartureAirportfldId]
    ,[DepartureAirportAirportName]
    ,[ArrivalAirportfldId]
    ,[ArrivalAirportAirportName]
    ,[ReturnDate]
    ,[ReturnDepartureAirportfldId]
    ,[ReturnDepartureAirportAirportName]
    ,[ReturnArrivalAirportfldId]
    ,[ReturnArrivalAirportAirportName]
    ,[PassengerName]
    FROM [Test]
    Thanks for your help.

    You can use UNION ALL approach
    SELECT BookID, CAST([DepartureAirportfldId]
    AS VARCHAR(20))+CAST([ReturnDepartureAirportfldId]
    AS VARCHAR(20)) ID,
    [DepartureAirportAirportName]
    departure ,[ArrivalAirportAirportName]
    arrival
    from TBL
    UNION ALL
    SELECT BookID,CAST([ArrivalAirportfldId]
    AS VARCHAR(20))+CAST([ReturnArrival]
    AS VARCHAR(20))[ReturnDepartureAirportAirportName]
    departure ,[ReturnDepartureAirportAirportName]
    arrival
    from TBL
    Another method could be using UNPIVOT (see example)
    DECLARE @Table TABLE
    UserId INT,
    Day1 INT NULL,
    Day2 INT NULL,
    Day3 INT NULL,
    Day4 INt NULL,
    DayMax Int NULL
    INSERT INTO @Table(UserId, Day1, Day2, Day3, Day4, DayMax)
    VALUES(1,10,null,20,3,null);
    INSERT INTO @Table(UserId, Day1, Day2, Day3, Day4, DayMax)
    VALUES(2,50,25,15,5,null);
    SELECT UserId, DayQuantity AS DayMax FROM @Table
    UNPIVOT (DayQuantity FOR DayNumber IN (Day1,Day2,Day3,Day4)) AS c
    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

  • How to load date with time zone using sql loader???

    Hi All,
    How to load following value in the table using SQL loader.
    [11/Jan/2006:15:20:14 -0800]
    What should be the datatype of the column in the table. I have tried with "timestamp with local time zone", but unable to load the record using sql loader. What should be the format string in the loader control file to load this type of record.
    Any help in this regard is highly appreciated.
    Sameer

    Try something like this in your control file:
    mycol char "TO_TIMESTAMP_TZ(mycol, 'DD/MON/YYYY:HH24:MI:SS TZH:TZM')"
    [pre]
    Message was edited by:
            Jens Petersen                                                                                                                                                                                                                                                                                                                                                                       

  • How to get multiple records using fn-bea:execute-sql()

    Hi,
    I created Proxy service(ALSB3.0) to get records from DB table. I have used Xquery function(fn-bea:execute-sql()). Using simple SQL query I got single record, but my table having multiple records. Please suggest how to get multiple records using fn-bea:execute-sql() and how to assign them in ALSB variable.
    Regards,
    Nagaraju
    Edited by: user10373980 on Sep 29, 2008 6:11 AM

    Hi,
    Am facing the same issue stated above that I couldnt get all the records in the table that am querying in the Proxyservice.
    For example:
    fn-bea:execute-sql('EsbDataSource', 'student', 'select Name from StudentList' ) is the query that am using to fetch the records from the table called StudentList which contains more than one records like
    Id Name
    01 XXX
    02 YYY
    03 ZZZ
    I tried to assign the result of the above query in a variable and while trying to log the variable, I can see the below
    <student>
    <Name>XXX</Name>
    </student>
    I want to have all the records from my table in xml format but it's not coming up. I get the value only from the first row of my table.
    Please suggest.
    regards,
    Venkat

  • How to process each records in the derived table which i created using cte table using sql server

    I want to process each row from the CTE table I created, how can I traverse from first row to second row and so on....
    how to process each records in the derived table which i created using  cte table using sql server

    Ideally you would be doing a set based processing rather than traversing row by row as thats more efficient. To answer it specific to your scenario we may need more info. Can you explain with some sample data your exact requirement?
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Unable to fetch 50,000 records from SQL using Orchestrator

    Hi Team,
    I have a table in MS SQL which is having more than 50,000 records. I am trying to fetch 50,000 records using orchestrator, but unable to fetch the records..
    There is no problem with the SQL query because I can able to get 40,000 records..
    I am using SCORCH DEV - SQL Integration Pack.
    Regards,
    Soundarajan.

    Hi,
    Thanks for your query.
    I have also used timeout parameter but it is not working.. As you said I also tried with Query database Activity which is out of the box...
    Now i can able to fetch more than 80,000 records but the output what i am getting is not in the format which we are looking for..
    Attached the output...
    How to edit this..?
    I tried to write the output in excel but all the data sits in the first column itself..
    Regards,
    Soundarajan.

Maybe you are looking for