COUNT with Group By clause issue

Good Morning everyone. I have a simple query that calculates the count of 3 expressions. It is supposed to group by region and province as a result set, but instead places the TOTAL count for each expression in the region and province fields. What am I doing wrong? This is my query:
SELECT TABLE1."Province",
TABLE1."Region",
(SELECT(COUNT(TABLE1."Nationality"))
FROM TABLE1
WHERE (TABLE1."Nationality" <> 'United States'
AND TABLE1."Nationality" <> 'Nat1')
OR (TABLE1."Medical" <> 'ON MEDICAL'
AND TABLE1."Region" <> 'CONUS')
) "TCN COUNT",
(SELECT(COUNT(TABLE1."Nationality"))
FROM TABLE1
WHERE (TABLE1."Nationality" = 'United States')
OR (TABLE1."Medical" <> 'ON MEDICAL'
AND TABLE1."Region" <> 'CONUS')
) "US COUNT",
(SELECT(COUNT(TABLE1."Nationality"))
FROM TABLE1
WHERE (TABLE1."Nationality" = 'Nat1')
OR (TABLE1."Medical" <> 'ON MEDICAL'
AND TABLE1."Region" <> 'CONUS')
) "HCN COUNT"
FROM TABLE1
GROUP BY TABLE1."Province",
TABLE1."Region";
Any help would be appreciated. Thank you.
Aqua

