Help on Aggregated Columns Through SQL

Hello All,
This is Sam, I am trying to pull the data from a table based on following criteria
1) Month To Date
2) Yesterday only
3) Whole Month
Below is the sql i created i could able to achieve SUM(NET_AMT) aggregated value for the Month To Date but could not able to figure out how i could create additional calculated columns for the other 2 criteria, could you please help me with the below query, the WITH clause i used is just to bring the total days in current month and current day number in the respective month, i think GROUP BY clause could not specified in CASE statement to aggregate based on dates, i am puzzled how to proceed further, appreciate your help, Thank you.
WITH X_DATE_H AS(
SELECT
PREV.DAY_DT AS PREV_DAY,
PREV.DAY_OF_MONTH AS CURR_DAY_MTD,
PREV.M_STRT_CAL_DT_WID AS MON_START_DAY_WID,
PREV.M_END_CAL_DT_WID AS MON_END_DAY_WID,
(PREV.M_END_CAL_DT_WID - PREV.M_STRT_CAL_DT_WID + 1) AS NO_DAYS_IN_MONTH
FROM W_DAY_D PREV, W_DAY_D CURR
WHERE CURR.DAY_DT = TRUNC(SYSDATE)
AND CURR.DAY_AGO_WID = PREV.ROW_WID
SELECT
SYSDATE AS SNAPSHOT_DATE,
TO_CHAR(SYSDATE,'YYYYMMDD') AS SNAPSHOT_DATE_WID,
PROD.PRODUCT_TYPE_CODE,
PROD.PRODUCT_TYPE_DESC,
X_DATE_H.CURR_DAY_MTD AS DAYS_MTD,
X_DATE_H.NO_DAYS_IN_MONTH AS DAYS_MONTH,
SUM(NET_AMT) AS BOOKING_AMT
FROM
X_DATE_H,
W_SALES_BOOKING_LINE_F BKG_F
LEFT JOIN W_PRODUCT_D PROD ON BKG_F.PRODUCT_WID=PROD.ROW_WID
LEFT OUTER JOIN W_INT_ORG_D ON (BKG_F.X_BRANCH_WID = W_INT_ORG_D.ROW_WID AND BKG_F.DATASOURCE_NUM_ID=W_INT_ORG_D.DATASOURCE_NUM_ID)
WHERE
BKG_F.X_MONTH_WID BETWEEN TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE,-1))+1,'YYYYMMDD') AND TO_CHAR(SYSDATE,'YYYYMMDD')
GROUP BY SYSDATE, TO_CHAR(SYSDATE,'YYYYMMDD'), PROD.PRODUCT_TYPE_CODE,PROD.PRODUCT_TYPE_DESC,X_DATE_H.CURR_DAY_MTD,X_DATE_H.NO_DAYS_IN_MONTH

Hi, Sam,
Whenever you have a question, it helps to post:
(1) The version of Oracle (and any other relevant software) you're using
(2) A little sample data (just enough to show what the problem is) from all the relevant tables. You can probably post a good example with just 2 products and 3 dates.
(3) The results you want from that data
(4) Your best attempt so far (formatted) (You posted this, but since it's unformated, it's very hard to read.)
Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
Formatted tabular output is okay for (3). Type these 6 characters:
{code}
(small letters only, inside curly brackets) before and after formatted text, to preserve spacing.
Analytic functions are very good for doing things like running totals (such as month-to-date).
SYSDATE is constant with any query, even if the query takes a long time to run.
There's no point in grouping by a constant, so
GROUP BY  SYSDATE
,         TO_CHAR (SYSDATE,'YYYYMMDD')
,         PROD.PRODUCT_TYPE_CODE
,         ...produces the same results as
GROUP BY  PROD.PRODUCT_TYPE_CODE
,         ...

