5

What I have is a query, where I am selecting locations, counts, etc. Here is my query: And, I am trying to basically PIVOT the tables I believe. I have looked into PIVOT and such, but it doesn't seem like there is a clear cut way to do it. If any help can be guided would be greatly appreciated.

Updated Query to include type and MT0 Suggestion

 WITH qry AS (
select Floor, 
"Mod", 
Count_Type, 
Remaining_Counts, 
Location, 
Floor || '' || "Mod" || '' || Count_Type || '' ||  Location as "Unique"
from
(
select bin_level as Floor, bin_module as "Mod", icqa_process_properties.icqa_process_property_value as Count_Type, count(icqa_processes.icqa_process_id) as Remaining_Counts, 
CASE when bin_type_name = '14-KIVA-DEEP' then ('KIVA-SHELF')  
     when bin_type_name = '18-KIVA-DEEP' then ('KIVA-SHELF')
     when bin_type_name = '24-KIVA-DEEP' then ('KIVA-SHELF')
     when bin_type_name = '30-KIVA-DEEP' then ('KIVA-SHELF')
     when bin_type_name = '34-KIVA-DEEP' then ('KIVA-SHELF')  
     when bin_type_name = '48-KIVA-DEEP' then ('KIVA-SHELF')
     when bin_type_name = '48-KIVA-XL' then ('KIVA-SHELF')
     when bin_type_name = '78-KIVA-TALL' then ('KIVA-SHELF')
     when bin_type_name = 'PALLET-SINGLE' and usage = '1024' then ('KIVA-PALLET')  
     else 'NON-KIVA' end as Location
from icqa_process_locations
join bins on bins.bin_id = icqa_process_locations.scannable_id
inner join icqa_processes on icqa_processes.icqa_process_id = icqa_process_locations.icqa_process_id
inner join icqa_process_properties on icqa_processes.icqa_process_id = icqa_process_properties.icqa_process_id
--inner join icqa_count_attempts on icqa_count_attempts.icqa_count_attempt_id = icqa_process_locations.icqa_count_attempt_id
where icqa_process_locations.icqa_count_attempt_id is NULL 
     and icqa_processes.process_status = ('Active')     
     and icqa_process_properties.icqa_process_property_value in ('CycleCount', 'SimpleBinCount')
group by CASE when bin_type_name = '14-KIVA-DEEP' then ('KIVA-SHELF')  
    when bin_type_name = '18-KIVA-DEEP' then ('KIVA-SHELF')
    when bin_type_name = '24-KIVA-DEEP' then ('KIVA-SHELF')
    when bin_type_name = '30-KIVA-DEEP' then ('KIVA-SHELF')
    when bin_type_name = '34-KIVA-DEEP' then ('KIVA-SHELF')  
    when bin_type_name = '48-KIVA-DEEP' then ('KIVA-SHELF')
    when bin_type_name = '48-KIVA-XL' then ('KIVA-SHELF')
    when bin_type_name = '78-KIVA-TALL' then ('KIVA-SHELF')
    when bin_type_name = 'PALLET-SINGLE' and usage = '1024' then ('KIVA-PALLET')  
    else 'NON-KIVA' end, bin_level, bin_module, icqa_process_properties.icqa_process_property_value
order by icqa_process_properties.icqa_process_property_value, Location))
SELECT Count_Type || Location,
       SUM(CASE when "Mod" = 'dz-P-1A' THEN Remaining_Counts else 0 END ) AS "P-1-A",
       SUM(CASE when "Mod" = 'dz-P-2A' THEN Remaining_Counts else 0 END ) AS "P-2-A",
       SUM(CASE when "Mod" = 'dz-R-1T' THEN Remaining_Counts else 0 END ) AS "R-1-T",
       SUM(CASE when "Mod" = 'dz-R-1F' THEN Remaining_Counts else 0 END ) AS "R-1-F",
       SUM(CASE when "Mod" = 'dz-R-1O' THEN Remaining_Counts else 0 END ) AS "R-1-O",
       SUM(CASE when "Mod" = 'dz-P-1B' THEN Remaining_Counts else 0 END ) AS "P-1-B",
       SUM(CASE when "Mod" = 'dz-P-1D' THEN Remaining_Counts else 0 END ) AS "P-1-D"