Because you are not passing any values to the inner query from the outer one..
Are you looking for this?
SELECT      TABLE1."Province",
     TABLE1."Region",
     sum (
       case when (
             TABLE1."Nationality" != 'United States'
             AND TABLE1."Nationality" !=  'Nat1'
            OR (
            TABLE1."Medical" != 'ON MEDICAL'
            AND TABLE1."Region" != 'CONUS'
             ) then 1 else 0 end
         ) "TCN COUNT",
     sum (
       case when (
            TABLE1."Nationality" = 'United States'
            OR (
            TABLE1."Medical" 'ON MEDICAL'
            AND TABLE1."Region" 'CONUS'
               ) then 1 else 0 end
         ) "US COUNT",
     sum (
       case when (
            TABLE1."Nationality" = 'Nat1'
              OR (
               TABLE1."Medical" 'ON MEDICAL'
               AND TABLE1."Region" 'CONUS'
                 ) then 1 else 0 end
         ) "HCN COUNT"
FROM TABLE1
GROUP BY TABLE1."Province",TABLE1."Region";

Similar Messages

  • Count(*) with group by max(date)

    SQL> select xdesc,xcust,xdate from coba1 order by xdesc,xcust,xdate;
    XDESC XCUST XDATE
    RUB-A 11026 01-JAN-06
    RUB-A 11026 05-JAN-06
    RUB-A 11026 08-JAN-06
    RUB-A 11027 10-JAN-06
    RUB-B 11026 02-JAN-06
    RUB-B 11026 08-JAN-06
    RUB-B 11026 09-JAN-06
    RUB-C 11027 08-JAN-06
    I want to make sql that result :
    XDESC     COUNT(*)
    RUB-A     2
    RUB-B 1
    RUB-C 1
    Criteria : GROUPING: XDESC XCUST AND MAX(DATE)
    bellow mark *** that was selected in count.
    XDESC XCUST XDATE
    RUB-A 11026 01-JAN-06
    RUB-A 11026 05-JAN-06
    RUB-A 11026 08-JAN-06 ***
    RUB-A 11027 10-JAN-06 ***
    ---------------------------------------------------------COUNT RUB-A = 2
    RUB-B 11026 02-JAN-06
    RUB-B 11026 08-JAN-06
    RUB-B 11026 09-JAN-06 ***
    ---------------------------------------------------------COUNT RUB-B = 1
    RUB-C 11027 08-JAN-06 ***
    --------------------------------------------------------COUNT RUB-C = 1
    Can Anybody help ?
    I tried :
    select xdesc,max(xdate),count(max(xdate)) from coba1 group by xdesc
    ERROR at line 1:
    ORA-00937: not a single-group group function
    Thank

    This one is duplicate. see the following link
    Count(*) with group by max(date)
    Thanks

  • Aggregate fuction with group by clause

    Hello,
    Following is assignment is given but i dont get correct output 
    so please i am request to all of us write code to solve my problem.
    There can be multiple records for one customer in VBAK tables with different combinations.
    Considering that we do not need details of each sales order,
    use Aggregate functions with GROUP BY clause in SELECT to read the fields.
    <garbled code removed>
    Moderator Message: Please paste the relevant portions of the code
    Edited by: Suhas Saha on Nov 18, 2011 1:48 PM

    So if you need not want all the repeated records, then you select all the values to an Internal table,
    and declare an internal table of same type and Usee COLLECT
    for ex:
    itab1 type  <xxxx>.
    wa_itba like line of itab1.
    itab2 type  <xxxx>. "<-This should be same type of above.
    select * from ..... into table itab1.
    and now...
    loop at itab1 into wa_itab.
    collect wa_itab1 into itab2.
    endloop.
    then you will get your desired result..

  • Incorrect warning when parsing query with group by clause

    I am using SQL Developer 4.0.0.13.80 with JDK 1.7.0_51 x64 on Windows 8.1
    I have the following SQL, which works perfectly:
    select substr(to_char(tot_amount), 0, 1) as digit, count(*)
    from transactions
    where tot_amount <> 0
    group by substr(to_char(tot_amount), 0, 1)
    order by digit;
    However, SQL Developer is yellow-underlining the first line, telling me that:
    SELECT list is inconsistent with GROUP BY; amend GROUP BY clause to: substr(to_char(rep_tot_amount), 0, 1), substr(to_char(rep_tot_amount),
    which is clearly wrong.
    Message was edited by: JamHan
    Added code formatting.

    Hello,
    I also have found the same issue with the GROUP BY hint. Another problem I found is that the hint suggests to amend the GROUP BY members to add also constant values that are included in the SELECTed columns and whenever those constants include strings with spaces, it generates an invalid query. Normally, constant values won't affect grouping functions and as such they can be omitted from the GROUP BY members, but it seems SQL Dev thinks it differently.
    For example, if you try the following query:
    SELECT d.DNAME, sum(e.sal) amt, 'Total salary' report_title, 100 report_nr
    FROM scott.emp e, scott.dept d
    WHERE e.DEPTNO = d.DEPTNO
    GROUP BY d.DNAME;
    when you hover the mouse pointer on the yellow hint, a popup will show the following message:
    SELECT list inconsistent with GROUP BY; amend GROUP BY clause to:
    d.DNAME, 'Total, 100
    If you click on the hint, it will amend the group by members to become:
    GROUP BY d.DNAME, 'Total, 100;
    that is clearly incorrect syntax. You may notice that after the change the yellow hint is still there, suggesting to amend further the GROUP BY members. If you click again on the hint, you will end with the following:
    GROUP BY d.DNAME, 'Total, 100;
    , 'Total, 100
    and so on.
    I am not sure if this behaviour was already known (Vadim??), but it would be nice if somebody could file a bug against it.
    Finally when writing big queries with complex functions and constant columns, those yellow lines extend all over the select list and they are visually annoying, so I wonder if there is a way to disable the GROUP BY hint until it gets fixed.
    Thanks for any suggestion,
    Paolo

  • Custom IKM Knowledge Modules are not working with Group By Clause

    Hi All,
       I am facing an issue with custom IKM knowledge modules. Those are IKM Sql Incremental Update and IKM Sql Control Append.
    My Scenario is
    1. Created an interface with table on source and temporary datastore with some columns in target.
    2. In the Interface on the target i defined one column as UD1 and other column as UD2  for which group by to be implemented .
    3. Customized  IKM Sql Incremental Update  with " Group by " by making  modification in my IKM Sql Incremental Update
    detail step "Insert flow into I$ table"  i.e., i replaced like this whereever i find this <%=odiRef.getGrpBy()%> API
    Group By
    <%=snpRef.getColList("","[EXPRESSION]","","","UD1")%>,
    <%=snpRef.getColList("","[EXPRESSION]","","","UD2")%>
    Up to UD5.
    . Here in the place of [EXPRESSION] i passed column names for UD1 similarly other column name for [EXPRESSION] of UD2.
    4.Made all the proper mappings and also selected the KM's needed for that interface like CKM Sql ,IKM Sql Incremental Update.
    5. Executed the Interface with global context.
    Error i am getting in this case is :
    Caused By: java.sql.SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression
      at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:462)
      at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:405)
      at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:931)
      at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:481)
      at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:205)
      at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:548)
      at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:217)
      at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1115)
    Here the columns in the select clause are there in Group By also.
    I did the same scenario using IKM Sql to file append .In that case i am able to do it. But not with the above mentioned KM's. Please let me know if any one know it or tried with this, as it is
    high priority for me.
    Please help me out.
    Thanks,
    keerthi

    Hi Keerthi,
    If your are transfering data from Oracle to Oracle (I means source is oracle Db and target is also oracle DB) then use below KM's and group by will come automatically based on the key values you selected on interface target datastore
    1) CKM Oracle
    2) LKM Oracle to Oracle (DBLINK)
    3) IKM Oracle Incremental Update (MERGE)
    Hope this will helps to resolve your issue
    Regards,
    Phanikanth

  • Default Sorting behaviour of Oracle 9i in 11g along with group by clause

    Hi,
    We have recently migrated from 9i to 11g. The reports from application comes in a jumbled fashion. Later we understood when there is a group by clause in the query, the recordset will be sorted by default in 9i and this feature is not available in 11g. Do anyone faced the same issue and resolved at the DB level.
    Only alternate we found is the change in code with addittional order by clause which will take a long time to complete and roll out the same.
    If anyone has got any immediate solution, please let me know.
    Thx in advance.
    Sheen

    Hi,
    A group by can sort (depending on the method of grouping) but it isn't necessary. If you want to sort the output you need the ORDER BY clause. There are different group by mechanismes between 9i and 11g. 10g introduced HASH GROUP BY where in 9i only the SORT GROUP BY existed. The latter gives a sorted set, the first not.
    if you want the same behaviour you can use "_gby_hash_aggregation_enabled parameter" = false, which disables the hash group by.
    Have also a look at the support document "'Group By' Does Not Guarantee a Sort Without Order By Clause In 10g and Above [ID 345048.1]".
    Herald ten Dam
    http://htendam.wordpress.com

  • Reg - Search Form for a VO with group by clause

    Hi,
    I have a Bar graph that displays data based on the Query below.
    SELECT STATUS.STATUS_ID AS STATUSID,STATUS.STATUS,COUNT(SR.SERVICEREQUEST_ID) AS SRCOUNT
    FROM SERVICE_REQUEST SR ,SERVICEREQUESTSTATUS STATUS
    WHERE SR.STATUS_ID = STATUS.STATUS_ID
    GROUP BY STATUS.STATUS_ID,STATUS.STATUS,SR.STATUS_ID
    It displays the count of SRs against a particular status.
    Now I need to add a search form to this graph with customer and date range.
    So we need to add the line below to the where clause.
    "SR.CUSTOMER_ID = :customerId AND SR.REQUESTED_ON BETWEEN :fromDate and :toDate"
    But the columns SR.CUSTOMER_ID, SR.REQUESTED_ON also need to be added to the select clause to create the View criteria for search panel.
    The two columns should also need to be added to the group by clause if we are to add them in the select clause.
    This would not produce the expected results.
    How do I create a search form with the criterias applied only at the where clause.Please help.
    With Regards,
    Guna

    The [url http://docs.oracle.com/cd/E16162_01/apirefs.1112/e17483/oracle/jbo/server/ViewObjectImpl.html]ViewObjectImpl has methods for doing this programmatically (setQuery, defineNamedWhereClauseParam, setNamedWhereClauseParam) that you can use to manipulate the query, the bind variables expected, and the values for the binds.
    John

  • Analytic Functions with GROUP-BY Clause?

    I'm just getting acquainted with analytical functions. I like them. I'm having a problem, though. I want to sum up the results, but either I'm running into a limitation or I'm writing the SQL wrong. Any hints for me?
    Hypothetical Table SALES consisting of a DAY_ID, PRODUCT_ID, PURCHASER_ID, PURCHASE_PRICE lists all the
    Hypothetical Business Question: Product prices can fluctuate over the course of a day. I want to know how much per day I would have made had I sold one each of all my products at their max price for that day. Silly question, I know, but it's the best I could come up with to show the problem.
    INSERT INTO SALES VALUES(1,1,1,1.0);
    INSERT INTO SALES VALUES(1,1,1,2.0);
    INSERT INTO SALES VALUES(1,2,1,3.0);
    INSERT INTO SALES VALUES(1,2,1,4.0);
    INSERT INTO SALES VALUES(2,1,1,5.0);
    INSERT INTO SALES VALUES(2,1,1,6.0);
    INSERT INTO SALES VALUES(2,2,1,7.0);
    INSERT INTO SALES VALUES(2,2,1,8.0);
    COMMIT;
    Day 1: Iif I had sold one product 1 at $2 and one product 2 at $4, I would have made 6$.
    Day 2: Iif I had sold one product 1 at $6 and one product 2 at $8, I would have made 14$.
    The desired result set is:
    DAY_ID                 MY_MEASURE
    1                        6
    1                       14The following SQL gets me tantalizingly close:
    SELECT DAY_ID,
      MAX(PURCHASE_PRICE)
      KEEP(DENSE_RANK FIRST ORDER BY PURCHASE_PRICE DESC)
      OVER(PARTITION BY DAY_ID, PRODUCT_ID) AS MY_MEASURE
      FROM SALES
    ORDER BY DAY_ID
    DAY_ID                 MY_MEASURE
    1                      2
    1                      2
    1                      4
    1                      4
    2                      6
    2                      6
    2                      8
    2                      8But as you can see, my result set is "longer" than I wanted it to be. I want a single row per DAY_ID. I understand what the analytical functions are doing here, and I acknowledge that I am "not doing it right." I just can't seem to figure out how to make it work.
    Trying to do a sum() of max() simply does not work, nor does any semblance of a group-by clause that I can come up with. Unfortunately, as soon as I add the windowing function, I am no longer allowed to use group-by expressions (I think).
    I am using a reporting tool, so unfortunately using things like inline views are not an option. I need to be able to define "MY_MEASURE" as something the query tool can apply the SUM() function to in its generated SQL.
    (Note: The actual problem is slightly less easy to conceptualize, but solving this conundrum will take me much closer to solving the other.)
    I humbly solicit your collective wisdom, oh forum.

    Thanks, SY. I went that way originally too. Unfortunately that's no different from what I could get without the RANK function.
    SELECT  DAY_ID,
            PRODUCT_ID,
            MAX(PURCHASE_PRICE) MAX_PRICE
      FROM  SALES
      GROUP BY DAY_ID,
               PRODUCT_ID
      ORDER BY DAY_ID,
               PRODUCT_ID
    DAY_ID                 PRODUCT_ID             MAX_PRICE             
    1                      1                      2                     
    1                      2                      4                     
    2                      1                      6                     
    2                      2                      8

  • Strange behavior in inner query with group by clause

    Hi All,
    I found a very strange behaviour with Inner query having a group by clause.
    Please look the sample code
    Select b,c,qty from (select a,b,c,sum(d) qty from tab_xyz group by b,c);
    This query gives output by summing b,c wise. But when i run the inner query it gives error as "not a group by expression "
    My question is - though the inner query is wrong, how is it possible that this query gives output.
    it behaves like -
    Select b,c,qty from (select b,c,sum(d) qty from tab_xyz group by b,c);
    Is it a normal behaviour ?
    If it is a normal behaviour, then how group by behaves inside a inner query.
    Thanks !!

    This case I have tested already and it throws error.
    But why the initial posted query is not throwing
    error even the inner query is wrong.
    In what way oracle behaves for the initial posted
    query?what is the scenario that throws the error? is it at the time when you only run the inner query or the whole query?

  • Group by clause issue

    Hi all,
    i am having some issue in grouping columns i have some specific criteria depending on that i need to group columns.
    i have table as transaction_table in that i have
    Transaction item prom_id
    296     A     null sale     -- prom --Source item
    296     B     1 sale     -- prom --Target item
    296 A null
    298     A null
    promotin
    prom_id id_ru_prd     
    1 123
    2 345
    source_base
    id_ru_prdv item
    123 A
    345 B
    in above table for transaction 296 i have item A twice but out of them one item is sold in promotion with target item B and other is non promotional item A.
    i have prom_id which tells me which target item is sold in promotion but to find out source item (in this example item A) i have column in other base table(promotion table) which tells me associated source item using prom_id
    i need to calculate sum of quantity depending weather item was sold in promotion or not
    result should be
    item quantity promotion / non promotional
    A 1 promotion
    B 1 promotion
    A 2 non promotional
    in group by it doesn't allow sub query , can anyone help me on this pls?

    I'm having a hard time understanding what you are trying to do.
    Can you write what you want in SQL, and then we'll see if we can get the same result in XQuery?
    I think what you want is the union of :
    select trans, count(*) from transaction_table group by trans where prom is null
    select trans, count(*) from transaction_table group by trans where prom is not null
    Edited by: mikereiche on Jul 7, 2011 4:11 PM

  • Count(*) with group by and a join

    I've the below tables:
    Tables:
    EMPLOYEE
    EMP_ID         LNAME             FNAME
    1000                 SLITT              JOHN
    1001                 SLITHER              STEVE
    1002                 JACOB              MARLYN
    1003                 STUFFEY     NOSE
    1004                 SLIPPY              MOUTH-----
    ACCESS_TYPE
    ACCESS_TYPE_ID     ACCESS_DESCRIPTION
    1                         EMPLOYEE
    2                         EMPLOYEE_ADMIN
    3                         CONTRACTOR-----
    EMPLOYEE_ACCESS
    ACCESS_ID      ACCESS_TYPE_ID     EMP_ID     ACCESS_EFF_DATE     
    1               1               1000              01-JAN-13
    2               1               1001              10-FEB-13
    3               1               1002              18-FEB-13
    4               2               1003              10-OCT-12
    5               3               1004              10-MAR-08-----
    I'm trying to figure out the count of employees group by their type and whose last name does not start with SL
    I've written the query below and I'm getting a 00936 Missing Expression. What am I doing wrong? Thanks
    SELECT EA.COUNT(*) , ACCESS_TYPE_ID
    FROM EMPLOYEE_ACCESS EA, EMPLOYEE E
    WHERE ACCESS_EFF_DATE >= '31-DEC-12' AND E.LNAME NOT LIKE 'SL%'
    GROUP BY ACCESS_TYPE_ID;
    Edited by: parsar on Mar 25, 2013 3:54 PM

    thanks for the response.
    If I don't use the alias prefix, which table rows will the query count because I've two tables in the from clause.
    I've modified the query as below but it doesn't return any data:
    SELECT COUNT(*) AS ACCESS_COUNT, ACCESS_TYPE_ID
    FROM EMPLOYEE_ACCESS EA , EMPLOYEE E
    WHERE EA.EMP_ID=E.EMP_ID AND
    ACCESS_EFF_DATE >= TO_DATE('2012-12-31', 'YYYY-MM-DD') AND
    E.LNAME NOT LIKE 'SL%'
    GROUP BY ACCESS_TYPE_ID;Please let me know which part of the query I screwed up.

  • Use of SUM and COUNT with GROUP BY

    All,
    I have a set like below:
    State DOB StartDt EndDt Desc Category MemberID SubID Code
    NC 2003-01-30 2014-01-01 2014-01-31 Child B 123456 2 38
    NC 2003-01-30 2014-01-01 2014-01-31 Child B 123456 2 39
    NC 2003-01-30 2014-02-01 2014-05-31 Child B 123456 2 38
    NC 2003-01-30 2014-02-01 2014-05-31 Child B 123456 2 39
    NC 2003-01-30 2014-06-01 2014-07-31 Child B 123456 2 38
    NC 2003-01-30 2014-06-01 2014-07-31 Child B 123456 2 39
    NC 2014-01-17 2014-01-01 2014-07-31 Infant S 456789 1 49
    NC 2014-02-04 2014-02-01 2014-07-31 Infant S 246376 3 49
    -- MemberID and SubID identify 1 member
    -- Member 123456 has 2 distinct "Code" i.e. 38,39 but each code has different "StartDt" and "EndDt"
    -- Expected Result
    State Desc Category CountOfDistinctCode TotalMonthsEnrolled
    NC Child B 2 (38 and 39 for same member) 7 months (1 + 4 + 2) (Difference between StartDt and EndDt:1 (1/31/2014 - 1/1/2014) + 4 (5/31/2014 - 2/1/2014).. )
    NC Infant S 2 (Same code 49 but different member) 13 months (7+6) (7/31/2014 - 1/1/2014 = 7 months + 7/31/2014 - 2/1/2014 = 6 months)
    I tried doing a count of distinct Code and the summing up the member months, grouped by State, Desc, Category, but I am not able to get 2 and 7 for child, it somehow calculates Months as 14 (7+7). Please let me know what you guys suggest.

    OK, so we need a different approach. We need a table of numbers, a concept that I discuss here:
    http://www.sommarskog.se/arrays-in-sql-2005.html#numbersasconcept
    (Only read down to the next header.)
    We join the numbers to temp table with BETWEEN over the numbers. Then we count the distinct combination of member ID and number.
    We also need to make an adjustment how to build the string for the DISTINCT. I initially assumed that your columns were integer, why I used str() which produces a right-adjusted fixed-length string of 10 characters. (The default, you can specify a different
    width as the second parameter.) But str() expects a float value as input, and will fail if for instance member id would be non-numeric. We cannot just concatenate the strings, since in that case (MemberID, SubID) = ('12345', '61') would be
    the same as ('123456', '1'). So we must convert to char to get fixed length. All that said, and some more test data added, here is a solution:
    CREATE TABLE #Test
    [State] CHAR(2),
    DOB DATE,
    StartDt DATE,
    EndDt DATE,
    [Desc] VARCHAR(8),
    Category CHAR(1),
    MemberID VARCHAR(10),
    SubID VARCHAR(2),
    Code VARCHAR(5)
    INSERT INTO #Test
    VALUES
    ('NC', '20130130', '20140101', '20140120', 'Child', 'B', '123456', '2', '38'),
    ('NC', '20130130', '20140121', '20140131', 'Child', 'B', '123456', '2', '39'),
    ('NC', '20130130', '20140201', '20140531', 'Child', 'B', '123456', '2', '38'),
    ('NC', '20130130', '20140401', '20140613', 'Child', 'B', '123456', '2', '39'),
    ('NC', '20130130', '20140601', '20140731', 'Child', 'B', '123456', '2', '38'),
    ('NC', '20130130', '20140614', '20140731', 'Child', 'B', '123456', '2', '39'),
    ('NC', '20130129', '20140601', '20140731', 'Child', 'B', '9123456', '1', '38'),
    ('NC', '20130129', '20140614', '20140831', 'Child', 'B', '9123456', '1', '39'),
    ('NC', '20140117', '20140101', '20140731', 'Infant', 'S', '456789', '1', '49'),
    ('NC', '20140204', '20140201', '20140731', 'Infant', 'S', '246376', '3', '49')
    SELECT * FROM #Test ORDER BY MemberID, StartDt, EndDt
    SELECT
    [State]
    , [Desc]
    , Category
    , COUNT(DISTINCT convert(char(10), MemberID) +
    convert(char(2), SubID) +
    convert(char(5), Code)) AS TotalCnt
    , COUNT(DISTINCT convert(char(10), MemberID) +
    convert(char(2), SubID) +
    str(N.Number)) AS CalMonths
    FROM #Test t
    JOIN Numbers N ON N.Number BETWEEN
    datediff(MONTH, '19000101', t.StartDt) AND
    datediff(MONTH, '19000101', t.EndDt)
    GROUP BY [State], [Desc], Category
    go
    DROP TABLE #Test
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Max, Min and Count with Group By

    Hello,
    i want the max, min and count of a table, which is grouped by a column
    I need a combination of these two selects:
    select
         max(COUNTRY_S) MAXVALUE,
         min(COUNTRY_S) MINVALUE
    from
         tab_Country
    select
         count(*)
    from
         (select COUNTRY_TXT from tab_Country group by COUNTRY_TXT) ;
    The result should be one row with the max and min value of the table and with the count of the grouped by table, not the max and min of each group! - i hope you understand my question?
    Is this possible in one SQL-select?
    Thank you very much
    Best regards
    Heidi

    Hi, Heidi,
    HeidiWeber wrote:
    Hello,
    i want the max, min and count of a table, which is grouped by a column
    I need a combination of these two selects:
    select 
         max(COUNTRY_S) MAXVALUE, 
         min(COUNTRY_S) MINVALUE 
    from 
         tab_Country 
    select 
         count(*) 
    from 
         (select COUNTRY_TXT from tab_Country group by COUNTRY_TXT) ; 
    The result should be one row with the max and min value of the table and with the count of the grouped by table, not the max and min of each group! - i hope you understand my question?
    Is this possible in one SQL-select?
    Thank you very much
    Best regards
    Heidi
    It's not clear what you want.  Perhaps
    SELECT  MAX (country_s)               AS max_country_s
    ,       MIN (country_s)               AS min_country_s
    ,       COUNT (DISTINCT country_txt)  AS count_country_txt
    FROM    tab_country
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the 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

  • Problem with group by clause

    Hi,
    this is my query
    SELECT inccodrid, inccau, count(*) AS record, sum(incprez) AS prezzo, sum(incpren) AS preno, sum(incprev) AS preve
    FROM incassi
    WHERE incuten='125' And incdemi Between #12/4/2002# And #12/4/2002#
    GROUP BY inccodrid, inccau
    ORDER BY inccau
    when i execute this query with jdbc the runtime give me an error "stato del cursore non valido", but if i try to execute in msaccess the query work fine.... Anyone can help me?
    Problem n 2
    SELECT produzione.proid, inccodrid, inccau, count(*) AS record, sum(incprez) AS prezzo, sum(incpren) AS preno, sum(incprev) AS preve
    FROM (produzione INNER JOIN titoli ON produzione.proid=titoli.titpro) INNER JOIN incassi ON titoli.titid=incassi.incevid
    WHERE produzione.proid=dato And incdemi Between data1 And data2
    GROUP BY produzione.proid, inccodrid, inccau
    ORDER BY produzione.proid, inccau;
    when i execute this query with jdbc he give me one record and no-error, but if i try to execute in msaccess the query work fine and give me 3 record???!!!
    I know that access is not the greatest dbengine in the world, but i need to use it...
    thanks
    Baro

    Can you please post part of your Java Code where you do your database things. maybe i can help you then.

  • Query with group by clause

    Hi all,
    I am struggling with one query.So Just i am giving the example..
    CREATE TABLE test(num NUMBER,val NUMBER,description VARCHAR2(100))
    table having the following data
    num val description
    1 100 first
    1 200 second
    1 300 third
    Now i want to take the sum and discription based on num field..
    i should get like ==> 1 600 first
    my query is...
    SELECT SUM(val),description
    FROM test
    GROUP BY description
    But it's giving all records.because description field is there in select statement..
    I tried two cursors for this case..its o.k
    But Is there any way instead of using two cursors.
    Thanks,

    Not sure what you are trying to achieve here. For the same value of NUM (say 1), you have 3 different values of VAL & DESCRIPTION. Do you want to sum up the VAL values for a particular value of NUM? In that case, what will be the corresponding DESCRIPTION?
    How did you arrive at the result --- 1 600 first
    -- whereas the DESCRIPTION is different for the 3 VAL values?

Maybe you are looking for