Please find me a better sql

Say I have a table TBL_PROJECT. Projects can have different status - Active, Hold, Complete
Project_ID
Descritption
Latest Status
Target Completion Date
1
Project A
Active
12/20/2013
2
Project B
Hold
03/04/2014
3
Project C
Complete
10/30/2013
Say I have an another tabel TBL_TRACK_STATUS which tracks the status change of the projects.
Project_ID
Status
Changed_Date
1
Active
01/20/2013
1
Hold
05/05/2013
1
Active
10/05/2013
2
Active
04/06/2013
2
Hold
08/08/2013
3
Active
02/10/2013
3
Complete
10/15/2013
Now I need a query that will get me result like below
Project_ID
Description
Active Date
Hold Date
Complete Date
1
Project A
10/05/2013
05/05/2013
2
Project B
04/06/2013
08/08/2013
3
Project C
02/10/2013
10/15/2013
Well, I can write the query as below:
SELECT A.PROJECT_ID,
  A.DESCRIPTION,
  (SELECT MAX(B.CHANGED_DATE)
  FROM TBL_TRACK_STATUS B
  WHERE A.PROJECT_ID = B.PROJECT_ID
  AND B.STATUS       = 'Active'
  ) AS "Active Date",
  (SELECT MAX(C.CHANGED_DATE)
  FROM TBL_TRACK_STATUS C
  WHERE A.PROJECT_ID = C.PROJECT_ID
  AND C.STATUS       = 'Hold'
  ) AS "Hold Date",
  (SELECT MAX(D.CHANGED_DATE)
  FROM TBL_TRACK_STATUS D
  WHERE A.PROJECT_ID = D.PROJECT_ID
  AND D.STATUS       = 'Complete'
  ) AS "Complete Date"
FROM TBL_PROJECT A;
OR
I can write the functions that will return the status date changed -
SELECT A.PROJECT_ID,  -- ASSUMING THE FUNCTIONS RETURNS THE DATE FOR DIFFERENT STATUSES
  GET_ACTIVE_DATE_CHANGED(A.PROJECT_ID)   AS "Active Date",
  GET_HOLD_DATE_CHANGED(A.PROJECT_ID)     AS "Hold Date",
  GET_COMPLETE_DATE_CHANGED(A.PROJECT_ID) AS "Complete Date"
FROM TBL_PROJECT A;
My question is - IS THERE ANY OTHE WAY THAT THE QUERY CAN BE WRITTEN IN EFFICIENT MANNER THAN WHAT I AM DOING HERE?

