I have a table projects in which I have projectid and projectname.
Each project has multiple users and the table is project_users and the fields are projectuserid, projectid.
Also each project has multiple solutions and the table is project_solutions and the fields are projectsolutionid, projectid.
Again each solution has multiple users these are stored in the table solution_users and the fields are solutionuserid, solutionid, userid
There are master tables here users for users with fields userid, username and solutions for solutions with fields solutionid, solutionname.
I am trying to get the output in single row where the first column will be projectid and second column is the respective users of the project in an array or json format the third is solutions for the project and the fourth is solution users
The tables are as below
projects
projectid projectname
1 abc
2 xyz
users
userid username image(data type image)
1 user1 04949949499994
2 user2 3434jj34kjd3434
3 user3 8934u34kj343434
solutions
solutionid solution_name
1 sol1
2 sol2
3 sol3
project_users
id projectuserid projectid
1 1 1
2 2 1
3 3 2
project_solutions
id solutionid projectid
1 1 1
2 3 1
3 2 2
project_solution_users
id projectsolutionid userid
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
The output which I am expecting is like below
{
projectid:1
users: [user1, user2],
solutions: [ sol1, sol3 ],
user_images: [04949949499994, 3434jj34kjd3434]
}
{
projectid:2
users: [user3],
solutions: [ sol2 ],
user_images: [04949949499994, 3434jj34kjd3434, 8934u34kj343434]
}
This is what I tried
select p.projectid, ps.solutionid, cast(cast (uu.image as varbinary) as varbinary) as userimage,
from projects as p
left join project_users as pu on pu.projectid = p.projectid
left join users as u on u.userid = su.userid
left join project_solutions as ps on ps.projectid = p.projectid
left join solutions as s on s.solutionid = ps.solutionid
left join project_solution_users as su on su.projectsolutionid = ps.solutionid
left join users as uu on uu.userid = su.userid
group by p.projectid, s.solution_name, cast(cast (uu.image as varbinary) as varbinary)
for json path, without_array_wrapper
But it generates multiple rows.
How can I achieve this using SQL Server? I have tried using inner joins and sub queries but I got stuck and not getting the respective output.
select t1.projectid,
username,
user_image,
solution_name
from dbo.project_solutions t1
left join dbo.project_users t2 on t2.projectid = t1.projectid
left join dbo.users t3 on t3.userid = t2.projectuserid
left join dbo.solutions t4 on t4.solutionid = t1.solutionid
for json auto
Related
Using SQL Server Management Studio 2016 (v 13.0).
I have two tables with two distinct keys I can use to join, with the catch being that there are mixed NULLs in both PK columns:
Table 1 Table 2
Acct1 App1 Acct2 App2 Product
----------- ----------------------
1 A NULL A Bed
2 B 2 B Sofa
3 C 3 NULL Bed
4 D 4 D Bed
Desired result in the joined table, only including those where Product = Bed:
Acct App Product
1 A Bed
3 C Bed
4 D Bed
Thank you!
While I agree #d219's answer should be the correct solution, a different approach could use an or in the join such like:
select Acct1,App1,Product
from table1 inner join table2
on App1=App2 or Acct1=Acct2
where Product='Bed'
See this post for discussion on using the or join.
One way would be to do two separate SELECT statements using each key and then UNION them, something like this:
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Acct1 = t2.Acct2
WHERE t2.Product = 'Bed'
UNION
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.App1 = t2.App2
WHERE t2.Product = 'Bed'
I am trying to learn SQL and want to understand a scenario where i can Join 2 separate queries.
I have my APP_MTRX table would join with Cust Table to retrieve all the records whose TYPE_ORD_NUM = 0 or 1
I need to join ADD_DTLS Table with SHR_ADR table based on SHR_ADR_ID and need to retrieve all the columns whose adr_type_id is either 0 or 1 again
joining the result of 1 and 2
below is my SQL
select * from app_mtrx ABC
left join cust on cust.cust_id = ABC.cust_id and abc.cust_ty_ord = 0
left join cust BBC on cust.cust_id = BBC.cust_id and abc.cust_ty_ord = 1
left join add_dtls DEF ON DEF.cust_id=BBC.cust_id
left join shr_adr SHR on shr.shr_adr_id = def.shr_Adr_id
Can you please suggest if this is the correct approach and also if it is joining my 1 & 2 correctly....
Try this:
SELECT * FROM APP_MTRX am INNER JOIN Cust c
ON am.cust_id = c.cust_id
AND am.cust_ty_ord in (0,1)
INNER JOIN add_dtls ad
ON ad.cust_id=c.cust_id
INNER JOIN shr_adr sh
ON ad.shr_adr_id = sh.shr_Adr_id
AND ad.adr_type_id in (0,1)
The main table want to join two time with the reffrence table twice with the same column.
The main table column is insert with integer and need to join with the refference table to get the character back to show back for user.
it show none of records by the sql below, why?
main table BD_BRAND
Sports_BR Leather_BR
2 1
Reffrence table BD_REF
ID NME REF_TYPE
1 NIKE Sports_Brand
2 ADIDAS Sports_Brand
3 PUMA Sports_Brand
1 CLACKS Leather_Brand
2 LOTTUSSE Leather_Brand
3 CHEANEY Leather_Brand
SELECT B.NME AS Sports_BR, C.NME AS Leather_BR
FROM BD_BRAND A
LEFT JOIN BD_REF B on B.ID = A.Sports_BR
LEFT JOIN BD_REF C on C.ID = A.Leather_BR
The Result i want as below:
Sports_BR Leather_BR
ADIDAS CLACKS
I think you're looking for:
SELECT B.NME AS Sports_BR, C.NME AS Leather_BR
FROM BD_BRAND A
LEFT JOIN BD_REF B on B.ID = A.Sports_BR and B.REF_TYPE = 'Sports_Brand'
LEFT JOIN BD_REF C on C.ID = A.Leather_BR and B.REF_TYPE = 'Leather_Brand'
Your ID's are present multiple times due to the different reference types, so you need to specify which ID to grab by the reference type.
on sqlServer i have 2 tables:
table1: Students => studentName=david, class1Id=2,class2Id=4
table2: classes=> classId=2, className="class1"
classId=4, className="class2"
class1Id and class2Id relate to classes.classId
i want to do sql query to get :
studentName=david, className1="class1",className2="class2",
I know to do join between 2 table but not like that
thanks!
You just need to JOIN to Classes twice:
Select S.StudentName,
C1.ClassName As ClassName1,
C2.ClassName As ClassName2
From Students S
Join Classes C1 On C1.ClassId = S.Class1Id
Join Classes C2 On C2.ClassId = S.Class2Id
I have the following issue :
table # 1 structure :
Requests table for example have 2 columns :
Request_ID Branch_Name
1 xx
2 yy
3 xx
4 xx
5 yy
The second table : Requests_Items for example have the following columns
Request_ID Price
1 100
1 30
1 450
2 10
2 18
So i want my result set to be like :
Branch Name Num_of_Requests Total_Price
xx 3 580
Can you please help
select r.Branch_Name,
count(r.request_id) as Num_of_Requests,
sum(i.price) as Total_Price
from Requests r
left join Requests_Items i on i.Request_ID = r.Request_ID
group by r.Branch_Name
jurgen d was almost there
select r.Request_Id, r.Branch_Name,
COUNT(i.Price) as [Number of Requests],
SUM(i.Price) as [Total]
from Requests r
left join Requests_Items i
on r.Request_Id = i.Request_Id
group by r.Request_Id, r.Branch_Name
select r.Branch_Name, COUNT(ri.Request_ID) as Num_of_Requests, SUM(ri.Price) as Total_Price
from dbo.Requests r
left join dbo.Requests_Items ri
on r.Request_ID = ri.Request_ID
group by r.Branch_Name
Let this is my [Employees] Table
Here is my Dept table
I want the output like this using join and group by
So here is the Query for this-
select max(e.salary) as maxsalary,d.Dept from Employees e inner join DEpt d on e.DeptId=d.id group by e.DeptId,d.Dept
enter code here