I want to select the data from any of the four tables. Data can be available on any one out of four. Four table also will have data. two table also will have data. one table also will have data. please correct me below.
select top 100 t1.*
from
Table1 t1
left JOIN Table2 t2 on t1.EventId = t2.EventId
LEFT JOIN Table3 t3 ON t1.EventId = t3.EventId
LEFT JOIN Table4 t4 ON t1.EventId = t4.EventId
WHERE
t1.EventId = 12345 AND
t1.EditType = 'D' and
t2.EditType = 'D'and
t3.EditType = 'D' and
t4.EditType = 'D'
Putting the conditions in the WHERE clause turns outer joins into inner joins. Put the conditions in the ON clause
Select *
from Table1 t1
left JOIN Table2 t2
on t1.EventId = t2.EventId
and t2.EditType = 'D'
left JOIN Table3 t3
ON t1.EventId = t3.EventId
and t3.EditType = 'D'
left JOIN Table4 t4
ON t1.EventId = t4.EventId
and t4.EditType = 'D'
where t1.EventId = 12345
and t1.EditType = 'D'
If your tables have the same structure, you might be better doing a union all in a view or CTE (common table expression), then selecting from it instead of doing a left join - that way your information is appearing in separate records:
WITH FullData AS (
SELECT *, 1 AS TableSource FROM Table1
UNION ALL
SELECT *, 2 AS TableSource FROM Table2
UNION ALL
SELECT *, 3 AS TableSource FROM Table3
UNION ALL
SELECT *, 4 AS TableSource FROM Table4 )
SELECT * FROM FullData WHERE EventID = 12345 And EventType = 'D'
As per my example, you can also add in a source identifier to tell which table the information is being pulled from - as long as the structure remains the same throughout, the union all will work fine.
Related
I'm creating 1 temp table (temp1) using table1.
and I want to check if data from temp table is present in table1 and table2.
table1 and table2 have same columns.
It's difficult to assess exactly what you need without further detail, but you could try a LEFT JOIN and a COUNT here to indicate whether there are any matching rows (whereby anything over 0 indicates matching rows)
SELECT
COUNT(*) AS matching_rows
FROM
(
SELECT
1 AS 'ColumnA'
) AS T1
LEFT OUTER JOIN
(
SELECT
2 AS 'ColumnA'
) AS T2
ON T1.ColumnA = T2.ColumnA
WHERE
T2.ColumnA IS NOT NULL
You can also use an INNER JOIN for this:
SELECT
COUNT(*) AS matching_rows
FROM
(
SELECT
1 AS 'ColumnA'
) AS T1
INNER JOIN
(
SELECT
2 AS 'ColumnA'
) AS T2
ON T1.ColumnA = T2.ColumnA
I have 4 tables as shown below
I basically want to get how many users from table1 are in tables 2, 3 and 4. Similarly for table2 I want to get how many users are present in table 1, 3 and 4. and same for tables 3 and 4
Basically all the possible combinations. The final result I want is something as below
One of the way I am trying to solve is by doing a left-join of table1 with other tables to followed by count to get first row of my output. But doing it for all the possible combinations is not optimized. I was looking for any other alternative that is possible
My code for the same
SELECT
COUNT(DISTINCT A.id) table1,
COUNT(DISTINCT B.id) table2,
COUNT(DISTINCT C.id) table3,
COUNT(DISTINCT D.id) table4
FROM table1 A
LEFT JOIN table2 B
ON A.id = B.id
LEFT JOIN table3 C
ON A.id = C.id
LEFT JOIN table4 D
ON A.id = D.id
db-fiddle (This fiddle is for mysql, I am looking for a generic SQL based approach than any db specific approach)
I would recommend:
with t as (
select 'table1' as which, id from table1 union all
select 'table2' as which, id from table2 union all
select 'table3' as which, id from table3 union all
select 'table4' as which, id from table4
)
select ta.which,
sum(case when tb.which = 'table1' then 1 else 0 end) as cnt_table1,
sum(case when tb.which = 'table2' then 1 else 0 end) as cnt_table2,
sum(case when tb.which = 'table3' then 1 else 0 end) as cnt_table3,
sum(case when tb.which = 'table4' then 1 else 0 end) as cnt_table4
from t ta left join
t tb
on ta.id = tb.id
group by ta.which;
Note: This assumes that id is unique in each of the tables. That is a reasonable assumption given the name of the column and the sample data. However, if there are duplicates, you can change the union all in the CTE to union.
This structure also readily generalizes to additional tables.
Use UNION ALL
DEMO
select 'table1' as col1,count(table1.id),count(table2.id),count(table3.id),count(table4.id)
from table1
left join table2 on table1.id=table2.id
left join table3 on table1.id=table3.id
left join table4 on table1.id=table4.id
union all
select 'table2' ,count(table1.id),count(table2.id),count(table3.id),count(table4.id)
from table2
left join table1 on table2.id=table1.id
left join table3 on table2.id=table3.id
left join table4 on table2.id=table4.id
union all
select 'table3' ,count(table1.id),count(table2.id),count(table3.id),count(table4.id)
from table3
left join table1 on table3.id=table1.id
left join table2 on table3.id=table2.id
left join table4 on table3.id=table4.id
union all
select 'table4' ,count(table1.id),count(table2.id),count(table3.id),count(table4.id)
from table4
left join table1 on table4.id=table1.id
left join table2 on table4.id=table2.id
left join table3 on table4.id=table3.id
OUTPUT:
col1 tbl1 tbl2 tbl3 tbl4
table1 8 3 2 2
table2 3 6 1 0
table3 2 1 5 0
table4 2 0 0 4
I'm new to mssql .Here am trying to get values from database by joining three tables .
Table 1:
Table 2 :
Here the image there is a possibility for a single user can have multiple image id form this I need to take any one of the image.
Table 3 :
Here am joining the Table 1 and Table 2 by using H_ID
and Table 2 and Table 3 by using IMG_ID.
What I want to do is Need to get all the colum values from Table 1 and Table 2 But the first URL from the Table 3.
In this case an employee has multiple images in the Table I need to take the 1 URL.
Result should be like this :
Query :
SELECT T1.H_ID AS 'ID',
T1.NAME,
T1.ROLE,
T2.SALARY,
T3.IMAGE
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.H_ID T2.H_ID
JOIN TABLE3 T3
ON T3.IMG_ID = T2.IMG_ID
WHERE T1.STATUS = 'ACTIVE'
Now this query returns 3 rows for the id H_ID = 1001 but It should be a single row.
Can anyone help me to fix this .
use row_number()
with cte as
(SELECT T1.H_ID AS 'ID',T1.NAME,T1.ROLE,T2.SALARY,T3.IMAGE
,row_number() over(partition by T2.img_id order by T3.id) rn
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.H_ID T2.H_ID
JOIN TABLE3 T3
ON T3.IMG_ID = T2.IMG_ID WHERE T1.STATUS = 'ACTIVE'
) select * from cte where rn=1
After you comments it seems you need subquery
select T1.*,T2.sal,a.url
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.H_ID T2.H_ID
left join ( select min(id),img_id,url from table3 group by img_id,url) a
on T2.IMG_ID= a.img_id
WHERE T1.STATUS = 'ACTIVE'
I think, You can simply use OUTER APPLY and TOP 1 for that
SELECT T1.H_ID AS 'ID',
T1.NAME,
T1.ROLE,
T2.SALARY,
T3.IMAGE
FROM TABLE1 T1
JOIN TABLE2 T2 ON T1.H_ID T2.H_ID
OUTER APPLY(SELECT TOP 1 T3.IMAGE
FROM TABLE3 T3 WHERE T3.IMG_ID = T2.IMG_ID
--ORDER BY <column_name> --to take top 1 value in specific order
) T3
WHERE T1.STATUS = 'ACTIVE'
how to show result of "name" in two different columns i,e. it has 2 different values?? from first query I got Admin in name field, while from 2nd Query I got Mobile_user in name field, my desired result will be showing admin and mobile_user in two different fields
SELECT T1.autgro_id id,
T1.name,
T2.use_id
FROM adm_auth_groups_wv T1
INNER JOIN adm_user_auth_groups_wv T2
ON T1.autgro_id=T2.autgro_id
INNER JOIN ym_customers_wv T3
ON T1.cus_id=T3.cus_id
WHERE T3.cus_id_parent = 4
AND T2.use_id =24
UNION All
SELECT T1.autgro_id id,
T1.name,
T2.use_id
FROM adm_auth_groups_wv T1
INNER JOIN adm_user_auth_groups_wv T2
ON T1.autgro_id = T2.autgro_id
WHERE yard_functions_pkg.get_parent_from_cus_id(T1.cus_id) = 4 AND
T1.autgro_id
NOT IN
(SELECT
T2.autgro_id
FROM
adm_user_auth_groups_wv T2
WHERE
T2.use_id=24)
Use a dummy column in both queries, populate one column in the first query and the other in the second.
SELECT T1.autgro_id id,
T1.name admin_user,
null mobile_user,
T2.use_id
FROM adm_auth_groups_wv T1
INNER JOIN adm_user_auth_groups_wv T2
ON T1.autgro_id=T2.autgro_id
INNER JOIN ym_customers_wv T3
ON T1.cus_id=T3.cus_id
WHERE T3.cus_id_parent = 4
AND T2.use_id =24
UNION All
SELECT T1.autgro_id id,
null admin_user,
T1.name mobile_user,
T2.use_id
FROM adm_auth_groups_wv T1
INNER JOIN adm_user_auth_groups_wv T2
ON T1.autgro_id = T2.autgro_id
WHERE yard_functions_pkg.get_parent_from_cus_id(T1.cus_id) = 4 AND
T1.autgro_id
NOT IN
(SELECT
T2.autgro_id
FROM
adm_user_auth_groups_wv T2
WHERE
T2.use_id=24)
I've two tables T1 and T2 with the following columns -
T1
Project_ID
Category
Column_X
Column_Y
Column_Z
T2
Proj_ID
Category
Parent_Project_ID
I want to write a query to get records from T1 with the following condition -
Get Projects with Category = "A" from T1
Get child projects of the above filtered projects
I'm not sure how to check the second condition only with the results coming out of first condition.
What is needed?
Projects from T1 where Category is A
Child projects of projects obtained from condition 1
Adding sample data and desired results as requested -
To get all records from second table then you can use the following query.
SELECT
t2.*
FROM T1 t1
RIGHT OUTER JOIN T2 t2 ON t1.Project_ID = t2.Project_ID
WHERE t1.Category = "A"
SELECT * FROM T2 WHERE T2.Proj_ID IN ( SELECT Project_ID FROM T1 WHERE Category = 'A' )
This should do the job needed.
SELECT * from T2 as d
WHERE EXISTS ( SELECT * from T1 as d1 where d1.Category = 'A' and d1.Project_ID = d.Proj_ID )
SELECT * from T1 as d1 right join T2 as d2 on d1.Project_ID = d2.Proj_ID
WHERE d1.CodTert = 500
I've made an update, these query give the same result, one uses the JOIN one doesn't.
I'm assuming that T2.Parent_Project_ID and T1.Project_ID are related. If so, you can use this:
Select T3.*
From T1
Join T2 On T2.Parent_Project_ID = T1.Project_ID
Join T1 T3 On T3.Project_ID = T2.Proj_ID
Where T1.Category = 'A'
This would get only child projects of projects that have a category of 'A'.
EDIT:
Based on the output format that has been added to the question, the following query, which uses a LEFT OUTER JOIN would render the exact result required:
SELECT
T2.PROJ_ID Project_ID,
T2.Category,
T1.Column_X,
T1.Column_Y,
T1.Column_Z,
T2.Parent_Project_ID
FROM T1 T1_PARENTS
INNER JOIN T2
ON T2.Parent_Project_ID = T1.Project_ID and T1.Category = 'A'
INNER JOIN T2 T2_CHILDREN
ON T2_CHILDREN.PROJ_ID = T2.Parent_Project_ID OR T2_CHILDREN.Parent_Project_ID = T2.Parent_Project_ID
LEFT OUTER JOIN T1
ON T2_CHILDREN.PROJ_ID = T1.Project_ID;