PLEASE HELP ME TO FETCH LAST 3Weeks data in SQL

I have to display 3 weeks data when the user selects one week.
In the front end i have start date and end date , after selecting the start and end date i have drop down list which will display the weeks . By passing weekid i need to fetch data for last 3 weeks, is this possible by passing only the week id. and what
if the user selects the last year as start date and current date as end date....
for example
if the user selects week -38
it should display data for week 38,37 and 36 . this is working if by decrementing the week by 1.
what if the user selects week 1, it should select the last 2 weeks of previous year as well. how will i do this by using week id...please help 

Please try following code in which you can change @increament value as you wish.
--sample DDL & DML
DECLARE @Table TABLE ( WeekId INT PRIMARY KEY );
INSERT @Table
VALUES ( 1 ), ( 2 ),( 3 ), ( 4 ),( 5 ),( 6 ), ( 7 ),( 8 );
--input parameters
DECLARE @WeekId INT = 1 ;
DECLARE @Increament INT = 2 ;
DECLARE @Mn INT ;
SELECT
@Mn = CASE
WHEN ( SELECT MAX(T2.WeekId) FROM @Table AS T2 WHERE T2.WeekId = T1.WeekId - @Increament ) IS NULL
THEN ( SELECT MAX(WeekId) FROM @Table ) - @Increament + T1.WeekId
ELSE ( SELECT MAX(T2.WeekId) FROM @Table AS T2 WHERE T2.WeekId = T1.WeekId - @Increament ) END
FROM
@Table AS T1
WHERE
T1.WeekId = @WeekId ;
IF @WeekId < @Mn
SELECT *
FROM @Table
WHERE WeekId <= @WeekId OR WeekId >= @Mn ;
ELSE
SELECT *
FROM @Table
WHERE WeekId BETWEEN @Mn AND @WeekId ;
T-SQL Articles
T-SQL e-book by TechNet Wiki Community
T-SQL blog

