While executing a query to compare the rates of two columns and display if one is grater than other I am getting ORA-01427 exception
Here is the data set. This is mocked up data . Data size is huge in databse
Table1
col1 col2 col3 col4 col5 col6
c1 c1test 85 85 I 5
c2 c2test 85 85 I 3
c3 c3test 85 85 E 6
c4 c4test G1 G1 E 7
c5 c5test G1 G1 E 5
c6 c6test G1 G1 E 8
c7 c7test G1 G1 I 3
c8 c8test G1 G1 G 7
Table2
col1 col2 col3 col4 col5
85 85 D I 3
85 85 D E 5
G1 G1 D E 5
G1 G1 D I 3
G1 G1 D G 5
G1 G1 E I 2
G1 G1 E E 2
85 85 E I 3
Expected result
We need to compare the value of col5 of table2 with the col6 of table1 to find out the values greater in col6 of table1 and display the records. Comparison needs to be done only for col3 equals D values.
col1 col2 col3 col4 col5 col6
c1 c1test 81 81 I 5
c3 c3test 81 81 E 6
c4 c4test G1 G1 E 7
c6 c6test G1 G1 E 8
c8 c8test G1 G1 G 7
I am using the below query
Select * from table1 where
col6 > (select col5 from
table2 where col3='D'
and col1=table1.col3
and col2=table1.col4
and col4=table1.col5
This throws an ora-01427 exception. Can you pls hel to get the expected output.
If you are looking for value grater than any value of table2.col5, try this
SELECT t1.*
FROM table1 t1
WHERE t1.col6 > ANY (SELECT t2.col5
FROM table2 t2
WHERE t2.col3 = 'D'
AND t2.col1 = t1.col3
AND t2.col2 = t1.col4
AND t2.col4 = t1.col5
);
If you need table1.col6 should be greater than all values found in table2.col5 use ALL instead of ANY
Your subquery is returning multiple rows. You can use min() or max() to get around this. I'm not sure which logic you really want:
Select t1.*
from table1 t1
where t1.col6 > (select max(t2.col5)
from table2 t2
where t2.col3 = 'D' and
t2.col1 = t1..col3 and
t2.col2 = t1.col4 and
t2.col4 = t1.col5
);
I have requirement where I have input data like
Col1 COl2 Col3
A1 2 B
A1 1 A
A1 3 B
B1 1 A
B2 2 B
B2 3 C
B4 4 C
B5 5 A
B6 6 B
Output Required:
Col1 COl2 Col3
A1 2 AB
A1 1 AB
A1 3 AB
B1 1 ABC
B2 2 ABC
B2 3 ABC
B4 4 ABC
B5 5 ABC
B6 6 ABC
Solution Tried:
select col1,col2,listagg(col3,'') within group (order by col3) over(partition by col1)
from tab
Output of the query:
Col1 COl2 Col3
A1 2 ABB
A1 1 ABB
A1 3 ABB
B1 1 AABBCC
B2 2 AABBCC
B2 3 AABBCC
B4 4 AABBCC
B5 5 AABBCC
B6 6 AABBCC
Can someone help here in removing repeating alphabets.
Thanks
You can use a subquery:
select col1, col2,
listagg(case when seqnum = 1 then col3 end, '') within group (order by col3) over (partition by col1)
from (select t.*,
row_number() over (partition by col1, col3 order by col3) as seqnum
from tab t
) t
I have the following table
Col1 Col2 Col3
A1 B1 C1
A1 B1 C2
A1 B2 C1
A1 B2 C2
A1 B2 C3
A2 B1 C1
A2 B1 C2
A2 B2 C1
A2 B2 C2
From this table I want all the unique records from Col1 where for the combination of col1 and col2 there's a different count for the same value in Col1. The only possible answer is A1 in the table above.
The following query gives me the count of each col1 and col2.
select col1, col2, count(*) from table
group by col1, col2;
Col1 Col2 Count
A1 B1 2
A1 B2 3
A2 B1 2
A2 B2 2
From the above query I can see that A1 has two records with a different count. How do I return A1 in a single query?
You can use another level of aggregation:
select col1
from (select col1, col2, count(*) as cnt
from table
group by col1, col2
) t
group by col1
having min(cnt) <> max(cnt);
I have a table with three columns, Col1, Col2 and Col3 where Col2 is primary key.
Below is the SOURCE table data as
Col1 Col2 Col3
G1 S1 C
G1 S2 Y
G1 S3 U
G2 S4 C
G2 S5 Y
G3 S6 C
G3 S7 C
G4 S8 Y
G4 S9 Y
G5 S10 U
G5 S11 U
G6 S12 C
G6 S13 U
G7 S14 Y
G7 S15 U
Expected TARGET table will ahve only Col1 and Col3 and data should be
Col1 Col3
G1 U
G2 B
G3 C
G4 Y
G5 U
G6 U
G7 U
Logic used is:
1) For a given Col1 record, if all the col2 records have same value in Col3 then return one record for all the col2 records ( Examples G3, G4, G5)
2) For a given Col1 record, if all the col2 records having either 'C' or 'Y then return 'B' (Example G2)
3) For all the remaining combinations return 'U' for Col3
Any one on the group please advice me on how to write query to get the data in target table
Thank you
Use a CASE construct for this. For the 'C' and 'Y' thing you'd have to count conditionally (i.e. also with CASE).
select
col1,
case
when min(col3) = max(col3) then min(col3)
when count(case when col3 not in ('C','Y') then 1 end) = 0 then 'B'
else 'U'
end as col3
from mytable
group by col1;
Here is the table structure, with the first 6 columns as composite keys.
col1 col2 col3 col4 col5 col6 col7 col8
A1 A2 A3 A4 A5 1 xx yy
A1 A2 A3 A4 A5 2 xxx yyy
A1 A2 A3 A4 A5 3 a b
A1 B2 A3 A4 A5 4 aa bb
B1 A2 A3 A4 A5 5 aaa bbb
B1 B2 B3 B4 B5 6 d e
B1 B2 B3 B4 B5 7 dd ee
B1 B3 C3 B4 B5 8 ddd eee
I need a stored procedure which returns the values like below
A1 A2 A3 A4 A5 xx yy xxx yyy a b
A1 B2 A3 A4 A5 aa bb
B1 A2 A3 A4 A5 aaa bbb
B1 B2 B3 B4 B5 d e dd ee
B1 B2 C3 B4 B5 ddd eee
Any pointers or help is appreciated.
If you just need a result like this:
A1 A2 A3 A4 A5 xx,yy,xxx,yyy,a,b
A1 B2 A3 A4 A5 aa,bb
B1 A2 A3 A4 A5 aaa,bbb
B1 B2 B3 B4 B5 d,e,dd,ee
B1 B3 C3 B4 B5 ddd,eee
Then a simple query will do:
select COL1
,COL2
,COL3
,COL4
,COL5
,LISTAGG(COL7 || ',' || COL8, ',') within group (order by COL6)
from TAB1
group by COL1, COL2, COL3, COL4, COL5
order by COL1, COL2, COL3, COL4, COL5
To get a dynamic number of columns you need to create a dynamic SQL. See this article for a guide on that.
First, as a standard result set from a query, you cant return variable numbers of columns, you can however return on column which is a xml column (intoduced in SQL 2005) http://msdn.microsoft.com/en-us/library/ms345117%28v=sql.90%29.aspx which you can then bind to. The binding will have to be dynamic.You can use the pivot function http://msdn.microsoft.com/en-us/library/ms177410.aspx may be of help in presenting the data in the format you want - or as stated above you can use dynamic SQL. If you do this you should generate your dynamic SQL inside a stored procedure and use sp_executesql to execute the string you generate http://msdn.microsoft.com/en-us/library/ms175170.aspx you'll probably need to pass parameters http://support.microsoft.com/kb/262499 to guard against injection attacks. Quite a few steps to learn... however each step is not particularly difficult and its a great exercise!
check this, don't know it helps you or not. I used sql server 2008, it returns result as per your requirments.
SELECT col1, col2, col3, col4, col5,
(SELECT ' ' + col7 + ' ' + col8 FROM TABLE1 t1
WHERE t1.col1 = t.col1 and t1.col2 = t.col2 and
t1.col3 = t.col3 and t1.col4 = t.col4 and t1.col5 = t.col5
ORDER BY col6
FOR XML PATH('')) AS mearge_col
FROM TABLE1 t
GROUP BY col1, col2, col3, col4, col5