SQL query which give unique entry only once in query table - sql

Basically I have 2 tables 1 OR 2 which is showed in below image.
i want to joint these 2 tables but the value column should be reflect only once in first entry only.
I have basic knowledge about SQL but please guide how can i get the required output as per the criteria which mention above.

You can do this with a left join:
select t2.*, t1.value
from table2 t2 left join
table1 t1
on t1.code = t2.code and t2.type = 'A';

Related

SQL join three tables, get value from second, if null, then from thrid

I am using SQL Server 2019.
I have three tables.
I want to get the value from second table using inner join, but if the value in second table does not exist, then the value should be retrieved from third table.
Enter image description here
I came up with the SQL below, but this returns no data.
Select
Table1.ID,
Case
When Table2.Value2 Is Not Null Then Table2.Value2
Else Table3.Value3
End as Value
from Table1
Inner Join Table2 On Table1.ID = Table2.ID2
Inner Join Table3 On Table1.ID = Table3.ID3
I googled, but I could not reach the answer.
Any help is appreciated, thank you.
We can use the COALESCE() function here:
SELECT t1.ID, COALESCE(t2.Value2, t2.Value3) AS Value
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID2
LEFT JOIN Table3 t3 ON t1.ID = t3.ID3;
A left, rather than inner, join is required in the event that the second table does not have a matching ID. In this case, the query retains all IDs from the first table and gives the chance for a match from the third table.

Does increasing the number of fields in JOIN statement increase/decrease the speed of execution?

I have two tables with 3.5 million rows of data. I am creating a left join between the two to create a new view.
Code 1:
SELECT t1.c1,t1.c2,t2.c3,t2.c4
from table1 as t1
left join table2 as t2
on t1.Location=t2.Location and t1.OrderNumber=t2.OrderNumber and t1.Customer=t2.Customer
Code 2:
SELECT t1.c1,t1.c2,t2.c3,t2.c4
from table1 as t1
left join table2 as t2
on t1.OrderNumber=t2.OrderNumber
Both snippets of code give the same desired result as the Order number field in table 2 has only unique values.
Is it better to give more fields to JOIN compared to only one?
SELECT t1.c1,t1.c2,t2.c3,t2.c4
from table1 as t1
left join table2 as t2
on t1.Location = t2.Location
and t1.OrderNumber = t2.OrderNumber
and t1.Customer = t2.Customer
If OrderNumber is the PK of either table then adding additional fields will not change the results and it will not improve performance unless an index as not present on the other side.
If Order number field in table 2 has only unique values it would not change the query. If it is a PK or has a unique constraint/index then addition fields would not help unless what Table2.OrderNumber was joined to was not indexed.

Clarification on retrieval of Distinct rows based on a column

SQL Server 2008 R2
I have a table - T1 with id, Title, Firstname, postalcode
Second table - T2 with id, Title and PostalCode.
id is the primarykey in T1 and id is corresponding foreign key in T2.
Now i want to list out the Title from T1, Title from T2 and their id's for the matching id between T1 and T2.
BUT THE MAIN thing is only distinct column values of Title and their correspoding tables T1 and T2 along with their id should be displayed.
For example, If a value 'Mr' is found and if second time if it is found, the value shouldnt be listed again.
Hope iam clear. Please advise.
COALESCE() function could be your friend here.
The Example is a little convoluted, but i think i understand the question.
you will like want to use the Distinct key word:
SELECT DISTINCT T1.Title
FROM T1 INNER JOIN T2 ON T1.id = T2.id
UNION ALL
SELECT DISTINCT T2.Title
FROM T1 INNER JOIN T2 ON T1.id = T2.id
This should grab distinct titles from T1 and T2. I hope this is what you were looking for, if not please describe what you are looking for as far as results a little bit more clearly, if you could add a table view that would be ideal.
Thanks,
~Madullah

SQL nested query

