Can SQL gurus, you create a SQL to do this?

I have a query that brings together the records of 2 columns:

ROOM_DESC Detail_count

Not reserved - clean, 4

Not reserved - salty, 30

Reserved - clean, 8

Reserved - salty, 47

Is what I want, without creating a temporary table, use the SQL existing as an input and output it:

Rooms, 89

Clean, 12

Dirty, 77

Even if getting to the rooms is not technically possible, to get the other 2 total records would be great.

Hello

This looks like a job for the REPORT GROUP.

Here's one way:

WITH original_query AS

(

SELECT...  AS room_desc

,        ...  AS detail_count

...

)

got_room_grp AS

(

SELECT SUBSTR (room_desc
, INSTR (room_desc, "-") + 3
) AS room_grp

detail_count

Of original_query

)

SELECT NVL (room_grp, 'Rooms') AS grp_label

SUM (detail_count) AS detail_count

OF got_room_grp

GROUP OF ROLLUP (room_grp)

ORDER BY NULLS FIRST room_grp

;

According to what did you do in the original request, there might be a much simpler way.  Chances are, you can derive from room_grp without devoting a separate subquery, as I did above.  You might be able to do all the work without any subqueries in all.

If you would care to post CREATE TABLE and INSERT instructions for sample data and your needs, then I could test it and maybe show you a simpler and more efficient way.

The query above assumes that room_desc always contains "-", followed by another text.  If this isn't the case, then you need a slightly more complicated expression for grp_label in the main query:

...

SELECT THE CHECK BOX

WHEN you GROUP (room_grp) = 0

THEN room_grp

ELSE 'rooms '.

END AS grp_label

...

or perhaps other slight modifications, but the general approach here show will still work.

Tags: Database

Similar Questions

Maybe you are looking for