I have a tree tables table_1, table_2 and table_3, its structures its
table_1
table_2
table_3
In table_1 I have a unit_id and it is general, I want to select data in all tables, for example unit_id = 5 used in table_2 and table_3 and buildings rows is six, parcel rows is two, I want to select six rows building_id, building_area and beside two rows parcel_id and parcel_area, for unit_id = 6 conversely 4 rows parcels and two rows buildings, and one row buildings when unit_id = 7 and one row parcel when unit_id = 8
select result example
unit_id building_id building_area parcel_id parcel_area
5 2 20 15 20
5 3 10 null null
5 4 30 null null
5 5 15 16 10
5 7 25 null null
5 8 15 null null
6 null null 21 30
6 null null 22 50
6 9 18 23 80
6 10 20 24 70
7 30 10 null null
8 null null 27 52
Seems like you want to left join table_2 and table_3 to table_1.
SELECT t1.unit_id,
t2.building_id,
t2.area building_area,
t3.parcel_id,
t3.area parcel_area
FROM table_1 t1
LEFT JOIN table_2 t2
ON t2.unit_id = t1.unit_id
LEFT JOIN table_3 t3
ON t3.unit_id = t1.unit_id;
You do want a left join between all three tables, but by looking at your sample data it seems like you need the relationship between tables 2 and 3 to include the area.
That would be :
SELECT
t1.unit_id,
t2.building_id,
t2.area AS building_area,
t3.parcel_id,
t3.area AS parcel_area
FROM
table_1 AS t1
LEFT JOIN table_2 AS t2
ON t2.unit_id = t1.unit_id
LEFT JOIN table_3 AS t3
ON t3.unit_id = t1.unit_id
AND t3.area = t2.area
Related
I have a table with records of the same type with ID's and a grouping. I need to update the id's of table 1 into table 2 based on the record id from largest to smallest using the position.
Table 1
ID
Group
Name
1
A
Apple
2
A
Apple
3
B
Apple
4
B
Apple
Table 2
ID
recordid
position
group
1
1
250
A
2
2
350
A
3
null
450
A
4
null
550
A
5
3
250
B
6
4
350
B
7
null
450
B
8
null
550
B
update table2
set recordid=T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
This isn't distinct so I don't think it's right, but I can't figure it out.
where ?????
the problem is you have multiple Id per group , so you have to choose one :
update table2
set recordid= ( select top 1 T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
)
where recordid is null
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
Hi I have two tables structure is like this
Table 1
Customer Company price qty item invno
1 a 89 8 item1 23
2 b 80 4 item2 22
3 c 90 3 item1 45
4 d 19 6 item3 12
table 2
Customer Company price qty item invno
1 a 89 8 item1 23
2 b 80 4 item2 18
3 c 90 3 item1 45
4 d 19 6 item3 15
basically table1 contains the current records and table2 current+past records and they both have other columns
what i want is get the all records from the table1 and table2 but in case of the duplication of invno i need that record from the table1 in this case resultset will contains the invno-23(table1),22(table1),45(table1),12(table1),18(table2),15(table2)
I tried using UNION but it gives different results on different column selection i stuck here any help would be great .
Here is one method, using union all and a filter:
select *
from table1 t1
union all
select *
from table2 t2
where not exists (select *
from table1 t1
where t1.invno = t2.invno
);
SELECT t2.Customer,
t2.Company,
t2.price,
t2.qty,
t2.item,
IFNULL(t1.invno,t2.invno)
FROM table2 AS t2
LEFT JOIN table1 AS t1
ON t2.Customer=t1.Customer
I have 2 tables with the same structure.
FIELD 1 INT
FIELD 2 VARCHAR(32) -- is a MD5 Hash
The query has to get matching FIELD 1 pairs from for records that have the exact combination of values for FIELD 2 in both TABLE 1 and TABLE 2.
These tables are pretty large ( 1 million records between the two ) but are deduced down to an ID and a Hash.
Example data:
TABLE 1
1 A
1 B
2 A
2 D
2 E
3 G
3 H
4 E
4 D
4 C
5 E
5 D
TABLE 2
8 A
8 B
9 E
9 D
9 C
10 F
11 G
11 H
12 B
12 D
13 A
13 B
14 E
14 A
The results of the query should be
8 1
9 4
11 3
13 1
I have tried creating a concatenated string of FIELD 2 using a correlated sub-query and FOR XML PATH string trick I read on here but that is very slow.
You can try following query also -
SELECT t_2.Field_1, t_1.Field_1 --1
FROM table_1 t_1, table_2 t_2 --2
WHERE t_1.Field_2 = t_2.Field_2 --3
GROUP BY t_1.Field_1, t_2.Field_1 --4
HAVING COUNT(*) = (SELECT COUNT(*) --5
FROM Table_1 t_1_1 --6
WHERE t_1_1.Field_1 = t_1.Field_1) --7
AND COUNT(*) = (SELECT COUNT(*) --8
FROM Table_2 t_2_1 --9
WHERE t_2_1.Field_1 =t_2.Field_1) --10
Edit
First the requested set of result is the combination of Field1 from both the tables where respective Field2 is exactly same.
so for that you can use one method which I have posted above.
Here
query will take the data from both the table based on field2 values (from line 1 to line 3)
then it will group the data based on field1 from table1 and field1 from table2 (line 4)
till this step you will get the result having field1 from table1 and field2 from table2 where it exists (at least one) matching based on field2 from tables for respective field1 values.
after this you just need to filter the result for correct (exactly same values for field2 values for respective field1 column value). so that you can make condition on row count.
here my assumption is that you don't have multiple values for field1 and field2 combination in either tables
means following rows will not be present -
1 b
1 b
In any of the tables.
if so, the rows count got for table1 and table2 for same field2 values should be match with the rows present in table1 for field1 and same rows only should present in tables2 for field2 value.
for this condition query has condition on count(*) in having clause (from line 5 to line 10).
Let me try to explain this version of the query:
select t1.field1 as t1field1, t2.field1 as t2field1
from (select t1.*,
count(*) over (partition by field1) as NumField2
from table1 t1
) t1 full outer join
(select t2.*,
count(*) over (partition by field1) as NumField2
from table2 t2
) t2
on t1.field2 = t2.field2
where t1.NumField2 = t2.NumField2
group by t1.Field1, t2.Field1
having count(t1.field2) = max(t1.NumField2) and
count(t2.field2) = max(t2.NumField2)
(which is here at SQLFiddle).
The idea is to compare the following counts for each pair of field1 values.
The number of field2 values on each.
The number of field2 values that they share.
All of these have to be equal.
Each subquery counts the number of values of field2 on each field1 value. For the first rows of your data, this produces:
1 A 2
1 B 2
2 A 3
2 D 3
2 E 3
. . .
And for the second table
8 A 2
8 B 2
9 E 3
9 D 3
9 C 3
Next, the full outer join is applied, requiring a match on both the count and the field2 value. This multiplies the data, producing rows such as:
1 A 2 8 A 2
1 B 2 8 B 2
2 A 3 NULL NULL NULL
2 D 3 9 D 3
2 E 3 9 E 3
NULL NULL NULL 9 C 3
And so on for all the possible combinations. Note that the NULLs appear due to the full outer join.
Note that when you have a pair, such as 1 and 8 that match, there are no rows with NULL values. When you have a pair with the same counts but they don't match, then you have NULL values. When you have a pair with different counts, they are filtered out by the where clause.
The filtering aggregation step applies these rules to get pairs that meet the first condition but not the second.
The having essentially removes any pair that has NULL values. When you count() a column, NULL values are not included. In that case, the count() on the column is fewer than the number of values expected (NumField2).
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.