Query to get a value for each day in a month

Hi All,
I'm needing a query (preferably without a loop or function) that can get a number representing each day of a given month.
For example for the month of February, the result would be 1,2,3,....28
Surely this can be done?
Thanks!

SQL> with a as (
  2  select trunc(sysdate,'Month') mon from dual)
  3  SELECT mon + LEVEL - 1 AS dates
  4  FROM a
  5  CONNECT BY LEVEL <= ADD_MONTHS (mon, 1)  - mon;
DATES
01-FEB-11
02-FEB-11
03-FEB-11
04-FEB-11
05-FEB-11
06-FEB-11
07-FEB-11
08-FEB-11
09-FEB-11
10-FEB-11
11-FEB-11
DATES
12-FEB-11
13-FEB-11
14-FEB-11
15-FEB-11
16-FEB-11
17-FEB-11
18-FEB-11
19-FEB-11
20-FEB-11
21-FEB-11
22-FEB-11
DATES
23-FEB-11
24-FEB-11
25-FEB-11
26-FEB-11
27-FEB-11
28-FEB-11
28 rows selected.or if u want it in one query,Then
SQL> SELECT trunc(sysdate,'Month') + LEVEL - 1 AS dates
  2  FROM dual
  3  CONNECT BY LEVEL <= ADD_MONTHS (trunc(sysdate,'Month'), 1)  - trunc(sysdate,'Month');
DATES
01-FEB-11
02-FEB-11
03-FEB-11
04-FEB-11
05-FEB-11
06-FEB-11
07-FEB-11
08-FEB-11
09-FEB-11
10-FEB-11
11-FEB-11
DATES
12-FEB-11
13-FEB-11
14-FEB-11
15-FEB-11
16-FEB-11
17-FEB-11
18-FEB-11
19-FEB-11
20-FEB-11
21-FEB-11
22-FEB-11
DATES
23-FEB-11
24-FEB-11
25-FEB-11
26-FEB-11
27-FEB-11
28-FEB-11
28 rows selected.
SQL> Regards
Umesh

