SQL query to get last 6 months records neglect the current month

Hi All;
I need help with 
sql query to get last 6 months records neglect the current month
Any help much appreciated
Thanks
Pradnya07

SELECT <> FROM tbl WHERE dt >=dateadd(month,-6,GETDATE())
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

Similar Messages

  • SQL query to get last 10 records in the table?

    Hi,
    Can anyone tell me the SQL query to get last 10 records in the table?
    Thanks!!
    MCP

    Please, define what "last" means. Sets are unordered by definition, so if you want to retrieve rows from a table in a specific order you need to specify what that order is - e.g. by maintaining a value in a column (or a combination of columns) that you can use in the ORDER BY clause of the SELECT statement.
    If, for instance, you kept the time when the row was inserted in a special column (InsertedTime), you could use this in your query like this:
    select top (10)
      <column list>
      from <table or view>
      where <restriction(s)>
      order by InsertedTime desc;
    ML
    Matija Lah, SQL Server MVP
    http://milambda.blogspot.com

  • Sql query to get distinct count

    Hi
    I use SQL Server Management Studio
    can I have a sql query to get count as shown below against  each month column and name column to get distinct count.
    for example if there is two rows with the same date period and same name then the count should be one in first row and zero in the next row of the same data.
    Table Name: Table1
    Column: Month, Name
    Month
    Name
    Count
    12/1/2012 0:00
    AK
    1
    12/1/2012 0:00
    AK
    0
    12/1/2012 0:00
    AB
    1
    1/1/2013 0:00
    AK
    1
    1/1/2013 0:00
    AK
    0
    1/1/2013 0:00
    AB
    1
    3/1/2013 0:00
    AA
    1
    3/1/2013 0:00
    AK
    1
    3/1/2013 0:00
    AK
    0
    6/1/2013 0:00
    AA
    1
    6/1/2013 0:00
    AK
    1
    6/1/2013 0:00
    AK
    0
    9/1/2013 0:00
    AA
    1
    9/1/2013 0:00
    AK
    1
    9/13/2013 0:00
    AK
    1
    10/1/2013 0:00
    AA
    1
    10/1/2013 0:00
    AK
    1
    10/1/2013 0:00
    AK
    0

    Hi,
    Thanks for the query but this query gives the total count like shown below
    if see the second row in the below table AK for 2012-12-1 gives total count as 2 but need the query to show the first row as 1 and there after 0
    query result
    Month name cnt
    2012-12-01 00:00:00.000 AB 1
    2012-12-01 00:00:00.000 AK 2
    2012-12-01 00:00:00.000 AK 2
    2013-01-01 00:00:00.000 AB 1
    2013-01-01 00:00:00.000 AK 2
    2013-01-01 00:00:00.000 AK 2
    2013-03-01 00:00:00.000 AA 1
    2013-03-01 00:00:00.000 AK 2
    2013-03-01 00:00:00.000 AK 2
    2013-06-01 00:00:00.000 AA 1
    2013-06-01 00:00:00.000 AK 2
    2013-06-01 00:00:00.000 AK 2
    2013-09-01 00:00:00.000 AA 1
    2013-09-01 00:00:00.000 AK 1
    2013-09-13 00:00:00.000 AK 1
    2013-10-01 00:00:00.000 AA 1
    2013-10-01 00:00:00.000 AK 2
    2013-10-01 00:00:00.000 AK 2

  • How to get last 24 months data (if user not enter any kind of information)

    Hi all,
    I am want a report such that
    1. if user does not enter months he has to get last 24 months data(from current month)
    eg1.
    Sales
    currentmonth     300
    currentmonth-1  400
    currentmonth-20 500
    2. if user  enter months he has to get required months data(from current month)
    eg1.assume that user entered the interval of last two months.
    Sales
    currentmonth     300
    currentmonth-1  400.
    friends please me in desinging this query.
    It's very urgent,waiting for responses.
    Thanks,
    James.

    Hi james,
    1. if user does not enter months he has to get last 24 months data(from current month)
    ANS: U have to create variable Of  Currentmonth with The following
             Process Type: SAP Exit
             Variable Represent: Single value
            Variable Entry: Optional
    Check chekboxes for <b>Ready for Input</b> & Can be change in Query navigation.
    b) After this u have to give offsets by restricting this variable
    2. if user enter months he has to get required months data(from current month)
    ANS:
    For this U have to use Process Type: User-exit( User Entry)
    & After that specify the offsets by restriction.
    Thanks,
    kiran.
    Message was edited by:
            kiran manyam

  • Get last august month from current date

    Hi,
    I need to get last august month from current date.
    e.g if current date is 1-OCT-2013 need to get last August date i.e.1-AUG-2013
    e.g. if current date is 1-MAY-2013 need to get last August date i.e.1-AUG-2012

    Something like this?
    SQL> WITH table_x AS(
      2     SELECT SYSDATE dt from dual UNION ALL
      3     SELECT TO_DATE('20-05-2013','dd-mm-yyyy') from dual
      4  )
      5  --
      6  ---
      7  --
      8  SELECT  dt,
      9    CASE
    10      WHEN (dt >= Add_Months(TRUNC(dt,'YEAR'),7)) THEN
    11          Add_Months(TRUNC(dt,'YEAR'),7)
    12      WHEN (dt < Add_Months(TRUNC(dt,'YEAR'),7)) THEN
    13          Add_Months(TRUNC(dt,'YEAR'),7) - 365
    14    END cs
    15  FROM table_x;
    DT        CS
    24-OCT-13 01-AUG-13
    20-MAY-13 01-AUG-12

  • SQL query to get a list of relations between workitems

    How can I create a SQL query to get a list of all problems with related changes?  And all problems with related incidents?
    I have tried to join the tables RelationshipTypeDim with ProblemDimKey and ChangeRequestDim, but the results are not correct.

    The relationships in the data warehouse can be kind of tricky. The relationships are contained in the WorkItemRelatesToWorkItemFactvw table. This table lists the related items by their WorkItemDimKey, so you cannot reference directly from the ChangeRequestDimvw
    or ProblemDimvw. You will need to reference the WorkItemDimvw to get the WorkItemDimKey for each entry.
    The query below will get all of the related work items from the Change Request class. The way the joins work is ChangeRequestDimvw gets the list of change requests. Then inner joins WorkItemDimvw to get the WorkItemDimKey for each CR. Then inner joins WorkItemRelatesToWorkItemFactvw
    to get all of the CRs with related work items. Then inner joins the WorkItemDimvw again to get the ID of the related work item. 
    Now the tricky part is it appears that these relationship are set based on which item  that created the relationship. So you need to union a second query that reverse the relationship on the WorkItemRelatesToWorkItemFactvw. 
    This query should give you a good start on getting the related work items. You can filter it down from here if you only want to include problems.
    SELECT C.ID, WIWI.ID
    FROM dbo.ChangeRequestDimvw C
    INNER JOIN dbo.WorkItemDimvw WI ON
    WI.EntityDimKey = C.EntityDimKey
    INNER JOIN dbo.WorkItemRelatesToWorkItemFactvw AS WIRWI ON
    WIRWI.WorkItemDimKey = WI.WorkItemDimKey
    INNER JOIN dbo.WorkItemDimvw AS WIWI ON
    WIWI.WorkItemDimKey = WIRWI.WorkItemRelatesToWorkItem_WorkItemDimKey
    union
    SELECT C.ID, WIWI.ID
    FROM dbo.ChangeRequestDimvw C
    INNER JOIN dbo.WorkItemDimvw WI ON
    WI.EntityDimKey = C.EntityDimKey
    INNER JOIN dbo.WorkItemRelatesToWorkItemFactvw AS WIRWI ON
    WIRWI.WorkItemRelatesToWorkItem_WorkItemDimKey = WI.WorkItemDimKey
    INNER JOIN dbo.WorkItemDimvw AS WIWI ON
    WIWI.WorkItemDimKey = WIRWI.WorkItemDimKey
    Order by C.ID
    Matthew Dowst |
    Blog | Twitter

  • SQL query to get the Datetime 06 hours prior to the table Datetime value

    Hi Experts,
                    I'm just trying to create a SQL query to get the Datetime which should be 06 hours prior to my Table column value(Executiontime),
    Eg: my Executiontime(column) value is 07:00AM means, this query should fetch the detail of first VMName from table at 01:00AM, 
    SQL Table Name: TestTable
    Columns: VMName(varchar),status(varchar) Executiontime(Datetime)
    SQL Query : Select Top 1 VMName from
    TestTable where convert(date,Exeutiontime)=convert(date,getdate()) and
    status='0' and ExecutionTime > dateadd(hour,6,getdate())
    Request someone to alter this Query to my requirement or give me the new one.
    Regards,
    Sundar
    Sundar

    Hi All,
            Thanks for your Prompt response. I tried the below queries, but still I don't have any luck. Actually the queries are returning the value before the condition met (say when the time difference is more than 06 hours). I want the
    query to return exactly @ 06 hour difference or less than 06 hours,
    Query 01: Select Top 1 VMName from TestTable where
    convert(date,Exeutiontime)=convert(date,getdate())
    and status='0'
    and ExecutionTime >
    dateadd(hour,-6,getdate())
    Query 02: Select
    Top 1 VMName from TestTable where
    status='0'
    and ExecutionTime >
    dateadd(hour,-6,getdate())
    Query 03: Select
    Top 1 VMName from TestTable where status='0'
    and ExecutionTime >
    dateadd(hour,-6,ExecutionTime)
              Can someone point out the mistake please.
    Regards,
    Sundar
    Sundar

  • SQL query to get the list of approvals

    Hi,
    Could someone let me know the SQL query to get the list of all the pending approvals for a user in OIM 11g R2.
    Thanks

    There are a few ways to do this:
    -  The easiest would be to use a Relationship Query from the CMC. To do this, go to the Universes section on the CMC, right click on the relevant universe, select tools >> Check Relationships.
    - Use Query Builder. You will need more than one query to pull the information you need. You could try something like the below (for Webi)
    SELECT SI_NAME, SI_WEBI, SI_DATACONNECTION FROM CI_APPOBJECTS
    WHERE SI_KIND = 'universe' and SI_NAME = 'Universe Name'
    This will give you a list of Webi Reports by SI_ID.
    You'll need another query to list Webi report names:
    SELECT SI_NAME FROM CI_INFOOBJECTS WHERE SI_ID IN (SI_ID from query above)
    - This is trivial via Auditing / the Activity universe. This of course will only return reports that have already run.
    Best.
    Srinivas

  • Dear Gurus, Can i have SQL query to get Ar cash receipt appled lines details in R12

    Dear Gurus, Can i have SQL query to get Ar cash receipt appled lines details in R12

    I need to get ar and ap details from GL through xla.For ex: Reference1,reference2,reference3,reference4,reference 5, in 11i these column values are displayed in gl_je_lines where as in R12 we need to join xla tables with gl_sl_link_id and gl_sl_link_table
    Can someone provide query to get subledger details particularly ar and ap?.How are the AP & GL Journal Tables linked? [ID 1188714.1]
    R12 Mapping Between Subledger Tables, SLA and GL Tables [ID 871622.1]
    eTRM
    http://etrm.oracle.com/
    Thanks,
    Hussein

  • SQL query to get transaction detail from DEFERRED_TRAN_ID

    Hi,
    I'm using Oracle Advance Replication and get the transaction detail from Enterprise Manager Console.
    So instead of using the console.
    Is there a way any SQL query which get transaction details like SQL query fired, old and new column value etc from DEFERRED_TRAN_ID.
    thanks

    quote:
    Originally posted by:
    lucapac
    I have two tables: tblWorkers and tblSkills. tblWorkers has a
    column, Skills, which is populated from a multiple-checkbox form
    field with one or more skill_IDs from tblSkills, so each
    tblWorkers.Skills consists of a list of one or more comma-delimited
    values. For any Skill_ID, I need to generate a listing of all
    Workers with the corresponding skill, so I have tried to do
    something along the lines of SELECT WorkerName FROM tblWorkers
    WHERE Skills IN (Skills, #FORM.Skill_ID#) ... or WHERE Skills IN
    (ListFind(Skills, #FORM.Skill_ID#)) ... etc. ??? My results (once I
    got data type mismatches out of the way) return all Workers, not
    just those with the desired Skill. There must be an easy way to do
    this ... How do people with a bit more CF/SQL experience than I
    have do this???
    As Kronin implied, we normalize our databases. If you don't
    understand that answer, the book "Database Design for Mere Mortals"
    is often mentioned on this forum.

  • SQL Query to get Project Plan Name and Resource Name from Reporting database of Project Server 2007

    Can you please help me to write an SQL Query to get Project Plan Name and Resource Name from Reporting database of Project Server 2007. Thanks!!

    Refer
    http://gallery.technet.microsoft.com/projectserver/Server-20072010-SQL-Get-a99d4bc6
    SELECT
    dbo.MSP_EpmAssignment_UserView.ProjectUID,
    dbo.MSP_EpmAssignment_UserView.TaskUID,
    dbo.MSP_EpmProject_UserView.ProjectName,
    dbo.MSP_EpmTask_UserView.TaskName,
    dbo.MSP_EpmAssignment_UserView.ResourceUID,
    dbo.MSP_EpmResource_UserView.ResourceName,
    dbo.MSP_EpmResource_UserView.ResourceInitials
    INTO #TempTable
    FROM dbo.MSP_EpmAssignment_UserView INNER JOIN
    dbo.MSP_EpmProject_UserView ON dbo.MSP_EpmAssignment_UserView.ProjectUID = dbo.MSP_EpmProject_UserView.ProjectUID INNER JOIN
    dbo.MSP_EpmTask_UserView ON dbo.MSP_EpmAssignment_UserView.TaskUID = dbo.MSP_EpmTask_UserView.TaskUID INNER JOIN
    dbo.MSP_EpmResource_UserView ON dbo.MSP_EpmAssignment_UserView.ResourceUID = dbo.MSP_EpmResource_UserView.ResourceUID
    SELECT
    ProjectUID,
    TaskUID,
    ProjectName,
    TaskName,
    STUFF((
    SELECT ', ' + ResourceInitials
    FROM #TempTable
    WHERE (TaskUID = Results.TaskUID)
    FOR XML PATH (''))
    ,1,2,'') AS ResourceInitialsCombined,
    STUFF((
    SELECT ', ' + ResourceName
    FROM #TempTable
    WHERE (TaskUID = Results.TaskUID)
    FOR XML PATH (''))
    ,1,2,'') AS ResourceNameCombined
    FROM #TempTable Results
    GROUP BY TaskUID,ProjectUID,ProjectName,TaskName
    DROP TABLE #TempTable
    -Prashanth

  • Sql query to get numbers from 0 to 99

    How can we write an sql query to get values from 0 to 99 ... this shouldn't be coming from any table

    Bawer wrote:
    Result: (quickly replied by oracle)
    SQL-Error: ORA-30009 (Not enough memory for connect by - 1GB reserved for oracle on VirtualBox)Oracle version? Takes about 8 seconds on my:
    SQL> select * from v$version
      2  /
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    Elapsed: 00:00:00.14
    SQL> select count(*) from (
      2  SELECT LEVEL - 1
      3  FROM DUAL
      4  CONNECT BY LEVEL <= 10000000
      5  )
      6  /
      COUNT(*)
      10000000
    Elapsed: 00:00:07.76
    SQL> SY.

  • Sql query to get df -h output

    Hi,
    Please provide me sql query to get df -h output. I don't have server (Solaris) access to get df -h output.
    I want to check the space availability on the mount point to increase space at the tablespace level.
    Thank you..

    sb92075 wrote:
    SQL> host df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1             220G   66G  144G  32% /
    none                  1.9G  308K  1.9G   1% /dev
    none                  1.9G  959M  896M  52% /dev/shm
    none                  1.9G   96K  1.9G   1% /var/run
    none                  1.9G     0  1.9G   0% /var/lock
    none                  1.9G     0  1.9G   0% /lib/init/rw
    The "host" command executes on the client machine running sqlplus, not the server that the OP says he doesn't have access to.

  • SQL query to get DDL for Adding PK.

    Guys,
    I'm looking for SQL query that gets me the "ALTER TABLE <TABLE_NAME> ADD CONSTRAINT <constraint_name> PRIMARY KEY (X,Y,...);" statments of all tables in my schema containing Primary Keys.
    Could someone help me with the query please?
    Regards,
    Bhagat

    You need this
    SELECT 'ALTER TABLE '||table_name||' ADD CONSTRAINT '||constraint_name||' PRIMARY KEY ('||column_name||');'
      FROM ( SELECT uc.table_name
                   ,uc.constraint_name
                   ,RTRIM (XMLAGG (XMLELEMENT (ucc, column_name || ',')).extract ('//text()'), ',')  column_name
              FROM user_constraints        uc
                  ,user_cons_columns       ucc
             WHERE uc.constraint_type      = 'P'
               AND uc.constraint_name      = ucc.constraint_name
          GROUP BY uc.table_name
                  ,uc.constraint_name
    ORDER BY table_name;   Regards
    Arun

  • Get three previous records of the current record

    Hi,
    I need to get three previous records of the current record in an Oracle Form
    Sorry for the lengthy explanation:
    I have a table name: ARCHIVE_DATA with column name: coll_time and its data type DATE.
    SQL> SELECT COLL_TIME FROM ARCHIVE_DATA;
    COLL_TIME
    12-AUG-05
    12-AUG-05
    12-AUG-05
    12-AUG-05
    12-AUG-05
    12-AUG-05
    12-AUG-05
    12-AUG-05
    12-AUG-05
    12-AUG-05
    10 rows selected.
    SQL> select to_char(coll_time,'dd-mon-yyyy:HH:MI:SS') from ARCHIVE_DATA;
    TO_CHAR(COLL_TIME)
    12-aug-2005:02:42:00
    12-aug-2005:02:43:00
    12-aug-2005:02:44:00
    12-aug-2005:02:45:00
    12-aug-2005:02:46:00
    12-aug-2005:02:47:00
    12-aug-2005:02:48:00
    12-aug-2005:02:49:00
    12-aug-2005:02:50:00
    12-aug-2005:02:51:00
    10 rows selected.
    This is the Requirement:
    In a Form's Block(BLK1), for example: the current_record is the fifth record from the top
    (i.e. 12-aug-2005:02:46:00)
    When the fifth record is the current_record and When I click a button, three previous records of the
    current_record should be populated on the screen.
    See what I did:
    I created another table same as the first table(ARCHIVE_DATA) and its name is: THREE_RECS.
    I am inserting three records from the first table(ARCHIVE_DATA) into the second table: THREE_RECS
    which are less than the current record and ORDER BY DESC.
    CANVAS:
    Two blocks (BLK1, BLK2) based on ARCHIVE_DATA and THREE_RECS are on the same CANVAS.
    But the first block (BLK1) which is based on the first table:ARCHIVE_DATA is populated with one record and the
    second block (BLK2) is empty.
    So when I click a particular button (ex: prev_recs), the second block(BLK2) should be populated with
    three previous records of the current record( :BLK1.COLL_TIME)
    (off course :BLK2 populates with one record and use arrows or scrollbar to get the other two records)
    This is the code I wrote in the trigger and followed by the error:
    1 BEGIN
    2 DECLARE
    3 cursor c1 IS
    4           SELECT MONITOR_ID,
    5               SAMPLE_ID,
    6               COLL_TIME,
    7               DEW_POINT
    8          FROM ARCHIVE_DATA;
    9 cursor c2(passing_date IN date) IS
    10           SELECT MONITOR_ID,
    11               SAMPLE_ID,
    12               COLL_TIME,
    13               DEW_POINT
    14          FROM (SELECT MONITOR_ID,
    15               SAMPLE_ID,
    16               COLL_TIME,
    17               DEW_POINT
    18          FROM ARCHIVE_DATA
    19          ORDER BY COLL_TIME desc)
    20      WHERE COLL_TIME < passing_date;
    21     BEGIN
    22     FOR cur_rec in c1
    23     LOOP
    24          IF (cur_rec.COLL_TIME = to_date(:BLK.COLL_TIME,'dd-mon-yyyy:HH24:mi:ss')) then
    25     FOR second_cur_rec in c2(second_cur_rec.COLL_TIME)
    26          LOOP
    27      IF c2%rowcount < 4 then
    28               BEGIN
    29               INSERT INTO THREE_RECS
    30                    values(second_cur_rec.MONITOR_ID,
    31                         second_cur_rec.SAMPLE_ID,
    32                         second_cur_rec.COLL_TIME,
    33                         second_cur_rec.DEW_POINT);
    34               COMMIT;
    35               END IF;
    36 END LOOP;
    37 END IF;
    38 END LOOP;
    39 END;
    40 END;
    This is the error I am getting:
    Error 103 at line 14
    Encountered the symbol "(" when expecting one of the following
    a PL/SQL variable or double quoted string
    an expanded name
    an expanded name link
    a table reference __expression
    a key word
    Resuming parse at line 126, column 46
    Thanks in advance

    Why not just a simple select that doesn't involve a second table and PL/SQL?
    sql>select * from t1 order by dt;
    DT
    08/12/2005 02:42:00am
    08/12/2005 02:43:00am
    08/12/2005 02:44:00am
    08/12/2005 02:45:00am
    08/12/2005 02:46:00am
    08/12/2005 02:47:00am
    08/12/2005 02:48:00am
    08/12/2005 02:49:00am
    08/12/2005 02:50:00am
    08/12/2005 02:51:00am
    10 rows selected.
    sql>select dt
      2    from (select dt, row_number() over (order by dt desc) rn
      3            from t1
      4           where dt < to_date('12-aug-2005:02:46:00', 'dd-mon-yyyy:hh:mi:ss'))
      5   where rn <= 3
      6   order by dt;
    DT
    08/12/2005 02:43:00am
    08/12/2005 02:44:00am
    08/12/2005 02:45:00am
    3 rows selected.If the use of an analytical function (row_number()) is a problem with Forms, then the query can also be done as:
    sql>select dt
      2    from (select dt
      3            from (select dt
      4                    from t1
      5                   where dt < to_date('12-aug-2005:02:46:00', 'dd-mon-yyyy:hh:mi:ss')
      6                   order by dt desc)
      7           where rownum <= 3)
      8   order by dt;
    DT
    08/12/2005 02:43:00am
    08/12/2005 02:44:00am
    08/12/2005 02:45:00am
    3 rows selected.

Maybe you are looking for

  • How do I block other users on one computer?

    My family shares one PC. When I open itunes, everything that's been downloaded by everyone is automatically shown, even when we're not signed in to individual accounts. For example, if I download a song, it's added to everyone's account. I would like

  • Differences in field IAKOSIST in MCIZ and in MCI8

    Hello! Through tx IFCU I have posted 5 documents, 50 L each one. 1,3 u20AC/L, so every time I filled 50L, IAKOSIST was adding 65u20AC After 5 documents posted, total is 325u20AC=65u20AC*5 This is the value of IAKOSIST is 325u20AC. I see in MCI8 and i

  • NVidia's new 197.16 driver release

    nVidia has just released the new 197.16 driver for most of their mobile GPUs. Wondering if anyone has upgraded yet, is the update good or bad and has anyone had any issues with the new driver?  The last driver update to the 195.62 had some overheatin

  • ACR Cache Constantly Rebuilds

    Everytime I click on a folder of images the cache rebuilds from scratch. This behavior started this week. I've trashed prefs but it didn't help. I'm using CS3 on Dell computer with 4GB of ram and Vista.

  • New set up from SuperDuper

    Hi I want to reinstall X.6 on my iMac and use what I'v saved through SuperDuper to transfer thing that I need that I have saved or updated from an external hard drive, like Skype contacts, Google Earth saved photos, iTunes tunes, Address book contact