SQL GROUP BY / HAVING a question

Hello

A few days earlier, I came across an interview question that I can not resolve correctly.

In short, there are two tables: BOOKS and TAGS.

ID TITLE AUTHOR
1TwilightStephenie Meyer
2Catch fireSuzanne Collins
3Animal farmGeorge Orwell

BOOK_ID TAG
1Best Seller
1Science Fiction
1TOP10
2Best Seller
2Note.
2TOP10
3TOP110

However several tags and the result must be a set of appropriate books, users can enter.

For example, in the case of tags 'Best Seller', 'Science Fiction' and 'Top 10', the result must be "Twilight."

My solution was something like this:

SELECT b.title
FROM books b
WHERE
    b.id IN (
        SELECT book_id
        FROM tags
        WHERE
            LOWER(tag) = 'best seller'
    )
    AND b.id IN (
        SELECT book_id
        FROM tags
        WHERE
            LOWER(tag) = 'science fiction'
    )
    AND b.id IN (
        SELECT book_id
        FROM tags
        WHERE
            LOWER(tag) = 'top10'
    )






But it is definetaly not an elegant and flexible query. In the case of labels of 10 or more incoming, we get a code 'spagetti '.

Somehow, it should be possible to resolve using GROUP BY and HAVING, but I don't really know how.

Could someone help what would be the most elegant solution to this problem?

Thank you so much in advance.

Maybe:

SQL >-generating sample data:

SQL > with books like)

2. select id 1, the title of 'Twilight', author of "Stephenie Meyer" Union double all the

3 select 2, "Taking fire", "Suzanne Collins' Union double all the

4 Select 3, 'Animal Farm', 'George Orwell' of the double

5)

6, tags such as)

7 select 1 book_id, tag "Best Seller" of all the double union

8. Select 1, 'Science Fiction' from dual union all

9. Select 1, 'TOP10' from dual union all

10. Select 2, 'Best Seller' from dual union all

11. Select 2, 'Roman' from dual union all

12. Select 2, 'TOP10' from dual union all

13. Select 3, 'TOP110' from dual

14)

15-

16 - the actual query:

17-

18 select b.title

Books b 19

20, tags t

21 where t.book_id = b.id

22 and t.tag ('Best Seller', 'Science Fiction', 'Top 10')

23 by b.title group

24 after having count (*) = 3

25.

TITLE

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

Twilight

1 selected line.

Tags: Database

