Generate Dynamic Update Statement via TSQL - tsql

I have a table with a number of columns:
col1
col2
col3
coln....
I need to generate dynamic UPDATE statement like below which will be used in production for bulk update:
UPDATE TableA
SET TableA.ColA = ValueOfCol2
WHERE
TableA.ColB='A'
Could anyone please share a TSQL script that generate n number of UPDATE statement as above, please?
Thank you

Unless I'm misunderstanding your problem, your example code works:
UPDATE [updateDemo] SET [updateDemo].[col2] = [updateDemo].[col3] WHERE [col4] = 'A'
This is based on the assumption that your table is something like this:
col1 | col2 | col3 | col4
1 P Z A
2 Y Z A
3 K S V
This above update query would result in (changes in square brackets):
col1 | col2 | col3 | col4
1 [Z] Z A
2 [Z] Z A
3 K S V

Related

DB2 - Concat all values in a column into a Single string

Let's say I have a table like this:
test
Col1
Col2
A
1
B
1
C
1
D
2
I am doing query select col1 from test where col2 = 1;
This will return a column with values A B and C in 3 separate rows.
I want the SQL to return a single row with value A|B|C. Is this possible to do? If it is how should I do it?
you can use LISTAGG function like this:
SELECT LISTAGG(col1, ',')
If LISTAGG is not available, it can be reproduced with XMLAGG:
SELECT SUBSTR(XMLSERIALIZE(XMLAGG(XMLTEXT('|'||"Col1"))),2)
FROM "test"
WHERE "Col2" = 1

filter rows from Postgres table based on specific conditions without missing relevant rows

I have table with following columns in postgres.
col1 col2 col3
1 Other a
2 Drug b
1 Procedure c
3 Combination Drug d
4 Biological e
3 Behavioral f
3 Drug g
5 Drug g
6 Procedure h
I would like to filter rows based on following filters.
select col1, col2, col3
from tbl
where col2 in ('Other', 'Drug', 'Combination Drug', 'Biological')
But this query will exclude below rows
1 Procedure c
3 Behavioral f
The Desired output is:
col1 col2 col3
1 Other a
1 Procedure c
2 Drug b
3 Combination Drug d
3 Behavioral f
3 Drug g
4 Biological e
5 Drug g
How can I achieve the above output without missing the mentioned rows.
Any suggestion here will be really helpful. Thanks
I think you want the rows where there is as col1 containing any of the values of col2 in the list:
select col1, col2, col3
from tbl
where col1 in (
select col1 from tbl
where col2 in ('Other', 'Drug', 'Combination Drug', 'Biological')
)
order by col1;
Or with EXISTS:
select t.col1, t.col2, t.col3
from tbl t
where exists (
select 1 from tbl
where col1 = t.col1
and col2 in ('Other', 'Drug', 'Combination Drug', 'Biological')
)
order by col1;
See the demo.

Selecting count of occurences of values in kdb

How to count occurences of distinct values from one column in another column in kdb. The idea is to return the count of values in another column.
The table looks like
Col1 : x,y,z and Col2: x,x,l
The idea is to find count of occurences of x,y,z from col1 in col2, which in above case is 2,0,0
You could try this:
tab:([]col1:`x`y`z;col2:`x`x`w)
q)exec([]distinct col1)!0^([]count each group col2)distinct col1 from tab
col1| col2
----| ----
x | 2
y | 0
z | 0
Desired value can be found as a map of Col2 occurrences. Which is later looked up by values from Col1
t: ([] Col1:`x`y`z; Col2:`x`x`l);
update Col1Col2Count: 0^(count each group Col2) Col1 from t

Delete column based on type in KDB+

I have a table with a bunch of columns of various types. I need to delete all columns of a particular type, but I can't figure out how to do this.
I would like something like this:
delete from quotes where type = 11
But this doesn't work. Is there a way to do this? I was also able to list the relevant columns with the command
select c from meta quotes where type="s"
But this gives me a one column table with the column headings and I don't know where to go from there.
Could use a functional delete (!), or a take (#) or a drop (_)
q)t:([] col1:`a`b`c;col2:1 2 3;col3:`x`y`z;col4:"foo")
q)![t;();0b;exec c from meta[t] where t="s"]
col2 col4
---------
1 f
2 o
3 o
q)(exec c from meta[t] where t<>"s")#t
col2 col4
---------
1 f
2 o
3 o
q)(exec c from meta[t] where t="s") _ t
col2 col4
---------
1 f
2 o
3 o

how to join two tables without repetation or the cells from second table in postgresql using PLSQL

When I try to join the below two table
I am not able to get the output I want by the join.
I tried using join but it didn't work let me know if its possible with plsql
Table 1:
col1 col2
1 a
1 b
1 c
2 a
2 b
3 a
table 2:
col1 col2
1 x
1 y
2 x
2 y
3 x
3 y
The output must be:
col1 col2 col3
1 a x
1 b y
1 c
2 a x
2 b y
3 a x
3 y
If use the join I am not able to get the same output as above.
The output I am getting is
1 a x
1 a y
1 b x
1 b y
1 c x
1 c y
2 a x
.....
.....
3 a x
3 a y
What you are searching is called a FULL OUTER JOIN. The result of this join contains elements from both input-tables, matching records get combined.
You can find more information here: https://stackoverflow.com/questions/4796872/full-outer-join-in-mysql
Using Window functions, specifically ROW_NUMBER() and partitioning by the Col1 in both tables, we can get a partitioned row_number that can be used as part of the join.
In other words, it seems to me that the order that the records are in is crucial for the join and result set you are desiring. Furthermore, using #Benvorth's suggestion of a FULL OUTER JOIN to achieve the NULLs in both direction.. I believe this might work:
SELECT
COALESCE(t1.col1,t2.col1) as col1,
t1.col2,
t2.col2
FROM
(SELECT col1, col2, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 ASC) as col1_row_number FROM table1) t1
FULL OUTER JOIN
(SELECT col1, col2, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 ASC) as col1_row_number FROM table2) t2 ON
t1.col1 = t2.col1 AND
t1.col1_row_number = t2.col1_row_number
That ROW_NUMBER() OVER (PARTITION BY col1, ORDER BY col2 ASC) bit will create row number for each record. The row_number will restart back at 1 for each new col1 value encountered. You can think of it like a RANK for each distinct Col1 value based on Col2's value. Table1's output from the subquery SELECT col1, col2, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 ASC) as col1_row_number FROM table1 will look like:
Table 1:
col1 col2 col1_row_number
1 a 1
1 b 2
1 c 3
2 a 1
2 b 2
3 a 1
So we do that with both tables, then we use that row number as part of the join along with col1.
A sqlfiddle showing this matching your desired result from the question