Comparison of multiple rows using a single identifier in SQL - sql-server-2008

I would like to compare three columns in two different tables by joing through unique identifier.But my for single identifier there are multiple rows will be returned.
Example:
Table A
Identifier Flag1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 11 89 65
Table B
Identifier Flag 1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 10 89 65
Now i would like to compare these two tables based on Identifier 1 , can you please help me out if all the column values are matching i need update the flag.Thanks in advance

I think below code will help,
below code will update flag of table A for records whose column values are same as that of table b
update a
set someflag = 1
where exists
(
select * from B
where b.flag1 = a.flag1
and b.flag2 = a.flag2
and b.flag3 = a.flag3
)

Related

Minimum difference of the values in column Y where the column X value must be different from each other

I want to know the minimum difference of 2 values in building_num where the worker_num must be different from each other. For example check this table:
worker_num | building_num
39 0
39 2
39 6
39 7
39 15
39 21
39 25
39 27
39 29
39 30
39 31
50 0
50 1
50 3
50 15
50 16
50 18
50 19
50 24
50 25
50 32
So The 2 closest compared numbers are 0(39) & 0(50) and 15(39) & 15(50) and 25(39) & 25(50). They are all the same values so the difference is 0. So it must output 0.
If these rows weren't in it, the closest numbers could be 2(39) & 1(50), which have a difference of 1. So then the output must be 1.
The SQL code must be so simple, but I couldn't find a source. Any help is appreciated.
SELECT MIN(t1.building_num - t2.building_num)
FROM table t1
JOIN table t2 ON t1.worker_num != t2.worker_num
AND t1.building_num >= t2.building_num

SQL - SELECT row where any of columns is of certain value

I need to create a SELECT query that would return rows where at least one of X columns is of certain value.
id name teamid player1 player2 player3 ... player12
1 Jane 2 43 46 12 ... 36
2 Mathew 6 12 56 18 ... 42
3 Shaun 8 53 55 12 ... 62
4 Jane 1 1 53 19 ... 34
5 Eugene 3 23 34 13 ... 44
In this table i have 14 columns (id, name and player1 to player12) and want to SELECT row of specific teamid, let's say teamid='2', and return the name of column that contains for example number 12 in this row.
Is this even possible with MySQL?
If I understand, then:
select t.*,
(case when player1 = 12 then 'player1'
when player2 = 12 then 'player2'
. . .
when player12 = 12 then 'player12'
end) player_with_12
from t
where t.teamid = 2;
You might want to revisit your data structure, so you have one row per team and player.

Select data basis of two values of one column matches of a table

suppose this is my table structure of table user
id field_id user_id value
1 1 37 Lalit
4 2 37 Test
5 13 37 123
6 18 37 324
7 28 37 english
8 33 37 203
9 21 37 201
10 1 39 Mukesh
11 2 39 Test
12 13 39 523
13 18 39 245
14 28 39 French
15 33 39 278
16 21 39 2897
So I wnat to get the result to match the two or three values from the column value and want the result
I made query like
SELECT DISTINCT user_id FROM user where value =123 AND value=523;
But it is not working please give solution how we get the result
A value in a row, as per your example, cannot be both 123 and 523. You have to use OR
SELECT DISTINCT(user_id) FROM user WHERE value=123 OR value=523;
Alternatively you can also use IN clause
SELECT DISTINCT user_id
FROM user
WHERE value IN (123, 523);

Find lowest value from the rows which contains similar foreign key value

Table:
pk fk price
1 1 23
2 1 12
3 1 3
4 2 53
5 2 75
6 3 95
7 3 113
8 3 63
9 3 73
10 3 93
11 4 113
11 4 150
11 4 105
In the above table:
How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.
I want a single SQL statement which could find lowest price for each common fk value.
You just want a basic aggregate per group.
select fk, min(price)
from your_table
group by fk;

Excluding results and performing an insert with SQL query

Table a
Rowid Msgid Userid
1 3 55
2 3 56
3 3 57
4 4 55
5 4 56
Table Group
RowID GroupID UseriD
1 2 55
2 2 56
3 2 57
4 2 58
5 2 59
6 2 60
7 3 60
8 3 55
Here there is a table a and group table. Rowid primary keys
I want to insert rows into table a
This query will
Insert rows into table a i.e for msgid 3 there is already 55 56 57 so it has to insert only 58 59 60.
Insert into
table a (msgid,Userid)
values(#msgid,#userid)
where userid not in table a
where tbl_a.msgid=3
and tbl_group.groupid = 2
For Msgid 3 I want to check if there are any group members (groupID 2) associated in table a, if not then add a row to it.
ie add to table a
rowid Msgid Userid
6 3 58
7 3 59
8 3 60
So I wont insert the userid 55,56,57 because it is already in the table a for msgid 3. How to do a query for this scenario
Try below code
Insert IGNORE into
table a (msgid,Userid)
values(#msgid,#userid)
where userid not in table a
where tbl_a.msgid=3
and tbl_group.groupid = 2
I am considering that your insert query is correct...
Good Luck!!!
It's actually quite simple:
INSERT INTO TABLE_GROUP
SELECT * FROM TABLE_A
WHERE ... -- you can have or not have a where clause as you like
ON DUPLICATE KEY UPDATE;