How to combine these tables? - sql

In sql server 2005, how do I get the result set in table3 below? table3 is created by combining table1 and table2 like this:
table1.empid
table1.ticket
table2.identid
And update against table1 joined to table2 doesn't work because empid isn't unique. If need to increment to the next table2.indentid if the ID is already being used by that employee.
Also, table3 below isn't created yet. It's a generated set from table1 and table2. I'm using table3 as an example of what the generated set should look like.
table1
empid ticketid indentid
1 7 20
1 9 4
2 9 21
table2
indentid empid
90 1
91 1
92 2
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 9 4 91
2 9 21 92

The only connection between table1 and table2 is empid.
The lines 1 and 2 of your table3 example have the same empid, so they should have the same table2_identid as well. Your example is not proper, or not possible to get with a query on the existing data.
But maybe this is what you want:
If you join the tables like so
SELECT empid, ticketid, t1.indentid as table1_indentid, t2.identid as table2_identid
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.empid = t2.empid
you will get
table3
empid ticketid table1_indentid table2_identid
1 7 20 90
1 7 20 91
1 9 4 90
1 9 4 91
2 9 21 92

This is what you need:
select t1.empid, t1.ticketid,
t1.indentid as table1_indentid,
t2.indentid as table2_identid
from table1 t1
inner join table2 t2 on t1.empid = t2.empid
You should however consider reviewing your data structure.

Related

SQL Server : multiple rows single line

I would like to get the representation of one record based on the primary key value from multiple tables. As shown below, each table can have multiple values based on this primary key value.
TABLE-1
ID
NAME
1
AA
2
BB
3
CC
4
DD
5
EE
TABLE-2
ID
SCHOOL
AUT
1
11
A
2
11
A
2
12
B
3
11
A
4
12
A
4
13
B
5
13
A
TABLE-3
ID
TC
1
101
2
102
2
103
2
104
3
105
4
106
4
107
5
108
The result below is the value obtained with an OUTER JOIN.
SELECT
T1.ID, T2.SCHOOL, T3.TC, T2.AUT
FROM
T1
LEFT OUTER JOIN
T2 ON T1.ID = T2.ID
LEFT OUTER JOIN
T3 ON T1.ID = T3.ID
ORDER BY
T1.ID ASC
ID
SCHOOL
TC
AUT
1
11
101
A
2
11
102
A
2
12
102
B
2
11
103
A
2
12
103
B
2
11
104
A
2
12
104
B
3
11
105
A
4
12
106
A
4
13
106
B
4
12
107
A
4
13
107
B
5
13
106
A
How can I get the result like below?
ID
SCHOOL
TC1
TC2
TC3
1
11
101
2
11
102
103
104
3
11
105
4
12
106
107
5
13
108
The important thing here is that in the result value, SCHOOL only shows that AUT is 'A'.
I would appreciate it if you let me know your query.
It looks, from your desired results, you just need to use row_number in combination with a conditional aggregate. Your sample data seems a little inadequate, I can't see any requirement for table1 at all.
Try the following:
with t as (
select t2.id,t2.school,t3.tc, Row_Number() over(partition by t2.id order by t3.tc) col
from t2 join t3 on t2.id=t3.id
where aut='A'
)
select id,school,
max(case when col=1 then tc end) TC1,
max(case when col=2 then tc end) TC2,
max(case when col=3 then tc end) TC3
from t
group by id, school
Example SQL Fiddle
SELECT
T1.ID, T2.SCHOOL,
GROUP_CONCAT(T3.TC),
GROUP_CONCAT(T2.AUT)
FROM
T1
LEFT OUTER JOIN
T2 ON T1.ID = T2.ID
LEFT OUTER JOIN
T3 ON T1.ID = T3.ID
GROUP BY
T1.ID, T2.SCHOOL
WHERE
T2.AUT = ‘A’
ORDER BY
T1.ID ASC
Notice that GROUP_CONCAT concatenates the values in the row.
EDIT: oh my, haven't seen that it's a SQL Server question!
Just replace GROUP_CONCAT with STRING_AGG if you’re using SQL Server 2017 or newer.

Join two tables, using value from the first unless it is null, otherwise use value from the second