FROM   qry
GROUP BY Count_Type || Location;

And the output is this(ALMOST THERE!):

enter image description here

But, it is producing Zero's When I tried to add type. Before I added the types it worked fine, but I may have missed syntax somewhere. Thanks.

11
  • 1
    What version of Oracle? Any chance you can create a sql fiddle with your tables and some sample data? Commented Dec 12, 2013 at 18:31
  • 1
    What @bluefeet you don't want to do that? :) Commented Dec 12, 2013 at 18:32
  • SQL Developer 3.2.20.09.87, and I'll check out sqlfiddle and see if I can make one. I haven't used sql fiddle before. Commented Dec 12, 2013 at 18:35
  • @Spartacus38 this is the version of your client tool. bluefeet mean the Oracle server version. Commented Dec 12, 2013 at 18:37
  • Whooaaahohoho! Hold on a minute there guy. March 1983: Oracle Database is rewritten in C for portability and Oracle version 3 is released. This might be part of your problem. Your database software is older than I am by three years. I think it might be time to upgrade. Commented Dec 12, 2013 at 18:39

2 Answers 2

5

You can do something like this:

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE tbl ( "mod", "floor", "Remaining Counts", CountType ) AS
          SELECT 'dz-P-1A', 1,    37, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-1D', 1,   321, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-1T', 1,    16, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-2A', 2,    25, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-R-1T', 1,  3318, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-1A', 1,  6351, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-P-1D', 1,   121, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-P-2A', 2, 12638, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-R-1F', 1,    68, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-R-1O', 1,    47, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-R-1T', 1,  2051, 'SimpleBinCount' FROM DUAL;

Query 1:

PIVOT using SUM( CASE ... ) statements:

SELECT CountType,
       SUM( CASE "mod" WHEN 'dz-P-1A' THEN "Remaining Counts" END ) AS "P-1-A",
       SUM( CASE "mod" WHEN 'dz-P-2A' THEN "Remaining Counts" END ) AS "P-2-A",
       SUM( CASE "mod" WHEN 'dz-R-1T' THEN "Remaining Counts" END ) AS "R-1-T",
       SUM( CASE "mod" WHEN 'dz-R-1F' THEN "Remaining Counts" END ) AS "R-1-F",
       SUM( CASE "mod" WHEN 'dz-R-1O' THEN "Remaining Counts" END ) AS "R-1-O",
       SUM( CASE "mod" WHEN 'dz-P-1B' THEN "Remaining Counts" END ) AS "P-1-B",
       SUM( CASE "mod" WHEN 'dz-P-1D' THEN "Remaining Counts" END ) AS "P-1-D"
FROM   tbl
GROUP BY CountType

Results:

|      COUNTTYPE | P-1-A | P-2-A | R-1-T |  R-1-F |  R-1-O |  P-1-B | P-1-D |
|----------------|-------|-------|-------|--------|--------|--------|-------|
| SimpleBinCount |  6351 | 12638 |  2051 |     68 |     47 | (null) |   121 |
|     CycleCount |    37 |    25 |  3318 | (null) | (null) | (null) |   321 |

Query 2:

Using PIVOT transposition:

SELECT * FROM (
   SELECT SUBSTR( "mod", 4 ) AS "mod",
          "Remaining Counts",
          CountType
   FROM   tbl t
)
PIVOT
(
   SUM("Remaining Counts")
   FOR "mod" IN ( 'P-1A', 'P-2A', 'R-1T', 'R-1F', 'R-1O', 'P-1B', 'P-1D' )
)

Results:

|      COUNTTYPE | 'P-1A' | 'P-2A' | 'R-1T' | 'R-1F' | 'R-1O' | 'P-1B' | 'P-1D' |
|----------------|--------|--------|--------|--------|--------|--------|--------|
| SimpleBinCount |   6351 |  12638 |   2051 |     68 |     47 | (null) |    121 |
|     CycleCount |     37 |     25 |   3318 | (null) | (null) | (null) |    321 |

