How to write a simple query.

I have a table where I have data shown below. Now, I want to write a simple query which lists me the project and the count of the distinct effective dates for which data is existant there.
Sample data:
Project Task Effective Date (xx_proj_task_data)
101 T1 01-Jan-2008
101 T1 01-Feb-2008
101 T1 01-Mar-2008
101 T2 01-Jan-2008
101 T2 01-Apr-2008
101 T3 01-Apr-2008
102 T1 01-Jan-2008
102 T1 01-Feb-2008
102 T2 01-Apr-2008
103 T1 01-Jan-2008
103 T1 01-Feb-2008
103 T1 01-Mar-2008
103 T1 01-Apr-2008
103 T2 01-May-2008
103 T3 01-Jun-2008
103 T1 01-Jan-2008
103 T1 01-Aug-2008
103 T2 01-Apr-2008
Output Reqd:
Project Count(Distinct Effective Dates)
101 4
102 3
103 7
I can write a query that says:
select project_id, count(1)
from (select distinct project_id, effective_date
from xx_proj_task_data) x
group by project_id;
But, is there a way I can achieve the same by avoiding the inner Query (x) and just by a simple query ?
Thanks!

Try below query:
select project_id
, count(distinct effective_date)
from xx_proj_task_data
group by project_id;
--venkata                                                                                                                                                                                                                                                                                       

