SQL Query date question

Hello,
I'm searching a query that would display me the number of rows made of a table for the last 4 weeks, grouped by each week.
I basicly have the query but I don't think it will be correct when we go to the next year.
Does anyone know how I can solve this problem?
Select count(*),to_char( create_dt, 'iw' )
from dossiers
where to_char( create_dt, 'iw' ) > to_char(SYSDATE, 'iw') - 4
group by to_char( create_dt, 'iw' )
order by to_char( create_dt, 'iw' ) desc;
Thanks in advance
OLI

This is what I use to count all my audits for the past 4 weeks:
T10> SELECT TRUNC(weeks.start_date) || ' - ' || TRUNC(weeks.start_date + 6)  audit_date_span
  2       , COUNT(*)  count
  3    FROM asset_audits,
  4         (
  5           SELECT NEXT_DAY(
  6                    ADD_MONTHS(SYSDATE, -1) - 1
  7                  , 'SUNDAY') + ((ROWNUM - 1) * 7) start_date
  8             FROM (
  9                    SELECT 1
10                      FROM DUAL
11                   CONNECT BY LEVEL < 6
12                  )
13         ) weeks
14   WHERE TRUNC(audit_date) BETWEEN TRUNC(weeks.start_date) AND TRUNC(weeks.start_date + 6)
15   GROUP BY TRUNC(weeks.start_date) || ' - ' || TRUNC(weeks.start_date + 6)
16   ORDER BY TO_DATE(SUBSTR(audit_date_span, 1, 9), 'DD-MON-YY') ASC
17  /
AUDIT_DATE_SPAN                COUNT
29-OCT-2006 - 04-NOV-2006        105
05-NOV-2006 - 11-NOV-2006        201
12-NOV-2006 - 18-NOV-2006        120
19-NOV-2006 - 25-NOV-2006        664
4 rows selected.Cheers.

