GROUP OF + SQL COUNT + SUM

Hello

I have a table, it has 2 colunms, (name, number)

name number
B1 7
B1 7
B1 28
B1 28
B1 28
B2 7
B2 28
B3 7
. .
. .
.
.


I want to see below

number
name sum 7sum 28sum

5 2 3 B1
2 1 1 B2
B3...
.
.
.
.
.


Can you help me

THANK YOU VERY MUCH FOR HELP

Like this?

SQL> ed
Wrote file afiedt.buf

  1  with t as (select 'B1' as name, 7 as num from dual union all
  2             select 'B1', 7 from dual union all
  3             select 'B1', 28 from dual union all
  4             select 'B1', 28 from dual union all
  5             select 'B1', 28 from dual union all
  6             select 'B2', 7 from dual union all
  7             select 'B2', 28 from dual union all
  8             select 'B3', 7 from dual)
  9  --
 10  -- end of test data - use query below
 11  --
 12  select name
 13        ,count(*)
 14        ,sum(decode(num,7,1,0)) as sum7
 15        ,sum(decode(num,28,1,0)) as sum28
 16  from t
 17  group by name
 18* order by 1
SQL> /

NA   COUNT(*)       SUM7      SUM28
-- ---------- ---------- ----------
B1          5          2          3
B2          2          1          1
B3          1          1          0

Tags: Database

