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
Related
I have two tables:
table1
A
B
C
A1
B1
C1
A2
B2
C2
A3
B3
C3
A4
B4
C4
A5
B5
C5
A6
B6
C6
table2
A
D
A1
D1
A3
D3
A5
D5
A6
D6
I would like to have table 1 updated with a column D which shows the value in column D joining by A. However, Is altering table 1 adding a column D and then merging both tables and update when matched the way to go or is there any better approach?
You can just join the value in when you need it:
select t1.*, t2.d
from table1 t1 left join
table2 t2
on t1.a = t2.a;
If that is not sufficient, you can add the column:
alter table1 add d <type>;
Then you can update it:
update table1 t1
set d = (select t2.d from table2 t2 where t2.a = t1.a)
where exists (select t2.d from table2 t2 where t2.a = t1.a);
I have read tables from pdf using tabula-py command with the following code:
table = tabula.read_pdf(files[0],pages = 'all',multiple_tables = True, stream = True)
Sometimes values from two columns are joined into a single column(separated by single space). For example:
col0
col1
col2
col3
col4
col5
col6
col7
a1
b1 c1
d1
e1 f1
g1
h1
NA
NA
a2
b2
c2
d2
e2
f2
g2
h2
How can i readjust the values into the correct columns, to get:
col0
col1
col2
col3
col4
col5
col6
col7
a1
b1
c1
d1
e1
f1
g1
h1
a2
b2
c2
d2
e2
f2
g2
h2
output as space delimited
replace quoted strings from step 1
read back as space delimited
import io
df = pd.read_csv(io.StringIO("""col0 col1 col2 col3 col4 col5 col6 col7
a1 b1 c1 d1 e1 f1 g1 h1 NA NA
a2 b2 c2 d2 e2 f2 g2 h2"""), sep="\t")
df = pd.read_csv(io.StringIO(df.to_csv(sep=" ").replace("\"", "")), sep="\s+")
output
col0 col1 col2 col3 col4 col5 col6 col7
a1 b1 c1 d1 e1 f1 g1 h1
a2 b2 c2 d2 e2 f2 g2 h2
Could you try
table = tabula.read_pdf(files[0],pages = 'all',multiple_tables = True,guess = False, stream = True)
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);