473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question on Creating Placeholder Rows

6 New Member
OK, so here’s the problem.
I have a flat table which has summary information for each department.
The only possible statuses are 1, 0 and -1.
When I run my plain vanilla SQL query I get something like this:

DEPT COUNT PERCENT TOTAL STATUS COMPLIANCY
Dept1 18 81.8 22 1 Compliant
Dept1 1 4.5 22 0 Non Compliant
Dept1 3 13.6 22 -1 Unknown
Dept2 6 85.7 7 1 Compliant
Dept2 1 14.3 7 -1 Unknown
Dept3 1 50 2 1 Compliant
Dept3 1 50 2 0 Non Compliant
Dept4 1 100 1 1 Compliant


Whenever there are records for all statuses, three rows are displayed.
But if a department does not have a particular status, then there is no row presented (obviously).
I would like to create ‘dummy’ rows in the result for those department statuses which are not present in the table. This is necessary so that I can export the results to Excel for further analysis i.e. pivots tables. Note that the “Compliancy” field is a Case statement based on Status. I’m not looking to update the table, just the result of the query.

For example, Dept1 in the above sample has all three statuses, so no rows need to be created.

Dept2 is missing a status 0 row, so I need to create a row such as:

Dept2 0 0 0 0

Likewise Dept3 needs a dummy row such as:

Dept3 0 0 0 -1

Dept4 needs 2 dummy rows such as:

Dept4 0 0 0 0
Dept4 0 0 0 -1

I don’t need a full solution, but if someone can provide the logic, that would be helpful. Though not necessary, if there’s a way to copy the “Total” amounts to the new rows that would be great.

Thanks in advance,

PaulT
Nov 29 '10 #1
5 2724
Oralloy
988 Recognized Expert Contributor
Paul,

Create a table of department/statuses and outer join that to your data. That will give you NULL values for the undefined fields.

Alternately, you can create a query that builds the necessary rows from a table of statuses and departments, and then join that to your basic query using a union. Something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT DEPT, COUNT, PERCENT, TOTAL, STATUS, COMPLIANCY
  2.   FROM StatusTable
  3. UNION
  4. SELECT std.DEPT, 0, 0, 0, sts.STATUS, 0
  5.   FROM (SELECT DISTINCT DEPT FROM StatusTable) std,
  6.        (SELECT DISTINCT STATUS FROM StatusTable) sts
  7.   WHERE (0 =
  8.          (SELECT ANY(*)
  9.             FROM StatusTable
  10.             WHERE (DEPT = std.DEPT)
  11.                   AND (STATUS = sts.STATUS))))
At least that's where I'd building the query at.

Please let us know what you come up with.

Good luck!
Nov 30 '10 #2
Paul Tennis
6 New Member
Thanks Oralloy,
I ended up linking to the Oracle table in Access through ODBC and creating a table of dummy records in Access, then running a query taking all the Oracle table records and a UNION to select those dummy records whose department/status was not one of the Oracle records. It worked fine, perhaps not the most elegant, but it worked. Thanks for pointing me in the right direction.

PaulT
Dec 3 '10 #3
amitpatel66
2,367 Recognized Expert Top Contributor
Also you can try this:

Expand|Select|Wrap|Line Numbers
  1. SQL> ed
  2. Wrote file afiedt.buf
  3.  
  4.   1  with t as (select 'dept1' col1,1 col2,'Compliant' col3 from dual UNION
  5.   2  select 'dept1',0,'Non Compliant' from dual UNION
  6.   3  select 'dept1',-1,'Unknown' from dual
  7.   4  UNION select 'dept2',-1,'Unknown' from dual
  8.   5  UNION select 'dept2',1,'Compliant' from dual
  9.   6  UNION select 'dept3',1,'Compliant' from dual)
  10.   7  select x.col1,x.num,t.col3 FROM
  11.   8  (SELECT distinct col1,num from t,(SELECT 0 num FROM Dual
  12.   9  union select 1 from dual
  13.  10  union select -1 from dual)) x,t
  14.  11  WHERE t.col1(+) = x.col1
  15.  12  AND   t.col2(+) = x.num
  16.  13* order by x.col1,x.num DESC
  17. SQL> /
  18.  
  19. COL1         NUM COL3
  20. ----- ---------- -------------
  21. dept1          1 Compliant
  22. dept1          0 Non Compliant
  23. dept1         -1 Unknown
  24. dept2          1 Compliant
  25. dept2          0
  26. dept2         -1 Unknown
  27. dept3          1 Compliant
  28. dept3          0
  29. dept3         -1
  30.  
  31. 9 rows selected.
  32.  
  33. SQL> 
  34.  
Dec 7 '10 #4
Paul Tennis
6 New Member
Oralloy,
I've been dissecting your SQL example to try to learn from it. I've never used the ANY operator. When I adapted your query I get an error which I think it occurring around this part:
WHERE (0 =
(SELECT ANY(*)

ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 7 Column: 11

Any insight is appreciated.
PaulT
Dec 8 '10 #5
Oralloy
988 Recognized Expert Contributor
Paul,

I did my example in Access 2007, so there may be an SQL disconnect between our dialects. More on this in a sec.

To answer your first question, the ANY aggregate function returns 0 if no rows match the WHERE clause, and 1 if any row at all matches the where clause. It's like the COUNT aggregate function, except that it allows the server to avoid actually counting all matches.

Another thing that may be biting you is my use of an embedded SELECT in the WHERE clause. Again, Access lets me do this, Oracle may have a problem with it. One way to test is to replace the SELECT with a constant or a simple calculation, just to see if the code gets past the parser.

Let me know what happens. Once we know the cause of the trouble, then we can modify the query to work properly.

Luck!
Oralloy
Dec 8 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pjotr Wedersteers | last post by:
Hi, I am rather new to PHP and a relative newbie to Linux too. I have a webserver at home (Apache 2.0.48 on SuSe 9.0, PHP4). I have some scripts, one of them needs to be able to create logfiles...
1
by: Justin | last post by:
Hi, In the process of localizing the 'regions' table, we added three new tables. The localized data will be stored in the TokenKeys and TokenValues tables. It would be easier if we did away with...
23
by: jeffc | last post by:
Let's say you're in some function , and you want to accomplish some task x that is provided for you by a class, A. Which of these do you prefer, and is there a technical reason for it? (This is...
8
by: Piro | last post by:
I have a class that I want to make accessible to a web service. This class does some work in its constructor method and sets some class variables in its various methods. The problem I am having...
0
by: Bobby | last post by:
Hello, I've a table 'A' with a reference to another table 'B'. I've created a form with A's fields, and a subform that displays B's fields, with a link between them in the subform properties...
0
by: simtin | last post by:
Hi guys. I'm using a DataSet, a OdbcDataAdapter, beeing constructed by pubDA_URL = new OdbcDataAdapter()( new OdbcCommand("select * from urls where project=" + id, pubDB) );
2
by: jm | last post by:
I have a Form1 and two class files. In the first class, I call the second class. The second class references the Form1 notifyicon and changes the icon. It works. The problems is, however,...
2
by: Al_C | last post by:
Hve been handed the task of taking a reference USB driver and making it work for our product. Have a lot of ANSI C experience, and fair bit of VB express, but VC++ express is a different critter....
6
by: JohnDoe528 | last post by:
In Access 2000, I have a table with the following columns: ID(pk); DateTime; CallerID; DNIS; AcctNumber1; ... AcctNumber15; The final 15 columns have different account numbers listed for the...
4
by: kageyone | last post by:
I have an access database with a table with two fields. They are 1. a GUID field and 2. a comma delimited set of values. I want to take this table and for each row I want to do the following: ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.