I have a table like below
id name dependency
-----------------------
1 xxxx 0
2 yyyy 1
3 zzzz 2
4 aaaaaa 0
5 bbbbbb 4
6 cccccc 5
the list goes on. I want to select group of rows from this table , by giving the name of 0 dependency in where clause of SQL and till it reaches a condition where there is no more dependency. (For ex. rows 1,2, 3 forms a group, and rows 4,5,6 is another group) .please help
Since you did not specify a product, I'll go with features available in the SQL specification. In this case, I'm using a common-table expression which are supported by many database products including SQL Server 2005+ and Oracle (but not MySQL):
With MyDependents As
(
Select id, name, 0 As level
From MyTable
Where dependency = 0
And name = 'some value'
Union All
Select T.id, T.name, T.Level + 1
From MyDependents As D
Join MyTable As T
On T.id = D.dependency
)
Select id, name, level
From MyDependents
Another solution which does not rely on common-table expressions but does assume a maximum level of depth (in this case two levels below level 0) would something like
Select T1.id, T1.name, 0 As level
From MyTable As T1
Where T1.name = 'some value'
Union All
Select T2.id, T2.name, 1
From MyTable As T1
Join MyTable As T2
On T2.Id = T1.Dependency
Where T1.name = 'some value'
Union All
Select T3.id, T3.name, 2
From MyTable As T1
Join MyTable As T2
On T2.Id = T1.Dependency
Join MyTable As T3
On T3.Id = T2.Dependency
Where T1.name = 'some value'
Sounds like you want to recursively query your table, for which you will need a Common Table Expression (CTE)
This MSDN article explains CTEs very well. They are confusing at first but surprisingly easy to implement.
BTW this is obviously only for SQL Server, I'm not sure how you'd achieve that in MySQL.
This is the first thing that came to mind. It can be probably done more directly/succinctly, I'll try to dwell on it a little.
SELECT *
FROM table T1
WHERE T1.id >=
(SELECT T2.id FROM table T2 WHERE T2.name = '---NAME HERE---')
AND T1.id <
(SELECT MIN(id)
FROM table T3
WHERE T3.dependency = 0 AND T3.id > T2.id)
If you can estimate a max depth, this works out to something like:
SELECT
COALESCE(t4.field1, t3.field1, t2.field1, t1.field1, t.field1),
COALESCE(t4.field2, t3.field2, t2.field2, t1.field2, t.field2),
COALESCE(t4.field3, t3.field3, t2.field3, t1.field3, t.field3),
....
FROM table AS t
LEFT JOIN table AS t1 ON t.dependency = t1.id
LEFT JOIN table AS t2 ON t1.dependency = t2.id
LEFT JOIN table AS t3 ON t2.dependency = t3.id
LEFT JOIN table AS t4 ON t3.dependency = t4.id
....
This is a wild guess just to be different, but I think it's kind of pretty, anyway. And it's at least as portable as any of the others. But I don't want to look to closely; I'd want to use sensible data, start testing, and check for sensible results.
Hierarchical query will do:
SELECT *
FROM your_table
START WITH id = :id_of_group_header_row
CONNECT BY dependency = PRIOR id
Query works like this:
1. select all rows satisfying START WITH condition (this rows are roots now)
2. select all rows satisfying CONNECT BY condition,
keyword PRIOR means this column's value will be taken from the root row
3. consider rows selected on step 2 to be roots
4. go to step 2 until there are no more rows

How do I Write a SQL Query With a Condition Involving a Second Table?

Table1
...
LogEntryID *PrimaryKey*
Value
ThresholdID - - - Link to the appropriate threshold being applied to this log entry.
...
Table2
...
ThresholdID *PrimaryKey*
Threshold
...
All fields are integers.
The "..." thingies are there to show that these tables hold a lot more imformation than just this. They are set up this way for a reason, and I can't change it at this point.
I need write a SQL statement to select every record from Table1 where the Value field in that particular log record is less than the Threshold field in the linked record of Table2.
I'm newish to SQL, so I know this is a basic question.
If anyone can show me how this SQL statement would be structured, it would be greatly appreciated.
SELECT T1.*
FROM Table1 T1
JOIN Table2 T2 ON T2.ThresholdID = T1.ThresholdID
WHERE T2.Threshold > T1.Value
SELECT t1.*
FROM dbo.Table1 t1 INNER JOIN dbo.Table2 t2 ON t1.ThresholdID = t2.ThresholdID
WHERE t2.Threshold > t1.Value
SELECT * from table1 t1 join table2 t2 on (t1.thresholdId = t2.thresholdId)
where t1.value < t2.threshold;
SELECT t1.LogEntryID, t1.Value, t1.ThresholdID
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.ThresholdID = t2.ThresholdID
WHERE t1.Value < t2.threshold
SELECT * FROM Table1
JOIN Table2
ON table1.ThresholdID = table2.ThresholdID --(assuming table 2 holds the same value to link them together)
WHERE
value < thresholdvalue
A 'JOIN' connects 2 tables based on the 'ON' clause (which can be multipart, using 'AND' and 'OR')
If you have 3 entries in table 2 which share table1's primary key (a one-to-many association) you will receive 3 rows in your result set.
for the tables below, for example:
Table 1:
Key Value
1 Hi
2 Bye
Table 2:
Table1Key 2nd_word
1 You
1 fellow
1 friend
2 now
this query:
SELECT * FROM Table1
JOIN Table2
on table1.key = table2.table1key
gets this result set:
Key Value Table1Key 2nd_word
1 Hi 1 You
1 Hi 1 fellow
1 Hi 1 friend
2 Bye 2 now
Note that JOIN will only return results when there is a match in the 2nd table, it will not return a result if there is no match. You can LEFT JOIN for that (all fields from the second table will be NULL).
JOINs can also be strung together, the result from the previous JOIN is used in place of the original table.