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

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;

Related

multiple select in sql server

I want to search between 2 tables but that field i want to search is foreign key in other table
my tables are like this:
table 1
ID TitleSR
1 888
2 999
table 2
ID TitleSR
1 11
2 22
3 33
4 44
table contain value
ID value
11 italy
22 swiss
888 lilium
999 mount
33 england
I think I understand you. Try this one:
Select *
From table3 as VCT Inner Join
(Select * From table1
Union
Select * From table2) as FGT
On VCT.ID = FGT.TitleSR
Where value = 'italy';
You can use either of these methods:
Returns only t1 fields
SELECT * FROM Table1 t1
WHERE t1.ID in (SELECT ID FROM Table2);
Returns ALL fields
SELECT * FROM Table1 t1
JOIN Table2 t2 on t1.ID = t2.ID;
If your 'values' exist in a separate table (tblValues), you can use any of these:
Returns tblValues fields
SELECT * FROM tblValues tval
WHERE tval.ID in (SELECT TitleSR FROM Table1);
returns ALL fields
SELECT *
FROM (tblValues tval
JOIN Table1 t1 on tval.ID = t1.TitleSR)
JOIN Table2 on tval.ID = Table2.TitleSR;

Multiple records for same id and i don't want to pull if id is not matching

I have a table 1 which is like this:
Id Values
100 1
100 2
100 3
110 1
110 2
110 4
120 3
I want the id where there is no 1 and 2 so my result should be like this
ID Values
120 3
you could try something like this.
SELECT id
,values_t
FROM Table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM Table1 t2
WHERE t1.id = t2.id
AND t2.values_t IN (
1
,2
)
);
Fiddle
This makes a select of all data without id=1 or id=2
SELECT *
FROM Table2
WHERE values != 1 OR values != 2
Also you can use this as WHERE:
WHERE values > 2
SELECT Id, Values
FROM table2
WHERE Id Not in (select Id from table2 where Values IN (1,2))
this is your query

How to inset one table colume data into another table column

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

require to form a sql query

I was working on preparing a query where I was stuck.
Consider tables below:
table1
id key col1
-- --- -----
1 1 abc
2 2 d
3 3 s
4 4 xyz
table2
id col1 foreignkey
-- ---- ----------
1 12 1
2 13 1
3 14 1
4 12 2
5 13 2
Now what I need is to select only those records from table1 for which the corresponding entries in table2 does not have say col1 value as 12.
So the challenge is after applying join even though it will skip for value 1 corresponding to col1 equal to 12 it still has another multiple rows whose values are say 13, 14 for which also they have same foreignkey. Now what I want is if there is a single row having value 12 then it should not pick that id at all from table1.
How can I form a query with this?
The output which i need is say from above table structure i want to get those records from table1 for which col1 value from table2 does not have value as 14.
so my query should return me only row 2 from table1 and not row 1.
Another way of doing that. The first two queries are just for making the sample data.
;WITH t1(id ,[key] ,col1) AS
(
SELECT 1 , 1 , 'abc' UNION ALL
SELECT 2 , 2 , 'd' UNION ALL
SELECT 3 , 3 , 's' UNION ALL
SELECT 4 , 4 , 'xyz'
)
,t2(id ,col1, foreignkey) AS
(
SELECT 1 , 12 , 1 UNION ALL
SELECT 2 , 13 , 1 UNION ALL
SELECT 3 , 14 , 1 UNION ALL
SELECT 4 ,12 , 2 UNION ALL
SELECT 5 ,13 , 2
)
SELECT id, [key], col1
FROM t1
WHERE id NOT IN (SELECT t2.Id
FROM t2
INNER JOIN t1 ON t1.Id = t2.foreignkey
WHERE t2.col1 = 14)
This is a typical case for NOT EXISTS:
SELECT id, [key], col1
FROM table1 t1
WHERE NOT EXISTS (SELECT 1
FROM table2 t2
WHERE t2.foreignkey = t1.id AND t2.col1 = 14)
The above query will not select a row from table1 if there is a single correlated row in table2 having col1 = 14.
Output:
id key col1
-------------
2 2 d
3 3 s
4 4 xyz
If you want to return records that, in addition to the criterion set above, also have correlated records in table2, then you can use the following query:
SELECT t1.id, MAX(t1.[key]) AS [key], MAX(t1.col1) AS col1
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.foreignkey
GROUP BY t1.id
HAVING COUNT(CASE WHEN t2.col1 = 14 THEN 1 END) = 0
Output:
id key col1
-------------
2 2 d
You can also achieve the same result with the second query using a combination of EXISTS and NOT EXISTS:
SELECT id, [key], col1
FROM table1 t1
WHERE EXISTS (SELECT 1
FROM table2 t2
WHERE t2.foreignkey = t1.id)
AND
NOT EXISTS (SELECT 1
FROM table2 t3
WHERE t3.foreignkey = t1.id AND t3.col1 = 14)
select t1.id,t1.key,
(select ROW_NUMBER() OVER(PARTITION BY col1 ORDER BY col1 DESC) AS Row,* into
#Temp from table1)
from table1 t1
inner join table2 t2 on t1.id=t2.foreignkey
where t2.col1=(select col1 from #temp where row>1)

How to get the records from multiple tables?

Hi I am new to the Database, and i am trying to get the records from the multiple tables, but depending upon there selection following is my tables
Table1
Column1 Column2
1 10
2 25
3 23
4 15
5 7
Table2
Column1 Column2
2 15
3 13
5 17
Table3
Column1 Column2
2 45
Resultant Table should have records like
Column1 Column2
1 10
2 45
3 13
4 15
5 17
i am trying but not got the output yet. Any help or the direction to work out this output will be great help.
UPDATE
What i want is get the all rows from table1 then if table2 contains the matching records then it will remove the matching records form the resultset and add the table2 matching records and then same is repeated by table3.
SELECT t1.column1, COALESCE(t3.column2,t2.column2,t1.column2)
FROM t1
LEFT JOIN t2 on t1.column1=t2.column1
LEFT JOIN t3 on t1.column1=t3.column1
Please use the Below Code and Try
select * from table1 where column1 not in ( select column1 from table2 union select column1 from table3)
union
select * from table2 where column1 not in (select column1 from table3)
union
select * from table3
select x.col1,max(x.col2) from (
select * from #t1
union
select * from #t2
union
select * from #t3
)x
group by x.col1
see it in action