How to inset one table colume data into another table column - sql

Can u help me how to insert Column A data into col3 at place of null.
See the attachment.
TABLE TAB1
--------------
col1 col2 col3
5 7 NULL
8 11 NULL
3 6 NULL
2 12 NULL
TABLE TAB2
-----------
ColA CoB
7 5
18 8
24 3
36 2
Desire Output Like
col1 col2 col3
5 7 7
8 11 18
3 6 24
2 12 36
This is called commutative sum.

1. It's work for same table.
update TABLENAME set col3=col2
**2.**For inserting one table column data into another table
INSERT into tab1(col1)
select col1 from tab2

From what I can tell, you want a cumulative sum from the second column:
with toupdate as (
select t1.*,
sum(t1.col2) over (order by ??) as cume_col2
from tab1 t1
)
update toupdate
set col3 = cume_col2;
In order to do a cumulative sum, you need a column that specifies the order for the sum. Your data as shown does not have an appropriate column.
EDIT:
Oh, I see. The ordering comes from the second table:
with toupdate as (
select t1.*,
sum(t1.col2) over (order by t2.cola) as cume_col2
from tab1 t1 join
tab2 t2
on t1.col1 = t2.colb
)
update tab1
set col3 = toupdate.cume_col2
from tab1 join
toupdate
on tab1.col1 = toupdate.col1;

Try this:
Update tab1
set Col3=tab2.ColA
from tab1
inner join tab2 on tab1.col1=tab2.colB

Related

Return rows where specific column has duplicate values

From the table below I want to show the two rows where the values in column 3 are duplicates:
ID
Col2
Col3
1
a
123
2
b
123
3
c
14
4
d
65
5
e
65
This means that the query that I need should return rows with ID 1, 2 and 4, 5.
I wrote query using having:
SELECT *
FROM t1
INNER JOIN (SELECT col3 FROM t1
GROUP BY col3
HAVING COUNT(*) > 1) a
ON t1.col3 = a.col3
This query though only returns 1 and 4 rows for example, not all duplicates.
I would appreciate the help.
Your query should work, but I would suggest window functions:
select t1.*
from (select t1.*, count(*) over (partition by col3) as cnt
from t1
) t1
where cnt > 1;

Combining columns from two unrelated tables in SQL

Let's say I have two tables
Table 1
COl_1 COl_2
1 5
2 6
3 7
4 8
AND Table 2
COL3 COL4
9 13
10 14
11 15
12 16
I want the following:
COL_1 COL_2 COL3 COL4
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Also, the number of rows are exactly the same everytime in each of the tables,
no key relation or integrity stuff.
Any clue?
You can do this using row_number() to add a "join" key to the two tables:
select t1.col_1, t1.col_2, t2.col_3, t2.col_4
from (select t1.*, row_number() over (order by col_1) as seqnum
from table1 t1
) t1 join
(select t2.*, row_number() over (order by col_3) as seqnum
from table2 t2
) t2
on t1.seqnum = t2.seqnum;
If the tables have different numbers of rows, you might want an outer join.
You can add an identity column as an alternative to row_number() as follows
alter table Table_1 add id int identity(1,1)
alter table Table_2 add id int identity(1,1)
Then using this ID column, you can join both tables as follows (as LONG suggested FULL JOIN)
select col1, col2, COL3, COL4
from Table_1
full outer join Table_2 on Table_1.id = Table_2.id
With some additional data you can get following results

SQL: KEEP Unique value of C1 with Highest value in C2

In my two columns of data I would like to keep only the unique values of ColumnOne that have the highest value in ColumnTwo.
For example
ColumnOne ColumnTwo
2 6
3 2
7 8
2 7
3 4
7 3
I would like the results:
ColumnOne ColumnTwo
2 7
3 4
7 8
You can do this with a group by statement:
select Column1, max(Column2)
from your_table
group by Column1
delete t1
from myTable t1
left join (select t2.Column1, max(t2.Column2) maxColumn2
from myTable t2
group by t2.Column1) tMax
on t1.Column1 = tMax.Column1
and t1.Column2 = tMax.maxColumn2
where tMax.Column1 is null
The below query will help you to accomplish your output for tables with huge no. of records:
create table table1_new as (select * from table1) with no data;
insert into table1_new
select columnone, max(columntwo) over(partition by columnone) from table1 group by columnone;
Validate data and then interchange table names:
drop table table1;
rename table1_new to table1;

Removing rows in SQL that have a duplicate column value

I have looked high and low on SO for an answer over the last couple of hours (subqueries, CTE's, left-joins with derived tables) to this question but none of the solutions are really meeting my criteria..
I have a table with data like this :
COL1 COL2 COL3
1 A 0
2 A 1
3 A 1
4 B 0
5 B 0
6 B 0
7 B 0
8 B 1
Where column1 1 is the primary key and is an int. Column 2 is nvarchar(max) and column 3 is an int. I have determined that by using this query:
select name, COUNT(name) as 'count'
FROM [dbo].[AppConfig]
group by Name
having COUNT(name) > 3
I can return the total counts of "A, B and C" only if they have an occurrence of column C more than 3 times. I am now trying to remove all the rows that occur after the initial value of column 3. The sample table I provided would look like this now:
COL1 COL2 COL3
1 A 0
2 A 1
4 B 0
8 B 1
Could anyone assist me with this?
If all you want is the first row with a ColB-ColC combination, the following will do it:
select min(id) as id, colB, colC
from tbl
group by colB, colC
order by id
SQL Fiddle
This should work:
;WITH numbered_rows as (
SELECT
Col1,
Col2,
Col3,
ROW_NUMBER() OVER(PARTITION BY Col2, Col3 ORDER BY Col3) as row
FROM AppConfig)
SELECT
Col1,
Col2,
Col3
FROM numbered_rows
WHERE row = 1
SELECT DISTINCT MIN(COL1) AS COL1,COL2,COL3
FROM TABLE
GROUP BY COL2,COL3
ORDER BY COL1

Find matching column data between two rows in the same table

I want to find the matching value between two rows in the same sqlite table. For example, if I have the following table:
rowid, col1, col2, col3
----- ---- ---- ----
1 5 3 1
2 3 6 9
3 9 12 5
So comparing row 1 and 2, I get the value 3.
Row 2 and 3 will give 9.
Row 3 and 1 will give 5.
There will always be one and only one matching value between any two rows in the table.
What it the correct sqlite query for this?
I hardcoded the values for the rows because i do not know how to declare variables in sqllite.
select t1.rowid as r1, t2.rowid as r2, t2.col as matchvalue from <yourtable> t1 join
(
select rowid, col1 col from <yourtable> where rowid = 3 union all
select rowid, col2 from <yourtable> where rowid = 3 union all
select rowid, col3 from <yourtable> where rowid = 3
) t2
on t2.col in (t1.col1, t1.col2, t1.col3)
and t1.rowid < t2.rowid -- you don't need this if you have two specific rows
and t1.rowid = 1
select col from
(
select rid, c1 as col from yourtable
union
select rid, c2 from yourtable
union
select rid, c3 from yourtable
) v
where rid in (3,2)
group by col
order by COUNT(*) desc
limit 1