Similar Messages

  • Hi all, can someone help me in getting last login date of a user in CQ5 please?

    Hi all, can someone help me in getting last login date of a user in CQ5 please?

    CQ is REST based and does not have the concept of session. So there is no feature to track login or logout details.  Most of our customers use some kind of central authentication Ex- SSO hence no need arises to have such functionality built in.  However if needed you have the ability to implement such at a project level solution. Ex:- custom login modules or auth-handler by taking project specific constraints and requirements into account

  • HT201270 i have just updated my old 3GS and now when i am on restore the window is comming up and asking for a password. i have not set up a password for restore, please help how i can restore my date???

    i have just updated my old 3GS and now when i am on restore the window is comming up and asking for a password. i have not set up a password for restore, please help how i can restore my date???

    capriz wrote:
    i have just updated my old 3GS ...
    To what... iOS 7 is Not supported on the 3 GS

  • Here is the message I got from the red "i" message... please help.The backup disk image "/Volumes/Data/Lisa's MacBook Pro.sparsebundle" is already in use.

    Here is the message I got from the red "i" message... please help.The backup disk image “/Volumes/Data/Lisa’s MacBook Pro.sparsebundle” is already in use.

    http://pondini.org/TM/C12.html

  • HT201302 please help.. i lost all the data in my ipad after upgrade to ios 702 i want my 3500 pictures back.. please help.. because my i tune also dont have the data

    please help.. i lost all the data in my ipad after upgrade to ios 702 i want my 3500 pictures back.. please help.. because my i tune also dont have the data. please anybody help me...

    Only possible if you have made an iTune or iCloud backup before the update.

  • How to fetch last changed date for Header in me22n?

    Hi Experts,
    I need to create a report for PO, where I have created one screen tab in Header level(customer data), therefore I need to fetch last changed date for header , I must mention that I do not want last changed date for item level, only for header last changed date is required.
    Kindly assist me on this.
    Thanx
    Shireen

    Read table CDHDR (object "EINKBELEG") and CDPOS (look for table name EKKO), keep the last CDHDR record with "EKKO" data actually changed.
    Regards,
    Raymond

  • Please add exit to the last line of the sql script

    Hi,
    I am executing a sql script from UNIX shell. When I execute it at the command prompt the script executes successfully, but when I schedule the script on cron, the script is throwing an error with the above message
    "Please add exit to the last line of the sql script"
    Any idea what is happening? Thanks in advance for any help
    thedba

    It works check this out..
    << script a.sh >>
    logfile=/home/oracle/sri/x.log
    sqlplus -s /nolog <<DOC >> ${logfile}
    whenever sqlerror exit sql.sqlcode
    connect scott/tiger
    @a.sql a.log
    DOC
    <<  script a.sql >>
    set feedback off
    set echo off
    spool &1
    select * from tab where tname like 'A%';
    spool off
    exit;
    << a.log >>
    TNAME                          TABTYPE  CLUSTERID                              
    AQ$DEF$_AQCALL                 VIEW                                            
    AQ$DEF$_AQERROR                VIEW                                            
    AQ$_DEF$_AQCALL_F              VIEW                                            
    AQ$_DEF$_AQERROR_F             VIEW                                            
    AQ$_INTERNET_AGENTS            TABLE                                           
    AQ$_INTERNET_AGENT_PRIVS       TABLE                                           
    AQ$_QUEUES                     TABLE                                           
    AQ$_QUEUE_TABLES               TABLE                                           
    AQ$_SCHEDULES                  TABLE                                           
    << x.log >>
    TNAME                          TABTYPE  CLUSTERID
    AQ$DEF$_AQCALL                 VIEW
    AQ$DEF$_AQERROR                VIEW
    AQ$_DEF$_AQCALL_F              VIEW
    AQ$_DEF$_AQERROR_F             VIEW
    AQ$_INTERNET_AGENTS            TABLE
    AQ$_INTERNET_AGENT_PRIVS       TABLE
    AQ$_QUEUES                     TABLE
    AQ$_QUEUE_TABLES               TABLE
    AQ$_SCHEDULES                  TABLE-Sri

  • To find last updated date in sql developer for a procedure or a function

    hi
    how to to find last updated date in sql developer for a procedure or a function.
    i m using sql developer version in 1.2

    I think you are in the wrong forum...
    Anyway you can try
    select * from
    all_objects o
    where o.object_type in ('FUNCTION','PROCEDURE');
    you have there the created date, last DDL and timestamp

  • Hello Technical Experts... please help me on this error :- 'Tax data is missing ArrayOffset 12, srcLineNum 0, srcGroupNum -1'.

    In my A/P credit memo screen i have incorporated a column for adding reference to closed A/P Invoices. upon filling this field with DocNum of APInvoice certain other fields also get filled from an SQL query. One among them is the TaxCode. My problem is that the TaxAmount(LC) field at the last row of the matrix is not filled with the appropriate value, it remains 0. On trying to add the document or pressing the link button on the TaxAmount field a status bar error message pops up showing, 'Tax data is missing ArrayOffset 12, srcLineNum 0, srcGroupNum -1'.
    Please Help me solve this issue.
    Thanks in Advance
    Rajeev

    Hi,
    Please check SAP note:
    1860926 - When updating a Purchase Order via DI API "-12109 Tax data
    is missing..." Error occurs
    Thanks & Regards,
    Nagarajan

  • Please help: User cannot see webi report data

    Hi,
    I've got a wired problem here in my webi report. Basically I am located in Singapore and produced a webi report for our user in UK. The report looks totally fine with all the data shown correctly. But when our report user logged into the dashboard from UK, she could only see partial data and some of the information are missing(blank cells in the report).
    To verify the cause I've done the following test:
    1. I remote control her computer and log into dashboard using my account, the result is I get same problem and I cannot see some of the data.
    2. I let her remote control my pc and log into dashboard using her account, she no longer has this problem and can see complete data.
    We use same version of IE, same JAVA version. And the report data is not time sensitive, so different time zone might not the possible cause.
    This problem is killing me. Can anyone please help and let me know any possible solution? Thanks so much.
    Edited by: Sinsin Tan on Jul 14, 2010 5:59 AM

    Hi Sinsin, what version of BusinessObjects are you using? Is it XI 3.1?
    I don't know if it is the same problem or not, but I have seen that before when a user cannot view the data, change the font and/or background color and the data becomes visible. Reinstalling the client fixed that problem. If you couldn't figure out the root cause, you might want to consider this option.
    If you are using the BusinessObjects tool, you should join [ASUG|www.asug.com]

  • Please help: Message no. M7090/Accounting data not yet maintained

    To whom it may concern-
    I am currently a student attending LSU working on an SAP project worth a very significant portion of our grade. The following is a detailed description of the problem, which should hopefully help you put your finger on a solution.
    My problem is that when I try to hit the accept button on PP10 for a goods issue, I get an error message "Accounting data not yet maintained" (please refer to attached screenshot #1). When I click the help button, I get a vague solution (please refer to attached picture #2). I have tried and "change" my material master record for Cushion 065 by going back to MM06/create material master for trading goods (please refer to attached picture #3), but I don't have the understanding of SAP to manipulate the information at this point. It is just giving me the ability to manipulate views and it is only letting me choose the following views (please refer to attached picture #4).
    Please help me find a more useful pathway so I can proceed. If whoever is helping me needs me detailed information, please let me know. Also, if someone could direct me to a user support function where I can talk to a representative in real time, I would appreciate that.
    Mack Bradford

    Extend the Accounting view with Tcode :MM50
    Go to MM50 , select Maintenance status as "B" and enter Material number and click on execute (F8), then select the Material line item and click on Maintain Materials tab.
    Regards,
    Ramesh

  • Please help me on how to pass date condition Dynamically.

    Hi Experts,
    I have the following control table.
    ID S_OWNER  SOURCE_TABLE     A_OWNER    ARC_TABLE             CONDITION_COLUMN     PERIOD_VALUE   PERIOD_UNIT
    1    wedb     Auction_table    wedb     Arc_Auction_table     Auction_date            15               Days
    1    wedb     Sales_table      wedb     ArcSales_table             Sales_date            180              Days
    1    hr       Accounts_table   hr       Arc_Accounts_table     Account_date             2             Years
    2    concor   Concur_table     con      Arc_Concur_table        Last_update_date        4               MonthsLike this 1000 entries are there in the control_table.
    I want to pass all the columns from my control table dynamically.
    I am able to pass all the columns dynamically,except the PERIOD_VALUE.
    I have stucked up how to implement this condition.
    My aim is to get the data which is <= sysdate-PERIOD_VALUES based on PERIOD_UNIT.
    For Example:
    For Auction_date column I want to get the data which 15 days old.
    For Sales_date column I want to get the data which 180 days old.
    For Account_date column I want to get the data which 2 yers old.
    For Last_update_date column I want to get the data which 4 months old.
    The conditions something like this.
    condition_column<=(sysdate-15 days)
    condition_column<=(sysdate-180 days)
    condition_column<=(sysdate-2 years)
    condition_column<=(sysdate-4 months)This is my procedure.
    CREATE OR REPLACE PROCEDURE WEDB.procedure_control(
       P_ID IN NUMBER)
    IS
       CURSOR C
       IS
            SELECT ID,S_OWNER,SOURCE_TABLE,A_OWNER,ARC_TABLE,CONDITION_COLUMN,PERIOD_VALUE,PERIOD_UNIT
              FROM wedb.CONTROL
             WHERE ID = p_id
          ORDER BY ID, SOURCE_TABLE;
          rec C%ROWTYPE;
    BEGIN
       FOR I IN C
       LOOP
        EXECUTE IMMEDIATE
                   'INSERT INTO '
                || rec.A_OWNER
                || '.'
                || rec.ARC_TABLE
                || '(SELECT * FROM '
                || rec.S_OWNER
                || '.'
                || rec.SOURCE_TABLE
                || ' WHERE '
                || rec.CONDITION_COLUMN
                || '<=I want to pass dynamic condition'
                || ')';
             EXECUTE IMMEDIATE
                   'DELETE FROM '
                || rec.S_OWNER
                || '.'
                || rec.SOURCE_TABLE
                || ' WHERE '
                || rec.CONDITION_COLUMN
                || '<= I want to pass dynamic condition'
                || '';
          COMMIT;
       END LOOP;
    EXCEPTION
       WHEN OTHERS
       THEN
          ROLLBACK;
          DBMS_OUTPUT.PUT_LINE (
             'An error was encountered - ' || SQLCODE || ' -ERROR- ' || SQLERRM);
    END procedure_control;
    /Please help me how to implement this.
    Thanks in advance.

    I have added ||')' paranthasis for each CASE stament to avoiid the error.
    Now I am getting the below error
    ORA-00903: invalid table name
    ORA-06512: at line 42
    I am using below code.
    Please find my below code.
    DECLARE
       stmt   LONG;
       CURSOR C
       IS
            SELECT GROUP_ID,
                   SOURCE_TABLE_OWNER_NAME,
                   SOURCE_TABLE_NAME,
                   ARCHIVE_TABLE_OWNER_NAME,
                   ARCHIVE_TABLE_NAME,
                   CONDITION_COLUMN_NAME,
                   RETENTION_VALUE,
                   RETENTION_UNIT,
                   FLAG
              FROM APPS_GLOBAL.control_table1
             WHERE GROUP_ID = 1
          ORDER BY GROUP_ID, source_table_name;
       rec    c%ROWTYPE;
    BEGIN
       FOR rec IN C
       LOOP
    stmt := 'INSERT INTO '
             || rec.ARCHIVE_TABLE_OWNER_NAME
             || '.'
             || rec.ARCHIVE_TABLE_NAME
             || '(SELECT * FROM '
             || rec.SOURCE_TABLE_OWNER_NAME
             || '.'
             || rec.SOURCE_TABLE_NAME
             || ' WHERE '
             || rec.CONDITION_COLUMN_NAME
             || '<=';
    CASE LOWER (rec.RETENTION_UNIT)
        WHEN 'd'
        THEN
         stmt:= stmt||'sysdate -'||rec.RETENTION_VALUE||')';
         ELSE
         stmt := stmt||'add_months(sysdate,'|| rec.RETENTION_VALUE||'*-12)'||')';
    END CASE;
    execute immediate stmt;
       END LOOP;
       COMMIT;
    END;
    /The values for the field RETENTION_UNIT in my control_table1 are D and Y.
    Before making the date comparison condition it was working fine.
    If I made it as dynamic why I am getting the error.
    If I use condition as below it's working fine.
    EXECUTE IMMEDIATE
                   'INSERT INTO '
                || I.ARCHIVE_TABLE_OWNER_NAME
                || '.'
                || I.ARCHIVE_TABLE_NAME
                || '(SELECT * FROM '
                || I.SOURCE_TABLE_OWNER_NAME
                || '.'
                || I.SOURCE_TABLE_NAME
                || ' WHERE '
                || I.CONDITION_COLUMN_NAME
                || '<=ADD_MONTHS(SYSDATE,-24)'
                || ')';Please help me .
    Thanks.

  • Fetch last match data..

    table
    id carno houseno entrytime                            exit time
    1  11       333        3/4/2014 3:15:18 PM         3/4/2014 3:25:18 PM
    2  11       333        3/4/2014 3:35:18 PM         3/4/2014 3:45:18 PM
    3 11       333        3/4/2014 3:55:18 PM         3/4/2014 4:25:18 PM
    4  14       33        3/4/2014 4:35:18 PM         3/4/2014 5:25:18 PM
    5   15      544      3/4/2014 6:35:18 PM         3/4/2014 7:25:18 PM
    6 11       333        4/4/2014 3:55:18 PM         null
    7 15      544        6/4/2014 3:55:18 PM         6/4/2014 4:25:18 PM
    this is my table structure.............
    so my query is i want to search row which use where clause to match carno and houseno.....in my table so many place carno and houseno are same but only timings are different ..i want to select the last entry which matchs...
    example i give input houseno-333 and carno-11 then i want to serch last entry for this same houseno and carno...that means id-6 will be fetch.......how to achieve this.....i m able to fetch last record with where clause like house no and carno condition.....then
    i want to check is exit time in null in dis row or having some value...
    fynl summary i want to retrive last row based on where clause after retrivinbg i want to check value of exit column....
    is it blank or some value in exit time..

    there are so many issue arise when i test with dummy values..
     some chnges.....
    id carno houseno entrytime                            exit time
    1  11       333        3/4/2014 3:15:18 PM         null
    2  11       333                       null                     3/4/2014 3:45:18 PM
    3 11       333        3/4/2014 3:55:18 PM        null
    4  14       33       null                                     3/4/2014 5:25:18 PM
    5   15      544      3/4/2014 6:35:18 PM         null
    6 11       333        4/4/2014 3:55:18 PM         null
    7 15      544        6/4/2014 3:55:18 PM         null
    now i have to find if there is two  simulatneously entry  happening in a sequence means without exit
     between  two entry of same carno and house no..   thn i need to beep alarm.....if entry tym is store then it should exit first for reenter again....if somehave two duplicate id of card no and house no..then we need to find duplicate of id....this
    is rfid tag which is used for enter in a society,,,,entry gate will open after  identify this tag
    example/////// id-5 carno-15 enter at  3/4/2014 6:35:18 PM  means last entry inserted...after tht no exit
    happen for this carno and houseno.......at 7 id it will try again to enter..but it doesnot have exit so we need to restrict this.......

  • Please Help - How to Fetch the Data from a Cube

    Dear All,
    We created a cube containing dimensions
    Customer, Product, Branch, Activity, Time dimensions
    using Oracle Analytical WorkSpace Manager.
    Once Cube is created,
    How can I see the Data existing in the Cube using normal SQL Queries ??? Through Analytical Workspace Manager Toll we r able to see the data. But our requirement is to see the data from the Cube using SQL Queries.
    Regards,
    S.Vamsi Krishna

    You appear to have the wrong forum. This forum is for the Oracle Workspaces application, which is a component of Oracle Collaboration Suite.
    Perhaps one of the forums in the database section will be more relevant:
    http://forums.oracle.com/forums/category.jspa?categoryID=18
    regards,
    -Neil.

  • Please help me remove the last elements of shell script from my applescript

    I read somewhere that running a shell script in an applescript was inefficient and created extra overhead, fine, so I am trying to take all of my shell script out of my applescript (this should fix issues with forward slashes in names as well...)
    however I am stuck on these last two lines (I know they are probably trivial but I've been up waaaay too long now) so I was wondering if anyone would be awesome enough to show me how to convert the bash to applescript.
    --does a recursive copy and rename
    do shell script "cp -R " & (thePath as text) & " " & (theOtherPathWithFile as text)
    --does a copy and rename, then a simple move
    do shell script "cp " & (quoted form of (POSIX path of this_item as text)) & " " & (theNewPath as text) & "&& mv " & (theOtherPathWithFile as text) & " ~/Desktop"
    Any and all help will be appreciated.

    There isn't anything wrong with using shell scripts, especially since AppleScript is designed to do this very thing. There is some overhead when using do shell script, just as there is some overhead when using an application tell statement, so it just depends on what you are doing.
    The performance of any particular script would also be up to whatever is acceptable to you (or your customers), and sometimes a shell script is the most efficient way to do something. Many of the system utilities are just GUI front-ends to a shell script, so they can't be all bad.
    To do your file copy or move with the Finder, you will need to tell it to duplicate or move the item(s) (there is that pesky overhead again). Shell scripts use POSIX paths while the Finder uses colons as its path delimiter, so you will need to use the correct file path references - there is a decent article about that at Satimage. Once your file paths have been defined (or using something like choose file or choose folder)), your script would be something like:
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px;
    color: #000000;
    background-color: #DAFFB6;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    tell application "Finder"
    duplicate source to destination
    -- move source to destination
    end tell
    </pre>
    See the Finder and *System Events* scripting dictionaries for more information about the ways they deal with files.

Maybe you are looking for

  • Excel download feature in Web Dynpro Abap using OAOR document

    Hi Friends, I am facing an issue in implementing excel download feature in Web Dynpro. Problem: I have stored an excel document in OAOR. I am displaying data on screen using ALV display in WDA. I will provide a button to download excel in toolbar of

  • How to use external CSS for standard items ?

    Hi, I would like to use my external CSS style-sheet for the standard items I add to my portal page. I build an UI template with a CSS-link to my css style-sheet, and I used this UI template for my portal page. But it seems that items cannot use this

  • Unable to add component palette in jdeveloper

    Hi, Im unable to add component palette in jdeveloper, it is giving exception and the stack trace is here below, pls let me know the solution. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> java.lang.NullPointerException      at oracle.ideimpl.palette.Pa

  • WRT160N and intermittent internet connections (even wired!!!)

    I just purchased and installed the LinkSys WRT160N router to replace an older Netgear wireless 'G' router. The reason for the replacement / upgrade was simply to have the wireless signal reach the rest of my house.  We do not have any 'N' receivers/a

  • HT201263 how to exit from recovery mode to normal mode

    i have trouble with my ipnone 4S 5.1.1.... recovery mode dan can't to normal mode...it always blang...when  i was restore from alway shown error 3491 or restor dan upgrading... Thank you max how to fixing error 3194?