Help in writing a query!
Dear All,
My database is 11gR2 on Linux.
I am struck in writing a difficult query, need help from your guys.
I have a table, an application is controlling the columns of this table. Columns are added by the application.
This is the structure of the table:
create table imran_test(
2 username varchar2(100),
3 layer1 number(1),
4 layer2 number(2),
5 layer3 number(2));
-- where layer1, layer2, layer3.... are increased up to layer 22 and could be increased more...
Now each username will have 1 in any one of the layer, and all other layers will have 0
Like if I consider the above table this could be the sample data:
imran 1 0 0
hafeez 0 1 0
james 0 0 1
Now the result my query should return is:
select username, <column name where value is 1> and value of it, as per sample data, this should be return by the query:
imran layer1 1
hafeez layer2 1
james layer 3 1
Note: Please remember the columns are not fixed, they are added/altered by application.
Regards,
Imran
The table design is incorrect as it is not in 3NF and has a repeating group
You should correct the 'design' first or turn the layer... columns in a VARRAY.
Also my feeling is you should change your subject line 'Help in writing a query!' in 'Write my query' as that is what you actually ask, and leave out the exclamation mark.
Basically you dump everything in this forum, and being a bit less demanding might suit you better.
Also you still act rude by not marking your questions as answered.
Change that
Sybrand Bakker
Senior Oracle DBA
Edited by: sybrand_b on 1-apr-2012 12:09
Similar Messages
-
Need help in writing the query
The initial data is as follows:
ID PARENT_ID
=============
1 NULL
2 NULL
3 2
4 6
5 3
6 7
7 4
8 NULL
but output is as follows in such way that Parent_Id should be displayed in ID place
ID PARENT_ID
=============
1 NULL
2 NULL
3 2
8 NULL
6 8
4 6
5 3
7 4
Can any one help how resolve this query?
Thanks in advanceYour output is inconsistent with initial data :) But
select * from t start with parent_id is null connect by parent_id = prior id; -
Hi,
I have a table with two columns as follows
child parent
mom grandma
daughter mom
How can I write a single query so that the result will be daughter,mom,grandma?
Thanks
Ravi.Responses to these postings will help:
pivot the column names
PIVOT in oracle.
Re: Pivot Query -
Hi evrybody,
I have a requirement which seems very wierd to me, I m unable to clue it also. Please help me in writing the same.
I have a column in a table which contains the data with spaces and everythng same as below:
SUBSCRIBER LINE TEST
VERSION 2
DEV SNB MP FCODE
LIBA-13 4222430012 11 3
FOREIGN VOLTAGE TESTS
DC AC
FVAE FVBE FVAB FVAE FVBE FVAB
LACC LACC 7.4 0.1 0.1 0.1
0.0 0.0 PASS PASS PASS PASS
PASS PASS
INSULATION RESISTANCE TESTS
IRAE IRBE IRABA IRBBA IRAB
LACC LACC LACC LACC 8113K
10000K >10000K >10000K >10000K PASSPASS PASS PASS PASS
CAPACITANCE MEASUREMENTS
CAE CBE CAB BRK
LACC LACC 4428 NO
68 72
NT LOOPBACK TEST
PASS
NT DC SIGNATURE TEST
V-HIGH V-LOW
83.920K LACC
PASS >10000K
PASS
NT CONNECTED
INV
CIRCUIT TEST RESULT
FAIL
I need to get the o/p as:
FVAELACC=0.0 FVBELACC=0.0 FVAB=7.4 FVAE=0.1 FVBE=0.1 FVAB=0.1
CAELACC=68 CBELACC=72 CAB=4428
NT CONNECTED=INV..
Thanks in advance.Evrybody thanks for advices. My Question is little unclear but the requirement is that itself.. Anyways I got the O/p finally.
Mr. Shahzad ,if any of the questions makes u feel like wasting ur technical time, jus donot read it.. The people who can undertsand the problem, they can answer to the post. If the requirement is like that, none of us are helpless. -
Need help with writing a query with dynamic FROM clause
Hi Folks,
I need help with an query that should generate the "FROM" clause dynamically.
My main query is as follows
select DT_SKEY, count(*)
from *???*
where DT_SKEY between 20110601 and 20110719
group by DT_SKEY
having count(*) = 0
order by 1; The "from" clause of the above query should be generated as below
select 'Schema_Name'||'.'||TABLE_NAME
from dba_tables
where OWNER = 'Schema_Name'Simply sticking the later query in the first query does not work.
Any pointers will be appreciated.
Thanks
rogers42Hi,
rogers42 wrote:
Hi Folks,
I need help with an query that should generate the "FROM" clause dynamically.
My main query is as follows
select DT_SKEY, count(*)
from *???*
where DT_SKEY between 20110601 and 20110719
group by DT_SKEY
having count(*) = 0
order by 1; The "from" clause of the above query should be generated as below
select 'Schema_Name'||'.'||TABLE_NAME
from dba_tables
where OWNER = 'Schema_Name'
Remember that anything inside quotes is case-sensitive. Is the owner really "Schema_Name" with a capital S and a capital N, and 8 lower-case letters?
Simply sticking the later query in the first query does not work.Right; the table name must be given when you compile the query. It's not an expression that you can generate in the query itself.
Any pointers will be appreciated.In SQL*Plus, you can do something like the query bleow.
Say you want to count the rows in scott.emp, but you're not certain that the name is emp; it could be emp_2011 or emp_august, or anything else that starts with e. (And the name could change every day, so you can't just look it up now and hard-code it in a query that you want to run in the future.)
Typically, how dynamic SQL works is that some code (such as a preliminary query) gets some of the information you need to write the query first, and you use that information in a SQL statement that is compiled and run after that. For example:
-- Preliminary Query:
COLUMN my_table_name_col NEW_VALUE my_table_name
SELECT table_name AS my_table_name_col
FROM all_tables
WHERE owner = 'SCOTT'
AND table_name LIKE 'E%';
-- Main Query:
SELECT COUNT (*) AS cnt
FROM scott.&my_table_name
;This assumes that the preliminary query will find exactly one row; that is, it assumes that SCOTT has exactly one table whose name starts with E. Could you have 0 tables in the schema, or more than 1? If so, what results would you want? Give a concrete example, preferably suing commonly available tables (like those in the SCOTT schema) so that the poepl who want to help you can re-create the problem and test their ideas.
Edited by: Frank Kulash on Aug 11, 2011 2:30 PM -
Help required writing a query!
Dear All,
My database is 11gR2 on Linux.
I have data like this:
ID Name
1 Mike
2 Mike Peter
3 Steven
4 Steven Andrew
In a select query I want to get the output
1 Mike
3 Steven
Logic, if it find next match for like for the first name, skip record, even if it contains second name
Kindly help.
Regards, ImranHi Imran,
This is defintely not a nobel price, but it returned what your specified.
WITH t1
AS (SELECT 1 AS id, 'Mike' AS name FROM DUAL UNION
SELECT 2, 'Mike Peter' FROM DUAL UNION
SELECT 3, 'Steven' FROM DUAL UNION
SELECT 4, 'Steven Andrew' FROM DUAL)
SELECT *
FROM t1
WHERE name IN (SELECT SUBSTR (name, 1, INSTR (name, ' ', 1) - 1) FROM t1)HTH,
Thierry -
Help on writing this query.
Hi Folks,
I'm trying to write a query to retrive the following information regarding snapshots. I tried lot of ways and couldn't get the solutions so seeking your help...
Can you send me a query that will give me all this information.
db name of snapshot site
snapshot name and their base table
refresh type (if it is fast, please list he snapshot log used)
refresh group which snapshot belongs to
dbms_job number which perform the refresh, job current status
refresh frequency
last refresh time
thanks
KarthikIf you tried a lot of ways ... post your work and show us what you have done.
What you are being asked to do is query data dictionary views. Do you know which ones?
SELECT view_name
FROM all_views
WHERE view_name LIKE '%<you fill this in>%';That is all the hints you get until you show your work. -
Need help in writing inline query or subquery
Hi All,
I have 4 select statements like below ,
1>Select customer_number, customer_name,customer_address1
FROM ABC
WHERE Site_use_code='BILL_TO'
2>Select customer_number, customer_name,customer_address1
FROM ABC
WHERE Site_use_code='SHIP_TO'
3>Select customer_number, customer_name,customer_address1
FROM ABC
WHERE Site_use_code='SOLD_TO'
I need to write all these 3 in single select statement in such a way that if any select statement doesnot return any data it should execute the other select statements means it should not fail.
for each column of the select statment i can write separate select statements in that way it should work but the query should be very big.
So please help me on thsi.
ThanksExample of my solution:
SQL> ed
Wrote file afiedt.buf
1 with t as (
2 select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'BILL_TO' as site_use_code from dual union all
3 select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
4 select 2 as customer_number, 'Bob' as customer_name, 'address2' as customer_address1, 'BILL_TO' as site_use_code from dual union all
5 select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'BILL_TO' as site_use_code from dual union all
6 select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
7 select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SOLD_TO' as site_use_code from dual union all
8 select 4 as customer_number, 'Tim' as customer_name, 'address4' as customer_address1, 'BILL_TO' as site_use_code from dual union all
9 select 5 as customer_number, 'Sam' as customer_name, 'address5' as customer_address1, 'BILL_TO' as site_use_code from dual
10 )
11 --
12 -- END OF TEST DATA
13 --
14 select customer_number
15 ,customer_name
16 ,customer_address1
17 ,site_use_code
18 from (
19 select customer_number
20 ,customer_name
21 ,customer_address1
22 ,site_use_code
23 ,row_number() over (partition by customer_number order by decode(Site_use_code,'BILL_TO',1,'SHIP_TO',2,3) desc) as rn
24 FROM t
25 WHERE Site_use_code IN ('BILL_TO','SHIP_TO','SOLD_TO')
26 )
27* where rn = 1
SQL> /
CUSTOMER_NUMBER CUSTOM CUSTOMER SITE_US
1 Fred address1 SHIP_TO
2 Bob address2 BILL_TO
3 George address3 SOLD_TO
4 Tim address4 BILL_TO
5 Sam address5 BILL_TO
SQL>Or alternative...
SQL> ed
Wrote file afiedt.buf
1 with t as (
2 select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'BILL_TO' as site_use_code from dual union all
3 select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
4 select 2 as customer_number, 'Bob' as customer_name, 'address2' as customer_address1, 'BILL_TO' as site_use_code from dual union all
5 select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'BILL_TO' as site_use_code from dual union all
6 select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
7 select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SOLD_TO' as site_use_code from dual union all
8 select 4 as customer_number, 'Tim' as customer_name, 'address4' as customer_address1, 'BILL_TO' as site_use_code from dual union all
9 select 5 as customer_number, 'Sam' as customer_name, 'address5' as customer_address1, 'BILL_TO' as site_use_code from dual
10 )
11 --
12 -- END OF TEST DATA
13 --
14 select distinct
15 customer_number
16 ,customer_name
17 ,customer_address1
18 ,first_value(site_use_code) over (partition by customer_number order by decode(Site_use_code,'BILL_TO',1,'SHIP_TO',2,3) desc) as site_use_code
19 FROM t
20 WHERE Site_use_code IN ('BILL_TO','SHIP_TO','SOLD_TO')
21* order by customer_number
SQL> /
CUSTOMER_NUMBER CUSTOM CUSTOMER SITE_US
1 Fred address1 SHIP_TO
2 Bob address2 BILL_TO
3 George address3 SOLD_TO
4 Tim address4 BILL_TO
5 Sam address5 BILL_TO -
Hi need help in writing this query
A table has three columns - id (primary key), categoryID (numeric) and
amount (numeric). The goal is to provide a query that would show categoryID
and sum of amounts within that category where categoryID is an odd number
and sum of amounts is greater than 100,000select categoryid,sum(amount) from table where mod(categoryid,2)=1
group by categoryid
having sum(amount) > 100000
Regards,
Yuri
[http://it.toolbox.com/blogs/living-happy-oracle] -
Hi,
I have a table which has columns that records the logon and logoff times of the users every thime they do so. here are the column descriptions
MSG DATE - DATE of the Logon\logoff
MSG TIME - TIME of the logon\log off
Log Type - Type of activity ( Logon or Logoff.)
User NAme
Q)My requirement is to find the users that haven't logged on in the past 6 months. Can you please help?
Many Thanks,
Bhanuselect [User Name] from LogActivity
GROUP BY [User Name]
HAVING MAX([MSG Date]) < DATEADD(month, -6, CURRENT_TIMESTAMP)
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles -
Hi,
I was given a task of changing the existing query the COPA.
I want to aggregate the last three months data into a single month.
Any suggestions for doing so.
Thanks,Hi Murali,
Can u be more specific on how to do it?
My report is in crystal and the source for tht is BW?
So ny suggetions. -
Need help in writing SQL query
Hi,
I have a rquirement where I need to convert columns into records.
Table1:
(note: I have around 150 columns i.e period1 to period150)
unit startdate Period1 period2 period3 period4 period5
A 1/1/2011 100 200 300 400 500
Now I need data in below format
UNIT startdate period
A 1/1/2011 100
A 1/1/2011 200
A 1/1/2011 300
A 1/1/2011 500
Please suggest me...
Thanks.With UNPIVOT you can "name" each of your rows using as.
One trick is to "name" the rows to a number - then you can add that number of weeks to your start date - like this:
with t as (
select 'A' unit, DATE '2011-01-01' startdate, 100 period1, 200 period2 ,300 period3, 400 period4 ,500 period5 from dual
select
unit,
startdate + numtodsinterval(week*7,'day') startdate,
qty
from t
unpivot(
qty for week in (
period1 as 0,
period2 as 1,
period3 as 2,
period4 as 3,
period5 as 4
;Theoretically the above should work - but somehow I get a NULL date on my 11.1 instance? And I don't have another version to test with, alas.
But try it and see if it works on your instance ;-) -
Hi All,
I have a table with below structure & Data.
{code}
CREATE TABLE SAMPLE
(PARENT VARCHAR2(100),
CHANGE VARCHAR2(100),
FLAG VARCHAR2(10),
CHILD VARCHAR2(100))
{code}
{code}
insert into sample values('5762','7219','A','5965');
insert into sample values('5762','7219','A','83653');
insert into sample values('5762','7219','A','62714');
insert into sample values('5762','7219','A','108258');
insert into sample values('5762','7219','A','35203');
insert into sample values('5762','7219','A','35758');
insert into sample values('5762','7219','A','104522');
insert into sample values('5762','7219','A','70087');
insert into sample values('5762','7219','A','123878');
insert into sample values('5762','7219','A','124098');
insert into sample values('5762','7219','A','124901');
insert into sample values('5762','7818','D','35758');
insert into sample values('5762','7859','D','104522');
insert into sample values('5762','8058','D','35203');
insert into sample values('5762','8058','A','43782');
{code}
I want to delete the records where the flag is 'D',also I want to delete the record if the same child had added(Flag 'A') in the earlier change orders .
For ex: child 35203 has been removed from 8058,same child had added in change 7219,which should be removed.
Output would be like this.
{code}
insert into sample values('5762','7219','A','5965');
insert into sample values('5762','7219','A','83653');
insert into sample values('5762','7219','A','62714');
insert into sample values('5762','7219','A','108258');
insert into sample values('5762','7219','A','70087');
insert into sample values('5762','7219','A','123878');
insert into sample values('5762','7219','A','124098');
insert into sample values('5762','7219','A','124901');
insert into sample values('5762','8058','A','43782');
{code}
Thanks in advance.
Regards,
LaxmanLike this
SQL> select * from sample;
PARENT CHANGE FLAG CHILD
5762 7219 A 5965
5762 7219 A 83653
5762 7219 A 62714
5762 7219 A 108258
5762 7219 A 35203
5762 7219 A 35758
5762 7219 A 104522
5762 7219 A 70087
5762 7219 A 123878
5762 7219 A 124098
5762 7219 A 124901
5762 7818 D 35758
5762 7859 D 104522
5762 8058 D 35203
5762 8058 A 43782
15 rows selected.
SQL> delete
2 from sample
3 where rowid in
4 (
5 select rid
6 from (
7 select rowid rid
8 , max(flag) over(partition by parent, child) max_flag
9 from sample
10 )
11 where max_flag = 'D'
12 );
6 rows deleted.
SQL> select * from sample;
PARENT CHANGE FLAG CHILD
5762 7219 A 5965
5762 7219 A 83653
5762 7219 A 62714
5762 7219 A 108258
5762 7219 A 70087
5762 7219 A 123878
5762 7219 A 124098
5762 7219 A 124901
5762 8058 A 43782
9 rows selected.
SQL> -
Help needed in writing a Query/Procedure
Hello All,
Need all ur help in writing a query or procedure
Lets say the Table name is DEMO
There i have one column like num it has values
1
2
3
4
5
8
9
10
my query output should be
1-5
8-10
i,e .. if the diff between two rows is greater than 1 then it result should be in a separate group
I need a query/Procedure for this. Kindly help
Regards,
ChandraTried obtaining the output using SQL and the result is as follows:
SQL> WITH T AS
2 (
3 SELECT 1 COL1 FROM DUAL
4 UNION
5 SELECT 2 COL1 FROM DUAL
6 UNION
7 SELECT 3 COL1 FROM DUAL
8 UNION
9 SELECT 4 COL1 FROM DUAL
10 UNION
11 SELECT 5 COL1 FROM DUAL
12 UNION
13 SELECT 8 COL1 FROM DUAL
14 UNION
15 SELECT 9 COL1 FROM DUAL
16 UNION
17 SELECT 10 COL1 FROM DUAL
18 UNION
19 SELECT 13 COL1 FROM DUAL
20 UNION
21 SELECT 14 COL1 FROM DUAL
22 UNION
23 SELECT 15 COL1 FROM DUAL
24 UNION
25 SELECT 16 COL1 FROM DUAL
26 UNION
27 SELECT 23 COL1 FROM DUAL
28 UNION
29 SELECT 24 COL1 FROM DUAL
30 )
31 SELECT OUTPUT FROM
32 (
33 SELECT DECODE(COL3,NULL,COL1, COL2) || '-' || LEAD(DECODE(COL3,NULL,COL3, COL1)) OVER (ORDER BY DECODE(COL3,NULL,COL1, COL2)) OUTPUT FROM
34 (
35 SELECT COL1, LEAD(COL1) OVER (ORDER BY COL1) COL2, LAG(COL1) OVER (ORDER BY COL1) COL3 FROM T
36 )
37 WHERE
38 (COL2 - COL1 > 1 OR COL2 IS NULL OR COL3 IS NULL)
39 )
40 WHERE OUTPUT != '-';
OUTPUT
1-5
8-10
13-16
23-24 -
Need help in writing SP notification Query
Hi,
My purpose of writing a query is to restrict a user to add journal entry in case account code lies in Expense Drawer and Cost Center Code is not in user define range.
Please help me on this:-
IF (@object_type = '30' AND (@transaction_type = 'A' Or @transaction_type = 'U'))
BEGIN
Declare @AccountCodeMaster as Nvarchar(20)
Declare @AccountonDocument as Nvarchar(20)
Declare @ProfitCentreMaster as Nvarchar(20)
Declare @ProfitCentreOnDocument as Nvarchar(20)
Set @AccountCodeMaster = (select AcctCode from oact where GroupMask = 5)
Set @AccountonDocument = (Select Account from jdt1 where TransId = @list_of_cols_val_tab_del)
Set @ProfitCentreMaster = (select PrcCode from ocr1 where PrcCode >= 91100001 and PrcCode <= 91921001)
Set @ProfitCentreOnDocument = (select ProfitCode from jdt1 where TransId = @list_of_cols_val_tab_del)
if (@AccountonDocument) in (@AccountCodeMaster) and (@ProfitCentreOnDocument) not in (@ProfitCentreMaster)
Begin
set @error =1
set @error_message = 'Profit Center is wrong,Please select the right profit Center else Consult to Finance Department'
End
END
ThanksHi,
Welcome you post on the forum.
You may try:
IF (@object_type = '30' AND @transaction_type in ('A', 'U')
BEGIN
If Exists (Select T0.Account from jdt1 T0 inner join oact T1 ON T1.AcctCode=T0.Account AND T1.GroupMask = 5
where T0.TransId = @list_of_cols_val_tab_del AND T0.ProfitCode NOT IN (select PrcCode from ocr1 where PrcCode >= 91100001 and PrcCode <= 91921001))
Begin
set @error =1
set @error_message = 'Profit Center is wrong,Please select the right profit Center else Consult to Finance Department'
End
END
Thanks,
Gordon
Maybe you are looking for
-
Install 16 bit Oracle client on Win 2003 server
Hello, Is it possible to install Oracle SQL* Net 2.3(16 bit) Client on Windows 2003 server. If it is then could someone give me links from where I could get more information on this (like the steps to install 16 bit Oracle client on Win 2003). Thanks
-
Why does Apple TV show me that is takes 19 hours to play a movie
Why does Apple TV show me that is takes 19 hours to play a movie
-
Table UDM_COLL_ITEM & UDM_COLL_LASTPAY Not updated
Hi, I've a peculiar issue with this particular customer that open items are cleared last week, however they are still appearing in a worklist. Last Payment details are also not showing correctly. I've observed that both the tables UDM_COLL_ITEM and U
-
Can you please send the link for Release Note of Solaris 9.2.0.7??
I can find the one for 9.2.0.8 but not for 9.2.0.7 and 9.2.0.6 !! -ashokM
-
Modifying the sales order status
Hi, We would like to check the Customer master is in Restricted Party List or not, So after creation of order we would like to Hold the order until the check. So How to Control this? How to modify the order status?