Similar Messages

  • Is there a way to consolidate calculations used in SQL Query Data Sets?

    I am building SQL Query Data Sets against multiple DB Views which all have different date formats. I have the date parameter working well, but want to consolidate the changes that I have to do to it especially for the quarter. For example the SQL against a monthly view would use "where year_month > to_char(:ST_DATE, 'YYYY-MM')" which is not too hard, but to make that work for the quarterly view I need several concats and a decode to get a "> '2012-Q2'". I would like not to have to do this within every where clause. I had put this into a global element hoping to use that in a filter for the data set, but the filter does not have access to global elements only parameters.
    Is there any way to refer to a calculated global element from the data set to do this? I am using Layout Templates, so even if I wanted to I could not do the condition within the template.
         Thanks,
         Rick

    Change the following line to include the red coloured parts
    <div class="MasterColumn" spry:repeat="ds4" spry:setrow="ds4" spry:hover="MasterColumnHover" spry:select="MasterColumnSelected">
      <span>{Dsp_#}</span><span>{Dsp_WkDay}</span><span>{Dsp_Event}</span>
    </div>
    The add the following style rule to your document:
    .MasterColumn span {
        display: inline-block;
    I hope this helps.
    Ben

  • With clause in SQL query data model

    Hello!
    isn't it possible to use the with clause in sql query data models?
    for example following query:
    WITH
    a_test as (
    select dummy from dual
    select *
    from a_test
    brings up a "XML Parsing Error: no element found"-error...
    BR Paul

    I tried a slightly different query (see below) and the query worked fine and retrieved data.. I did not get any errors.
    WITH
    a_test as (
    select 'dummy' from dual
    select *
    from a_test
    This works as well.. retrieving the value of the parameter
    WITH
    a_test as (
    select :Test_ID from dual
    select *
    from a_test
    thanks,
    BIPuser

  • SQL Concat function seem not to work properly in SQL query data model. BIP 11g

    I try to use this function to show entire name CONCAT(name, ' ', surname) and it doesnt show the surname. it looks like it only tooks 2 arguments. I use it with the option "Add element by expression" within a SQL Query - Data set.
    Lucia

    i don't believe you
    please post more info, may be screen or sql or some more useful statement
    CONCAT(name, ' ', surname) and it doesnt show the surname
    may be because for some case "surname" is empty  or may be you incorrect use concat function
    one way mentioned by BluShadow is " || " operator  - http://docs.oracle.com/cd/B28359_01/server.111/b28286/operators003.htm#SQLRF51156
    and another is concat function http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions026.htm#SQLRF00619
    so for your case it can be like
    CONCAT(CONCAT(name, ' '), surname)

  • Different LOV behavior between SQL query data model and data template

    I have noticed different behavior when using parameters linked to list of values (LOV) of type menu with the multiple selection option enabled and a SQL query data model vs a data template. Here's the example because that first sentence was probably really confusing.
    SQL Query:
    select
    plmc.MonthCode, plmc.ModalityDim, plmc.ModalityName,plmc.RegionDim
    from
    DataOut.dbo.PatientLabMonthlyCross plmc
    where
    plmc.MonthCode = 200202
    and plmc.RegionDim = 1209
    and 1 =
    case
    when coalesce(:modalityDim,null) is null
    then 1
    else
    case
    when plmc.ModalityDim in (:modalityDim)
    then 1
    else 0
    end
    end
    Putting BI Publisher into debug mode, defining a data model of type SQL Query, defining a parameter called :modalityDim linked to a LOV that allows multiple selections, and selecting a couple of values from the LOV the output of the prepared statement is:
    [081607_122647956][][STATEMENT] Sql Query : select
    plmc.MonthCode,
    plmc.ModalityDim,
    plmc.ModalityName,
    plmc.RegionDim
    from
    DataOut.dbo.PatientLabMonthlyCross plmc
    where
    plmc.MonthCode = 200202
    and plmc.RegionDim = 1209
    and 1 =
    case
    when coalesce(?,?,null) is null
    then 1
    else
    case
    when plmc.ModalityDim in (?,?)
    then 1
    else 0
    end
    end
    [081607_122647956][][STATEMENT] 1:6
    [081607_122647956][][STATEMENT] 2:7
    [081607_122647956][][STATEMENT] 3:6
    [081607_122647956][][STATEMENT] 4:7
    [081607_122654713][][EVENT] Data Generation Completed...
    [081607_122654713][][EVENT] Total Data Generation Time 7.0 seconds
    Note how the bind variable :modalityDim was changed into two parameters in the prepared statement.
    When I use this same SQL Query in a data template the output is:
    [081607_012113018][][STATEMENT] Sql Query : select
    plmc.MonthCode,
    plmc.ModalityDim,
    plmc.ModalityName,
    plmc.RegionDim
    from
    DataOut.dbo.PatientLabMonthlyCross plmc
    where
    plmc.MonthCode = 200202
    and plmc.RegionDim = 1209
    and 1 =
    case
    when coalesce(?,null) is null
    then 1
    else
    case
    when plmc.ModalityDim in (?)
    then 1
    else 0
    end
    end
    [081607_012113018][][STATEMENT] 1:'6','7'
    [081607_012113018][][STATEMENT] 2:'6','7'
    [081607_012113574][][EXCEPTION] java.sql.SQLException: Syntax error converting the nvarchar value ''6','7'' to a column of data type int.
    Note the exception because it is trying to convert the multiple parameter values.
    Am I doing something completely wrong here? I really need to use a data template because I will need to link a couple of queries together from different database vendors.
    -mark

    This is for 10.1.3.4 - because in 11g every SQL query is automatially part of a data model.
    In 10g SQL query is for simple unrelated SQL queries.
    If you need to use advance features such as:
    a) multiple SQL queries that are joined in master-detail relation ships
    b) before/after report triggers
    Then you will need to use the data template, which is an XML description
    of the queries, links, and PL/SQL calls.
    hope that helps,
    Klaus

  • SQL Query Data Model Question

    Hi All,
    I'm new to XMLPublisher so i'll try to explain the best i can the problem i'm having creating a Data Model for my report.
    I'm using Database XE and have installed XMLPublisher, as XE is extremely flexible in creating apps i'm having huge trouble finding a reporting solution for my applicaiton. I have created an app to track and control time and expense which i want to generate timesheet, expense sheets, invoice etc from.
    Having done some read i think XML is the best approach for me however i'm having huge troubles with my data model.
    EXAMPLE:
    I have 2 tables to start with, first table holds client information and the second table holds project information. One client can have many projects.
    When i create the Data Model using SQL i get the obvious problem that when a client has more then one project my XML structure is incorrect.
    SELECT gc.name AS CLIENT,
    gp.name AS PROJECT
    FROM gte_client gc,
    gte_project gp
    WHERE gc.client_id = gp.client_id(+)
    The above SQL creates the following XML
    - <ROWSET>
    - <ROW>
    <NAME>Symatrix Ltd</NAME>
    <NAME>Symatrix Pre-Sales</NAME>
    </ROW>
    - <ROW>
    <NAME>Aston Carter</NAME>
    <NAME>MOD</NAME>
    </ROW>
    - <ROW>
    <NAME>Symatrix Ltd</NAME>
    <NAME>Fujitsu</NAME>
    </ROW>
    </ROWSET>
    Obvious problem here is the fact that client Symatrix appears twice as there are 2 projects.
    I have tried to write SQL/XML and i think i'm beginning to understand the structure of the XML functions however XMLPublisher doesn't appear to understand the syntax when creating a SQL Query for the data model.
    I hope all this makes sense.
    Kind Regards
    Glen

    Hi All,
    Ok, so with a little searching through documents and articles on the web i found this information.
    http://blogs.oracle.com/xmlpublisher/2006/05/05#a38
    this details the exact thing i'm trying to achieve so anyone else who searches this forum with a similar problem check this out.
    Regards
    Glen

  • SQL Query returns question marks

    I am seeing a SQL Query, executed via ADODB in VB, from a 10G client (don't know exact version) to a Sun Solaris based 9.2.0.5.0 Server return all question marks...Table queried consists of 3 columns, 2 varchar2, one long: Here query:
    SELECT SETTINGVALUE FROM EWORKFLO.CONFIG WHERE SETTINGGROUP='New test' AND SETTINGNAME='Single Page'
    The query results in 2576 question marks...(????? etc).
    Any idea what may be going on here?
    Thanks...
    Leendert

    Hello Tak,
    Thanks for replying. When this query is run from SQLPLUS we get a proper result back, that is the actual values from the SETTINGVALUE column. This is the LONG column and contains data like this:
    [Scan Section]
    DeviceTimeout=15
    Display=1
    hDCCreate=0
    Unit=0
    DialogTitle=
    IniFileName=c:\winnt\temp\kf.ini
    IniSectionName=Scan Section
    DeviceCache=1
    etc....etc....
    Depending on the settings created, the return string may vary in length.
    Leen

  • Need help with sql query dates

    Hi,
    I have a sql query where i need to extract some info between given dates. The where clause of this query is as follows:
    WHERE CPD_BUS_UNIT=:ESI_PRM_1
    AND CPD_VOUCHER_DATE >= :P_DATE_FROM
    AND CPD_VOUCHER_DATE < (:P_DATE_TO+1)
    When i execute the query in toad, i can view the data but not the execution plan.It gives an error ORA-00932-Inconsistent Datatypes.
    But when i remove (+1) from :P_DATE_TO, i can c the execution plan and data. Will the data be different from the previous one.
    Please suggest how to rewrite the query.

    Can you please give it a try?
    WHERE CPD_BUS_UNIT=:ESI_PRM_1
    AND CPD_VOUCHER_DATE >= :to_date(P_DATE_FROM)
    AND CPD_VOUCHER_DATE < (to_date(:P_DATE_TO)+1) Regards

  • How to get sql query data and write into csv file?

    I am writing to seek help, in how can I create bat. script which can execute the following logic:
    connection to the database
    run sql query
    create CSV file
    output query data, into CSV file
    save the CSV file
    osql
    -S 84.18.111.111
    -U adw
    -P rem
    -i "c:\query.sql"
    send ""
    sed -e 's/,\s\+/,/g' MCI_04Dec2014.csv > localNoSpaces.csv
    -o "c:\MCI_04Dec2014.csv"
    This what i have so far, and I am little struggling with the logic after creating CSV file. I am unable to get the above script to work, please advice further, where I may be going wrong. 
    Can you create if statement logic within window's script, to check for null parameters or data feeds?
    Any hints would be most appreciated. 

    Thank you for your reply. 
    Apology for posting the code irrelevant to the forum, as I am still novice scripting user.  
    My goal is to create window's script which can compute the above logic and send the final output file (csv), to FTP folder. 
    Can this logic be implemented via bat. script, if so, is there a example or tutorial i could follow, in order to achieve my task. 
    Any help would be much appreciated. 

  • JSTL sql:query date field has zero time part

    I have a little JSP where I am using JSTL to do a query against a table "ph_application" that has a date field "rundate". This field has many different dates and times in it.
    Here is the code snippet:
    <sql:setDataSource url="jdbc:oracle:thin:@oraprd02:1521:ipp4" user="site" password="pmc_site"/>
    <sql:query var="application" sql="select * from ph_application"/>
    <c:forEach items="${application.rows}" var="row">
    <tr>
    <td><c:out value="${row.name}" default=" " escapeXml="false"/></td>
    <td><fmt:formatDate value="${row.rundate}" type="both"/></td>
    </tr>
    </c:forEach>
    The resulting date/time output always has a 12:00:00 AM time regardless of what is actually in the table. I have changed the fmt:formatDate to a c:out of the row.rundate.time value and it appears that the time part of the date is actually zero at this point.
    Is there an inconsistancy between the oracle date type and what JSTL is expecting (somewhere)?
    Any help and/or verification is appreciated.
    Richard

    I'm running this from within JDeveloper 10g.

  • Limitation of SQL-Query data source !?

    Hello,
    I faced a situation that required the use of "case ... when ... then ...end" in my select-statement defining the data source. But I doesn't seem to work! I checked the statement in sql developer and it worked fine!
    Is there any idea about that?
    Thanks in advance.
    Dirk

    Hi Tim,
    thank you for your response - I created a sample query of what I used and it looks like this one:
    select col1
    ,col2
    , case col2 when '12' then (select a2.col2 from a2 where a2.col1 = a1.col1 )
    when '13' then (select a3.col2 from a3 where a3.col1 = a1.col1 )
         end
    from a1
    Dirk

  • Plsql procedure with sql query data

    plsql newbie(learning sql): please excuse for asking a basic plsql question
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod on windows server 2003
    PL/SQL Release 10.2.0.1.0 - Production
    "CORE     10.2.0.1.0     Production"
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    CREATE OR REPLACE PROCEDURE p_check
    AS
    v1 NUMBER(20,0);
    BEGIN
    SELECT count(*) INTO v1
    FROM ad_reg;
    dbms_output.put_line(v1);
    end;
    inserts the number of rows in the table ad_reg into v1
    and displays v1 by using dbms_output
    i want my procedure to return table data
    CREATE TABLE "TEST_SH"
    "TEXT_ID" NUMBER,
    "TEXT" CLOB
    Insert into TEST_SH (TEXT_ID) values (1);
    Insert into TEST_SH (TEXT_ID) values (2);
    Insert into TEST_SH (TEXT_ID) values (3);
    Insert into TEST_SH (TEXT_ID) values (4);
    there will be a way with which i can see all the data(select * from test_sh) as the output of the plsql procedure
    1.please tel me what to google
    2.please guide me with a site or tell what is actually used and help me
    3.or post an example here
    thank you much
    Edited by: 946207 on Nov 20, 2012 2:05 PM

    946207 wrote:
    there will be a way with which i can see all the data(select * from test_sh) as the output of the plsql procedurePL/SQL is
    a) nothing at all like T-SQL
    b) two different languages, PL (Programming Logic) and SQL
    SQL is used to access data in the Oracle database. So select * from test_sh is the correct statement to access the data.
    The client can make execute this SQL statement itself. And view the data. However, this requires the client to understand the SQL language, and the database model. Which is typically not that you want. You want to abstract that from the client.
    Why would you want the client to deal with the complexities of the SQL language? Or deal with the complexities of your database model in Oracle?
    So instead of the client doing SQL, PL/SQL can do it on behalf of the client. The client uses PL/SQL as an API - in a similar fashion as the client would use any other API.
    The PL/SQL code determines the SQL statement - and returns a SQL cursor handle to the client. The client then simply uses the SQL cursor that the PL/SQL code created for it (and the client needs to close the SQL cursor when done, so as to not cause server resource leakage).
    E.g. (using PL/SQL as an API to abstract SQL and the data model)
    create or replace procedure TestProc( c out sys_refcursor ) is
    begin
      open c for select * from test_sh order by 1;
    end;The client then calls TestProc and gets a reference cursor handle. E.g. (the client call to Oracle):
    begin
      TestProc( c => :1 );
    end;The client needs to supply a host (client language) bind variable of type cursor, as value for bind variable +:1+.
    This is the correct way to deal with PL/SQL and cursors from a client GUI/language perspective. Any other method, like using PL/SQL to cache SQL data as a collection, is usually very wrong.

  • Small Validation in sql Query Date range..

    hi.
    I need Some suggestions for validating the Date.
    In customization form From date and to date i given.
    like
    01/08/2014 and 30/08/2014..
    Second time if  they give...
    i can validate like below
    select  DocEntry from  [@PR_OTIMESHEET] where U_frmdate >='20140801' and U_todate <='20140830'
    It's fine everything is fine. but.
    if i give like from date 15/08/2014 and 30/08/2014 it should be validate and stop it..
    if i run the  above query by replace the dates it is not working..
    Simply.
    Date is  200   to  300 if they give first time it will be save in db.
    second time if i give 150 to 400 it should not accept bcz 150 all ready comes between 200 to 300
    How can i restrict it.

    Hi,
    If your question is related to SDK, please post it there.
    Thanks,
    Gordon

  • SQL query performance question

    So I had this long query that looked like this:
    SELECT a.BEGIN_DATE, a.END_DATE, a.DEAL_KEY, (select name from ideal dd where a.deal_key = dd.deal_key) DEALNAME, a.deal_term_key
    FROM
    ideal d, ideal_term a,( select deal_key, deal_term_key, max(createdOn) maxdate    from Ideal_term B
    where createdOn <= '03-OCT-12 10.03.00 AM' group by deal_key, deal_term_key ) B
    WHERE  a.begin_date <= '20-MAR-09 01.01.00 AM'
    *     and a.end_date >= '19-MAR-09 01.00.00 AM'*
    *     and A.deal_key = b.deal_key*
    *     and A.deal_term_key = b.deal_term_key*
    *     and     a.createdOn = b.maxdate*
    *     and d.deal_key = a.deal_key*
    *     and d.name like 'MVPP1 B'*
    order by
    *     a.begin_date, a.deal_key, a.deal_term_key;*
    This performed very poorly for a record in one of the tables that has 43,000+ revisions. It took about 1 minute and 40 seconds. I asked the database guy at my company for help with it and he re-wrote it like so:
    SELECT a.BEGIN_DATE, a.END_DATE, a.DEAL_KEY, (select name from ideal dd where a.deal_key = dd.deal_key) DEALNAME, a.deal_term_key
    FROM ideal d
    INNER JOIN (SELECT deal_key,
    deal_term_key,
    MAX(createdOn) maxdate
    FROM Ideal_term B2
    WHERE '03-OCT-12 10.03.00 AM' >= createdOn
    GROUP BY deal_key, deal_term_key) B1
    ON d.deal_key = B1.deal_key
    INNER JOIN ideal_term a
    ON B1.deal_key = A.deal_key
    AND B1.deal_term_key = A.deal_term_key
    AND B1.maxdate = a.createdOn
    AND d.deal_key = a.deal_key + 0
    WHERE a.begin_date <= '20-MAR-09 01.01.00 AM'
    AND a.end_date >= '19-MAR-09 01.00.00 AM'
    AND d.name LIKE 'MVPP1 B'
    ORDER BY a.begin_date, a.deal_key, a.deal_term_key
    this works much better, it only takes 0.13 seconds. I've bee trying to figure out why exaclty his version performs so much better. His only epxlanation was that the "+ 0" in the WHERE clause prevented Oracle from using an index for that column which created a bad plan initially.
    I think there has to be more to it than that though. Can someone give me a detailed explanation of why the second version of the query performed so much faster.
    Thanks.
    Edited by: su**** on Oct 10, 2012 1:31 PM

    I used Autotrace in SQL developer. Is that sufficient? Here is the Autotrace and Explain for the slow query:
    and for the fast query:
    I said that I thought there was more to it because when my team members and I looked at the re-worked query the database guy sent us, our initial thoughts were that in the slow query some of the tables didn't have joins and because of that the query formed a Cartesian product and this resulted in a huge 43,000+ rows matrix.
    In his version all tables had joins properly defined and in addition he had that +0 which told it to ignore the index for the attribute deal_key of table ideal_term. I spoke with the database guy today and he confirmed our theory.

  • Sql query data horizontally with sum and condition in a row

    I am trying to get data from the following query in a row with total but not succeed and getting error message. Please help and advise.
    Select empid,name,country,
    sum(case category when 'JOB' then sum(core+RR) else 0 end) as 'JOB-Sum',
    sum(case category when 'PRIVATE' then sum(core+PP) else 0 end) as 'PRIVATE-Sum',
    group by empid,name,country
    Note: Following query is good for getting result in one row but i have to put sum calculation with 'THEN' and want result in a row.
    SELECT ID, MAX(case when Col1 = 'Yes' then 1 else 0 end) AS Col1,
    MAX(case when Col2 = 'Yes' then 1 else 0 end) AS Col2
    FROM MyTable
    GROUP BY ID

    Please show some sample data and expected output.
    Regards,
    Rob.

Maybe you are looking for

  • What is BI7.0?what is new in BI7.0?

    Hi, What is BI7.0?what is new in BI7.0? Can any body have goodmeterial about BI7.0?Please send to this mail id [email protected] immediate reply is highly appriciable. Regards, Chow.

  • PDF attachments are changed when emailed

    I have a user that whenever she creates or prints a PDF (Adobe 8 Professional) and then tries to email it within Adobe (going to file -> attach to an email), it converts to either a winmail.dat, .eml or a .mht file when the person receives it. It onl

  • Forefront for Exchange Server is not blocking emails by IP

    I have 3 Servers for Exchange: Client, Mailbox, and Edge. I have Antispam configured on my Edge server with IP Block list enabled but I am getting numerous emails from China's IP block even though I blocked all of the .cn network. The following is en

  • SapPhone Server Tester - SPS Tester

    Hello friends, I read about the SPS Tester on help.sap.com. I found also information about the installation of the SPS Tester, but where can I get this programm? Does somebody know where I can get or download the SPS Tester? daniel

  • Store signature image in Active Directory and deploy it to each users desktop

    What I am trying to achieve is to have each user a hand written signature scanned in and stored in the .jpgPhoto attribute in Active Directory and then have some sort of script, like our login script, pull that information and copy the file to the us