Similar Messages

  • Row value needs to be changes as column through SQL Query

    HI ALL
    I have a table like below
    ID Month VALUES
    1 01-jan 10
    1 01-feb 20
    2 01-jan 10
    2 01-feb 20
    I need the output like below
    ID 01-jan 01-feb
    1 10 20
    2 10 20
    How can i get it through SQL Query?. Please help me on that i have urgent work like this

    In effect because you are wanting to take X rows and squish them down to 1 row per id, you are gouping on the ID to typically a group by clause is the best way to do this.
    If you really wanted to do it without aggregate functions and a group by clause you would be looking for something like this...
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t as (select 1 as id, '01-jan' as month, 10 as val from dual union all
      2             select 1, '01-feb', 20 from dual union all
      3             select 1, '01-mar', 30 from dual union all
      4             select 2, '01-jan', 10 from dual union all
      5             select 2, '01-feb', 30 from dual union all
      6             select 2, '01-mar', 60 from dual)
      7  --
      8  select id, jan, feb, mar
      9  from (
    10    select id
    11          ,row_number() over (partition by id order by to_date(month,'dd-mon')) as rn
    12          ,val as jan
    13          ,lead(val) over (partition by id order by to_date(month,'dd-mon')) as feb
    14          ,lead(val,2) over (partition by id order by to_date(month,'dd-mon')) as mar
    15    from t
    16    )
    17* where rn = 1
    SQL> /
            ID        JAN        FEB        MAR
             1         10         20         30
             2         10         30         60
    SQL>Although this will only work if you can guarantee that there is a '01-jan' value for each id. If there could be missing values then you'll have to use aggregate functionality.

  • Help needed in Exporting tables data through SQL query

    Hi All,
    I need to write a shell script(ksh) to take some of the tables data backup.
    The tables list is not static, and those are selecting through dynamic sql
    query.
    Can any body tell help me how to write the export command to export tables
    which are selected dynamically through SQL query.
    I tried like this
    exp ------ tables = query \" select empno from emp where ename\= \'SSS\' \"
    but its throws the following error
    EXP-00035: QUERY parameter valid only for table mode exports
    Thanks in advance,

    Hi,
    You can dynamically generate parameter file for export utility using shell script. This export parameter file can contain any table list you want every time. Then simply run the command
    $ exp parfile=myfile.txt

  • Defaulting org id value into a table through SQL Loader program

    Hi ,
    We have a requirement that we need to load some data from flat file to a table.we are using sql loader to do that. so far no problem but now the requirement is that we need to populate the org id from which we are running the program.
    I tried fnd_profile.value('ORG_ID') and it is populating site level org id.
    Coudl any one please help me how to default org id or request id into a table through sql loader program.
    Thanks,
    Y

    user12001627 wrote:
    Hi Srini,
    Thanks for looking into this!!
    We are on EBS 11.5.10 and OS is solaris.
    I tried fnd_profile,fnd_global but no luck.
    Here is the control file which we are using to load data.
    load data
    infile *
    replace into table XXXX_YYYY_STAG
    trailing nullcols
    (line POSITION(1:2000)
    I would like to populate org id when I load the data from file.unfortunately there is no identifier in the file that says for which org id the data is in the file.Only the way to identify the file org is based on file name
    Where do you want to populate the ORG_ID ? There is no column for it in your stage table above
    Is there way we can pass through concurrent program parameters?
    Thanks
    YHTH
    Srini

  • Error While executing a SSIS package which contains a script task through SQL Server Agent job

    Hi,
    I have a SQL Server 2012 SSIS package with a script task along with other tasks [data flow, execute sql tasks ]. When I manually executed the job through BIDS, its completed successfully. 
    Then I have automated the execution of the package through SQL Server Agent Job. But when I executed the package through SQL Agent job, it runs successfully for all the tasks except script task. When it comes to execute the Script Task, it is getting failed
    with the below error message.
    "Error: 2012-08-29 12:45:14.67
       Code: 0x00000001
       Source: Script Task 
       Description: Exception has been thrown by the target of an invocation.
    End Error
    DTExec: The package execution returned DTSER_FAILURE (1).
    Started:  12:45:10 PM
    Finished: 12:45:14 PM
    Elapsed:  4.353 seconds
    I have installed the SSIS on the 64-bit environment and SSIS service is running. Also I tried to run the job through 32 bit [job option] but I am getting the above error in all cases.
    Any help will be greatly appreaciated !
    Thanks,
    Navin
    - naveen.reddy

    Hi Arthur,
    My script task access the excel files in a network share, refresh them all and save them. When I execute the ETL manually or thru DTEXEC, it is executing successfully. I am facing the issue when I am executing thru SQL Agent Job only. Logging also showing
    the same error.
    "Error: 2012-08-23 12:45:14.67
       Code: 0x00000001
       Source: Script Task 
       Description: Exception has been thrown by the target of an invocation.
    End Error
    DTExec: The package execution returned DTSER_FAILURE (1).
    Started:  12:45:10 PM
    Finished: 12:45:14 PM
    Elapsed:  4.353 seconds
    - naveen.reddy

  • Issue in creation of group in oim database through sql query.

    hi guys,
    i am trying to create a group in oim database through sql query:
    insert into ugp(ugp_key,ugp_name,ugp_create,ugp_update,ugp_createby,ugp_updateby,)values(786,'dbrole','09-jul-12','09-jul-12',1,1);
    it is inserting the group in ugp table but it is not showing in admin console.
    After that i also tried with this query:
    insert into gpp(ugp_key,gpp_ugp_key,gpp_write,gpp_delete,gpp_create,gpp_createby,gpp_update,gpp_updateby)values(786,1,1,1,'09-jul-12',1,'09-jul-12',1);
    After that i tried with this query.but still no use.
    and i also tried to assign a user to the group through query:
    insert into usg(ugp_key,usr_key,usg_priority,usg_create,usg_update,usg_createby,usg_updateby)values(4,81,1,'09-jul-12','09-jul-12',1,1);
    But still the same problem.it is inserting in db.but not listing in admin console.
    thanks,
    hanuman.

    Hanuman Thota wrote:
    hi vladimir,
    i didn't find this 'ugp_seq'.is this a table or column?where is it?
    It is a sequence.
    See here for details on oracle sequences:
    http://www.techonthenet.com/oracle/sequences.php
    Most of the OIM database schema is created with the following script, located in the RCU distribution:
    $RCU_HOME/rcu/integration/oim/sql/xell.sql
    there you'll find plenty of sequence creation directives like:
    create sequence UGP_SEQ
    increment by 1
    start with 1
    cache 20
    to create a sequence, and
    INSERT INTO UGP (UGP_KEY, UGP_NAME, UGP_UPDATEBY, UGP_UPDATE, UGP_CREATEBY, UGP_CREATE,UGP_ROWVER, UGP_DATA_LEVEL, UGP_ROLE_CATEGORY_KEY, UGP_ROLE_OWNER_KEY, UGP_DISPLAY_NAME, UGP_ROLENAME, UGP_DESCRIPTION, UGP_NAMESPACE)
    VALUES (ugp_seq.nextval,'SYSTEM ADMINISTRATORS', sysadmUsrKey , SYSDATE,sysadmUsrKey , SYSDATE, hextoraw('0000000000000000'), 1, roleCategoryKey, sysadmUsrKey, 'SYSTEM ADMINISTRATORS', 'SYSTEM ADMINISTRATORS', 'System Administrator role for OIM', 'Default');
    as a sequence usage example.
    Regards,
    Vladimir

  • Reading values from lookup columns through custom workflow in SharePoint 2013

    We are able to read the values of text, number columns through custom workflow (via coding) in SharePoint 2013. However, we are not able to read values from lookup columns. So, request anyone to provide help on this.
    Thanks & regards,
    Aditya

    Hi,
    According to your post, my understanding is that you want to read values from lookup columns through custom workflow in SharePoint 2013.
    Since the workflow just doesn't get lookup fields, let's give it something static to work with instead. If we can capture the ID of the lookup field and store that as a static value in our list, the workflow can happily use that to look up our related.
    For more information, you can refer to:
    SharePoint 2013 Workflows and Lookup Columns
    Thanks,
    Linda Li                
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Linda Li
    TechNet Community Support

  • Getting error while trying to upload the data in excel from SSIS package through sql agent job

    We are getting below errors.
    Error:
    The Microsoft Jet database engine cannot open the file '\\serversdev\Documents\QC Files\Prod.xls'. It is already opened exclusively by another
    user, or you need permission to view its data.
    Please suggest ASAP
    Regards,
    Ramu
    Ramu Gade

    Hi Dikshan Gade,
    According to your description, you want to upload data from excel to database, when you call ssis package through SQL Server Agent job, you got the error message.
    To troubleshoot the problem, please refer to the following steps:
    Validate that the account has permissions on the file and the folder.
    Verify that the file and the data source name (DSN) are not marked as Exclusive.
    Make sure SQL Server Agent Services service account has the permission to access the database.
    We can check SQL Server Agent’s activity logs, Windows Event logs and SSIS logs to get more clues. Also the tool Process Monitor is helpful to track the cause of registry or file access related issues. For more information about the issue, please refer to
    the following KB article:
    http://support.microsoft.com/kb/306269
    If you have any more questions, please feel free to ask.
    Thanks,
    Wendy Fu
    Wendy Fu
    TechNet Community Support

  • Error: Failed to acquire connection while exporting a file using SSIS package through SQl Server Agent job.

    Hi Team,
    I am running a sql server job which is loading data to a sharepoint document library.File is an excel format and is getting loaded
    when executed through SSDT solution .But when i am using an SQL Agent job to pick up the file it doesnt get the path and agent job fails with the error "Failed to acquire connection".I have deployed the SSIS Package through SQL Server 2012 deployment
    methodologies.
    I am not able to trace the error of loading data from a sharepoint library.library. Sql agent jobs works fine when i give local path.The
    account which is running the sql agent job is the same who has access to document library.I also tried to run the job through proxy account. I am using a xml configuraion to store my credentials.I am able to palce the same file to a network drive in the same
    server through sql server agent job. The only problem is when I am trying to load the file to a sharepoint folder.
    Thanks,
    Arsa..

    Is that the WebDAV method that you are suggesting here to map the n/w drive?
    Its kind of webdav but you will be using windows nw drive to store data
    yes, I am able to create the file to D: , but didn't check with the C:.
    Try with c:, as c: require special permisssions
    If this helped you resolve your issue, please mark it Answered

  • Fetch SSIS error through SQL Server Agent JOB

    Hi,
    I am trying to fetch eventmessages from catalog.eventmessages view in SSISDB inside a sql job which executes a package. This utility is supposed to send an email with the description of failure reason and step whenever the JOB fails. But I could not find
    a way to reference a package in SSISDB with the corresponding JOB through SQL query.
    sysjobsteps stores the step info, but how can I refer to the SSIS package using the info from this table or any other table. Please provide an expert view for this problem.
    Regards
    Mohit

    sysjobsteps stores the step info, but how can I refer to the SSIS package using the info from this
    table or any other table
    Can you try like this? 
    SELECT e.package_name,e.message
    FROM msdb.dbo.sysjobsteps js
    inner join SSISDB.catalog.event_messages e
    On js.command collate database_default like'%\' + e.package_name collate database_default + '%'
    --WHERE e.event_name ='OnError'
    Uncomment the WHERE clause if you want to specifically look for errors
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • How do I migrate views from MS SQL 2008 to Oracle 11g through SQL Developer

    Is there any way to migrate the views from MS SQL 2008 to Oracle 11g through SQL Developer? Please give me some detail steps. Thanks for your help.
    Kevin

    Hi Kevin,
    user13531850 wrote:
    Hi Turloch,
    When I use migrate to oracle, I got a problem, the migrate tool create a new schema for me in my case (AZTECA_KSMMS), it migrates all the stuffs under that schema (AZTECA_KSMMS). However my application need the all the Oracle data under schema AZTECA instead of AZTECA_KSMMS. Is there any way to specify specific schema (AZTECA) for target oracle database? Schema remapping is available:
    First Capture (separately) then during right click convert on the captured model there is a Specify the conversion options with a Object Naming tab where the schema (and other) name changes are editable.
    I have not used this recently.
    Also during the migration process, when I choose repository, there is a check box for truncate to reset repository to empty state, Do I need to check that truncate Check Box so the repository will be cleared from last migration?The repository can hold multple migration attempts. Check truncate to get rid of previous attempts information. This cleans up the repository - not the destination database.
    There are also online database and offline database options during the migration process, what are the difference between these two choices? After I migrated to Oracle, all my views has a red cross icon next to it. Does that mean the view migration is failed or not? Please give me your comments. Thanks for your help.offline: for big (amount of data) databases with simple data types,
    uses bcp + files + scripts + sqlldr.
    online: for small (amount of data) databases (easier),
    uses (Java) jdbc.
    The view is likely to be broken - recompiling it may help.
    The Oracle schema is created using a .sql file - see under generated in the directory you gave originally in the wizard. There is a .out file that contains the result of running this script including any errors. During conversion there are also likely to be warnings displayed on the UI.
    There may be a single issue that is causing multiple issues - if viewa depends on functionb, and functionb is broken, viewa will also fail.
    >
    Kevin-Turloch
    SQLDeveloper Team

  • Desc doesn't show datetime columns from SQl Server

    Hi Everyone,
    I have working db link to sql server using hsodbc:
    Oracle 10.2 on AIX using freetds odbc driver version 0.82.
    I can access tables on sql server but oracle sees only varchars and numerics columns in a table.
    Is there a way to select datetime columns from sql server using hsodbc?

    when hsodbc does not show a certain data type it is commonly related to the fact that HSODBC doesn't support the related ODBC data type.
    In general you have a data type in your SQL Server which is mapped to an ODBC data type by the ODBC driver. HSODBC then maps the ODBC data type to an Oracle equivalent and when the ODBC data type isn't supported by HSODBC it just drops it from the select list.
    So for a root cause analysis an ODBC trace is required to determine the mapping of the FreeTDS ODBC driver.
    With this info we can then compare the mapping documented in the HSODBC manual and if the ODBC data type isn't listed as a supported data type you then need to cast the data type on the source side to a different data type (for example varchar/char).
    So please post the ODBC trace and I'll have a look.
    - Klaus
    Edited by: kgronau on Apr 2, 2013 12:50 PM
    BTW, here the link to the HSODBC docu:
    http://docs.oracle.com/cd/B19306_01/server.102/b14232.pdf
    Oracle® Database
    Heterogeneous Connectivity Administrator's Guide
    10g Release 2 (10.2)
    B14232-01
    B.1 Mapping ANSI Data Types to Oracle Data Types Through an ODBC
    Interface

  • Error connecting oracle 11g through SQL Developer

    i am using SQL Developer to connect to oracle 11g which is installed to another machine in same network
    means i am using a client machine to get connected to oracle installed to host machine through SQL Developer
    iam getting this error
    network adapter could not establish the connection
    vendor code 170002
    my listener.ora
    LISTENER =
    (DESCRIPTION_LIST =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = his-zoheb.PCSTECH.COM)(PORT = 1521))
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    my tnsnames.ora
    ORCL =
    (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = his-ansari.pcstech.com)(PORT = 1521))
    (CONNECT_DATA =
    (SERVICE_NAME = orcl)
    iam not understanding what is problem
    please somebody help

    I would say it's vendor code 17002 (not 170002). And such a message is thrown by SQL Developer , unfortunately very generic for any kind of connection problems.
    Start sqlplus and try to connect, very likely you get a better error message.
    Recently I had such a connection problem and the reason was - No Listener.
    Werner

  • Migration Error from Access to Oracle through SQL Developer.

    Hi,
    Actually I am trying to migrate data from MS Access 2002 to Oracle 9i database through the SQL Developer. But Whenever I go to Capture Database from Access it will show me an error.... Invalid procedure Call and then it shows an error message... >>>>>>
    ShowSplashScreen("_OracleSplashScreen",3)
    after that i wont be able to do this task anymore..... So please help me get out of it... How Cam I Maigrate data from Access to Oracle 9i...
    Is any other tool i use or you can help me for this tool to migrate date...
    Please tell me..
    If yu can send me a mail then mail me on [email protected]
    regards,
    Vishal

    Hi Vishal,
    I have responded to your related thread on the Migration Workbench forum - Migration Error from Access to Oracle through SQL Developer.
    Regards,
    Hilary

  • Migration from Access to Oracle through SQL developer

    Hi,
    I want to migrate an mdb file which contains a set of tables to oracle. During this process, i have created schema and all the tables in Oracle using DDL scripts. I would like to transfer only the data to Oracle from access through SQL developer. How can I do that?
    Regards,
    Meena

    Hi Meena,
    You mention that you have created the schema and tables in Oracle using DDL scripts. Did you use the Oracle SQL Developer Migration Workbench, creating a Migration Repository, to carry out this process? I see from your other postings that you are experiencing issues using the Exporter tool, which would lead me to believe that you haven't used the Migration Workbench to migrate your database. Obviously you will need to resolve your Exporter issues before you can proceed to the next phase of the migration process. I will aim to assist you with those issues on the seperate thread - ShowSplashScreen("_OracleSplashScreen",3) error The XML file generated by the Exporter tool forms the basis of your MS Access migration.
    In order to migrate your data from MS Access to Oracle using the Migrate Data option in Oracle SQL Developer, you will need to have access to the Migration Repository used for the migration of the MS Access database. The information contained in the Converted Model, in the Migration Repository, is used during the data migration process. The data migration requires the following connections:
    1. a source database connection i.e. a database connection to the MS Access MDB file
    2. a target database connection i.e. a database connection to the migration Oracle schema
    3. a Converted Model connection
    For further information on the creation of a Migration Repository, and the migration process, please refer to the accompanying documentation available via the online Help within Oracle SQL Developer, and also from OTN - http://download.oracle.com/docs/cd/E10405_01/doc/nav/portal_booklist.htm.
    I hope this helps.
    Regards,
    Hilary

Maybe you are looking for

  • Field Catalog in ALV

    hi, can any one explain the below program. What is the output of it. thanks REPORT  ZALV_REPORT2                            . TYPES: BEGIN OF TY_KNA1, KUNNR TYPE KNA1-KUNNR, NAME1 TYPE KNA1-NAME1, LAND1 TYPE KNA1-LAND1, END OF TY_KNA1. TYPE-POOLS SLI

  • Sales order for a make to order item

    HI when i create a sales order in crm for a make to order item. its not creating any requirements. like for a make to order item  will have a BOM  and routing attached to it. it creates  a production order or a purchase requisition.  but  its not hap

  • Transport of mitigation controls from GRC Dev to GRC Production in 10.0

    Hi All, Is there an option to transport mitigation controls from Dev to Prod in 10.0. Where is that option available. We could not find even download or upload option unlike 5.3 in 10.0 Thanks and Best Regards, Srihari.K

  • Setup of  security issue

    Hi Gurus, Please help! What do we need to do to resolve the below security issue? The Functional Guys says: I do not know how an authorization group on the material master works within SAP security.  For example, we need to be able to prevent someone

  • How to create a time control for while loop?

    Hello all, I would like to control my while loop's execution time. In other words I want to stop while loop's execution after time (in ms) which is set by user. For example user sets 5 seconds to front panel control and a while loop runs and stops af