Edit - Wrapping your query:

WITH qry AS (
  select drop_zone_id as "Mod", 
      bin_level as "Floor",
      icqa_process_properties.icqa_process_property_value as "Count Type",
      count(*) as "Remaining Counts",    
      concat(drop_zone_id, icqa_process_properties.icqa_process_property_value) as "Unique",
      to_char(sysdate,'hh:mi:ssam') as "Time Last Updated",
      to_char(sysdate, 'MM-DD-YYYY') as "Date Last Updated"
  from icqa_process_locations 
      inner join icqa_processes on icqa_processes.icqa_process_id = icqa_process_locations.icqa_process_id
      inner join icqa_process_properties on icqa_processes.icqa_process_id = icqa_process_properties.icqa_process_id
      inner join bins on bins.bin_id = icqa_process_locations.scannable_id
  where icqa_count_attempt_id is NULL and icqa_processes.process_status = ('Active')
      and drop_zone_id not like 'dz-R-1B' and drop_zone_id not like 'dz-P-1Z' and drop_zone_id not like 'dz-P-EACH_1'
      and icqa_process_properties.icqa_process_property_value in ('CycleCount', 'SimpleBinCount') 
  group by icqa_process_properties.icqa_process_property_value, bin_level, drop_zone_id
  order by icqa_process_properties.icqa_process_property_value, drop_zone_id
)
SELECT "Count Type",
       SUM( CASE "Mod" WHEN 'dz-P-1A' THEN "Remaining Counts" END ) AS "P-1-A",
       SUM( CASE "Mod" WHEN 'dz-P-2A' THEN "Remaining Counts" END ) AS "P-2-A",
       SUM( CASE "Mod" WHEN 'dz-R-1T' THEN "Remaining Counts" END ) AS "R-1-T",
       SUM( CASE "Mod" WHEN 'dz-R-1F' THEN "Remaining Counts" END ) AS "R-1-F",
       SUM( CASE "Mod" WHEN 'dz-R-1O' THEN "Remaining Counts" END ) AS "R-1-O",
       SUM( CASE "Mod" WHEN 'dz-P-1B' THEN "Remaining Counts" END ) AS "P-1-B",
       SUM( CASE "Mod" WHEN 'dz-P-1D' THEN "Remaining Counts" END ) AS "P-1-D"
FROM   qry
GROUP BY "Count Type";
Sign up to request clarification or add additional context in comments.

8 Comments

this looks exactly like how I want, and doesn't seem complicated at all, BUT is there a way to do it without creating a new table? Currently our access to the DB is Read-Only. And Will it work with dynamic data?
Just wrap your query in a WITH statement to create a named sub-query and then reference it in place of a table - like: WITH qry AS ( ... <INSERT YOUR QUERY HERE> ... ) SELECT CountType, SUM( CASE "mod" WHEN 'dz-P-1A' THEN "Remaining Counts" END ) AS "P-1-A", ... SUM( CASE "mod" WHEN 'dz-P-1D' THEN "Remaining Counts" END ) AS "P-1-D" FROM qry GROUP BY CountType
Okay, let me see what I can do thanks. On the <INSERT YOUR QUERY HERE> is that my original query or your addition?
See my edit for an example. I don't have any tables/data so the edit is untested but you (hopefully) get the principle.
Updated my question with my new result, I'm almost there!
|
0

If you have access to PLSQL in your application then "user-defined aggregate functions" may be a good way to go. I have found that it provides a nice way to encapsulate transpose functionality and performs very well. I wrote one recently that took massive amounts of interval data (rows) and pivoted them into columns of interval (1-24) by day (rows). It did take a good amount of coding, but I found that the code was clear and easy to maintain.

Here is a link to a list of string aggregation techniques available in Oracle. One of them uses a user_defined function. You essentially do the same things, but return a PLSQL collection of columns instead of an aggregated string:

http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php#user_defined_aggregate_function

I also liked the user-defined aggregate function due to the way Oracle handles the parallellization for performance. Here is more doc in Oracle 11g:

http://docs.oracle.com/cd/B19306_01/appdev.102/b14289/dciaggfns.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.