Split date range without Calendar table

Hi guys!
Is there any way to split date ranges (to days / hours / minutes) in one table without using Calendar table (without joining 2 tables)?
Reason I'm asking this is because we have a huge Fact table (500+ million records) and joining small Calendar table and this huge Fact table takes considerable amount of time.
So far we tried:
Join 2 tables (Fact and Calendar)
Cross Applying Calendar and table-valued function (that returns necessary measures from Fact table)
and both approaches took few minutes for 1 day to complete.
Most successful test we had so far was using CLR and executing table-valued functions in parallel for different 15 minute periods. This way we were able to get the results in 3-4 seconds.
Anyway, is there any effective pure T-SQL way to accomplish this?
Thanks in advance,
Miljan

Try using a temporary table rather than table variable.. You can have a CI on the date column.
or
declare @PeriodStart datetime2(0) = '2013-01-05 00:00:00'
declare @PeriodEnd datetime2(0) = '2013-01-05 02:00:00'
select T.PeriodStart, T.PeriodEnd, count(Column1)as count1, sum(Column2)as sum1
from Fact F with(nolock)WHERE F.[Start]>= '19000101' AND <=@PeriodStartand F.[End] >= @PeriodEnd and F.[End] <='20200101'
group by T.PeriodStart, T.PeriodEnd
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

  • MTD,YTD,PTD without calendar table

              QTY
          Month
          Year
    QTY_YTD
    QTY_MTD
    QTY_PTD
    10
    1
    2010
    20
    2
    2010
    20
    1
    2012
    30
    3
    2012
    Above is how my table looks like and  all are having integer datatypes
    I dont have a calendar table to join hence without calendar table can I compute YTD,MTD,PTD?
    If so what will be the case queries
    Mudassar

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
    use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
    This is minimal polite behavior on SQL forums. 
    Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML.
    The report period table gives a name to a range of dates that is common to the entire enterprise. 
    CREATE TABLE Something_Report_Periods
    (something_report_name CHAR(10) NOT NULL PRIMARY KEY
       CHECK (something_report_name LIKE <pattern>),
     something_report_start_date DATE NOT NULL,
     something_report_end_date DATE NOT NULL,
      CONSTRAINT date_ordering
        CHECK (something_report_start_date <= something_report_end_date),
    etc);
    These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601
    data format required by Standard SQL and it is language independent. The pattern for validation is '[12][0-9][0-9][0-9]-00-00' and '[12][0-9][0-9][0-9]-[01][0-9]-00'
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Need work around to split date range into months

    Hi All,
    I have a requirement to split the data range in source table to months.
    here is the source table
    ID Sdate Edate Code
    ===============================
    501 1/4/2009 4/30/2009 AH
    501 5/1/2009 9/30/2009 AB
    502 1/1/2014 5/31/2014 AC
    Here is my target
    ID Sdate Edate Code
    ================================
    501 1/4/2009 1/31/2009 AH
    501 2/1/2009 2/28/2009 AH
    501 3/1/2009 3/31/2009 AH
    501 4/1/2009 4/30/2009 AH
    501 5/1/2009 5/31/2009 AB
    501 6/1/2009 6/30/2009 AB
    501 7/1/2009 7/31/2009 AB
    501 8/1/2009 8/31/2009 AB
    501 9/1/2009 9/30/2009 AB
    502 1/1/2014 1/31/2014 AC
    502 2/1/2014 2/28/2014 AC
    502 3/1/2014 3/31/2014 AC
    502 4/1/2014 4/30/2014 AC
    502 5/1/2014 5/31/2014 AC
    Please provide some input.
    Thanks a lot in advance.

    That means your database is in a different locale than mine. When I select cast(Edate as varchar), I get the date returned in the format 'YYYY-MM-DD'. That's why I use(left(cast(Edate as varchar),4) + right(left(cast(Edate as varchar),7),2) to get it the format 'YYYYMM' before converting it into an integer.
    Your database most probably returns dates in a format like D/MM/YYYY. That's the reason for the error you get. You'll have to select (right(cast(Edate as varchar),4) + left(right(cast(Edate as varchar),7),2) to obtain the same results as I do.
    Alternatively, you can use the convert function to obtain the same results:
         select max(
         cast(left(convert(varchar,Edate,102),4) + right(left(convert(varchar,Edate,102),7),2) as int) + 1 -
         cast(left(convert(varchar,Sdate,102),4) + right(left(convert(varchar,Sdate,102),7),2) as int))
         FROM src_split'
    It's always good practice to test the SQL-statements in your database client first before including them in a DS script.
    Also, I suddenly realise this statement will only return the correct result when your Edate and Sdate are always within the same year. If that's not the case, but the gap between Edate and Sdate is always less than 10 years, change your statement to:
         select max(
         cast(left(convert(varchar,Edate,102),4) + right(left(convert(varchar,Edate,102),7),2) as int) + 1 -
         cast(left(convert(varchar,Sdate,102),4) + right(left(convert(varchar,Sdate,102),7),2) as int)) % 88
         FROM src_split'

  • Neen help with date range searches for Table Sources

    Hi all,
    I need help, please. I am trying to satisfy a Level 1 client requirement for the ability to search for records in crawled table sources by a date and/or date range. I have performed the following steps, and did not get accurate results from Advanced searching for date. Please help me understand what I am doing wrong, and/or if there is a way to define a date search attribute without creating a LOV for a date column. (My tables have 500,00 rows.)
    I am using SES 10.1.8.3 on Windows 32.
    My Oracle 10g Spatial Table is called REPORTS and this table has the following columns:
    TRACKNUM Varchar2
    TITLE Varchar2
    SUMMARY CLOB
    SYMBOLCODE Varchar2
    Timestamp Date
    OBSDATE Date
    GEOM SDO_GEOMETRY
    I set up the REPORTS table source in SES, using TRACKNUM as the Primary Key (unique and not null), and SUMMARY as the CONTENT Column. In the Table Column Mappings I defined TITLE as String and TITLE.
    Under Global Settings > Search Attributes I defined a new Search Attribute (type Date) called DATE OCCURRED (DD-MON-YY).
    Went back to REPORTS source previously defined and added a new Table Column Mapping - mapping OBSDATE to the newly defined DATE OCCURRED (DD-MON-YY) search attribute.
    I then modified the Schedule for the REPORTS source Crawler Policy to “Process All Documents”.
    Schedule crawls and indexes entire REPORTS table.
    In SES Advanced Search page, I enter my search keyword, select Specific Source Group as REPORTS, select All Match, and used the pick list to select the DATE OCCURRED (DD-MON-YY) Attribute Name, operator of Greater than equal, and entered the Value 01-JAN-07. Then the second attribute name of DATE_OCCURRED (DD-MON-YY), less than equals, 10-JAN-07.
    Search results gave me 38,000 documents, and the first 25 I looked at had dates NOT within the 01-JAN-07 / 10-JAN-07 range. (e.g. OBSDATE= 24-MAR-07, 22-SEP-), 02-FEB-08, etc.)
    And, none of the results I opened had ANY dates within the SUMMARY CLOB…in case that’s what was being found in the search.
    Can someone help me figure out how to allow my client to search for specific dated records in a db table using a single column for the date? This is a major requirement and they are anxiously awaiting my solution.
    Thanks, in advance….

    raford,
    Thanks very much for your reply. However, from what I've read in the SES Admin Document is that (I think) the date format DD/MM/YYYY pertains only to searches on "file system" sources (e.g. Word, Excel, Powerpoint, PDF, etc.). We have 3 file system sources among our 25 total sources. The remaining 22 sources are all TABLE or DATABASE sources. The DBA here has done a great job getting the data standardized using the typical/default Oracle DATE type format in our TABLE sources (DD-MON-YY). Our tables have anywhere from 1500 rows to 2 million rows.
    I tested your theory that the dates we are entering are being changed to Strings behind the scenes and on the Advanced Page, searched for results using OBSDATE equals 01/02/2007 in an attempt to find data that I know for certain to be in the mapped OBSDATE table column as 01-FEB-07. My result set contained data that had an OBSDATE of 03-MAR-07 and none containing 01-FEB-07.
    Here is the big issue...in order for my client to fulfill his primary mission, one of the top 5 requirements is that he/she be able to find specific table rows that are contain a specific date or range of dates.
    thanks very much!

  • Need help with date range searches for Table Sources in SES

    Hi all,
    I need help, please. I am trying to satisfy a Level 1 client requirement for the ability to search for records in crawled table sources by a date and/or date range. I have performed the following steps, and did not get accurate results from Advanced searching for date. Please help me understand what I am doing wrong, and/or if there is a way to define a date search attribute without creating a LOV for a date column. (My tables have 500,00 rows.)
    I am using SES 10.1.8.3 on Windows 32.
    My Oracle 10g Spatial Table is called REPORTS and this table has the following columns:
    TRACKNUM Varchar2
    TITLE Varchar2
    SUMMARY CLOB
    SYMBOLCODE Varchar2
    Timestamp Date
    OBSDATE Date
    GEOM SDO_GEOMETRY
    I set up the REPORTS table source in SES, using TRACKNUM as the Primary Key (unique and not null), and SUMMARY as the CONTENT Column. In the Table Column Mappings I defined TITLE as String and TITLE.
    Under Global Settings > Search Attributes I defined a new Search Attribute (type Date) called DATE OCCURRED (DD-MON-YY).
    Went back to REPORTS source previously defined and added a new Table Column Mapping - mapping OBSDATE to the newly defined DATE OCCURRED (DD-MON-YY) search attribute.
    I then modified the Schedule for the REPORTS source Crawler Policy to “Process All Documents”.
    Schedule crawls and indexes entire REPORTS table.
    In SES Advanced Search page, I enter my search keyword, select Specific Source Group as REPORTS, select All Match, and used the pick list to select the DATE OCCURRED (DD-MON-YY) Attribute Name, operator of Greater than equal, and entered the Value 01-JAN-07. Then the second attribute name of DATE_OCCURRED (DD-MON-YY), less than equals, 10-JAN-07.
    Search results gave me 38,000 documents, and the first 25 I looked at had dates NOT within the 01-JAN-07 / 10-JAN-07 range. (e.g. OBSDATE= 10-MAR-07, 22-SEP-07, 02-FEB-08, etc.)
    And, none of the results I opened had ANY dates within the SUMMARY CLOB…in case that’s what was being found in the search.
    Can someone help me figure out how to allow my client to search for specific dated records in a db table using a single column for the date? This is a major requirement and they are anxiously awaiting my solution.
    Thanks very much, in advance….

    raford,
    Thanks very much for your reply. However, from what I've read in the SES Admin Document is that (I think) the date format DD/MM/YYYY pertains only to searches on "file system" sources (e.g. Word, Excel, Powerpoint, PDF, etc.). We have 3 file system sources among our 25 total sources. The remaining 22 sources are all TABLE or DATABASE sources. The DBA here has done a great job getting the data standardized using the typical/default Oracle DATE type format in our TABLE sources (DD-MON-YY). Our tables have anywhere from 1500 rows to 2 million rows.
    I tested your theory that the dates we are entering are being changed to Strings behind the scenes and on the Advanced Page, searched for results using OBSDATE equals 01/02/2007 in an attempt to find data that I know for certain to be in the mapped OBSDATE table column as 01-FEB-07. My result set contained data that had an OBSDATE of 03-MAR-07 and none containing 01-FEB-07.
    Here is the big issue...in order for my client to fulfill his primary mission, one of the top 5 requirements is that he/she be able to find specific table rows that are contain a specific date or range of dates.
    thanks very much!

  • Help with PowerShell to delete a date range of Calendar meetings

    I'm trying to delete all Calendar items within a date range, from a mailbox. But first I'd like to just make sure my date filter is actually working.
    Here is what i HAVE working:
    Search-Mailbox ME -SearchQuery "Received:01/01/2010..03/18/2010" -EstimateResultOnly
    This works, it gives me a count of all things in my mailbox
    Search-Mailbox ME -SearchQuery kind:meetings -EstimateResultOnly
    This works, it gives me a count of all Meetings in my Calendar.
    I reallly need now to get the date range to work in conjunction with the kind:meetings.
    How can I do that?

    Hi,
    The results include the deleted meetings. I test it in my lab.
    So the command to delete Calendar meetings with a date range is this:
    Search-Mailbox "username" -SearchQuery "Received:01/01/2010..3/18/2015 kind:meetings" –EstimateResultOnly -DeleteContent
    Best Regards.
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected]
    Lynn-Li
    TechNet Community Support

  • Show date range in Calendar

    Hello,
    I have created a calendar using the calendar widget available in APEX 4.1
    This is SQL based and also has a input form to create a new event by clicking on a particular date.
    select id,activity,code,location,date_from from cal_activity a,cal_activity_type t where a.type_id = t.type_id order by code,activity
    The calendar works fine when I try to create/update an event.
    However the date range is not visible for events that last longer than one day.
    The event end_date is being captured.Then why is it that the calendar shows only sysdate events and not the ones before and after?
    Here is the link to the application :-
    https://apex.oraclecorp.com/pls/apex/f?p=17137:1:317233609286701:::::
    Could someone kindly explain what I might be missing here?
    Thanks and Regards,
    Priya Jetley
    Edited by: user637618 on Feb 13, 2012 1:35 AM

    According to CRM Support
    CRM Support wrote:the ability to block off a multi-day appointment in the Weekly and Monthly Calendar Views is not currently available on our product. However, we have submitted this functionality to our engineers as an enhancement request. At this time we are unable to provide an estimated time of implementation while this request is being analyzed and processed.
    Simple things seem hard for the Siebel guys.
    Edited by: nsidev on Aug 14, 2009 7:11 AM
    Edited by: nsidev on Aug 14, 2009 7:12 AM

  • Compare date ranges in two tables

    Hello,
    I'm trying to figure out a way of comparing the range of dates between two date columns from a single row in one table, to the range of dates from multiple rows in a related table.
    t1 (t1_id, absent_start_date, absent_stop_date)
    t2 (t2_id, t1_id, cover_start_date, cover_end_date)
    t2 has multiple rows for each row in t1
    I need to select rows from t1, in which the set of days for rows in t2 do not match the set of days between absent_start_date and absent_stop_date.
    For example, assume this row in t1:
    1, '10/08/2007', '15/08/2007'
    The set of days would be 10/08/07,11/08/07,12/08/07,13/08/07,14/08/07,15/08/07
    and these rows in t2
    1, 1, '10/08/2007', '11/08/2007'
    2, 1, '12/08/2007', '12/08/2007'
    3, 1, '14/08/2007', '15/08/2007'
    The set of days would be 10/08/07,11/08/07,12/08/07,14/08/07,15/08/07
    In this case, the related rows in t2 do not cover the same date range as the master row in t1, and so I need to select this row from t1 somehow.
    Any ideas anyone?
    Thanks.

    I am not sure about the exact output you want, but the next query will give you all gaps in the periods, so the presence of a row in this output may be enough?
    SQL> create table t1
      2  as
      3  select 1 t1_id, date '2007-08-10' absent_start_date, date '2007-08-15' absent_stop_date from dual union all
      4  select 2, date '2007-09-01', date '2007-09-10' from dual
      5  /
    Tabel is aangemaakt.
    SQL> create table t2
      2  as
      3  select 1 t2_id, 1 t1_id, date '2007-08-10' cover_start_date, date '2007-08-11' cover_stop_date from dual union all
      4  select 2, 1, date '2007-08-12', date '2007-08-12' from dual union all
      5  select 3, 1, date '2007-08-14', date '2007-08-15' from dual union all
      6  select 4, 2, date '2007-09-03', date '2007-09-05' from dual union all
      7  select 5, 2, date '2007-09-07', date '2007-09-08' from dual
      8  /
    Tabel is aangemaakt.
    SQL> select  *
      2    from ( select t2.t1_id
      3                , lag(t2.end_date+1,1,t1.absent_start_date) over (partition by t2.t1_id order by t2.start_date) gap_start_date
      4                , t2.start_date-1 gap_end_date
      5             from t1
      6                , ( select t1_id
      7                         , cover_start_date start_date
      8                         , cover_stop_date end_date
      9                      from t2
    10                     union all
    11                    select t1_id
    12                         , absent_stop_date+1
    13                         , absent_stop_date+1
    14                      from t1
    15                  ) t2
    16            where t1.t1_id = t2.t1_id
    17         )
    18   where gap_start_date <= gap_end_date
    19   order by t1_id
    20       , gap_start_date
    21  /
                                     T1_ID GAP_START_DATE      GAP_END_DATE
                                         1 13-08-2007 00:00:00 13-08-2007 00:00:00
                                         2 01-09-2007 00:00:00 02-09-2007 00:00:00
                                         2 06-09-2007 00:00:00 06-09-2007 00:00:00
                                         2 09-09-2007 00:00:00 10-09-2007 00:00:00
    4 rijen zijn geselecteerd.Regards,
    Rob.

  • Date range using dual table

    I want to get the range of two date(two variables: e_date and s_date) in a proc code. Or I want to ensure the date range must be less than 180 days.
    I used: "SELECT to_date(:e_date1, 'DDMONYY') - to_date(:s_date1, 'DDMONYY') INTO :count FROM DUAL;" and "if (count <= 180)" in a proc code.
    I don't know whether there is some risks in that? Thanks.

    BluShadow wrote:
    EdStevens wrote:
    bill wrote:
    "There's a risk in using 2-digit years. Use 4-digit years to reduce errors."
    Because I only want to get the date range of two date(using :sdate and :edate variables), in anther words the relative value of two days, I think that 2 digits for the year part is permitted. Isn't it? Thanks.I can't believe Y2K was only 12 years ago and we are already forgetting the lessons.
    I few years ago (even closer to Y2k) I was in a conversation with a young programmer who expressed the opinion that Y2K was just a big hoax. After all, none of the bad things predicted really happened. I had to inform him that had he been around in 1998 and 1999 (I think he was in junior high at the time) he would have seen that the reason "nothing happened" is because armies of people like me spent two years working our a**** off to make sure nothing happened.Yep.... I was there too... checking...re-working...testing... just to ensure that everything went ok. I even had to go in and check all the servers on New Years day (oh, yes I made sure I got paid well for that one!) to make sure they were all still up and running. Only one server had a slight issue, but it was an oldie and not fixable and not a major issue.
    Yeah, me too. Sat in the server room all night watching the server light blink :-)
    Please, do yourself and your colleagues a favor and banish from your brain the entire concept of 2-digit years. When "modern" data processing got started, data records were limited to an 80-column punch card. We no longer have those constraints.4 digit years all the time.... oh yeah!You're just burying your head in the sand. It's time to start thinking about the Y10K problem, assuming we get through Dec. 21 :-)
    John

  • Query to split one row to multiple based on date range

    Hi,
    I need to split single row into multple based on date range defined in a column, start_dt and end_dt
    I have a data
    ID      From date             End_dt                measure
    1        2013-12-01         2013-12-03            1
    1        2013-12-04         2013-12-06            2
    2        2013-12-01         2013-12-02            11
    3        2013-12-03         2013-12-04          22
    I required output as
    ID      Date                      measure
    1        2013-12-01              1
    1        2013-12-02              1
    1        2013-12-03              1
    1        2013-12-04              2
    1        2013-12-05              2
    1        2013-12-06              2
    2        2013-12-01             11
    2        2013-12-02             11
    3        2013-12-03             22
    3        2013-12-04            22
    Please provide me sq, query for the same
    Amit
    Please mark as answer if helpful
    http://fascinatingsql.wordpress.com/

    Have a calendar table for example and then probably using UNION ALL from date  and stat date JOIN the Calendar table
    SELECT ID,From date  FROM tbl
    union all
    SELECT ID,End_dt FROM tbl
    with tmp(plant_date) as
       select cast('20130101' as datetime)
       union all
       select plant_date + 1
         from tmp
        where plant_date < '20131231'
    select*
      from  tmp
    option (maxrecursion 0)
    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

  • Need help to join two tables using three joins, one of which is a (between) date range.

    I am trying to develop a query in MS Access 2010 to join two tables using three joins, one of which is a (between) date range. The tables are contained in Access. The reason
    the tables are contained in access because they are imported from different ODBC warehouses and the data is formatted for uniformity. I believe this cannot be developed using MS Visual Query Designer. I think writing a query in SQL would be suiting this project.
    ABCPART links to XYZPART. ABCSERIAL links to XYZSERIAL. ABCDATE links to (between) XYZDATE1 and ZYZDATE2.
    [ABCTABLE]
    ABCORDER
    ABCPART
    ABCSERIAL
    ABCDATE
    [ZYXTABLE]
    XYZORDER
    XYZPART
    XYZSERIAL
    XYZDATE1
    XYZDATE2

    Thank you for the looking at the post. The actual table names are rather ambiguous. I renamed them so it would make more sense. I will explain more and give the actual names. What I do not have is the actual data in the table. That is something I don't have
    on this computer. There are no "Null" fields in either of the tables. 
    This table has many orders (MSORDER) that need to match one order (GLORDER) in GLORDR. This is based on MSPART joined to GLPART, MSSERIAL joined to GLSERIAL, and MSOPNDATE joined if it falls between GLSTARTDATE and GLENDDATE.
    [MSORDR]
    MSORDER
    MSPART
    MSSERIAL
    MSOPNDATE
    11111111
    4444444
    55555
    2/4/2015
    22222222
    6666666
    11111
    1/6/2015
    33333333
    6666666
    11111
    3/5/2015
    This table has one order for every part number and every serial number.
    [GLORDR]
    GLORDER
    GLPART
    GLSERIAL
    GLSTARTDATE
    GLENDDATE
    ABC11111
    444444
    55555
    1/2/2015
    4/4/2015
    ABC22222
    666666
    11111
    1/5/2015
    4/10/2015
    AAA11111
    555555
    22222
    3/2/2015
    4/10/2015
    Post Query table
    GLORDER
    MSORDER
    GLSTARTDATE
    GLENDDATE
    MSOPNDATE
    ABC11111
    11111111
    1/2/2015
    4/4/2015
    2/4/2015
    ABC22222
    22222222
    1/5/2015
    4/10/2015
    1/6/2015
    ABC22222
    33333333
    1/5/2015
    4/10/2015
    3/5/2015
    This is the SQL minus the between date join.
    SELECT GLORDR.GLORDER, MSORDR.MSORDER, GLORDR.GLSTARTDATE, GLORDR.GLENDDATE, MSORDR.MSOPNDATE
    FROM GLORDR INNER JOIN MSORDR ON (GLORDR.GLSERIAL = MSORDR.MSSERIAL) AND (GLORDR.GLPART = MSORDR.MSPART);

  • To select from database table based on date range

    hi
    i have a selection screen in which date range is being given
    say eg 23/06/07  to 23/12/08
    based on this date i want to select data from a ztable
    eg i want to select a field amount from table
    and three is a field date range on the table
    for this particular field i want to select all records for amount field  and factual field falling wiithing this date range and sum it
    eg
    based on date range as in selcetion screen
    select amount( field1)  factual ( field2) from ztable into it_ztable where date = ?....
    please give me code for it  and how to sum all values as i will get from the ztable into internal table the two values as fetched from the ztable
    please suggest asap
    regards
    arora

    hi
    i am using
    sELECT field1 field2 FROM Ztable  INto it_matu
                       where DATE GE sl_dat-low    
                        AND  DATE LE sl_dat-high.   
    i am getting data in internal table but
    say i have twelve records now i want to sum it the both the columns into and use that sum final amount to display
    let me know how to use sume in the intrranal tabl do i need to use control statement
    how to use the sum for two columns and take into a serperate variable to display
    regards
    aRora

  • Date range question

    hi
    I have a table with record fields like this
    employeeid int
    start date datetime
    end date datetime
    e.g.  
    101 2015-02-04 2015-02-07
    i would need a script so that it can be in the table like this
    101 2015-02-04 2015-02-04
    101 2015-02-05 2015-02-05
    101 2015-02-06 2015-02-06
    101-2015-02-07 2015-02-07
    basically lay out the whole date range in the table as one date as a record.  This is for a specific request from a user to list all date in a pivot table one date at a time.
    any suggestion please, i am not sure how to approach this.
    thank you in advance.
    Ted.

    >>  I have a table with record [sic] fields [sic] like this ..<<
    Please follow the basic Netiquette of all SQL forums for the past 35+ years on the Internet. Post DDL that follows ISO-11179 rules for data element names. Ansd you no diea how to do a correct data mdoel!  Use industry standard encodings (ISBN, UPC,
    GTIN, etc) and avoid needless dialect. Give clear specifications. Give sample data.
    If you do not know that rows are not records, fields are not columns (“field” has a speical meainign SQL) and tables are not files, then you should not be posting. If your tables have no keys, you should not be posting. If you have not tried any DML yourself,
    you should not be posting. 
    CREATE TABLE Personnel
    (emp_id CHAR(3) NOT NULL,
     employment_start_date DATE NOT NULL,
    PRIMARY KEY (emp_id, employment_start_date),
     employment_end_date DATE);
    INSERT INTO Personnel
    VALUES 
    ('101', '2015-02-04, '2015-02-07');
    >> basically lay out the whole date range in the table as one date p[sic] s a record [sic]. This is for a specific request from a user to list all dates in a pivot table one date at a time. <<
    PIVOT is a report and not a table; another fundamental concpetual errort. Use your Calendar table; if you do not know what that is, then Google it. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • SQL to select data between a date range

    Hello,
    I am a new user to HTMLDB and I have an application where I need to select the rows from a table that are between a specified date range. The table description is
    name varchar2
    class varchar2
    sal number
    hdate date
    I want to select the rows of all names between date value 1 and date value 2. The SQL query I use in SQLPLUS does not work in HTML DB. The query I used is
    select null link, name from tab_data where hdate between TO_DATE('06-JAN-04','DD-MON-YY') and TO_DATE('08-JAN-04','DD-MON-YY')
    I also have 2 date picker fields in my form and I used a similar query which did not work.
    select null link, name from tab_data where hdate between TO_DATE(:P2_DATE1,'DD-MON-YY') and TO_DATE(:P2_DATE2,'DD-MON-YY').
    Can someone help me out?
    Thanks

    To All,
    This is getting frustrating... I have tried every combination and permutation I can think of with zero success. The table column I am querying is of type date. The nls_date_format is 'DD-MON-YY' on a 10g database on Linux ES. I am using datepicker 'DD-MON-YY'. I have converted it using TO_DATE, TO_CHAR(TO_DATE(, trunc etc. Is it just my installation? I have upgraded to the latest patch, so that can't be the problem. I created various test scenarios (virgin tables, one row, etc). I've been writing PL/SQL for fourteen years and I think I'm pretty good, but I've sure run up against a stone wall here. Surely somebody has done a date compare out there...
    select to_char(to_date('06-JAN-04','DD-MON-YY'),'MM/DD/YYYY') from dual;
    06-JAN-2004
    select to_char(YOUR_DATE_COLUMN,'MM/DD/YYYY') from YOUR_TABLE_WITH_DATE_COLUMN;
    15-OCT-2004
    15-OCT-2004
    15-OCT-2004

  • Date Ranges Query

    Hi Experts,
    A very happy new year to all of you(to some in advance)!!
    I have a table where I have the employee id and task assigned with start dates & end dates. Overlapping dates of tasks is a data issue however I know how to spot them & eradicate them. So there will be no overlapping task dates
    Employee_id
    Task_No
    Task_Start_date
    Task_End_Date
    Sample Data
    1     T1     01-Jan-2014     28-Feb-2014
    1     T2     01-Mar-2014     31-Dec-2014
    2     T1     23-Jan-2014     31-Dec-2014
    2     T2     01-Jan-2015     31-Dec-2073 (Means end of time)
    3     T3     01-Jan-2014     15-Jul-2014
    3     T4     01-Aug-2014     31-Dec-2014
    4     T5     01-Jan-2014     31-Dec-2073
    I want to devise a query where i provide the end date & it will list out all the employees who are free for even one day starting 01-Jan-2014. So for example If I give 31-Dec-2014, it will list out
    EmpId     (First day on which the employee is free)
    2               01-Jan-2014
    3               16-Jul-2014
    If I give end date = end of time(31-Dec-2013), Expected Result -
    EmpId     (First day on which the employee is free)
    1               01-Jan-2015
    2               01-Jan-2014
    3               16-Jul-2014
    If I give end date = 31 Jan 2014, Expected Result -
    EmpId     (First day on which the employee is free)
    1               01-Jan-2015
    I devised following query, however it does not catch employee id 2. Also it does not provide flexibility to change end date-
    select *
      from (select employee_id,
                   task_start_date,
                   task_end_date,
                   lag(task_end_date, 1, task_start_date) over(partition by employee_id order by task_start_date) prev_end_date
              from shop.employee_tasks
             where task_end_date >= trunc(sysdate))
    where task_start_date - prev_end_date > 1
    Thanks in advance!!
    Regards,

    This is an example of what I call the "free time" query: you have the dates when you are busy and you want the dates when you are free.
    You can find a beautiful solution to the basic problem here: Ask Tom "SQL Query to find gaps in date ranges" (search for Antony Boucher's solution). Please note that this solution works even with overlapping date ranges.
    To apply the solution here, first create the test data (please do this yourself in later questions).
    create table t(Employee_id, task_no, Task_Start_date, task_end_date)
    as select
    1, 'T1', to_date('01-jan-2014'), to_date('28-feb-2014') from dual union all select
    1, 'T2', to_date('01-Mar-2014'), to_date('31-Dec-2014') from dual union all select
    2, 'T1', to_date('23-jan-2014'), to_date('31-dec-2014') from dual union all select
    2, 'T2', to_date('01-jan-2015'), to_date('31-dec-2073') from dual union all select
    3, 'T3', to_date('01-jan-2014'), to_date('15-jul-2014') from dual union all select
    3, 'T4', to_date('01-aug-2014'), to_date('31-dec-2014') from dual union all select
    4, 'T5', to_date('01-Jan-2014'), to_date('31-Dec-2073') from dual;
    In the query, you have to add records for yesterday and for the "end date" you want. This allows you to find "free time" before and after the date ranges in your table.
    Now you partition by Employee_id and order by Task_Start_date. Using the max(task_end_date) analytic function, you get the latest end date up to now. Add 1 to this and you get the first free date (maybe). To make sure that date is free, it has to be before the next start date.
    variable end_date varchar2(64)
    exec :end_date := '31-Dec-2073';
    with boundaries as (
      select trunc(sysdate)-1 task_start_date, trunc(sysdate)-1 task_end_date from dual
      union all
      select to_date(:end_date)+1, to_date(:end_date)+1 from dual
    ), data as (
      select * from t
    where task_end_date >= trunc(sysdate)
      and task_start_date < :end_date
      union all
      select distinct a.employee_id, null, b.task_start_date, b.task_end_date
      from t a, boundaries b
    select employee_id, min(free_start) free_start
    from (
      select employee_id,
      max(task_end_date) over (partition by employee_id order by task_start_date)+1 free_start,
      lead(task_start_date) over (partition by employee_id order by task_start_date)-1 free_end
      from data
    where free_start <= free_end
    group by employee_id;
    EMPLOYEE_ID
    FREE_START
    1
    01-JAN-15
    2
    01-JAN-14
    3
    16-JUL-14

Maybe you are looking for