I have three tables which look like those:
TABLE 1
id j_id
1 1
2 2
3 3
TABLE 2
id j_id table1_id
1 57 1
2 84 1
3 1 1
4 9 2
5 2 2
and every j has a value in a third table
id value
1 1abc
2 2bcd
3 3abc
57 57abc
84 84abc
9 9abc
I am trying to write a query which will join table 1 and table 2 and use the J value from the third table instead of the j_id, but the problem is that I want to use the j value from the second table if it exists and otherwise use the value from the first table.
in order the make it clearer this is my query result without using the third table:
tbl1.j_id tbl2.j_id
1 1
1 84
1 57
2 2
2 9
3 null
I want the end query result to use the second table's j value unless it is null:
tbl1.j_id tbl2.j_id j_id
1 1 1abc
1 84 84abc
1 57 57abc
2 2 2abc
2 9 9abc
3 null 3abc
(Question and title edits are more than welcome, weren't that sure how to phrase them..)
You can simply JOIN to table3 on the COALESCE of table2.j_id and table1.j_id:
SELECT t1.j_id AS t1_j_id, t2.j_id AS t2_j_id, t3.value
FROM table1 t1
LEFT JOIN table2 t2 ON t2.table1_id = t1.id
JOIN table3 t3 ON t3.id = COALESCE(t2.j_id, t1.j_id)
Output:
t1_j_id t2_j_id value
1 1 1abc
1 57 57abc
1 84 84abc
2 2 2bcd
2 9 9abc
3 null 3abc
Demo on dbfiddle
One solution is to left join table3 twice:
select
t1.j_id,
t2.j_id,
coalesce(t31.value, t32.value) j_value
from
table1 t1
left join table2 t2 on t2.table1_id = t1.id
left join table3 t31 on t31.id = t2.j_id
left join table3 t32 on t32.id = t1.j_id

sql with two table and group and result from that table?

I have two tables. I need to take away from the first table second table. Stim to another table without all the rows of the first table.
Table1
Table2
Result = Table1(value1) – Table2(value1) -----groupe no. 2 or no.1
Result (groupe no. 2)
Result
id value1 groupe
_______________________
1 10 2
2 9 2
3 10 2
5 5 2
6 11 2
7 12 2
I need the result of which I can write the group number and get result for that group.
Try this query:
Select
t1.id,
t1.value1-t2.value1 as value,
t1.groupe from
table1 t1,table2 t2
where t1.id=t2.id and t1.groupe=2;
try this:
SELECT
T1.id, T2.group, T1.valor - T2.valor AS value
FROM
Table1 T1 INNER JOIN
Table2 T2 ON T1.id = T2.id AND T1.group = T2.group
WHERE (T1.group = 2)

SQL Server - Using Join to show only entries from second table

I am using SQL Server
The data that I have is:
table1:
R_Time ID Q1
2012-02-26 14 8
2012-02-27 14 7
2012-02-27 15 8
2012-02-27 16 9
2012-02-27 11 10
2012-02-28 11 6
2012-02-28 14 10
2012-02-28 15 9
and
table2:
ID Supervisor
11 2
14 2
15 3
16 3
What i am trying to due is only show R_Time and Q1 Entries from table1 where table2 Supervisor is 3
I know I will somehow have to do a join but im not quite sure how to.
Thanks.
Description
You can use a inner join to get this done.
T-SQL INNER JOIN operator can be used in any FROM clause to combine records from two tables.
Sample
Select tbl1.R_Time, tbl1.Q1 from table1 tbl1
inner join table2 tbl2 on tbl2.Id = tbl1.Id
where tbl2.Supervisor = 3
More Information
T-SQL - Inner Join
hope it helps!
select t2.time
from table1 t1
inner join table2 t2
on t1.Id = t2.Id
where t2.Supervisor = 3
Yup, you need to do an inner join:
select
a.r_time, a.q1
from
table1 a (nolock)
inner join table2 b (nolock) on b.id = a.id
where
b.supervisor = 3

SQL Access help in sum from 2 different tables

i have these tables
table 1
id price
1 30
2 40
3 50
table 2
id price
1 70
2 5
3 10
i want a query that would sum the the price value based on the ID
like if table1.id=table2.id then sum table1.price and table2.price
the end result should be something like this
table 3
id price
1 100
2 45
3 60
You can;
SELECT
TABLE1.ID,
TABLE1.PRICE+TABLE2.PRICE
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID;
Or if there are duplicate IDs in either table;
SELECT
TABLE1.ID,
SUM(TABLE1.PRICE+TABLE2.PRICE)
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
GROUP BY TABLE1.ID;