Similar Messages

  • Query to get possible values for each segment

    Hi Gurus,
    Give the flex value set id, what is the query to get all possible values for each segment (just like what is shown in the accounting flex window)?
    Thank you,
    Beibei

    Hi Beibei,
    The table FND_FLEX_VALUES_VL can give you details of values available in the Value set i.e. 1 segment at a time.
    If you are looking for a concatenated view of Accounting Flexfield values, that would be available in GL_CODE_COMBINATIONS table.
    Regards,
    Ivruksha

  • TSQL Get Previous values for each group by

    Dear Friends,
    I have a problem with my TSQL statment. 
    I need to get previous value for each day and specific unit. Everything goes fine if I have always 2 unit in each day. But if same day has just one unit or less, I cannot have the previous value in the current record.
    The Output is this one:
    SK_DAY SK_UNIT
    VALUE VALUE_PREVIUS_DAY
    20131112 2
    30.00 NULL
    20131112 3
    34.00 NULL
    20131113 2
    40.00 30.00
    20131113 3
    45.00 34.00
    20131114 2
    50.00 40.00
    I dont have the second record for 2013-11-14, because in this date I just have value for unit 2.
    The final output should include the record:
    20131114 3
    0 45.00
    The Statment I have is:
    SELECT MAIN.SK_DAY, MAIN.SK_UNIT, SUM(MAIN.VALUE) AS VALUE
     SELECT SUM(VND1.VALUE) AS VALUE
     FROM FCT_TEST VND1
     WHERE CONVERT(DATE,CONVERT(VARCHAR(10),VND1.SK_DAY))>=DATEADD(dd,-1,CONVERT(DATE,CONVERT(VARCHAR(10),MAIN.SK_DAY)))
    AND CONVERT(DATE,CONVERT(VARCHAR(10),VND1.SK_DAY))<CONVERT(DATE,CONVERT(VARCHAR(10),MAIN.SK_DAY))
    AND VND1.SK_UNIT=MAIN.SK_UNIT
    ) AS VALUE_PREVIUS_DAY
    FROM FCT_TEST MAIN
    GROUP BY SK_DAY, MAIN.SK_UNIT
    SQL CREATE SCRIPT:
    CREATE TABLE [dbo].[FCT_TEST](
    [SK_DAY] [int] NOT NULL,
    [SK_UNIT] [int] NOT NULL,
    [VALUE] [decimal](18, 2) NULL,
     CONSTRAINT [PK_FCT_TEST] PRIMARY KEY CLUSTERED 
    [SK_DAY] ASC,
    [SK_UNIT] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    Thank you!!!
    PPSQL

    Thank you Kalman,
    But my problem is not with date! Fortunately I have dates for all days.
    The problem is for dimension tables like UNITS that I dont have values for all days, and when I try to get the previous value for previous day and each unit I dont have records. 
    20131112
    2
     30.00
    NULL
    20131112
    3
     34.00
    NULL
    20131113
    2
     40.00
    30.00
    20131113
    3
     45.00
    34.00
    20131114
    2
     50.00
    40.00
    20131114
    3
     0
    45.00 [I NEED THIS RECORD AS IS]
    Could you help me??
    Thank you!!

  • Sql query - Selecting last recorded values for each date in specified period

    Hello,
    Can someone please help me with my problem.
    I'm trying to get last recorded balance for each day for specific box (1 or 2) in specified period of days from ms access database using ADOTool.
    I'm trying to get that information with SQL query but so far unsuccessfully...  
    My table looks like this:
    Table name: TestTable
    Date Time Location Box Balance
    20.10.2014. 06:00:00 1 1 345
    20.10.2014. 12:00:00 1 1 7356
    20.10.2014. 18:45:00 1 1 5678
    20.10.2014. 23:54:00 1 1 9845
    20.10.2014. 06:00:02 1 2 35
    20.10.2014. 12:00:04 1 2 756
    20.10.2014. 18:45:06 1 2 578
    20.10.2014. 23:54:10 1 2 845
    21.10.2014. 06:00:00 1 1 34
    21.10.2014. 12:05:03 1 1 5789
    21.10.2014. 15:00:34 1 1 1237
    21.10.2014. 06:00:00 1 2 374
    21.10.2014. 12:05:03 1 2 54789
    21.10.2014. 15:00:34 1 2 13237
    22.10.2014. 06:00:00 1 1 8562
    22.10.2014. 10:00:00 1 1 1234
    22.10.2014. 17:03:45 1 1 3415
    22.10.2014. 22:00:00 1 1 6742
    22.10.2014. 06:00:05 1 2 562
    22.10.2014. 10:00:16 1 2 123
    22.10.2014. 17:03:50 1 2 415
    22.10.2014. 22:00:10 1 2 642
    23.10.2014. 06:00:00 1 1 9876
    23.10.2014. 09:13:00 1 1 223
    23.10.2014. 13:50:17 1 1 7768
    23.10.2014. 19:47:40 1 1 3456
    23.10.2014. 21:30:00 1 1 789
    23.10.2014. 23:57:12 1 1 25
    23.10.2014. 06:00:07 1 2 976
    23.10.2014. 09:13:45 1 2 223
    23.10.2014. 13:50:40 1 2 78
    23.10.2014. 19:47:55 1 2 346
    23.10.2014. 21:30:03 1 2 89
    23.10.2014. 23:57:18 1 2 25
    24.10.2014. 06:00:55 1 1 346
    24.10.2014. 12:30:22 1 1 8329
    24.10.2014. 23:50:19 1 1 2225
    24.10.2014. 06:01:00 1 2 3546
    24.10.2014. 12:30:26 1 2 89
    24.10.2014. 23:51:10 1 2 25
    Let's say the period is 21.10.2014. - 23.10.2014. and I want to get last recorded balance for box 1. for each day. The result should look like this:
    Date Time Location Box Balance
    21.10.2014. 15:00:34 1 1 1237
    22.10.2014. 22:00:00 1 1 6742
    23.10.2014. 23:57:12 1 1 25
    So far I've managed to write a query that gives me balance for ONLY ONE date (date with highest time in whole table), but I need balance for EVERY date in specific period.
    My incorrect code (didn't manage to implement "BETWEEN" for dates...):
    SELECT TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
    FROM TestTable
    WHERE Time=(SELECT MAX(Time)
    FROM TestTable
    WHERE Location=1 AND Box=1 );
    Tnx!
    Solved!
    Go to Solution.

    For loop
    following query keep day (here 24 in below query) Variable from ( 1 to 28-29/30/31 as per month)
    SELECT TOP 1 TestTable.[Date], TestTable.[Time], TestTable.[Location], TestTable.[Box], TestTable.[Balance]
    FROM Test Table.
    WHERE  Time=(SELECT MAX(Time) FROM TestTable WHERE Location=1 AND Box=1 )
    AND DATE = "2014-10-24";
    PBP (CLAD)
    Labview 6.1 - 2014
    KUDOS ARE WELCOMED.
    If your problem get solved then mark as solution.

  • Select last value for each day from table

    Hi!
    I have a table that stores several measures for each day. I need two queries against this table and I am not quite sure how to write them.
    The table stores these lines (sample data)
    *DateCol1                 Value       Database*
    27.09.2009 12:00:00       100           DB1
    27.09.2009 20:00:00       150           DB1
    27.09.2009 12:00:00       1000          DB2
    27.09.2009 20:00:00       1100          DB2
    28.09.2009 12:00:00       200           DB1
    28.09.2009 20:00:00       220           DB1
    28.09.2009 12:00:00       1500          DB2
    28.09.2009 20:00:00       2000          DB2Explanation of data in the sample table:
    We measure the size of the data files belonging to each database one or more times each day. The value column shows the size of the database files for each database at a given time (European format for date in DateCol1).
    What I need:
    Query 1:
    The query should return the latest measurement for each day and database. Like this:
    *DateCol1       Value      Database*
    27.09.2009        150          DB1
    27.09.2009       1100          DB2
    28.09.2009        220          DB1
    28.09.2009       2000          DB2Query 2:
    The query should return the average measurement for each day and database. Like this:
    *DateCol1       Value      Database*
    27.09.2009       125          DB1
    27.09.2009      1050          DB2
    28.09.2009       210          DB1
    28.09.2009      1750          DB2Could someone please help me to write these two queries?
    Please let me know if you need further information.
    Edited by: user7066552 on Sep 29, 2009 10:17 AM
    Edited by: user7066552 on Sep 29, 2009 10:17 AM

    For first query you can use analytic function and solve it.
    with t
    as
    select to_date('27.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss') dt,       100 val,           'DB1' db from dual union all
    select to_date('27.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       150,           'DB1' from dual union all
    select to_date('27.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss'),       1000,          'DB2' from dual union all
    select to_date('27.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       1100,          'DB2' from dual union all
    select to_date('28.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss'),       200,           'DB1' from dual union all
    select to_date('28.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       220,           'DB1' from dual union all
    select to_date('28.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss'),       1500,          'DB2' from dual union all
    select to_date('28.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       2000,          'DB2' from dual
    select dt, val, db
      from (
    select row_number() over(partition by trunc(dt), db order by dt) rno,
           count(*) over(partition by trunc(dt), db) cnt,
           t.*
      from t)
    where rno = cntFor second you can just group by
    with t
    as
    select to_date('27.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss') dt,       100 val,           'DB1' db from dual union all
    select to_date('27.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       150,           'DB1' from dual union all
    select to_date('27.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss'),       1000,          'DB2' from dual union all
    select to_date('27.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       1100,          'DB2' from dual union all
    select to_date('28.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss'),       200,           'DB1' from dual union all
    select to_date('28.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       220,           'DB1' from dual union all
    select to_date('28.09.2009 12:00:00', 'dd.mm.yyyy hh24:mi:ss'),       1500,          'DB2' from dual union all
    select to_date('28.09.2009 20:00:00', 'dd.mm.yyyy hh24:mi:ss'),       2000,          'DB2' from dual
    select trunc(dt) dt, avg(val) val, db
      from t
    group by trunc(dt), db
    order by trunc(dt)

  • Get bit value for each pixel.

    i use the 16bit image so i need to get the bit value for each pixel. hope someone knows it can help me. thanks.

    Get the BufferedImage Object and do a getData() on it . This returns a Raster object. The Raster Object has methods for retrieving the pixel information.
    hope this helps.

  • SQL help: return number of records for each day of last month.

    Hi: I have records in the database with a field in the table which contains the Unix epoch time for each record. Letz say the Table name is ED and the field utime contains the Unix epoch time.
    Is there a way to get a count of number of records for each day of the last one month? Essentially I want a query which returns a list of count (number of records for each day) with the utime field containing the Unix epoch time. If a particular day does not have any records I want the query to return 0 for that day. I have no clue where to start. Would I need another table which has the list of days?
    Thanks
    Ray

    Peter: thanks. That helps but not completely.
    When I run the query to include only records for July using a statement such as following
    ============
    SELECT /*+ FIRST_ROWS */ COUNT(ED.UTIMESTAMP), TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY') + (ED.UTIMESTAMP/86400)), 'MM/DD') AS DATA
    FROM EVENT_DATA ED
    WHERE AGENT_ID = 160
    AND (TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY')+(ED.UTIMESTAMP/86400)), 'MM/YYYY') = TO_CHAR(SYSDATE-15, 'MM/YYYY'))
    GROUP BY TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY') + (ED.UTIMESTAMP/86400)), 'MM/DD')
    ORDER BY TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY') + (ED.UTIMESTAMP/86400)), 'MM/DD');
    =============
    I get the following
    COUNT(ED.UTIMESTAMP) DATA
    1 07/20
    1 07/21
    1 07/24
    2 07/25
    2 07/27
    2 07/28
    2 07/29
    1 07/30
    2 07/31
    Some dates donot have any records and so no output. Is there a way to show the missing dates with a COUNT value = 0?
    Thanks
    Ray

  • Issue with Report to get Position value for each Class ID.

    Hello Experts,
    m working on a report where I want Position values as on date.
    for an entered date report should give position value(Amount for transaction) for each class Id.
    I m trying using the exit variable but could not reach the output.
    For date I created an exit variable which shoud return the latest position for each class Id.
    Now this exit variable is "ready for Input" . when I debug my code it takes i_step =3 and i_vanm= " " after i_step = 1 and i_vnam = "<variablename>"
    how do I go about to get the solution.
    Guide me to come up with desired output.
    Thanks in Adv.
    Bhavna.

    Hello Experts,
    m working on a report where I want Position values as on date.
    for an entered date report should give position value(Amount for transaction) for each class Id.
    I m trying using the exit variable but could not reach the output.
    For date I created an exit variable which shoud return the latest position for each class Id.
    Now this exit variable is "ready for Input" . when I debug my code it takes i_step =3 and i_vanm= " " after i_step = 1 and i_vnam = "<variablename>"
    how do I go about to get the solution.
    Guide me to come up with desired output.
    Thanks in Adv.
    Bhavna.

  • Date for first day of current month

    How can i get the date for first day of current month ?

    select trunc(sysdate,'MM'),to_char(trunc(sysdate,'MM'),'DD'),to_char(trunc(sysdate,'MM'),'Day') from dual;

  • How to Get Missing Dates for Each Support Ticket In My Query?

    Hello -
    I'm really baffled as to how to get missing dates for each support ticket in my query.  I did a search for this and found several CTE's however they only provide ways to find missing dates in a date table rather than missing dates for another column
    in a table.  Let me explain a bit further here -
    I have a query which has a list of support tickets for the month of January.  Each support ticket is supposed to be updated daily by a support rep, however that isn't happening so the business wants to know for each ticket which dates have NOT been
    updated.  So, for example, I might have support ticket 44BS which was updated on 2014-01-01, 2014-01-05, 2014-01-07.  Each time the ticket is updated a new row is inserted into the table.  I need a query which will return the missing dates per
    each support ticket.
    I should also add that I DO NOT have any sort of admin nor write permissions to the database...none at all.  My team has tried and they won't give 'em.   So proposing a function or storable solution will not work.  I'm stuck with doing everything
    in a query.
    I'll try and provide some sample data as an example -
    CREATE TABLE #Tickets
    TicketNo VARCHAR(4)
    ,DateUpdated DATE
    INSERT INTO #Tickets VALUES ('44BS', '2014-01-01')
    INSERT INTO #Tickets VALUES ('44BS', '2014-01-05')
    INSERT INTO #Tickets VALUES ('44BS', '2014-01-07')
    INSERT INTO #Tickets VALUES ('32VT', '2014-01-03')
    INSERT INTO #Tickets VALUES ('32VT', '2014-01-09')
    INSERT INTO #Tickets VALUES ('32VT', '2014-01-11')
    So for ticket 44BS, I need to return the missing dates between January 1st and January 5th, again between January 5th and January 7th.  A set-based solution would be best.
    I'm sure this is easier than i'm making it.  However, after playing around for a couple of hours my head hurts and I need sleep.  If anyone can help, you'd be a job-saver :)
    Thanks!!

    CREATE TABLE #Tickets (
    TicketNo VARCHAR(4)
    ,DateUpdated DATETIME
    GO
    INSERT INTO #Tickets
    VALUES (
    '44BS'
    ,'2014-01-01'
    INSERT INTO #Tickets
    VALUES (
    '44BS'
    ,'2014-01-05'
    INSERT INTO #Tickets
    VALUES (
    '44BS'
    ,'2014-01-07'
    INSERT INTO #Tickets
    VALUES (
    '32VT'
    ,'2014-01-03'
    INSERT INTO #Tickets
    VALUES (
    '32VT'
    ,'2014-01-09'
    INSERT INTO #Tickets
    VALUES (
    '32VT'
    ,'2014-01-11'
    GO
    GO
    SELECT *
    FROM #Tickets
    GO
    GO
    CREATE TABLE #tempDist (
    NRow INT
    ,TicketNo VARCHAR(4)
    ,MinDate DATETIME
    ,MaxDate DATETIME
    GO
    CREATE TABLE #tempUnUserdDate (
    TicketNo VARCHAR(4)
    ,MissDate DATETIME
    GO
    INSERT INTO #tempDist
    SELECT Row_Number() OVER (
    ORDER BY TicketNo
    ) AS NROw
    ,TicketNo
    ,Min(DateUpdated) AS MinDate
    ,MAx(DateUpdated) AS MaxDate
    FROM #Tickets
    GROUP BY TicketNo
    SELECT *
    FROM #tempDist
    GO
    -- Get the number of rows in the looping table
    DECLARE @RowCount INT
    SET @RowCount = (
    SELECT COUNT(TicketNo)
    FROM #tempDist
    -- Declare an iterator
    DECLARE @I INT
    -- Initialize the iterator
    SET @I = 1
    -- Loop through the rows of a table @myTable
    WHILE (@I <= @RowCount)
    BEGIN
    --  Declare variables to hold the data which we get after looping each record
    DECLARE @MyDate DATETIME
    DECLARE @TicketNo VARCHAR(50)
    ,@MinDate DATETIME
    ,@MaxDate DATETIME
    -- Get the data from table and set to variables
    SELECT @TicketNo = TicketNo
    ,@MinDate = MinDate
    ,@MaxDate = MaxDate
    FROM #tempDist
    WHERE NRow = @I
    SET @MyDate = @MinDate
    WHILE @MaxDate > @MyDate
    BEGIN
    IF NOT EXISTS (
    SELECT *
    FROM #Tickets
    WHERE TicketNo = @TicketNo
    AND DateUpdated = @MyDate
    BEGIN
    INSERT INTO #tempUnUserdDate
    VALUES (
    @TicketNo
    ,@MyDate
    END
    SET @MyDate = dateadd(d, 1, @MyDate)
    END
    SET @I = @I + 1
    END
    GO
    SELECT *
    FROM #tempUnUserdDate
    GO
    GO
    DROP TABLE #tickets
    GO
    DROP TABLE #tempDist
    GO
    DROP TABLE #tempUnUserdDate
    Thanks, 
    Shridhar J Joshi 
    <If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>

  • How to get Privacy Policy value for each userprofile in sharepoint 2010?

    In userprofile application, we have defined Privacy Policy of mobilephone feild as optional, so every user has option to choose visibility scope of this property to "Everyone/My Manager/my colegues/Only Me" .
    Now I am trying to get mobilephone value and their selected visibility option for each user.
    I am able to get mobilephone value but I could not get "what each user has chosen as their visibility scope"?

    Hi,
    According to your post, my understanding is that you want to get Privacy Policy value for each userprofile in sharepoint 2010.
    You need to use RunWithElevatedPrivileges method to impersonate user.
    To get the get mobilephone policy, you can use user["CellPhone "].Privacy.
    For more information, you can refer to:
    c# - Getting property privacy with Sharepoint 2010
    How to Programmatically Impersonate Users in SharePoint
    Managing Sharepoint 2010 Profiles Programmatically
    Thanks,
    Linda Li                
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Linda Li
    TechNet Community Support

  • I need a query that selects the amount of records for each day in a table.

    I need a query that selects the amount of records for each
    day in a table.
    Eg the result would be:
    date 1 14
    date 2 3
    etc
    Any ideas?

    sorted:
    SELECT count([commentID]),convert(varchar, dateAdded, 112)
    FROM COMMENTSgroup by convert(varchar, dateAdded,
    112)

  • Find out and get the bit value for each pixel in image

    i use 16bit image. the image is 200x200 pixels. i need to know what is
    the bit value for each pixel. example(0111111011111101).
    where i can see the bit value output. if somebody got the example surce code please send it to me.coz i need the bit value to find out how many RGB color that use in the image. thanks for ur help.

    How many logins do you have???

  • Get the Count for each row

    I'm trying to get the count for each row to total count for each month
    Something like this
    Hardware     |      Jan
    Monitors       |       5
    Processors   |      137
    Printers        |      57
    etc........
    How can I write a query for this. I can get the Hardware column but don't know how to get the next column.

    If you can provide more data like sample input DML statements it would have been wonderful..
    Assuming is , you need a pivot. Here is an article on basic Pivot..
    http://sqlsaga.com/sql-server/how-to-use-pivot-to-transform-rows-into-columns-in-sql-server/
    something like this may be..
    DECLARE @Input TABLE
    Hardware VARCHAR(20),
    [Date] VARCHAR(20)
    INSERT INTO @Input VALUES('Monitor', '01/01/2014'), ('CPU', '01/01/2014'), ('Monitor', '01/03/2014')
    , ('ABC', '01/01/2014'),('Monitor', '02/01/2014')
    ;WITH CTE AS
    SELECT Hardware, LEFT(DATENAME(M, [Date]),3) AS [MonthName] FROM @Input
    SELECT *
    FROM
    SELECT Hardware, [MonthName], COUNT(Hardware) AS Count FROM CTE GROUP BY Hardware, [MonthName]) a
    PIVOT (MAX([Count]) FOR [MonthName] IN ([Jan], [Feb])) pvt
    Please mark as answer, if this has helped you solve the issue.
    Good Luck :) .. visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.

  • XML DB: is it possible to get a row for each element in a container element?

    I have an XML document containing a container element (collection). If I query, using an XPath expression, the contained elements I get a row for each container element with the contained element concatenated. Is it possible to get a row for each contained element?
    I run this simple query:
    select extract(xmltype('<colors><color>Red</color><color>Green</color></colors>')
    , '/colors/color/text()').getstringval() from dual
    And get this result:
    EXTRACT(XMLTYPE('<COLORS><COLOR>RED</COLOR><COLOR>GREEN</COLOR></COLORS>'),'/COL
    RedGreen
    1 row selected.
    What I would like to have is:
    Red
    Green
    2 rows selected.
    Wishful thinking or possible? Many thanks!

    Sure. This is where our XMLSequence() function comes in. It allows you to treat the top-level nodes in a nodeset as if they were rows in a table when combined with the TABLE() operator. Here's an example.
    First, to make the SQL look a little cleaner, I like to define a function like this:
    create or replace function testdoc return xmltype as
    begin
      return xmltype('<colors><color>Red</color><color>Green</color></colors>');
    end;.
    To break out the nodeset of <color> elements as a table, we use the following query:
    select value(list_of_color_elements).extract('*/text()').getStringVal() as color
    from TABLE( XMLSequence( extract(testdoc(),'/colors/color'))) list_of_color_elements.
    Or, using the new-in-9.2 extractValue() operator so we don't have to remember the text() part:
    select extractValue( value(list_of_color_elements), '.') as color
    from TABLE( XMLSequence( extract( testdoc() ,'/colors/color'))) list_of_color_elements.
    Here the TABLE(XMLSequence(...)) combo produces a table of XMLType, with one XMLType object in each row of the table.
    In general, if the XMLType instance were coming from an XMLType table xmltab the query would look like this:
    select extractValue( value(colors), '.') as color
    from xmltab x, /* Important that this table comes earlier in the FROM clause! */
         TABLE( XMLSequence( extract( value(x),'/colors/color'))) colors.
    And if the XMLType were instead in a column of XMLType named doc in a table xmltab, then we would have the syntax:
    select extractValue( value(colors), '.') as color
    from xmltab x, /* Important that this table comes earlier in the FROM clause! */
         TABLE( XMLSequence( extract( x.doc ,'/colors/color'))) colorsOnce you get the hang of it, you'll see that the combination of TABLE(XMLSequence()) to "shred" XML nodes into rows, and XMLAgg() to aggregate fragments of XML across multiple rows back into a single document, is quite powerful.

Maybe you are looking for