Similar Questions

  • SQL count (*) with Group of

    Hello

    I need help to fix this SQL.

    Fields in the table are like that.
    ID(PK)
    user_id
    user_name
    login_time(timestamp)
    Basically I want the values of user_id, user_name, last time the logged-in user and total number of times the user
    select  distinct user_id , user_name, login_date from USER_LOGIN
    where login_date in (select max(login_date) from USER_LOGIN group by user_id) 
    the above query is to give the result set with user_id, user_name, last time that the user logged on, but how can I include count (*) Group of user_id in the sql above
    select  distinct user_id , user_name, login_date, count(*) from USER_LOGIN
    where login_date in (select max(login_date) from USER_LOGIN group by user_id)  group by user_id 
    the sql above does not work.

    can you help me to get the number of records by user_id group.

    Thank you
    SK

    Hello

    Looks like you want something like this:

    SELECT       user_id
    ,       user_name
    ,       MAX (login_time)     AS last_login_time
    ,       COUNT (*)          AS total_rows
    FROM       user_login
    GROUP BY  user_id
    ,            user_name
    ;
    

    This assumes that user_login is off standard (like him are often seen), such that every row of the same user_id will also have the same user_name.

    I hope that this answers your question.
    If not, post a small example of data (CREATE TABLE and INSERT statements) and the results desired from these data.

  • Group by and count on the same table

    Hello

    DB version: 11.2.0.3

    Tried to SQL:

    create table log1 (identification number, status varchar2 (15) date of insert1);

    insert into log1 values (1, 'SUCCESS', sysdate-1);

    insert into log1 values (2, 'SUCCESS', sysdate-2);

    insert into log1 values (3, 'FAIL', sysdate-1);

    insert into log1 values (4, 'FAIL', sysdate-2);

    insert into log1 values (5, 'FAIL', sysdate-1);

    insert into log1 values (6, 'ERROR', sysdate-1);

    insert into log1 values (7, 'ERROR', sysdate-1);

    insert into log1 values (8, 'ERROR', sysdate-1);

    insert into log1 values (9, 'ERROR', sysdate-1);

    WITH AS TBL1

    (SELECT THE STATE OF 'SUCCESS' OF THE DUAL UNION ALL

    SELECT "FAIL" STATUS OF THE DUAL UNION ALL

    SELECT 'PENDING' STATUS OF DOUBLE)

    SELECT NVL (TBL1. STATUS OF STATE, 'TOTAL'), COUNT (LOG1.ID)

    FROM TBL1 JOIN LOG1 FULL EXTERNAL

    ON LOG1. STATUS = TBL1. STATUS

    GROUP BY ROLLUP (TBL1. STATUS)

    STATUSCOUNT (LOG1.ID)
    IN CASE OF FAILURE3
    SUCCESS2
    PENDING0
    TOTAL (*)4
    TOTAL9

    t need only State of SUCCESS, FAILURE, WAITING. and NUMBER of global ID omit the rest of type status as ERROR, etc. EXECUTION in the result set.

    I don't need the TOTAL (*) of the above result set. Which is total (Error).

    Please tell us how the query above can be changed to produce the result to wait.

    Thank you

    Hello

    So, you want to see all the tbl1 lines, if they have a match in log1, and for the grand total of lines, you must count the log1 lines if they have a match with tbl1.  This looks like a job for a FULL OUTER JOIN:

    SELECT t.status

    EARL of (l.status) AS cnt

    OF log1 l

    FULL OUTER JOIN tbl1 t WE t.status = l.status

    GROUP OF ROLLUP (t.status)

    HAVING t.status IS NOT NULL

    OR of a GROUP (t.status) = 1

    ;

  • SQL Count query - Assistance needed

    Hello

    Hoping someone can point me in the right direction. Here's the query I have so far. What I'm trying to do is to compare and add the names of the skills of individuals. My current query will count them, however, I need to take a step further where if the person has a name of "Spanish" skills can only which is counted and not their names other skills.

    Current query:

    WITH AS TBL1

    (SELECT

    TRIM (A.NWIE_ID) AS NWIE_ID,

    A.AGENT_NM,

    B.BUSINESS

    Of

    LIGHTHOUSE. IEX_AGENTS A JOIN

    LIGHTHOUSE. TBL_GEN_MU_BUSINESS B

    ON A.MU_ID = B.MU_ID

    WHERE B.BUSINESS IN ('PL_LEGACY'));

    TBL2 ACE

    (SELECT

    TRIM (C.USER_ID) AS USER_ID,

    TRUNC (ASOF_DT, 'IW') AS WEEKBEGINNING,

    CASE

    WHERE (SKILL_NM = 'PLSCLegacy' AND SKILL_LEVEL < = 2) THEN "basic".

    WHERE (SKILL_NM = 'PLSCLegacy' AND SKILL_LEVEL < = 7) THEN "moderate."

    WHERE (SKILL_NM = 'PLSCLegacy' AND SKILL_LEVEL < = 10) THEN 'complex '.

    WHERE (SKILL_NM = "PLSCLegacySpanish") THEN 'Spanish '.

    Another null

    END AS SKILL_NM

    THE LIGHTHOUSE. C CFG_PERSON_SKILL_HIST

    )

    SELECT

    WEEKBEGINNING,

    SKILL_NM,

    COUNT (DISTINCT NWIE_ID) TOTAL_SKILLED

    Of

    TBL1,

    TBL2

    WHERE TBL1. NWIE_ID = TBL2. USER_ID

    AND SKILL_NM NOT IN ('NULL %')

    SKILL_NM GROUP, WEEKBEGINNING

    ORDER OF WEEKBEGINNING, SKILL_NM

    Create the Table data:

    CREATE TABLE IEX_AGENTS

    (

    AGENT_NM VARCHAR2 (20).

    NWIE_ID CHAR (8),

    NUMBER OF MU_ID (5)

    )

    CREATE TABLE TBL_GEN_MU_BUSINESS

    (

    BUSINESS VARCHAR2 (100),

    NUMBER OF MU_ID (5)

    )

    CREATE TABLE CFG_PERSON_SKILL_HIST

    (

    USER_ID VARCHAR2 (8).

    DATE OF ASOF_DT,

    SKILL_NM VARCHAR2 (64).

    NUMBER OF SKILL_LEVEL (10)

    )

    INSERT INTO IEX_AGENTS (AGENT_NM, NWIE_ID, MU_ID) VALUES ("Marcelle, Athalie", "MARCELA1", 5)

    INSERT INTO IEX_AGENTS (AGENT_NM, NWIE_ID, MU_ID) VALUES ('Lister, Laura", 'LISTERL3', 6)

    INSERT INTO IEX_AGENTS (AGENT_NM, NWIE_ID, MU_ID) VALUES ("Purvis, Bradley", 'PURVIB1', 4)

    INSERT INTO IEX_AGENTS (AGENT_NM, NWIE_ID, MU_ID) VALUES ("Cannon, Mark", "CANNON1", 4)

    INSERT INTO TBL_GEN_MU_BUSINESS (BUSINESS, MU_ID) VALUES ('PL_LEGACY', 5)

    INSERT INTO TBL_GEN_MU_BUSINESS (BUSINESS, MU_ID) VALUES ('PL_LEGACY', 6)

    INSERT INTO TBL_GEN_MU_BUSINESS (BUSINESS, MU_ID) VALUES ('PL_LEGACY', 4)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('PURVIB1', 3/9/2014 12:00 ',' PLSCLegacy', 1).

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('MARCELIA1', 3/9/2014 12:00 ',' PLSCLegacy', 10)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('LISTERL3', 3/9/2014 12:00 ',' PLSCLegacy', 7)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('CANNON1', 3/9/2014 12:00 ',' PLSCLegacy', 7)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('MARCELIA1', 3/9/2014 12:00 ',' PLSCLegacySpanish', 3)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('LISTERL3', 3/9/2014 12:00 ',' PLSCLegacySpanish', 6)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('PURVIB1', 3/9/2014 12:00 ',' PLSCLegacySpanish', 2)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('PURVIB1', 3/19/2014 12:00 ',' PLSCLegacy', 10)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('LISTERL3', 3/19/2014 12:00 ',' PLSCLegacy', 2)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('MARCELIA1', 3/9/2014 12:00 ',' PLSCLegacy', 10)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('CANNON1', 3/19/2014 12:00 ',' PLSCLegacy', 7)

    INSERT INTO CFG_PERSON_SKILL_HIST (USER_ID, ASOF_DT, SKILL_NM, SKILL_LEVEL) VALUES ('CANNON1', 3/19/2014 12:00 ',' PLSCLegacySpanish', 7)

    Results of the current query:

    WEEKBEGINNINGSKILL_NMTOTAL_SKILLED
    07/03/2014 12:00:00 AMFoundational1
    07/03/2014 12:00:00 AMComplex1
    07/03/2014 12:00:00 AMModerate2
    07/03/2014 12:00:00 AMSpanish3
    14/03/2014 12:00:00 AMFoundational1
    14/03/2014 12:00:00 AMComplex2
    14/03/2014 12:00:00 AMModerate1
    14/03/2014 12:00:00 AMSpanish1

    Desired results:

    WEEKBEGINNINGSKILL_NMTOTAL_SKILLED
    07/03/2014 12:00:00 AMFoundational0
    07/03/2014 12:00:00 AMComplex0
    07/03/2014 12:00:00 AMModerate1
    07/03/2014 12:00:00 AMSpanish3
    14/03/2014 12:00:00 AMFoundational1
    14/03/2014 12:00:00 AMComplex2
    14/03/2014 12:00:00 AMModerate0
    14/03/2014 12:00:00 AMSpanish1

    WITH ACE TBL1)

    SELECT TRIM (A.NWIE_ID) AS NWIE_ID,

    A.AGENT_NM,

    B.BUSINESS

    OF IEX_AGENTS HAS

    JOIN THE

    TBL_GEN_MU_BUSINESS B

    ON A.MU_ID = B.MU_ID

    WHERE B.BUSINESS IN ("PL_LEGACY")

    ),

    (ACE TBL2)

    SELECT USER_ID, TRIM (C.USER_ID)

    TRUNC (ASOF_DT, 'IW') AS WEEKBEGINNING,

    CASE

    WHERE (SKILL_NM = 'PLSCLegacy' AND SKILL_LEVEL<= 2)="" then="">

    WHERE (SKILL_NM = 'PLSCLegacy' AND SKILL_LEVEL<= 7)="" then ="">

    WHERE (SKILL_NM = 'PLSCLegacy' AND SKILL_LEVEL<= 10)="" then ="">

    WHERE (SKILL_NM = "PLSCLegacySpanish") THEN 'Spanish '.

    END AS SKILL_NM

    OF CFG_PERSON_SKILL_HIST C

    ),

    (AS TBL3)

    SELECT USER_ID,

    WEEKBEGINNING,

    SKILL_NM,

    COUNT)

    SKILL_NM SEPARATE CASE

    WHEN 'Spanish', 1

    END

    ) HAS_SPANISH OVER (PARTITION BY USER_ID, WEEKBEGINNING)

    FOR TBL2

    )

    SELECT WEEKBEGINNING,

    SKILL_NM,

    COUNT)

    SEPARATE CASE

    WHEN HAS_SPANISH = 0 THEN NWIE_ID

    WHEN SKILL_NM = "Spanish" THEN NWIE_ID

    END

    ) TOTAL_SKILLED

    FROM TBL1;

    TBL3

    WHERE TBL1. NWIE_ID = TBL3. USER_ID

    AND SKILL_NM NOT IN ('NULL %')

    SKILL_NM GROUP,

    WEEKBEGINNING

    ORDER OF WEEKBEGINNING,

    SKILL_NM

    /

    WEEKBEGINNING SKILL_NM TOTAL_SKILLED
    ---------------------- ------------ -------------
    03/03/2014 12:00:00 am founders 0
    03/03/2014 moderate from 12:00:00 am 0
    03/03/2014 12:00:00 am 2 Spanish
    17/03/2014 12:00:00 am complex 1
    17/03/2014 12:00:00 am basic 1
    17/03/2014 12:00:00 am Spanish 1

    6 selected lines.

    SQL >

    SY.

  • Need of a just rows in table using SQL counter bearing

    Oracle 11 g 2

    RHEL 6.4

    ---------------

    Given the following table data:

    EMPLOYEE HIRE_DATE TERM_DATE DEPT

    ------------------     -----------------     ------------------      ---------

    John Doe 01/01/13 101

    Jane Smith 05/01/13 102

    Bob Jones 102 02/04/13, 22/04/13

    Jenny Boo 03/12/13 03/31/13 103

    Joe Schmoe 24/03/13 102

    Bill Max 23/04/13 103

    Jill clay 24/04/13 103

    Joe Boom 11/05/13 102

    I want to return the number of employees who work for each month, as long as they were hired at anytime during the month and what if they ended this month (ie the month worries me).  So I would expect

    EMPLOYEE OF THE MONTH

    Jan 2

    3 Feb

    5 March

    April 6 (Jenny Boo ended)

    May 6 (Jenny Boo and Bob Jones have been completed)

    I know there are bright people out there who are SQL expert, but I'm not one of them.  If there is a way to do this in SQL just I would like to see it.

    Hello stew,.

    Thanks for your reply.  Looking at your SQL, where did you get the column "cnt"?  I don't see that in any table, physical or virtual.

    It is in the clause "unpivot" of the request he posted.

    Create the table "emp1" and the data that Martin condition and then run the query that provided stew.

    Works for me:

    Select to_char (Lun, ' FMMonth, YYYY') month,

    Sum (Sum (TO_NUMBER (CNT))) more employees (command per LUN)

    de)

    Select trunc (hire_date, 'MM') "1."

    trunc (ADD_MONTHS(term_date,1), 'MM') '-1 '.

    from emp1

    ) a

    UNPIVOT (LUN for NTC in ("1", "-1"))

    LUN group

    order by Lun;

    Header 1 Header 2

    MONTH

    EMPLOYEES January 2013 2 February 2013 3 March 2013 5 April 2013 6 May 2013 6

  • How the group using SQL for the desired output.

    Hi all

    I am currently using oracle 10.2.0.4.0

    Create a table script:
    CREATE TABLE FORTEST
    ( gpno VARCHAR2(10 BYTE),
      classnumber  VARCHAR2(10 byte),
      age_min NUMBER,
      age_max NUMBER,
      amount NUMBER)
    INSERT statement:
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 01,0,29,1) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 01,30,35,2) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 01,36,40,3) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 02,0,29,1) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 02,30,35,2) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 02,36,40,5) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 03,0,29,1) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 03,30,35,2) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G123' , 03,36,40,3) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G124' , 01,0,29,1) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G124' , 01,30,35,2) 
    insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
    ('G124' , 01,36,40,3) 
    power required:
    gpno    classnumber    age_min    age_max    amount
    G123    1,3                0        29        1
    G123    1,3                30       35        2
    G123    1,3                36       40        3
    G123    2                  0        29        1
    G123    2                  30       35        2
    G123    2                  36       40        5
    G124    1                  0        29        1
    G124    1                  30       35        2
    G124    1                  36       40        3
    as for gpno g123, classnumber 1 and 3, the rates are the same in all the age_min and age_max they need to be grouped.
    even if gpno 123 classnumber 2 has the same rates as the classesnumber 1 and 3 for the age groups 0 to 29 and 30 to 35,
    rates are different for ages 36 to 40. so it should not be placed together. How can I do this in SQL

    any help is appreciated.

    Thanks in advance.

    Hello

    Thorny problem!

    Unfortunately, LISTAGG was created to the Oracle 11.2. About half of the complexity here is the aggregation of chain, i.e. forming the list of the classnumbers, as '1.3', using only functions available in Oracle 10.2.

    Here's a solution:

    WITH     got_gpno_classnumber_cnt   AS
    (
         SELECT     gpno, classnumber, age_min, age_max, amount
         ,     COUNT (*) OVER ( PARTITION BY  gpno
                                      ,            classnumber
                          )   AS gpno_classnumber_cnt
         FROM    fortest
    --     WHERE     ...     -- If you need any filtering, this is where it goes
    )
    ,     pairs          AS
    (
         SELECT    a.gpno
         ,       a.classnumber
         ,       MIN (b.classnumber)
                    OVER ( PARTITION BY  a.gpno
                              ,                    a.classnumber
                      )     AS super_classnumber
         FROM       got_gpno_classnumber_cnt  a
         JOIN       got_gpno_classnumber_cnt  b  ON   a.gpno     = b.gpno
                                      AND  a.age_min     = b.age_min
                                    AND  a.age_max     = b.age_max
                                    AND  a.amount     = b.amount
                                    AND  a.gpno_classnumber_cnt
                                            = b.gpno_classnumber_cnt
         GROUP BY  a.gpno
         ,            a.classnumber
         ,       b.classnumber
         HAVING       COUNT (*)     = MIN (a.gpno_classnumber_cnt)
    )
    ,     got_rnk          AS
    (
         SELECT DISTINCT
                 gpno, classnumber, super_classnumber
         ,     DENSE_RANK () OVER ( PARTITION BY  gpno
                                   ,                    super_classnumber
                                   ORDER BY          classnumber
                           )         AS rnk
         FROM    pairs
    )
    ,     got_classnumbers     AS
    (
         SELECT     gpno, classnumber, super_classnumber
         ,      SUBSTR ( SYS_CONNECT_BY_PATH (classnumber, ',')
                       , 2
                     )     AS classnumbers
         FROM     got_rnk
         WHERE     CONNECT_BY_ISLEAF = 1
         START WITH     rnk             = 1
         CONNECT BY     rnk             = PRIOR rnk + 1
              AND     gpno             = PRIOR gpno
              AND     super_classnumber  = PRIOR super_classnumber
    )
    SELECT DISTINCT
           g.gpno
    ,       c.classnumbers
    ,       g.age_min
    ,       g.age_max
    ,       g.amount
    FROM       got_gpno_classnumber_cnt  g
    JOIN       got_classnumbers         c  ON   c.gpno        = g.gpno
                                 AND  c.classnumber  = g.classnumber
    ORDER BY  g.gpno
    ,            c.classnumbers
    ;
    

    Out (just as you requested):

    GPNO       CLASSNUMBERS       AGE_MIN    AGE_MAX     AMOUNT
    ---------- --------------- ---------- ---------- ----------
    G123       1,3                      0         29          1
    G123       1,3                     30         35          2
    G123       1,3                     36         40          3
    G123       2                        0         29          1
    G123       2                       30         35          2
    G123       2                       36         40          5
    G124       1                        0         29          1
    G124       1                       30         35          2
    G124       1                       36         40          3
    
  • When count sum ID number total &lt; 300

    Afternoon Folks,

    I was wondering if someone could help me with a query of code?

    I'm counting the number of Packs of Service where the total payment of the amount for each separate sp_id is less than 300 pounds and also more than 300 pounds.

    This is the code I have so far:

    Select the specialty
    count (distinct sp_id) "Service Packs",
    Sum (payment_total) "Total paid £»,
    round (sum (payment_total) / count (distinct sp_id), 0) 'average spend by MS.
    of a3_fact_sms_service_inv
    where specialized ("podiatry", "Podiatry")
    and service_date between '01 Jan 11' and 31 Dec 11'
    Specialty group

    This is the table that I want it to look

    SPECIALTY Service Packs Total paid £ average spend by SP * < 300 * > 300 *.
    Podiatry 129 25682,26 199 * 107 * 22 *.
    Podiatry 622 206651,08 332 * 403 * 219 *.

    I'm currently running application 10 g

    Concerning
    Farming :)

    First of all, ' 01 Jan 11' is not a date, but rather a string. Use literals date (or at least to_date):

    with t as (
               select  specialty,
                       sp_id,
                       sum(payment_total) sp_id_total
                 from  a3_fact_sms_service_inv
                 where specialty in ('Chiropody','Podiatry')
                   and service_date between date '2011-01-01' and date '2011-12-31'
                 group by specialty,
                          sp_id
              )
    select  specialty,
            count(sp_id) "Service Packs",
            sum(sp_id_total) "Total Paid £",
            round(sum(sp_id_total) / count(sp_id)) "Average Spend per SP",
            count(case when sp_id_total < 300 then 1 end) "Service Packs Less £300",
            count(case when sp_id_total = 300 then 1 end) "Service Packs Equal £300",
            count(case when sp_id_total > 300 then 1 end) "Service Packs Greater £300"
      from  t
      group by specialty
    /
    

    SY.

  • A "group"double"SQL query

    I have a table called consignment with values as follows:

    RID THE CTIME USERID
    ---------- ---------- ----------
    1-1-1
    1 1 2
    1 1 3
    1 2 1
    1-2-2
    1-3-1
    1 3 4
    1-3-6
    1 3 7
    2-1-1
    2-1-7

    RID THE CTIME USERID
    ---------- ---------- ----------
    2 1 5
    2-2-2
    2-3-1
    2 4 5
    2-4-7
    3-2-4
    3-2-6
    3-2-7
    3-2-8
    3-2-9
    3 4 1

    RID THE CTIME USERID
    ---------- ---------- ----------
    3 4 2
    3-4-3
    3-1-1


    For each DIN, I need to find what USERID check has the largest number of times. Here, for example. to rid = 1, userid = 3 check-in has the most number of times, to rid = 2, userid = 1 has the largest check in number of times and to rid = 3, userid = 2A check-in the large number of times.

    How to write such a request? Help, please. I'm new to SQL. I understand GROUP BY, but here it seems that I would need a double collection and I have no idea how do.

    Hello

    842474 wrote:
    In this case, to rid = 1, userid = 1 and userid = 3 have the same number of check-ins. NOTE that ctime is not needed for anything whatsoever.

    OK, so forget the AMOUNT and total_ctime; Use COUNT and cnt:

    WITH     got_rnk          AS
    (
         SELECT    rid
         ,       userid
         ,       COUNT (*)     AS cnt
         ,       RANK () OVER ( PARTITION BY  rid
                                       ORDER BY      COUNT (*)     DESC
                          ) AS rnk
         FROM      checkin
         GROUP BY  rid
         ,            userid
    )
    SELECT     rid
    ,     userid
    ,     cnt
    FROM     got_rnk
    WHERE     rnk     = 1
    ;
    
  • SQL Count helps

    Hello world

    I need to display the SPER_STATUS_TEXT count as 0 if there is no data from query below. Could someone help me please

    SQL > SELECT a.sper_status_text, COUNT (*)
    2 from (SELECT sper.assettxt,
    CASE 3
    4. WHEN sper.sper_status_text = "assessment" said.
    5 THEN
    6 'confirmed assessment '.
    7. WHEN sper.sper_status_text AS "update - rated %".
    8 THEN
    9 "update - rated up to."
    10. WHEN sper.sper_status_text AS "update - % evaluated.
    11. CAN
    12 'update - rated down"
    13 ELSE
    14 "other responses.
    15 END
    16 sper_status_text
    17 zzcus.zzcus_sper_data sper
    18 WHERE sper.sper_dates = ' 20100801-20100831'
    19 AND sper.customer_id = 'NATFINS. '
    20 AND sper.task_inquiry_type = "Vendor Comparison"
    21 AND sper.assettxt <>'! MAD
    (22) a
    23 GROUP BY a.sper_status_text
    ORDER 24 (CASE
    25. WHEN a.sper_status_text = "Said assessment" THEN 1
    26. WHEN a.sper_status_text = "update - evaluated until ' THEN 2
    27. WHEN a.sper_status_text = 'Update - rated down' THEN 3
    28 4 SOMETHING ELSE
    END 29);

    Results:
    SPER_STATUS_TEXT COUNT (*)
    ------------------------ ----------
    Claims assessment 2

    I need to display as below (if there no data I need to display the number as 0)
    SPER_STATUS_TEXT COUNT (*)
    ------------------------ ----------
    Claims assessment 2
    Updated - evaluated up to 0
    Updated - rated low 0
    Other answers 0

    Please advice

    You can get this. Using the TYPE clause.

    SQL>
    SQL> CREATE TABLE ZZCUS_SPER_DATA1
      2  (
      3     SPER_STATUS_TEXT VARCHAR2 (30)
      4    ,TASK_INQUIRY_TYPE VARCHAR2 (100)
      5  );
    
    Table created.
    
    SQL>
    SQL> INSERT INTO ZZCUS_SPER_DATA1
      2       VALUES (
      3                 'Data Updated', 'Descriptive Data Challenge - Maturity date / Redemption date');
    
    1 row created.
    
    SQL>
    SQL> INSERT INTO ZZCUS_SPER_DATA1
      2       VALUES ('Data Updated', 'Descriptive Data Challenge - Ticker / Local Code');
    
    1 row created.
    
    SQL>
    SQL> SELECT *
      2    FROM (  SELECT A.SPER_STATUS_TEXT, COUNT (*) CNT
      3              FROM (SELECT CASE
      4                              WHEN SPER.SPER_STATUS_TEXT = 'Data Confirmed'
      5                              THEN
      6                                 'Data Item Confirmed'
      7                              WHEN SPER.SPER_STATUS_TEXT LIKE 'Data Updated'
      8                              THEN
      9                                 'Data Item Updated'
     10                              ELSE
     11                                 'Other Responses'
     12                           END
     13                              SPER_STATUS_TEXT
     14                      FROM ZZCUS_SPER_DATA1 SPER
     15                     WHERE 1 = 1
     16                           AND SPER.TASK_INQUIRY_TYPE LIKE 'Descriptive Data Challenge%') A
     17          GROUP BY A.SPER_STATUS_TEXT
     18          ORDER BY (CASE
     19                       WHEN A.SPER_STATUS_TEXT = 'Data Item Confirmed' THEN 1
     20                       WHEN A.SPER_STATUS_TEXT = 'Data Item Updated' THEN 2
     21                       WHEN A.SPER_STATUS_TEXT = 'Other Responses' THEN 3
     22                       ELSE 4
     23                    END))
     24  MODEL
     25     DIMENSION BY (SPER_STATUS_TEXT)
     26     MEASURES (CNT)
     27     RULES
     28        (CNT ['Data Item Confirmed'] = NVL (CNT[CV ()], 0),
     29        CNT ['Data Item Updated'] = NVL (CNT[CV ()], 0),
     30        CNT ['Other Responses'] = NVL (CNT[CV ()], 0));
    SPER_STATUS_TEXT           CNT
    ------------------- ----------
    Data Item Updated            2
    Other Responses              0
    Data Item Confirmed          0
    
    3 rows selected.
    
    SQL> 
    

    G.

  • Grouping of SQL statement

    I need help can rewrite this query to get the result I wanted. Any help would be grealy appreciated.

    Here, I'm trying to get the total number of master accounts and not master created per month. With the query below, I get the data, but I have to do a lot of coding to display data in a tabular and graphic form on the web.

    Select last_day (add_months (trunc (created),-1)) + 1 as count (*) created, master, as the number
    of web_users
    Last_day (add_months (trunc (created),-1)) + 1, master group;

    Now it returns the data


    Created Master Number*.

    01/01/2004 Y 100
    01/01/2004 N 120
    01/02/2004 Y 100
    01/02/2004 N 120
    01/03/2004 Y 100
    01/04/2004 Y 100
    01/04/2004 N 120

    I need data output format. It won't be easy for me view the data on the web

    Created Master1 Countnumber1 Master2 Countnumber2 *.

    01/01/2004 N Y 100 120
    01/02/2004 O N 100, 120
    01/03/2004 O N 100, 0
    01/04/2004 O N 100, 120
    SQL> create table web_users (created,master)
      2  as
      3   select case
      4          when level <= 220 then date '2004-01-01'
      5          when level <= 440 then date '2004-02-01'
      6          when level <= 540 then date '2004-03-01'
      7          else date '2004-04-01'
      8          end
      9        , case
     10          when level <= 100 then 'Y'
     11          when level <= 220 then 'N'
     12          when level <= 320 then 'Y'
     13          when level <= 440 then 'N'
     14          when level <= 540 then 'Y'
     15          when level <= 640 then 'Y'
     16          else 'N'
     17          end
     18     from dual
     19  connect by level <= 760
     20  /
    
    Table created.
    
    SQL> select last_day(add_months(trunc(created),-1))+1 as created
      2       , master
      3       , count(*) as countnumber
      4    from web_users
      5   group by last_day(add_months(trunc(created),-1))+1
      6       , master
      7   order by created
      8       , master
      9  /
    
    CREATED             M COUNTNUMBER
    ------------------- - -----------
    01-01-2004 00:00:00 N         120
    01-01-2004 00:00:00 Y         100
    01-02-2004 00:00:00 N         120
    01-02-2004 00:00:00 Y         100
    01-03-2004 00:00:00 Y         100
    01-04-2004 00:00:00 N         120
    01-04-2004 00:00:00 Y         100
    
    7 rows selected.
    
    SQL> select trunc(created,'mm') created
      2       , 'Y' master1
      3       , count(case master when 'Y' then 1 end) countnumber1
      4       , 'N' master2
      5       , count(case master when 'N' then 1 end) countnumber2
      6    from web_users
      7   group by trunc(created,'mm')
      8   order by created
      9  /
    
    CREATED             M COUNTNUMBER1 M COUNTNUMBER2
    ------------------- - ------------ - ------------
    01-01-2004 00:00:00 Y          100 N          120
    01-02-2004 00:00:00 Y          100 N          120
    01-03-2004 00:00:00 Y          100 N            0
    01-04-2004 00:00:00 Y          100 N          120
    
    4 rows selected.
    

    Kind regards
    Rob.

  • Grouping of SQL with a subquery

    Hi all

    I had to get an output by combining the same field by using 2 different condition. I tried to use the subquery, but the output is not getting properly completed.

    example of the query will be something like:

    Select count (*), _ (select count (*) from TableX where = 1 condition) from TableX Grouper by FieldA.

    The output is considered the first condition + (Count + have 2 folders and subquery (.. .or condition = 1) with also 2 records.)

    But the real result for the subquery if I run the query separately is only 1 card. The query has filled the second row with the same performance as a result of the first condition with 2 records to return. Anyway I put a condition to bring together both the condition?

    Thank you.

    Something like that?

    select sum(case when  then 1 else 0 end ) first_count, sum(1) total_count from TableX group by FieldA;
    

    Arun-

    Not tested

  • SQL query SUM and AVG

    Hi all

    I think I'm really stupid but I have problems of SQL query.

    I have a table with many lines of operations which all refer to different stores, for example:

    Store... _Sales_... _Month_
    Reading... 200 k... April
    Leeds... 50k................ April
    Manchester... 70k................ May
    Reading... 100 k... May

    I need to arrive at the average sales for the month combined as a total. That is the average income of store for a given period is 200 k + 50 k + 70 k + 100 k / * 3 * (because there are only 3 unique shops) - I hope this makes sense!

    So, basically, I'm doing both a query of the SUM (and company store) and then out of the average of all the stores together. Is this possible?


    Thank you

    Hello

    This query returns 140 which seems to be the correct value:

    with data as
    ( select 'Reading' Store
      , 200 sales
      from dual
      union
      select 'Leeds'
      , 50
      from dual
      union
      select 'Manchester'
      , 70
      from dual
      union
      select 'Reading'
      , 100
      from dual
    )
    select sum(sales)
    , count( distinct store )
    , sum(sales)/count(distinct store)
    from data
    

    There are several options:
    1. you can get two IR on one page (using IFRAMEs - search for that word on the Forum)...
    2. you create another region with the above query and that position just below the report
    3. in the foot of the region call a process of PL/SQL (using AJAX) that calculates the value using the query and print it (via htp.p)

    Greetings,
    Roel

    http://roelhartman.blogspot.com/
    http://www.bloggingaboutoracle.org/
    http://www.Logica.com/

    You can assign this answer to your question in marking it as useful or Correct ;-)

  • SQL Count - is it possible?

    To be used at the APEX, I am trying to accomplish the following:

    Have a read to the user indicating how much of a certain task is available:

    IE:
    There is 'X' associated tasks 'name of the project.

    However, I get an error whenever an attempt to save the code, the code is as follows:
    select 
        'There are ' ||count(distinct pd.fk_tasks_id) ||' tasks for project ' ||pm.name
    
    from
        protrac_master pm,
        protrac_detail pd
    
    where
        pk_proj_master_id = pk_proj_detail_id
    I tried both the single and double quotes.

    Hello

    Leland says:
    ... Wouldn't put an alias defeat the whole point of what I was trying to do?

    Lol why do you think that it would frustrate the object?

    select
        'There are ' ||count(distinct pd.fk_tasks_id) ||' tasks for project ' ||pm.name
            AS  txt
    from protrac_master pm, protrac_detail pd
    where pk_proj_master_id = pk_proj_detail_id
    GROUP BY pm.name
    

    In addition, the statement runs a toad.

    He would probably run in SQL * more, too, but not in all contexts where you would have to refer to columns.

  • Lack of local groups of SQL Server service causing my upgrade failed... (SQL2008R2 to SQL2014) emergency assistance

    Hi all
    I have problems with the upgrade of SQL Server 2008 R2 at SQL2014 with the error "no mapping between account names and security IDS was done" and the reason for the error is SQL server local groups that were created during installation (SQL2008 R2) are not on the local server groups.
    Can anyone help identify the good fix for this problem?
    SQLServerMSSQLUser$$MSSQLSERVER
    SQLServerSQLAgentUser$$MSSQLSERVER
    SQLServerReportServerUser$$MSRS10_50.MSSQLSERVER
    SQLServerFDHostUser$$MSSQLSERVER
    Please let me know if you need any further details.
    Thank you
    Naz
    S/N

    This issue is beyond the scope of this site (for consumers) and to be sure, you get the best (and fastest) reply, we have to ask either on Technet (for IT Pro) or MSDN (for developers)
    *
  • Group by and count in every 5 minutes

    Dear guy,

    I need to recover table data transaction report for the number of transactions sumarizing and the sum of the value in every 5 minutes. Please look below

    Last timeTypeValue
    --------------------
    12:01:12buy10
    12:02:34pay a20
    12:04:43topup10
    12:05:55pay a30
    12:08:34buy40
    12:11:21buy40
    12:12:46topup5
    12:15:50pay a30
    12:16:23pay a25
    12:19:40pay a15
    12:19:50buy15
    12:19:59buy5

    -get the number of buy and pay a transaction every 5 minutes and the sum of the value

    the result of the will:

    Last timetransactionnumber of value
    --------------------         -----
    12:00-12:05pay a1 20
    12:00-12:05buy1 10
    12:05 to 12:10pay a130
    12:05 to 12:10buy140
    12:10-12:15pay a00
    12:10-12:15buy00
    12:15-12:20pay a3 70
    12:15-12:20buy2 20

    Do you have ideas for this?

    So thank you and enjoy

    Ch

    WITH DATA (last time, TYPE, VALUE)

    AS (SELECT to_date('12:01:12','hh24:mi:ss'), 'buy', 10 double UNION ALL)

    SELECT to_date('12:02:34','hh24:mi:ss'), "billpay", 20 FROM dual UNION ALL

    Select to_date('12:04:43','hh24:mi:ss'), 'charging', 10 double UNION ALL

    SELECT to_date('12:05:55','hh24:mi:ss'), pay 'a', 30 double UNION ALL

    Select to_date('12:08:34','hh24:mi:ss'), 'buy', 40 double UNION ALL

    SELECT to_date('12:11:21','hh24:mi:ss'), 'buy', 40 double UNION ALL

    Select to_date('12:12:46','hh24:mi:ss'), 'charging', 5 double UNION ALL

    SELECT to_date('12:15:50','hh24:mi:ss'), pay 'a', 30 double UNION ALL

    SELECT to_date('12:16:23','hh24:mi:ss'), "billpay", 25 FROM dual UNION ALL

    Select to_date('12:19:40','hh24:mi:ss'), pay 'a', 15 double UNION ALL

    SELECT to_date('12:19:50','hh24:mi:ss'), 'buy', 15 double UNION ALL

    SELECT to_date('12:19:59','hh24:mi:ss'), 'buy', 5 FROM dual)

    -1/1440 is a minute so 5 / 1440 would be 5 minutes

    To_char (SELECT trunc (SYSDATE) + (PIECE * (5 / 1440)), "HH24")

    TO_CHAR (trunc (SYSDATE) + ((CHUNK + 1) * (5 / 1440)), "HH24")

    TYPE

    value

    OF (-piece is the number of periods of 5 minutes since midnight)

    Floor (to_number (to_char(lasttime,'SSSSS')) SELECT / 300) AS CHUNK

    TYPE

    value of MySQL

    FROM THE DATA

    GROUP BY floor (to_number (to_char (last time, 'SSSSS')) / 300)

    TYPE)

    /

    HTH

Maybe you are looking for

  • Share items AddressBook

    I must be a detail view, but I need a little help on this: Since sharing of agendas (subscriptions) is possible and (later) because Cloudsoultions showed, I hoped to be able to share (parts of) my AddressBook with my wife for obvious reasons. I tried

  • New iPad restore from backup to iCloud

    message being (two weeks) this new iPad is still iCloud backup restore.

  • Container for several tables of different length

    Hey,. I'm doing an algorithm for labelling component connected in LabVIEW.  I came across the problem that I need some sort of data structure that I can't create.  I hope that I've missed something and someone can report this to me. What I'm looking

  • Cannot manually remove Intel PROSet / Wireless

    Attempt to upgrade to Windows 7 Windows 8.  Windows Upgrade Assistant is instructing manually uninstall me Intel PROSet / Wireless, because it is not compatible with Windows 8.  I'm unable to do so.

  • Question to install element Adobe Premiere &amp; Photoshop 14 x64bits W7 (en)

    HelloI bought, downloaded and installed (try to), Photoshop and first element 14 today on windows 7 x 64 ENI tried first with Photoshop 14, at the end of installation says there are errors... I'll check the link to the journal: 0 - fatal error...So I