Similar Questions

  • SQL group

    SQL in group by

    I have a table called test and the data in the table from below

    Amount of SNO startdate enddate

    10000 10/1 / 2001 9/30/2003 10.34

    10000 10/1 / 2003 9/30/2005 15.89

    10000 10/1 / 2005 9/30/2007 15.89

    10000 10/1 / 2007 9/30/2013 10.34

    10000 10/1 / 11/30/2013 2013, 15.89

    10000 12/1 / 2013 12/31/4 000 27

    I want the results in this way. expected results of this way

    Amount of SNO startdate enddate

    10000 10/1 / 2001 9/30/2003 10.34

    10000 10/1 / 2003 9/30/2007 15.89

    10000 10/1 / 2007 9/30/2013 10.34

    10000 10/1 / 11/30/2013 2013, 15.89

    10000 12/1 / 2013 12/31/4 000 27

    Under query not giving good results. Please help any with the query

    Select min (startdate), max (endate), sno, amount

    of the TEST

    Group by sno, amount

    Hello

    Why do you want 2 separate lines of output for amount = 15.89?  Why you want to combine the lines where startdate is in 2033 and in 2005, but not the line where startdate is in 2013?  Is this because another line, with a different amount, comes between 2005 and 2013, and but no rank with a different amount comes between 2003 and 2005?

    If so, maybe you want something like:

    WITH got_diff AS

    (

    SELECT sno, startdate, enddate, amount

    , ROW_NUMBER () OVER (PARTITION BY sno - just guessing

    ORDER BY startdate

    )

    -ROW_NUMBER () OVER (PARTITION BY sno, amount

    ORDER BY startdate

    ) AS diff

    OF the test

    )

    SELECT sno

    (MIN) startdate) AS first_startdate

    MAX (enddate) AS last_enddate

    quantity

    OF got_diff

    GROUP BY sno, amount, diff

    ORDER BY sno, first_startdate

    ;

    For an explanation of the technique used here fixed difference, see

    Analytic Question lag and lead and/or

    Re: Ranking of queries

  • SQL grouping and summing impossible?

    I want to create a sql query to summarize some data, but I'm starting to think that it is impossible with sql only. The data I have is of the following form:
    TRAN_DT     TRAN_RS     DEBT     CRED
    10-Jan     701     100     0
    20-Jan     701     150     0
    21-Jan     701     250     0
    22-Jan     705     0     500
    23-Jan     571     100     0
    24-Jan     571     50     0
    25-Jan     701     50     0
    26-Jan     701     20     0
    27-Jan     705     0     300
    The data are classified by TRAN_DT and by TRAN_RS. THA grouping and summing data based on tran_rs, but only when it changes. If in the table above, I don't want to see all the first 3 records but only one value DEBT the sum of these 3 i.e. 100 + 150 + 250 = 500. If the table above after grouping would be similar to that below:
    TRAN_DT     TRAN_RS     DEBT     CRED
    21-Jan     701     500     0
    22-Jan     705     0     500
    24-Jan     571     150     0
    26-Jan     701     70     0
    27-Jan     705     0     300
    The TRAN_DT is the last value of the record sum. I undestand that the tran_dt are not selectable. What I've tried so far is the following query:
    select tran_dt,
             tran_rs,
             sum(debt)over(partition by tran_rs order by tran_dt rows unbounded preceding),
             sum(cred)over(partition by tran_rs order by tran_dt rows unbounded preceding) from that_table
    Is it still possible with only sql, any thoughts?

    The report that I am creating in BI Publisher.Maybe it is possible to group the data in the model and my question here?

    The Re: tutorial method Tabibitosan by Aketi Jyuuzou me would be very useful here:

    with sample_data as (select to_date('10/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 100 debt, 0 cred from dual union all
                         select to_date('20/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 150 debt, 0 cred from dual union all
                         select to_date('21/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 250 debt, 0 cred from dual union all
                         select to_date('22/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 500 cred from dual union all
                         select to_date('23/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 100 debt, 0 cred from dual union all
                         select to_date('24/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 50 debt, 0 cred from dual union all
                         select to_date('25/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 50 debt, 0 cred from dual union all
                         select to_date('26/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 20 debt, 0 cred from dual union all
                         select to_date('27/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 300 cred from dual),
         tabibitosan as (select tran_dt,
                                tran_rs,
                                debt,
                                cred,
                                dense_rank() over (order by tran_dt, tran_rs, rownum)
                                  - dense_rank() over (partition by tran_rs order by tran_dt, rownum) grp
                         from   sample_data)
    select max(tran_dt),
           tran_rs,
           sum(debt) debt,
           sum(cred) cred
    from   tabibitosan
    group by tran_rs,
             grp
    order by 1, 2;
    
    TRAN_DT       TRAN_RS       DEBT       CRED
    ---------- ---------- ---------- ----------
    21/01/2012        701        500          0
    22/01/2012        705          0        500
    24/01/2012        571        150          0
    26/01/2012        701         70          0
    27/01/2012        705          0        300
    

    ETA: I added the rownum in analytical functions to take account of the "encounters" where a tran_rs has multiple entries for the same tran_dt. If you have some other unique key to use (for example, the primary key of the table) then I would use instead.

    Published by: Boneist on February 17, 2012 11:41

  • Group by best practical question

    Consider this example:

    TABLE: SALES_DATA

    firm_id | sales_amt | d_date | d_data

    415. 45. 20090615 | Lincoln Financial
    415. 30. 20090531 | Lincoln AG
    416. 10. 20081005 | AM General
    416. 20. 20080115 | AM General Inc.

    I want the output to be grouped by firm_id with the sum of sales_amt and the d_data
    This corresponds to the last d_date (i.e. max (d_date))

    Application project:

    Select sum (sales_amt) total_sales, substr (max (d_data), firm_id, instr (max (d_data), ' ~') + 1) firm_name)
    Select firm_id, sales_amt, d_date | '~' || sales_data d_data
    )
    Firm_id group

    output is as expected:

    firm_id | total_sales | firm_name

    415. 75. Lincoln Financial
    416. 30. AM General

    I know it works, but my QUESTION is: is there a better way to do and is the method described above to concatenate the columns when you want to group several columns against best practices.

    Thank you very much!

    Here is a way that uses analytical (I just like them):

    SQL> select * from sales_data;
    
                 FIRM_ID            SALES_AMT D_DATE               D_DATA
    -------------------- -------------------- -------------------- ------------------------------
                     415                   45 15-JUN-2009 00:00:00 Lincoln Financial
                     415                   30 31-MAY-2009 00:00:00 Lincoln AG
                     416                   10 05-OCT-2008 00:00:00 AM General
                     416                   20 15-JAN-2008 00:00:00 AM General Inc.
    
    SQL> select firm_id, sum_amt, d_data
      2  from
      3  (
      4     select firm_id, d_data
      5           ,sum(sales_amt) over (partition by firm_id) sum_amt
      6           ,row_number() over (partition by firm_id order by d_date desc) rn
      7     from   sales_data
      8  )
      9  where rn = 1
     10  ;
    
                 FIRM_ID              SUM_AMT D_DATA
    -------------------- -------------------- ------------------------------
                     415                   75 Lincoln Financial
                     416                   30 AM General
    
  • Firefox is having "Script" questions (no response) und eventually Flash issues?

    Okay, downloaded Firefox for the first time like a year yesterday. It worked perfectly, oh, how I missed it. I hate using Google Chrome with a fiery burning passion.

    However, although it worked perfectly yesterday, I have not downloaded anything or has any plug-ins/extensions, whatever.

    Today, however, I can go on www.google.com (on firefox), but if I try to go on my site banks, Habbo.es, or something like that, all the sudden Firefox decided to hang. It freezes und I get that annoying "(ne répond pas)" upstairs. " I have to shut completely out of the browser, open und renounce session restore.

    Some websites is okay, however, it seems that many of them are not.

    I get a script error message, this day for Neopets.com , which displayed was "lookout.bluecava" or something like that. Asked if I wanted to wait for script to respond or if I wanted to stop him. Whatever it is, it ends with closing me und on return from google.

    Ask this question, I had to re - download google chrome, und log in from this browser since Firefox is not even want to support right now, it's own support forum! Stuff of crazy, right?

    In any case, since it is having a problem with "script", I went to java, made sure I had the java update installed on my computer, then I made sure that I got the update java plugin. Und I even downloaded java 64 bit for him as well, the 7u45 or something else.

    So, my java on my comp und firefox browser are apparently up to date, und the thing of the add-on for Firefox to check if the plugins are up to date says that they are all up to date.

    Still having the issue. At the time where I go to imvu or another site that has this issue, it freezes for 10 seconds und that's all she wrote.

    So I'm not really wanting to go back to Google Chrome for another year, I miss the hell out of Firefox.

    To add: the question that I have now with Firefox is the same problem, I had a year before, when I had to uninstall firefox und go with Chrome in the first place.

    But, since yesterday it was normal when I installed it, I thought it was fixed. Wrong in less than 24 hours.

    Before I submitted this, I decided to jump on Firefox und try something.

    I disabled the Shockwave Flash und plugins Shockwave Director. Now, Firefox works again.

    so.

    He asks me if I want to install the 'flash' plugins needed to view content on the page (I used Neopets.com trying after disabling the Shockwave), but still runs.

    So, what should I do for flash? Or better yet, what is happening with it?

    I downloaded the flash yesterday in a half hour startup of Firefox after you download it. Und it worked very well for hours.

    I turned off the computer, he packed up, went home, back to work, hop on the net und BAM. All the shock wave suddenly is having problems with Firefox when nothing was done he commit? Is not supposed.

    Other info:
    Windows 7 Home Premium on HP laptop.

    Well, I don't know what was the root cause of the problems I had. But I had a friend completely erase my computer und installation Windows 7 Professional on this topic. Und now, I no longer have problems with Firefox about flash. It's purr in this Department.

  • Want that output SQL group

    I have a request in the form

    Select Group, bill, table Qty.

    and output as below

    Advantage Bill Qty

    === ===== =======

    x 1 XXX 50

    x 1 XXX 50

    x 1 XXX 60

    Y1 YYY 23

    Y1 YYY 50

    but I want to output as below

    Advantage Bill Qty

    === ===== =======

    x 1 XXX 50

    50

    60

    Y1 YYY 23

    50

    How can I get this help please

    Hello

    If you use SQL * more then you simply need to use 'BREAK' (and an ORDER BY.)

    WE BREAK bill WE left

    SELECT Bill, part Qty.

    FROM MyTable

    ORDER BY 1, 2, 3

    ;

    Best regards

    Bruno Vroman.

  • -SQL - GROUP BY clause: fields of nonaggregate mandate

    Hello

    I study data (especially data recovery) and found something interesting.

    When you use an aggregate function in the SELECT clause, it is mandatory to have all fields that are not aggregated in the SELECT clause to be there in the GROUP BY clause.
    For example,.

    SELECT dept_no, Salary
    The EMPLOYEE
    GROUP BY dept_no;

    The SQL above works fine.
    But what happens if the user forgets the dept_no in the GROUP BY clause or the clause GROUP BY itself is missing?
    Certainly, it is a mistake.

    Why this error is not handled by the database. I mean, the database must be smart/pretty smart to add the GROUP BY clause by itself. So let's assume that, if I miss the GROUP BY clause or miss a field no aggregated from the SELECT clause when I get at least an aggregate function on a field with at least a no aggregated field in the SELECT clause, the database should check the GROUP BY clause at compile time and add mandate missed the fields in the GROUP BY clause.

    Example,

    SQL1:_
    SELECT dept_no, Salary
    The EMPLOYEE
    GROUP BY dept_no;

    SQL2:_
    SELECT dept_no, Salary
    The EMPLOYEE;

    Here, the SQL1 and SQL2, both should give me same output without error.

    I can't understand why this is handled?

    Hello

    998478 wrote:
    ... If we mix the aggregated and non-aggregated values, then there must be a GROUP BY clause that contains all non-aggregated values. Why this is handled by the database/compiler itself?

    It IS managed by the compiler itself. The compiler manages to trigger an error. The compiler has no way of knowing if you want to remove something from the SELECT clause, or add something to the GROUP BY clause, or not to use the aggregate functions or use several aggregate functions, or a combination of the above. If the compiler re-writes your code and none of these things done automatically, it would be wrong more often that he was right, and you would (rightly) complain about his behavior.

    For example, it is clearly wrong:

    SELECT    deptno
    ,       job
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;
    

    What is the right way to fix it?

    1. remove something from the SELECT clause

    SELECT    deptno
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;
    

    2. add something to the GROUP BY clause

    SELECT    deptno
    ,       job
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ,         job
    ;
    

    3. do not use aggregate functions

    SELECT    deptno
    ,       job
    ,       sal
    FROM       scott.emp
    ;
    

    4. use several aggregate functions

    SELECT    deptno
    ,       MIN (job)
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;
    

    What are all the options, either. For example, the correct solution would be to use analytical functions instead of aggregate functions.
    How can anyone tell which of them is right? They all have the right answer for some problem.

    Moreover, by saying that everying in the SELECT clause must be an aggregate or in the GROUP BY clause is a bit oversimplified.
    Fuller, here's the ABC of GROUP BY:
    When you use a GROUP BY clause or in an aggregate function, then all in the SELECT clause must be:
    (A) a ggregate function,
    (B) one of the expressions "group By."
    (C) adding to C, or
    (D) something that Depends on the foregoing. (For example, if you "GROUP BY TRUNC (dt)", you can SELECT "TO_CHAR (TRUNC (dt), 'Mon - DD')").

    Published by: Frank Kulash on April 13, 2013 13:44
    Additional code examples.

  • SQL Group function

    --General Employee table
    -- Version:  oracle 11g Rel 2
    
    create table employee (ID varchar2(100),first_name varcha2(200),last_name varchar2(200),start_date date,end_date date,city varchar2(100))
    
    select listagg(First_name,',') within group (order by first_name) from employee  group by city ;
    op:
    1. XXX,YYy,ZZZ
    2. AAA,SSS
    
    Excepted OP:
    
    1. 1.XXX,2.YYY,3.ZZZ
    2. 1.AAA,2.SSS
    Need a query sql for obtaining the planned op, please notify

    Thanks in advance
    Carole Kumar

    Edited by: 876377 may 1, 2012 22:04

    876377 wrote:

    --General Employee table
    -- Version:  oracle 11g Rel 2
    
    select listagg(First_name,',') within group (order by first_name) from employee;
    op:
    1. XXX,YYy,ZZZ
    2. AAA,SSS
    
    Excepted OP:
    
    1. 1.XXX,2.YYY,3.ZZZ
    2. 1.AAA,2.SSS
    

    Need a query sql for obtaining the planned op, please notify

    Thanks in advance
    Carole Kumar

    Try to use

    row_number over (partition by ID_COLUMN order by first_name) as RN
    

    And then

    listagg( rn || first_name)
    

    Since you don't validate your description of table we do not know what are the columns, etc... I guessed that the ID column is named ID_COLUMN, adjust accordingly.

  • SQL - grouping of cases

    Hello everyone,
    I'm trying to get a count of all nulls v/s of the non-null values in a table. I'm trying to to do this, use the CASE statement.
    I trained the SQL following so far.
    SELECT count(Aj.Asset_Hdr_Id),
           CASE Nvl(Aj.Asset_Hdr_Id,
                0)
             WHEN 0 THEN
              'IS NULL'
             ELSE
              'IS NOT NULL'
           END
      FROM Asset_Jt Aj
     GROUP BY Nvl(Aj.Asset_Hdr_Id, 0);
    It only gives me the desired result. My goal will look like
    2089 IS NULL
    12340 IS NOT NULL
    I get a result that looks like this
    1     0     IS NULL
    2     1     IS NOT NULL
    3     1     IS NOT NULL
    4     1     IS NOT NULL
    5     1     IS NOT NULL

    Try this,

    SELECT COUNT (*), result
      FROM (SELECT CASE NVL (Aj.Asset_Hdr_Id, 0)
                     WHEN 0 THEN 'IS NULL'
                     ELSE 'IS NOT NULL'
                   END
                     result
              FROM Asset_Jt Aj)
    GROUP BY result;
    

    G.

  • SQL group by term

    Hi all

    I'm writing a query that selects records that are not within a period of 90 days from the other beginning with the first date. Is that records are selected that are more than 90 days, independently of each other. So, for the next game


    (Tbl) CREATE TABLE
    class VARCHAR2 (3200),.
    actiondate DATE);

    INSERT INTO tbl
    VALUES ("C1",
    "(2007-02-12');

    INSERT INTO tbl
    VALUES ("C1",
    "(2007-06-01');

    INSERT INTO tbl
    VALUES ("C1",
    "(01/05/2009 ');

    INSERT INTO tbl
    VALUES ("C1",
    "(01/07/2009 ');

    INSERT INTO tbl
    VALUES ("C1",
    "(01/09/2009 ');

    INSERT INTO tbl
    VALUES ("C1",
    October 1, 2009 ");"

    INSERT INTO tbl
    VALUES ("C1",
    01/01/2010');

    INSERT INTO tbl
    VALUES ("C1",
    "(2010-02-01');

    INSERT INTO tbl
    VALUES ("C1",
    "(2010-03-01');

    INSERT INTO tbl
    VALUES ("C1",
    "(2010-05-01');

    INSERT INTO tbl
    VALUES ("C1",
    "(07/30/2009 ');

    INSERT INTO tbl
    VALUES ("C1",
    "(29/07/2009 ');

    INSERT INTO tbl
    VALUES ("C2",
    "(2008-02-01');

    INSERT INTO tbl
    VALUES ("C2",
    "(2008-05-02');

    INSERT INTO tbl
    VALUES ("C2",
    "(2008-06-01');

    INSERT INTO tbl
    VALUES ("C2",
    October 15, 2008 ');

    INSERT INTO tbl
    VALUES ("C2",
    01/01/2009');

    INSERT INTO tbl
    VALUES ("C2",
    ' (15/02/2009 ');

    INSERT INTO tbl
    VALUES ("C2",
    "(30/05/2009 ');

    INSERT INTO tbl
    VALUES ("C2",
    October 1, 2009 ");"


    I need to return the following documents

    CLASS ACTIONDATE
    C1, 12/02/2007
    C1, 01/06/2007
    C1, 01/05/2009
    C1, 01/09/2009
    C1, 01/01/2010
    C1, 01/05/2010
    C2, 01/02/2008
    C2, 02/05/2008
    C2, 15/10/2008
    C2, 15/02/2009
    C2, 30/05/2009
    C2, 01/10/2009


    He tried the following query does not work

    SELECT Actiondate,
    Min (actiondate)
    COURSES (ORDER BY actiondate SCOPE BETWEEN the '90' LINE PREVIOUS AND CURRENT DAY LEVEL) dt
    FROM tbl


    I also tried the following method, but does not work.


    SELECT Ceil ((actiondate-DATE "-4712-01-01') / 90) * 90 + DATE" - 4712-01-01', "
    Min (actiondate)
    DUNGEON (DENSE_RANK LAST ORDER BY actiondate, rowid)
    FROM tbl
    GROUP BY Ceil ((actiondate-DATE "-4712-01-01') / 90) * 90 + DATE" - 4712-01-01'"



    Is there a SQL to achieve or PL/SQL is the only way to do this? The pointers will be greatly appreciated.

    Please consider that the table contains more than 3 million lines.

    Rgds
    Thank you.

    Hello

    Here's the Dimacit of [the aforementioned thread | http://forums.oracle.com/forums/message.jspa?messageID=2526363#2526363].
    I just changed the names of table and column, the length of the period spent under 2 (exclusive) days to 90 days (included) and changed the main query for your needs.

    WITH  rns AS
    (
         SELECT  tbl.*
         ,     ROW_NUMBER() OVER ( PARTITION BY  cls
                                 ORDER BY         actiondate
                          ) AS rn
         FROM     tbl
    )
    ,     grp_starts     AS
    (
         SELECT     cls
         ,     actiondate
         ,     rn
         ,     grp_start
         FROM     rns
         MODEL     PARTITION BY (cls)
              DIMENSION BY (rn)
                    MEASURES ( actiondate
                    , actiondate  grp_start
                    )
                    RULES ( grp_start [ANY ] ORDER BY rn = CASE
                                                                   WHEN  grp_start [CV() - 1] IS PRESENT
                                                             AND      grp_start [CV()] - grp_start [CV() - 1] <= 90
                                                             THEN      grp_start [CV() - 1]
                                                           ELSE  actiondate [CV()]
                                                        END
                          )
    )
    SELECT       cls
    ,       actiondate
    FROM       grp_starts
    WHERE       actiondate = grp_start
    ORDER BY  cls
    ,            actiondate;
    

    Subqueries, up and including grp_starts, assign each line to a group. This is great if you want to GROUP BY this expression. If all what you want is to show the group identifier, then you might be able to shorten the above query a bit.

    Class is a keyword, so it's not a good choice for a column name. I called this cls of the column instead.

    July 30 is exactly 90 days after May 1. If you want a group to contain all less than 90 days after the date of the first in the group, then it seems that you would want a new group to begin July 30. The output you posted, it's what I expected, if a group is all less than or equal to 90 days after the beginning of the group.

  • PL SQL SQL - group

    Hi all

    Thanks for your time in advance. I have the situation where I need quantities of different domain group but one of the fields must be reorganized. Not sure how best to explain this in the text. But, please see the example below. With the help of 11g
    SELECT 80 AMOUNT, 111 ACCOUNT_NUM, 123 CHECK_NUM FROM DUAL
    UNION ALL
    SELECT 100 AMOUNT, 111 ACCOUNT_NUM, 124 CHECK_NUM FROM DUAL
    UNION ALL
    SELECT 120 AMOUNT, 111 ACCOUNT_NUM, 125 CHECK_NUM FROM DUAL
    Expected result:
    AMOUNT  ACCOUNT_NUM     CHECK_NUM
    200                 111                     123,124,125
    Thank you

    I guess you wanted to say 300 was the amount in your example? (80 + 100 + 120)?

    I have 10g, but here's one way:

    with t as (
    SELECT 80 AMOUNT, 111 ACCOUNT_NUM, 123 CHECK_NUM FROM DUAL
    UNION ALL
    SELECT 100 AMOUNT, 111 ACCOUNT_NUM, 124 CHECK_NUM FROM DUAL
    UNION ALL
    SELECT 120 AMOUNT, 111 ACCOUNT_NUM, 125 CHECK_NUM FROM DUAL
    )
    select sum(amount), account_num,
    RTRIM(XMLAGG(XMLELEMENT(c, check_num||',') order by check_num).EXTRACT ('//text()'), ',') check_nums
    from t
    group by account_num
    
    SUM(AMOUNT)     ACCOUNT_NUM     CHECK_NUMS
    300                     111                   123,124,125
    
  • MS SQL or Exchange Storage Tuning Question

    Does anyone know how better to tune SQL or Exchange storage?

    We create 2 TB LUN using the SAS 8 x 300 GB on an IBM DS3400 FC SAN disks.

    (1) for the creation of the IBM Storage Manager dashboard, should what segment size we use? the indicated only are 128 KB or 256 KB. (512 can be done using the CLI)

    (2) what VMFS block size should be used? Then this SQL affect disk performance?

    (3) is there any special NTFS formatting required in the operating system (Windows Server 2003) to get better disk performance?

    Advice or guidance would be appreciated.

    Concerning

    Nicholas

    Reading the messages of various virtualization experts, the size of the block of VMFS has little impact on performance. If you have more than 256 GB virtual disks, then a larger block size would be better.

    Also, be sure to create the VMFS with Virtual Center partition as this will automatically align the VMFS.

    You need to align your XXXX in Windows Server 2003 for best performance. VMware has a guide that can be referenced here:

    http://www.VMware.com/PDF/esx3_partition_align.PDF

  • SQL group by age

    Hi friends
    I have a table like this
    name      birth_date
    john         01.01.1990
    sue          02.05.2000
    abraham    04.07.1998
    ..........
    I want to make a report as
    age      sum
    0-17      30
    18-24    25
    15-34    32
    35-44    14
    45-54     8
    55+        4
    How can I do this?

    The geometrico elsewhere perhaps is just an internal query where you define groups of age, and then outside the Group:

    with dummydata as (
    select 'AAA' name, to_date('01.01.1995','dd.mm.yyyy') birth_date from dual union all
    select 'AAA' name, to_date('01.01.1990','dd.mm.yyyy') birth_date from dual union all
    select 'AAA' name, to_date('01.11.1975','dd.mm.yyyy') birth_date from dual union all
    select 'AAA' name, to_date('01.11.1980','dd.mm.yyyy') birth_date from dual union all
    select 'AAA' name, to_date('01.01.1991','dd.mm.yyyy') birth_date from dual union all
    select 'AAA' name, to_date('01.01.1969','dd.mm.yyyy') birth_date from dual union all
    select 'AAA' name, to_date('01.01.1970','dd.mm.yyyy') birth_date from dual union all
    select 'AAA' name, to_date('01.01.1960','dd.mm.yyyy') birth_date from dual ) 
    
    select age_group , count(*)
    from(
        select
            case
                when trunc( months_between(sysdate, birth_date) / 12 ) <= 17 then '0-17'
                when trunc( months_between(sysdate, birth_date) / 12 ) <= 24 then '18-24'
                when trunc( months_between(sysdate, birth_date) / 12 ) <= 34 then '25-34'
                when trunc( months_between(sysdate, birth_date) / 12 ) <= 44 then '35-44'
                when trunc( months_between(sysdate, birth_date) / 12 ) <= 54 then '45-54'
    
                -- and so on... 
    
                else 'n/a'
            end age_group
        from dummydata
    )
    group by age_group
    order by age_group
    ;
    
    AGE_GROUP   COUNT(*)
    --------- ----------
    0-17               1
    18-24              2
    25-34              1
    35-44              3
    45-54              1
    
    5 rows selected.
    
  • Execute sql custom with DB adater question

    Hi all

    Problems with my custom in my jdeveloper, sql

    It's my custom sql:

    Select * from A table1, table2 B WHERE A.id = B.id AND A.id = #id

    Its not to return the complete set of data, its only 1column return value and yet I stipulated *.

    Any ideas why this happens?

    Thank you
    K

    Hello. Out of the blue suggestion: make a select qualified, including aliases for tables. For example, select A.id, A.col2, A.col3, B.id, B.col2, B.col3. I hope this helps.

  • ADF group height and width Question

    Hello world
    I'm new to ADF. I want to put a Panel such as panelgrouplayout to get a fix width and height, although there is only an outputtext. Help, please!

    Set the height property through the inlineStyle for the panelGroupLayout attribute.

    Sample:

          
            
              
            
            
          
        
    

    Thank you
    Nini

Maybe you are looking for

  • Accidentally removed the HD from the Finder icon. Need new!

    I'm new to iMac.  Do not know how to retrieve the icon. All of the suggestions I've found are General and do not tell me how to get to where I need to go to follow the directions. Sometimes, I don't know where are these screens. I tried things differ

  • SMART WIFI

    Hi guys,. Setting up the wifi chip... Click the button to send the e-mail verification (10 x now on two days) and have not received the e-mail (link to check my account)... WHAT GIVES? Thank you!

  • Corrupted script

    That means the following;  "Script is damaged OR has been tampered with?

  • XAV-601BT MirrorLink

    Someone has a link to the plans for progress and expansion MirrorLink? What phones are designed to be compatible in the future? There talks with Apple or the iPhone jailbreak community to allow MirrorLink capacity (limited or otherwise)?

  • HELP in drawing and adding captions to picture

    Please forgive my English and my beginner skills. I am really new to QT, QPainter and graphics programming. I develop the application snap2chat.  And I badly need help with drawing and add captions to Images. I tried QPainter paint brushes in the pic