Similar Messages

  • How to write a SQL query in SAP B1 2007 B with input parameters?

    How to write a SQL query in SAP B1 2007 B with input parameters, on execution of which will ask for some input value from the user and the values will be selected from a list such as item list?

    The syntax like
    SELECT * FROM OITM T0 WHERE T0.ItemCode = '[%0\]'
    Thanks,
    Gordon

  • How to write the given query using 'ANY ' operator

    Hi,
    How to write the given query using 'ANY ' operator , I dont need to fetch to grade_master table twice in database, just need to fetch within the result set.
    SELECT dsg_code,dsg_name,dsg_grade FROM designation_master WHERE dsg_orgn='&&Orgn' and dsg_ctry='&&ctry'
    And dsg_loc ='&&loc' And dsg_oru = '&&oru' and dsg_grade in decode('&&radio_group',
    1, SELECT grd_code FROM grade_master WHERE grd_osm_code in (Select grd_osm_code FROM grade_master WHERE grd_orgn='&&Orgn' and grd_ctry='&&ctry' And grd_loc ='&&loc' And grd_oru = '&&oru' and grd_code ='&&emp_grade'),
    2, SELECT grd_code FROM grade_master WHERE grd_osm_code > (Select grd_osm_code FROM grade_master WHERE grd_orgn='&&orgn' and grd_ctry='&&ctry' and grd_loc ='&&loc' And grd_oru = '&&oru' and grd_code),
    3, SELECT grd_code FROM grade_master WHERE grd_osm_code < (Select grd_osm_code FROM grade_master WHERE grd_orgn='&&orgn' and grd_ctry='&&ctry' And grd_loc ='&&loc' And grd_oru = '&&oru' and grd_code ='&&emp_grade'))
    thanks
    rincy

    Hi,
    One thing I understood my your issue is you want to perform, execution of query once or fetch the results sets my minimizing the number of times executions of queries. It would be hard for us to check in this way, atleast provide some temporary data and some business rules. Only I can IN, >, < (queries logical conditons on inner query)
    - Pavan Kumar N
    - ORACLE OCP - 9i/10g
    https://www.oracleinternals.blogspot.com

  • How to write a sql query to calculate weights using CTE

    Hi guys,
    want some help using a CTE to generate data using recursive SQL - input data in table A to be transformed into table B shown below
    Table A
    Instru_id_index     instru_id_name    instru_id_constit  con_name   weight
            56                       INDEX A                     
    23                  A                 25
            56                       INDEX A                     
    24                 B                  25
            56                       INDEX A                     
    25                  C                 25
            56                       INDEX A                     
    57              
    INDEX  B       25
            57                      
    INDEX B                     31                 
    D                 33
            57                      
    INDEX B                     32                 
    E                  33
            57                      
    INDEX B                     33                 
    F                  33
    (Logic should be recursive in order to be able to handle multi-level, not just level 2.)
    Table B
    Instru_id_index     instru_id_name    instru_id_constit  constit_name   weight
            56                       INDEX A                        
    23                A                   25
            56                       INDEX A                        
    24                B                   25
            56                       INDEX A                        
    25                C                   25
            56                       INDEX A                        
    31                D                   8.3
            56                       INDEX A                        
    32                E                    8.3
            56                       INDEX A                        
    33                F                    8.3
            57                       
    INDEX B                       31                
    D                   33
            57                       
    INDEX B                       32                E                   
    33
            57                       
    INDEX B                       33               
    F                     33
    how can I write a simple CTE construct to  display the data in table B -  How can i calculate the values of weights as 8.3 respectively - calculate these without changing the structure of the tables.
    Can I do this in a CTE

    Full join?
    Anyway, thanks for Rsignh to produces a script with CREATE TABLE and INSERT statements. I've extended the data to one more level of recursion.
    create table weight_tab(
     instrument_id_index int,
     instrument_id_name varchar(10),
     instrument_id_constituent int,
     constituent_name varchar(10),
     [weight] decimal(10,2))
     insert into weight_tab values
     (56,'INDEX A',23,'A',25),(56,'INDEX A', 24,'B',25),
    (56,'INDEX A',25,'C',25),(56,'INDEX A', 57,'INDEX  B',25),
    (57,'INDEX B',31,'D',33), (57,'INDEX B', 32,'INDEX E',33),
    (57,'INDEX B',33,'INDEX C',33),
    (33,'INDEX C',42,'Alfa',60),
    (33,'INDEX C',43,'Beta',40),
    (32,'INDEX C',142,'Gamma',90),
    (32,'INDEX C',143,'Delta',10)
    go
    SELECT * FROM weight_tab
    go
    ; WITH rekurs AS (
        SELECT instrument_id_index, instrument_id_name, instrument_id_constituent,
               cast(weight as float) AS weight, cnt = 1
        FROM   weight_tab a
        WHERE  NOT EXISTS (SELECT *
                           FROM   weight_tab b
                           WHERE  b.instrument_id_constituent = a.instrument_id_index)
        UNION ALL
        SELECT r.instrument_id_index, r.instrument_id_name, w.instrument_id_constituent,
               r.weight * w.weight / 100, r.cnt  + 1
        FROM   rekurs r
        JOIN   weight_tab w ON r.instrument_id_constituent = w.instrument_id_index
        WHERE r.cnt < 4
    SELECT instrument_id_index, instrument_id_name, instrument_id_constituent,
           cast(weight AS decimal(10,2))
    FROM   rekurs
    go
    DROP TABLE weight_tab
    Erland Sommarskog, SQL Server MVP, [email protected]

  • How to write a sql query to retrieve data entered in the past 2 weeks

    Hi,
    I have file names and last accessed date(java.sql.Date format) stored in my database table, I would like to know how I can write a query to get the name of files accessed in the past 2 weeks,I use open sql server at the back end.
    Thanks in advance.

    This has essentially nothing to do with JDBC. JDBC is just an API to execute the SQL language using Java and thus interact with the databases.
    Your problem is related to the SQL language, you don't know how to write the SQL language. I suggest you to go through a SQL tutorial (there is one at w3schools.com) and to read the SQL documentation which come along with the database in question. A decent database manfacturer has a website and probably also a discussion forum / mailinglist as well.
    I'll give you a hint: you can just use equality operators in SQL like everywhere. For example: "WHERE date < somedate".

  • How to write this sql query in php code ?

    for example:
    insert into temp
    select *
    from testtable;
    after this, i will query data from sql below:
    select *
    from temp;
    how to write this php code ?
    who can help me ?
    thanks!

    Have a look at the manual to find out how to issue queries.
    http://us3.php.net/oci8

  • How to write XSJS Select Query with input parameters

    Hello Experts,
    I am creating a xsjs file and in that file I am trying to write a Select Query based on a Calculation View
    I have tried it the following way:
    var query = 'SELECT TOP 100 \"Name\", \"Address\", \"City\", \"Country\" FROM \"_SYS_BIC\".\"Test.HL/AddressView\"'
        + 'WITH PARAMETERS(\'PLACEHOLDER\' = (\'$$P_Name$$\', \' Akhil \'),'
      + '\'PLACEHOLDER\' = (\'$$P_City$$\', \' Lucknow \'))';
    But it gives me the "Mixed spaces and tabs error".
    How should I write XSJS Select Query with input parameters?
    Regards,
    Rohit

    >But it gives me the "Mixed spaces and tabs error".
    Mixed spaces and tabs has nothing to do with the syntax of the statement. You used both spaces and the tab in the content - which JSLint doesn't like.  Remove the beginning spaces of each line and use only one or the other.
    The actual syntax of your statement doesn't look right.  The problem is that you are escaping the \ when you don't need to if you are using ' instead of " for your string.  You escape with \" in the first line but then escape with \' in the 2nd and 3rd line.  That is going to cause serious parsing problems with the command.

  • How to write data from query into Real time cube?

    Hi All,
    Can anyone explain me step by step how to write data into a real time cube from front end queries.
    Thanks in advance

    Hi
    You can do this using Integrated Planning
    You need to create a aggregation level on the Real Time infocube and can create Planning function/sequence, Variables if needed.
    Then you can create query on this aggregation level and you can make the keyfigures Input ready in property pane and you can change the data and save it into cube.
    Please find below help link which clearly explains step by step about Integrated Planning like creating input ready queries etc.,
    http://help.sap.com/saphelp_nw70ehp1/helpdata/en/43/0c033316cd2bc4e10000000a114cbd/frameset.htm
    Regards
    Ravi

  • How to write a simple DB2 client?

    hello,
    I'm going to write a simple DB2 client in my new project,which can issue some simple db2 commands and get the result,for example,the "list tables" command. Can JDBC finish this job?
    any help appreciated.

    JDBC can do that and more.
    http://java.sun.com/docs/books/tutorial/jdbc/index.html
    %

  • How to write a SQL query in Java

    Hi, I'm writing a program that pulls all data from the database with a simple "SELECT * FROM [TABLE NAME]. The problem is that I don't know how to set it up properly. My code will only connect to the database but not execute the query. Please help.
    {code}import java.sql.*;
    import java.util.Properties;
    import java.sql.*;
    public class DatabaseSelect {
    public static void main(String[] args) {
    System.out.println("Connected to the database!");
    Connection conn = null;
    String url = "jdbc:oracle://localhost:1571/";
    String dbName = "jdbc";
    String driver = "com.oracle.jdbc.Driver";
    String userName = "HR";
    String password = "database";
    String query = "SELECT * FROM HR";
    try {
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,userName,password);
    System.out.println("Connected to the database");
    } catch (Exception e) {
    e.printStackTrace();
    }{code}

    pbsacct_1 wrote:
    I added it between my System.out statement and the catch exception. This is what I put, but I'm getting the same result. I looks like a lot of errors. The only thing that prints is the "Connected to database".
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    ResultSet srs = stmt.executeQuery("SELECT * FROM HR");Here is what displays in the console.
    Connected to the database!
    java.lang.ClassNotFoundException: com.oracle.jdbc.Driver
         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
         at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
         at java.lang.Class.forName0(Native Method)
         at java.lang.Class.forName(Class.java:164)
         at Discussion10.DatabaseSelect.main(DatabaseSelect.java:26)You are not connecting.
    Because you aren't even loading the driver.
    To be honest if you can't decipher a stack trace with "java.lang.ClassNotFoundException" on your own you are not ready for JDBC anyway.

  • How To Write A Matrix Query in Oracle 10g

    Hi All,
    I need to write a query displaying total of each Month and each quarter total. Here is example:
    CREATE TABLE T_CUST_REG(
    CUST_ID NUMBER,
    CUST_NAME VARCHAR2(255),
    REGDATE  DATE);
    INSERT INTO T_CUST_REG VALUES (1, 'A','01-JAN-2012');
    INSERT INTO T_CUST_REG VALUES (2, 'B','01-FEB-2012');
    INSERT INTO T_CUST_REG VALUES (3, 'C','01-MAR-2012');
    INSERT INTO T_CUST_REG VALUES (4, 'D','01-APR-2012');
    INSERT INTO T_CUST_REG VALUES (5, 'E','01-MAY-2012');
    INSERT INTO T_CUST_REG VALUES (6, 'F','01-JUN-2012');
    INSERT INTO T_CUST_REG VALUES (7, 'G','01-JUL-2012');
    INSERT INTO T_CUST_REG VALUES (8, 'H','01-AUG-2012');
    INSERT INTO T_CUST_REG VALUES (9, 'I','01-SEP-2012');
    INSERT INTO T_CUST_REG VALUES (10, 'J','01-OCT-2012');
    INSERT INTO T_CUST_REG VALUES (11, 'K','01-NOV-2012');
    INSERT INTO T_CUST_REG VALUES (12, 'L','01-DEC-2012');
    Output REQUIRED:
    JAN
    FEB
    MAR
    Q1
    APR
    MAY
    JUN
    Q2
    JUL
    AUG
    SEP
    Q3
    OCT
    NOV
    DEC
    Q4
    1
    1
    1
    3
    1
    1
    1
    3
    1
    1
    1
    3
    1
    1
    1
    3
    I am able to create matrix with following query, but issue is how to put Quarter(total) in between.
    Select Count(1), TO_CHAR(REGDATE, 'MON') MON 
    FROM T_CUST_REG T
    GROUP BY TO_CHAR(REGDATE, 'MON');
    Regards

    Hi,
    The query you posted will produce a separate row for each month, not a separate column.  Is that what you want?
    If so, you can add the quarterly totals with GROUPING SETS, like this:
    SELECT    COUNT (*)    AS cnt
    ,         CASE
                  WHEN  GROUPING (TRUNC (regdate, 'MONTH')) = 0
                  THEN  TO_CHAR (TRUNC (regdate, 'MONTH'), 'YYYY MON')
                  ELSE  TO_CHAR (TRUNC (regdate, 'Q'),     'YYYY "Q"Q')
              END          AS label
    FROM      t_cust_reg
    GROUP BY  GROUPING SETS ( (TRUNC (regdate, 'MONTH'))
                            , (TRUNC (regdate, 'Q'))
    Instead of calling TRUNC over and over, you might want to do it just 2 times, in a sub-query, giving aliases to the results, and then use the aliases over and over in the main query.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the exact results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • How to write EJB-QL query with IN notation and list values EJB3.0?

    hi,
    I would like to write an EJB-QL query where the query would return the objects whose name equals to one of the names in the list.
    SELECT DISTINCT OBJECT(feature) FROM MAPSHEET_LOCK feature WHERE feature.name IN ?1
    For ?1 I would like to pass in a list. How can I write this query?
    Thanks

    List<String> names;
    StringBuilder sb = new StringBuilder("SELECT DISTINCT OBJECT(feature) FROM MAPSHEET_LOCK feature WHERE feature.name IN (");
    String separator = "";
    for (String name : names) {
      sb.append(separator);
       sb.append("'");
       sb.append(name);
       sb.append("'");
       separator = ", ";
    sb.append(")");
    String ejbql = sb.toString();
    // Code for execute query
    ....Message was edited by:
    Rulas

  • How To Write This Complex Query

    hi all
    i have following tables
    1. employees
    2. departments
    3. vaccination_type
    4. vaccination_subtype
    5. vaccination_given
    a vaccination suppose 'FLU' is to be given to an employee or an department(all employees of this department)
    EXAMPLE:
    vaccination type is "FLU", vaccination subtypes are "FLU1" and "FLU2"
    vaccination type is "CHIC", vaccination subtypes are "CHIC1" and "CHIC2"
    employee # 4444 dept# 40 assigned "FLU"
    dept 50 assigned "CHIC"
    employee 4444 has given only one dose means "FLU1" >>> it means defaulters
    employee 5555 of dept 50 have given all 2 doses >>>>>> it means 9 emps are defaulters (total emps are 10 in dept 50)
    i need to write a query to identify those employees who are defaulters
    means not a single vaccination given or 1 of 3, or 2 of 3 etc
    query should return 9 employees of dept 50 and employee 4444
    hope you understand my problem.
    Regards,

    How about this one :
    select empno,a.vactype,count(vacstype) From vaccination_given a
    group by empno,a.vactype
    having count(vacstype) < (select count(*) from vaccination_subtype where vactype = a.vactype)
    EMPNO     VACTYPE     COUNT(VACSTYPE)
    5553     CHIC     1
    5554     CHIC     1
    5560     CHIC     1
    5551     CHIC     1
    5559     CHIC     1
    5558     CHIC     1
    4444     FLU     1
    5552     CHIC     1
    5556     CHIC     1
    5557     CHIC     1

  • How to write this SQL query

    We have a table, the structure as below: (I put a blank row in the middle to make it clear)
    Product_ID     Retailer     Retail_Price
    1001          A          1.2
    1001          B          1.5
    1001          C          1.4
    1002          B          2.0
    1002          C          2.1
    1002          E          1.8
    1002          F          2.0
    1003          A          1.7
    1003          C          1.5
    Basically we use the table to compare the retail price for different retailers in terms of different products. My customer asked me to give a report based on above table but only shows the retail prices which contain retailer A.
    For example, on above table I need to give a report like below:
    Product_ID     Retailer     Retail_Price
    1001          A          1.2
    1001          B          1.5
    1001          C          1.4
    1003          A          1.7
    1003          C          1.5
    Can anybody give some ideas on how I should write a SQL to achieve this.
    Many thanks!

    SQL> create table product(
    2 Product_ID varchar2(10),
    3 Retailer varchar2(10),
    4 Retail_Price number(7,2)
    5 )
    6 /
    Table created.
    SQL> insert into product values('1001','A',1.2)
    2 /
    1 row created.
    SQL> insert into product values('1001','B',1.5)
    2 /
    1 row created.
    SQL> insert into product values('1001','C',1.4)
    2 /
    1 row created.
    SQL> insert into product values('1002','B',2.0)
    2 /
    1 row created.
    SQL> insert into product values('1002','C',2.1)
    2 /
    1 row created.
    SQL> insert into product values('1002','E',1.8)
    2 /
    1 row created.
    SQL> insert into product values('1002','F',2.0)
    2 /
    1 row created.
    SQL> insert into product values('1003','A',1.7)
    2 /
    1 row created.
    SQL> insert into product values('1003','C',1.5)
    2 /
    1 row created.
    SQL> select * from product
    2 /
    PRODUCT_ID RETAILER RETAIL_PRICE
    1001 A 1.2
    1001 B 1.5
    1001 C 1.4
    1002 B 2
    1002 C 2.1
    1002 E 1.8
    1002 F 2
    1003 A 1.7
    1003 C 1.5
    9 rows selected.
    SQL> select * from product where PRODUCT_ID in (
    2 select distinct PRODUCT_ID from product where RETAILER='A')
    3 /
    PRODUCT_ID RETAILER RETAIL_PRICE
    1001 A 1.2
    1001 B 1.5
    1001 C 1.4
    1003 A 1.7
    1003 C 1.5

  • How to write a sql query for this condition?

    i have one table with columns v_sub,v_visit and v_date and the structure is like this
    v_sub v_visit v_date
    1 visit-1 01-mar-09
    1 visit-2 05-mar-09
    1 visit-3 17-mar-09
    2 visit-1 04-feb-09
    2 visit-2 12-mar-09
    2 visit-3 20-mar-09
    i want to write a query which check weather it is in chronological order or not.(for v_sub,v_visit and v_date should be in chronological order as above)
    and i want to check the condition as below:
    v_sub v_visit v_date
    1 visit-1 01-mar-09
    1 visit-2 05-feb-09
    1 visit-3 17-mar-09
    2 visit-1 04-feb-09
    2 visit-2 12-jan-09
    2 visit-3 20-mar-09
    Thanks in advance

    use LAG function to get the previous date
    SQL> -- sample data
    SQL> with t
      2  as
      3  (
      4     select 1 v_sub, 'visit-1' v_visit, to_date('01-mar-09','dd-mon-yy') v_date from dual union all
      5     select 1, 'visit-2', to_date('05-mar-09','dd-mon-yy') from dual union all
      6     select 1, 'visit-3', to_date('17-mar-09','dd-mon-yy') from dual union all
      7     select 2, 'visit-1', to_date('04-feb-09','dd-mon-yy') from dual union all
      8     select 2, 'visit-2', to_date('12-mar-09','dd-mon-yy') from dual union all
      9     select 2, 'visit-3', to_date('20-mar-09','dd-mon-yy') from dual
    10  )
    11  -- end of sample data
    12  select v_sub, v_visit, v_date, lag(v_date) over(partition by v_sub order by v_visit, v_date) v_previous_date
    13    from t
    14  /
         V_SUB V_VISIT V_DATE    V_PREVIOU
             1 visit-1 01-MAR-09
             1 visit-2 05-MAR-09 01-MAR-09
             1 visit-3 17-MAR-09 05-MAR-09
             2 visit-1 04-FEB-09
             2 visit-2 12-MAR-09 04-FEB-09
             2 visit-3 20-MAR-09 12-MAR-09
    6 rows selected.Now you can check if the previous_date is less than v_date

Maybe you are looking for

  • I cant download apps from store in lumia 720

    Hello sir or madam pls fix my problem some times am getting error when am using store to download apps

  • Track count

    I have the following set-up: powerbook G4 1.67Ghz,2gig ram, motu 828mkII fw 400 interface, owc fw800 600 gig raid array outboard hard drive, Logic 7.2 and I need to know what kind of track count,recording live audio, can I expect at 24bit 48Khz ... a

  • What exactly is the issue with CS5 and the retina display?

    Just picked up a late 2013 MBP with retina display and have PS CS5 running on it. What is the exact issue that I should be having when it comes to running CS5 on these machines ?  Is it just that the UI looks fuzzy?  Other than that it seems to run f

  • Need to put help document on home page.

    Hi All..... in my project several users are not trained on oracle HRMS as to how to use the selfservice due to being on leave. I have prepared a document with screenshots on how to use the functions. I want to put this document in home page. So when

  • Reader version 17.0.0.169 (for ActiveX) for Windows 8

    Reader version 17.0.0.169 (for ActiveX) is latest but Microsoft doesn't have it available for Windows 8 and YOUR site won't allow me to download it myself. Please make it available. Version 17.0.0.134 is a vulnerable version. I don't want to have to