You could consider analytics ...
This query is untested, but it should give you the idea:
with w_base as (
      select p.project_id, p.description,
               max(case when status = 'Active' then t.changed_date else null end)
                     over (partition by t.project_id
                           order by case when status = 'Active' then t.changed_date else null end nulls last ) active_date,
               max(case when status = 'Hold' then t.changed_date else null end)
                     over (partition by t.project_id
                           order by case when status = 'Hold' then t.changed_date else null end nulls last ) hold_date,
               max(case when status = 'Complete' then t.changed_date else null end)
                     over (partition by t.project_id
                           order by case when status = 'Complete' then t.changed_date else null end nulls last ) complete_date,
             row_number() over (partition by p.project_id order by t.changed_date) rnum
        from tbl_project p,
             tbl_track_status t
       where p.project_id = t.project_id
select project_id, description, active_date, hold_date, complete_date
  from w_base
where rnum = 1

Similar Messages

  • Find out free hand sql reports

    Hi All,
    I need find out free hand sql reports we have above 3000 reports and we are using business objects 6.5.
    Could you please tell me any one how track these reports which table these informations.
    Advance Thanks.
    Regards,

    Hi Sreenivas,
    The following information might help you to retrieve the information you require.
    There is a universe available as a part of Auditor product for which you need a separate license. This product helps you to have better control of your repository usage. The universe is REP_ORA.
    You can create a very simple universe using the OBJ_M_DOCATVAR table. This is a new table in version 6.5.x. This table gives the source of each dataprovider. Full client reports can have more dataproviders, so it is a one-to-many relationship.
    If you add the OBJ_M_DOCUMENTS table to the universe and join them by doc_id (OBJ_M_DOCATVAR.M_DOC_N_ID = OBJ_M_DOCUMENTS.M_DOC_N_ID), then you will have the proper document (report) names instead of just the ID numbers.
    The OBJ_M_DOCATVAR table is updated each time a BusinessObjects (6.5.x) or a WebIntelligence (2.5) document is exported to the repository (Send to users, Send to Broadcast Server, Publish to Corporate documents).
    The basic fields you will need in the report:
    OBJ_M_DOCUMENTS.M_DOC_C_NAME: The name of the document. The name of the document is unique for each given document domain.
    OBJ_M_DOCATVAR.M_DOC_N_ID: The unique identifier of the document. This value is referenced in OBJ_M_DOCUMENTS. The properties are therefore only applied to exported documents.
    OBJ_M_DOCATVAR.M_DOCATV_C_DPNAME: The name of the Data Provider (DP) for the current document.
    OBJ_M_DOCATVAR.M_SRC_C_NAME: The name of the source. You can see it in the end-user module by opening a document and choosing to view the data manager. It can be a universe name or a connection depending on the type of Data Provider.
    Regards,
    Sarbhjeet Kaur

  • How to find where the below sql is used in database

    Hi all,
    I am tring to find the below SQL in our data base where it is used?
    I had done the checking in all reports manually, But have no luck.
    But i am sure that the below sql is in our data base and having some issue on it.
    Can any one please let me know how to find where the below query is used in our database? I mean in packages/ triggers/ procedures or any where in our database
    Please find the query
    select count(sh.shipment_gid) from SHIPMENT sh, SHIPMENT_TYPE stp, LOCATION ld where (sh.shipment_gid in (select sstop.shipment_gid from SHIPMENT_STOP sstop where (sstop.appointment_delivery > trunc(TO_DATE(:1, :2), :3)) and (sstop.appointment_delivery is not null))) and (sh.is_to_be_held=:"SYS_B_0") and (sh.domain_name=:4) and (sh.insurance_policy_number like :"SYS_B_1") and (sh.perspective=:"SYS_B_2") and (sh.shipment_type_gid=stp.shipment_type_gid) and (sh.dest_location_gid=ld.location_gid(+)) and (stp.shipment_type_xid!=:5) order by ld.location_name, sh.start_time
    Regards,
    Dpk

    There is video demonstration how to import from data dictionary.
    After import you can select from the main menu: View->DDL File Editor menu item to open the DDL dialog and then click on "Generate" button on the dialog. When the DDL is generated you can search in it using "Find" button.
    Ivan

  • Installation Error: Could not find SOFTWARE\Microsoft\Microsoft SQL Server\120 registry key.

    Dear Expert,
    I setup SharePoint for 3 tiers, WFE, APP + DB Server.
    I installed SQL 2014 reporting service Add-in on APP Server and already enable sql reporting service in DB Server. DB Server can do report server.
    I follow the step : https://msdn.microsoft.com/en-us/library/jj219068(v=sql.120).aspx  but I stuck in no.2, I cannot start the services, in picture below.
    I checked the SSRS provision is disabled not online
    Please help.

    Hi MasterBird,
    According to your description, when you register and start the Reporting Services SharePoint Service, the error: Could not find SOFTWARE\Microsoft\Microsoft SQL Server\120 registry key is thrown out.
    As this
    article say, if you use three-computer deployment, the first computer hosts an instance of a SharePoint product. The Reporting Services Add-in is installed on this computer. The second computer hosts SQL Server Reporting Services and the third computer
    hosts an instance of the Database Engine.
    Regarding the error message, it could cause by the Reporting Services – SharePoint feature and Reporting Services Add-in for SharePoint Products haven’t been installed. Please refer to articles:
    SSRS SharePoint 2013 Create Service Application error: Could not find SOFTWARE\Microsoft\Microsoft SQL Server\110 registry key Error
    Installing SSRS 2012 in SharePoint Mode on SP2010
    If issue persists, please check the error detailed information in ULS logs, and check Program Files\Microsoft SQL Server\120\Setup Bootstrap\Log to see if any error occurred during set up. For more information, please refer to this article:
    Troubleshoot a Reporting Services Installation.
    If you have any question, please feel free to ask.
    Best regards,
    Qiuyun Yu
    Qiuyun Yu
    TechNet Community Support

  • Cannot find a report for SQL Server monitoring

    We installed a SCOM 2012 environment recently and imported the SQL Server Management packs successfully. SQL Servers are being monitored. 
    However, in the reporting section, i cannot find a group for SQL Server Reporting. All other show up ( Active Directory, Appication Monitoring, Web monitoring, etc) Just none for the SQL Reporting.
    Please help

    Hi,
    I assume that you miss some SQL management packs, would you please re-download the management pack here and import it:
    https://www.microsoft.com/en-hk/download/details.aspx?id=10631
    After successful importing, you should see it under reporting workspace:
    If you still cannot see it, please check operation manager event logs for more information to help to troubleshoot this issue.
    Regards,
    Yan Li
    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]

  • APP-ALR-04106: Please correct the user-defined SQL statement for this alert

    Hi All,
    I have created an alert for engineering module in R12. It got tested and was working fine. when the user testing it, while trigger the alert getting the error, "APP-ALR-04106: Please correct the user-defined SQL statement for this alert".
    when verified the alert, it got verified and ran also. It parsed the query successfully and when run it fetched few records.
    Need help in resolving the issue.
    Thanks in advance.
    Regards,
    sri
    Edited by: user10939296 on Jan 18, 2010 1:16 AM

    Hi Sri;
    I have already gone through the Note: 948037.1. But this note is related to 11i. The solution provided in the Note is for 11i.
    I am facing this issue in R12. Is this patch applicable to R12?I belive its not. But u can check Solution part 4 for your instance, at least it can give you idea. The other note in metalink related bug and all for R11 too.
    I belive its better way to rise Sr while waiting other forum user response to that thread
    Regard
    Helios

  • Can anyone please find me examples of these methods.......

    I'm trying to get a better idea what "Helper methods" do but cannot find any good examples, can anyone please find me examples of these method.
    Also how do I print methods between different classes in a program??
    many thanks

    Also how do I print methods between differentclasses
    in a program??This question makes no sense to me.I've got some classes that need to print methods from
    an input/output class, what code is needed for
    this????You want to print methods? You might use Reflection for this. An Input/Output class? What is that? You want to print method signatures from a class that handles IO? Why should it matter what the class does?
    I've no idea what code might be needed to do whatever it is you are wanting to do here - please clarify and see if we can get a more useful answer.

  • Please help me convert the SQL to HQL

    select
    count(*) as col_0_0_
    from
    edu.hr_people_all hrpeopleal0_
    left outer join
    edu.conditions_journal conditions1_
    on hrpeopleal0_.people_id=conditions1_.people_id
    and conditions1_.personal_number like '%'
    where
    hrpeopleal0_.status=11

    Hibernate version: 3
    HQL as I 've done
    FROM PMSProjectsORO pjt LEFT JOIN PMSChecklistORO pcl
    ON pjt.projectId = pcl.projectId order by
    pjt.projectCode ascHQL is probably incorrect.
    Try this:
    FROM
    PMSProjectsORO pjt
    LEFT JOIN
    PMSChecklistORO pcl
    ORDER BY
    pjt.projectCode asc>
    Original SQL for MSQL5:
    FROM PMSProject LEFT JOIN PMSCheckList ON
    PMSProject.PROJECT_ID = PMSCheckList.PROJECT_ID order
    by PMSProject.PROJECT_CODE desc
    Name and version of the database you are using:
    MYSQL 5
    Please help me convert the sql to hql since what i
    've done is throwing error like unexpected token...
    Please reply ASAPLet me know if that's better.
    %

  • How to find session information in sql plus

    Can someone please tell me how to find session information in sql plus? I specifically want to know my privileges for the current session. Thanks in advance.

    SELECT * FROM session_privs;
    SELECT * FROM session_roles;

  • TS4006 my mobile was stolen 2days before. please find my iphone and give the location. my mobile IMEI Number is - 012854005421769

    Dear Sir,
    my mobile was stolen 2days before. please find my iphone and give the location.
    my mobile IMEI Number is - > 012854005421769
    this is my mobile user name - > VINOTH P
    country  - > india
    state - > tamil nadu
    City - > chennai
    Mobile Phone Bill Details :
    Apple Iphn 8gb #GS Airtel BLK
    166104 U4
    CSH N N  Purchasing Date ( 01-01-2012 )
    Please i am very critical situation now, My Business is very down. please help me sir.

    You aren't talking to Apple here. This is a user to user technical help forum.
    Apple will not find your phone.

  • How to find leap year in sql query

    How to find leap year in sql query

    Select
    CASE
      WHEN result = 0 THEN 'Leap_Year'
      WHEN result <> 0 THEN 'Not_A_Leap_Year'
    END
    From (Select mod((EXTRACT(YEAR FROM DATE '2013-08-24')), 4) result FROM DUAL);

  • How to find out the failed sql command and its data from DEFERROR

    Hi,
    has anybody a procedure or some other possibilities to read the content of column USER_DATA of the advanced replication view DEFERROR in order to find out the failed sql command and its column values?
    Thanks in advance.

    Hi Vishwa,
                 The control would be something like this for navigation in Get_p_xxx method u mention as link and u mention a event name which gets triggered on the click of this hyperlink. So your GET_P_XXX method would have the following code:
    CASE iv_property.
        WHEN if_bsp_wd_model_setter_getter=>fp_fieldtype.
          rv_value = cl_bsp_dlc_view_descriptor=>field_type_event_link.
        WHEN if_bsp_wd_model_setter_getter=>fp_onclick.
          rv_value = 'EXAMPLE'.
    Now you have to create a method as EH_ONEXAMPLE at your IMPL class and within which you would give an outbound plug method. Within the outbound plug the target data would be filled in the collection and window outbound plug would be triggered.
    This is a huge topic and  i have just mentioned you basic things.
    Regards,
    Bharathy.

  • How to find out who installed SQL Server in Big orgnization?

    Hi all,,,
    I want to find out who installed SQL server in their computers/servers. The internet is open and it's easy to install SQL server EXPRESS. Iam not looking for the command which will results computers in my vlan only. Iam looking for a query to result out
    all SQL installed with different Vlan within the organization.
    Anyway to find them out?!  Thanks.

    No need to know WHO install it but is it possible to know the Host name that SQL installed in?!
    You asked for "who", not for "where".
    You can use MS MAP =
    Microsoft Assessment and Planning Toolkit : "Software Usage Tracker Feature Overview ....
    SQL Server" to get reports, which software is running on which machine.
    Or you can use this PowerShell Script
    Create Inventory of SQL Server in Network as Html
    For both you need permissions to fetch the required information from all machines, of course.
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • SharePoint 2010 with SQL Reporting Services 2012 "Installation Error: Could not find SOFTWARE\Microsoft\Microsoft SQL Server\110 registry key"

    i have a small sharepoint farm "2 servers" (WFE) and SQL Server 2012 ,
    i installed rsSharePoint for reporting services , as a read the Reporting services is Application Service  with SQL 2012 not as SQL 2008 R2 with SharePoint integrated mode, so after installation when am trying to configure the  SQL Server
    Reporting Services Application services i got the below error :
    "Installation Error: Could not find SOFTWARE\Microsoft\Microsoft SQL Server\110 registry key"
    any kind of help would be much appreciated .

    I'm having the same issue, but to me it seems like the instructions I've seen are all over the place.  I already have SP 2013 and SQL 2012 SP 1 installed.  I already added the features for Reporting Services - Sharepoint and Reporting Services
    Add-in for SharePoint Products.  I didn't see the Reporting Service service in CA, so I ran the powershell scripts to add SPRSService and SPRSServiceProxy. 
    My initial quest, was to get the BI features enabled, which took me on the journey of adding the Report Services, then I got the error.
    Then when I read this about installing the BI features
    http://blogs.msdn.com/b/querysimon/archive/2012/11/26/installing-the-bi-features-of-sharepoint-2013.aspx, which I already had SQL and SP installed.  Do I have to rerun and selct the SQL Server PowerPoint for SharePoint option?  I have the PowerPoint
    Configuration Tool on my SQL server, I run it and I get PowerPivot for SP 2013 Configuranoi is missing prerequisits:  SP 2013 EE is required.  Which I have EE.
    Is Reporting Services different or in addition to the BI Feature (PowerPivot)? 
    Have I missed a step?
    Thanks for you help.
    Kim
    Kim.Williams

  • Find & Replace in PL/SQL crashing

    This problem is getting incredibly irritating as forms is crashing so frequently that I'm losing hours in the day just recovering the forms I have open...
    If anyone knows how I can stop this happening, I'd be really happy to know - is there a workaround? a patch? or a registry fix?
    I can't upgrade to a new version as this is a company installation.
    Find and Replace PL/SQL...
    Search for something that has a couple of "pages" of results. Drag the button on the scroll bar down to the bottom then use the mouse wheel. Forms hangs and then crashes.
    "Oracle Forms Designer has encountered a problem and needs to close. We are sorry for the inconvenience."
    (I'm getting sick of seeing that!)
    There's no real information
    I can't copy the Error Report Contents, the text won't copy but there is a lot of it and it starts
    Exception Information
    Code: 0xc0000005 Flags: 0x00000000
    Record: 0x0000000000000000 Address: 0x00000000000000b2
    System Information
    Windows NT 5.1 Build: 2600
    etc.
    Module 1
    frmbld.exe
    Image Base: 0x00400000 Image Size: 0x00000000
    Checksum: 0x00000000 Time Stamp: 0x42d63632
    Version Information
    Signature: feef04bd
    StrucVer: 00010000
    FileVer: (10.1:2.0)
    ProdVer: (10.1:2.0)
    Similar information for 167 Modules.
    Then dozens of pages of Thread 1 information.
    If you need any of those details let me know...
    There is no information
    I have images and the crash log but I can't find out how to attach them to this question...
    This is the start of the crash log:
    <?xml version="1.0" encoding="UTF-16"?>
    <DATABASE>
    <EXE NAME="frmbld.exe" FILTER="GRABMI_FILTER_PRIVACY">
    <MATCHING_FILE NAME="40COMUPD.EXE" SIZE="511424" CHECKSUM="0x60B737E3" BIN_FILE_VERSION="4.71.603.0" BIN_PRODUCT_VERSION="4.71.603.0" PRODUCT_VERSION="4.71.0603.0" FILE_DESCRIPTION="Win32 Cabinet Self-Extractor " COMPANY_NAME="Microsoft Corporation" PRODUCT_NAME="Microsoft(R) Windows NT(R) Operating System" FILE_VERSION="4.71.0603.0" ORIGINAL_FILENAME="WEXTRACT.EXE " INTERNAL_NAME="Wextract " LEGAL_COPYRIGHT="Copyright (C) Microsoft Corp. 1995" VERFILEDATEHI="0x0" VERFILEDATELO="0x0" VERFILEOS="0x40004" VERFILETYPE="0x1" MODULE_TYPE="WIN32" PE_CHECKSUM="0x817A7" LINKER_VERSION="0x40000" UPTO_BIN_FILE_VERSION="4.71.603.0" UPTO_BIN_PRODUCT_VERSION="4.71.603.0" LINK_DATE="03/03/1997 12:44:52" UPTO_LINK_DATE="03/03/1997 12:44:52" VER_LANGUAGE="English (United States) [0x409]" />
    <snip>
    Edited by: JA12 on Nov 21, 2008 2:01 AM
    Edited by: JA12 on Nov 21, 2008 2:03 AM

    Hi, Tina
    I am from Oracle, and I could reproduce that behavior with the following:
    Windows NT, Forms 6.0, doing that while connected.
    It worked fine with Windows 95.
    It worked fine if you disconnect prior to doing the Find and Replace...
    I'm guessing that you still use version 6.0.
    If so, you should upgrade to Forms 6i (the latest version), which has none of these issues:
    You can Find and replace PL/SQL using Windows NT and while connected to the database.
    Hope this helps,
    Pedro

Maybe you are looking for

  • Deskjet 6940 pdf docs are printing as jumbled up letters and symbols

    I have a deskjet 6940. When I print pdf docs they are a jumbled up mess of letters and or symbols. I have uninstalled and reinstalled the adobe program more than once and the problem is still ongoing.

  • Sync messages from different phone

    I currently have a Nokia phone and plan to get a new one, but of different model. Can I sync my messages from the old phone to the new phone through Nokia Ovi?

  • Can't Download Past 50%

    Everytime I try to download and run Adobe free Flash player, it tells me that a twcapp has to be closed and this is all greek to me, so therefore I don't know what I'm supposed to be closing.  Can anyone advise me on what to do so that I can get the

  • DAQ Troubleshooting Question

    When I was setting up my USB 6210 I hit the run button to see what my voltage values were. Without doing anything to my DAQ assistant or touching the DAQ, the voltage inputs do this. Any ideas as to what may be causing this?

  • Using Office 365 - how to close sub folders in iPhone-mail app.

    using Office 365 - how to close sub folders in iPhone-mail app.