I need to build a query to get the following details.
Table 1
Code Text
1 A
2 B
3 C
Table 2
Code Min Max
1 1.00 1.75
2 1.76 2.25
3 2.26 3.00
Table 3
Eid Value
1234 1.2
3456 2.56
I am looking at a query which gives me the following output in a single SQL query.
Table 3, should look at the Value, compare the value in Table 2 to see if it lies between Min and max and get the equivalent code and get the code compare with the Table 1 and get the final Text value.
Final Output
Eid Text
1234 A
3456 C
here is a way to do this
select t3.eid,t1.text
from t3
join t2
on t3.value between t2.min and t2.max
join t1
on t2.code=t1.code
You can try the below -
select eid,text
from table3 t3 inner join table2 t2 on t3.Value>=t2.min and t3.Value<=t2.max
inner join table1 t1 on t1.code=t2.code
Related
I have a table T1 as below
product_id val
123,567 5
999 4
999 3
and another table T2,
t_product_id // this maps to product_id in above table
123
999
In the final output, for t_product_id in table T2 I have to get value for it from T1. For duplicate product_ids (999) I want to get the min value, and for 123 I want to get 5
This is how output should look like
product_id value
123 5
999 3
My query ->
select t1.product_id, min(t1.value)
from T1 t1
group by t1.product_id
I am not sure what needs to be done next. How to separate comma separated values and check if 123 from T2 exists in T1 and get the value for it
it's not possible to keep only one product_id per row in table T1?
I think this would simplify matters for you. T1 would be:
123 | 5
567 | 5
999 | 4
999 | 3
Use join to only select the ids that exist in T2
select t1.product_id, min(t1.value)
from T1 t1 join T2 on (t1.product_id = t2.t_product_id)
group by t1.product_id
I have the following tables:
T1 T2 Desired result
CA CB CA CC CA CB
1 2 1 3 1 4
1 4 1 2 2 1
1 3 1 5 2 3
2 1 2 4
2 3
3 6
3 1
4 ...
I need to make a join between T1 and T2 (using column CA) and return only those rows which the values in CB do not exists in T2.CC
A simple way to achieve that is using the following query:
SELECT T1.* FROM T1 INNER JOIN T2 ON t1.CA = t2.CA AND
t1.CB NOT IN (SELECT CC FROM T2 WHERE T2.CA = T1.CA)
I think the previous query is not very efficient. For that reason I am looking for something better
Any help will be appreciated
Generally, a more efficient means of achieving this sort of result is finding records which fail a simpler join condition. Those can be found by doing an outer join and checking for null, as follows:
select t1.ca, t1.cb
from t1 left outer join t2 on t1.ca=t2.ca and t1.cb=t2.cc
where t2.ca is null;
I think you just want not exists:
select t1.*
from t1
where not exists (select 1
from t2
where t2.ca = t1.ca and t2.cb = t1.cb
);
For performance, you want an index on t2(ca, cb).
Why does this query produce duplicates in some scenarios?
Table_1
ID
1
2
3
Table_2
ID
1
2
4
Table_3
ID
1
3
4
Query:
SELECT COALESCE(Table_1.ID, Table_2.ID, Table_3.ID)
FROM Table_1
FULL OUTER JOIN TABLE_2
ON Table1.ID=Table_2.ID
FULL OUTER JOIN TABLE_3
ON Table1.ID=Table3.ID;
Result:
1
2
3
4
4
The query duplicates all values where T1 is null and T2/T3 share the same value. Duplicates are removed for any other combination.
This is a little bit hard to explain. If you show the other ids, you will see the full range of what happens:
"coalesce" "id1" "id2" "id3"
1 1 1 1
2 2 2 .
3 3 . 3
4 . 4 .
4 . . 4
You can see the results here.
So, You get one row because t1 & t2 create a row with t2.id = 4 and t1.id = null when they don't match. Then, you get the same thing when t3.id = 4. The comparison is to t1.id -- so you get another row. There is no comparison to t2.id.
I suspect that you intend logic more like this:
select coalesce(t1.id, t2.id, t3.id)
from t1 full join
t2
using (id) full join
t3
using (id);
Here is the SQL Fiddle.
Can anyone help to get this result in 1 sql statement?
I have 2 tables with the 2nd table having dominance over the first -- as follows:
TableA
Code | Quantity
1 5
3 5
4 5
TableB
Code | Quantity
1 5
2 1
4 6
Query Result
Code | Quantity
1 5 - code exists in both - return record from TableB
2 1 - distinct only in TableB
3 5 - distinct only in TableA
4 6 - code exists in both - return record from TableB
Appreciate your help. Thanks!
A FULL JOIN will do:
SELECT COALESCE(t1.Code, t2.Code) AS Code,
COALESCE(t2.Quantity, t1.Quantity) AS Quantity
FROM TableA AS t1
FULL OUTER JOIN TableB AS t2 ON t1.code = t2.code
COALESCE(t2.Quantity, t1.Quantity) essentially implements your requirement for 'dominance' of TableB over TableA: Quantity is picked up from TableA only in case there is no matching record in TableB.
For an explanation on how a FULL JOIN works you can have a look here:
FULL OUTER JOIN, includes all rows from both tables, regardless
of whether or not the other table has a matching value.
I have two tables in both the table common field name is SID
Table1:
sp id
ram 1
varun 2
santosh 3
krishnan 4
Table2:
id wv rf
1 6.2 1.2
1 3.2 1.4
1 1.4 5.2
1 1.5 3.2
2 5.4 6.2
2 3.5 6.3
I want to filter the rows in the second table where ID value of both tables are matched, if i select ram in sp it will display the values of id =1 in wv and rf in table 2.
How to make query for this, Is there any examples ple sensd me...
I think you need this
SELECT T1.sp,T2.wv,T2.rf
FROM Table1 T1 JOIN Table2 T2 ON T1.id=T2.id
Joins Between Tables
Update
SELECT T1.sp,T2.wv,T2.rf
FROM Table1 T1 JOIN Table2 T2 ON T1.id=T2.id
WHERE T1.sp='ram'
Try this:
select s.sp, p.wv, p.rf
from table1 s
inner join table2 p
on s.id=p.id