Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Table 1
Id(Key) Name
1 XYZ
2 ABC
Table 2
ID(Key) Bank
1 SC
2 CP
Table 3
Id(no key) month year noofduty
1 03 12 20
2 04 12 22
1 03 12 25
2 04 12 15
Required Result
ID Name Bank TotalDuty
1 XYZ SC 42
2 ABC CP 40
I am confused with GroupBy in multiple tables, a help is required. using SQL 2000 Server
Here is a SQL Fiddle with a Demo
SELECT t1.id
, t1.name
, t2.bank
, sum(t3.noofduty) as totalduty
FROM t1
INNER JOIN t3
on t1.id = t3.id
INNER JOIN t2
ON t2.id = t3.id
GROUP BY t1.id, t1.name, t2.bank
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
For example in this simple table:
Group
Element
xxx
a
xxx
b
xxx
c
xxx
d
xxx
e
yyy
a
yyy
b
yyy
f
yyy
g
zzz
a
zzz
b
zzz
c
zzz
g
'a' and 'b' are the combination of elements present the most in all groups
You can do it with a triangle join
with c1 as (
select t1.grp, t1.Element e1, t2.Element e2
from tbl t1
join tbl t2 on t1.grp = t2.grp and t1.Element < t2.Element
)
select e1,e2
from c1
group by e1,e2
order by count(*) desc
limit 1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Table 1 Table2
Name Month Cast Month
-------------------------- ----------------
Client1 Jan 200 Jan
Client1 Feb 150 Feb
Client2 Jan 110 Mar
Client2 Apr 120 .
.
Dec
Ouput
-----------------------
Name Month Cast
--------------------------
Client1 Jan 200
Client1 Feb 150
Client1 Mar 0
Client1 Apr 0
...
....
Client2 Jan 110
Client2 Feb 0
Client2 Mar 0
Client2 Apr 120
Client2 May 0
.....
....
Result must show all clients with all the months (if not cast to 0) which may not present in table 1.
I've tried right outer and cross join...no result.
You can try this below script-
SELECT A.Name,B.month,COALESCE(C.Cast,0) Cast
FROM (
SELECT DISTINCT 1 CN, Name FROM Table1
) A
INNER JOIN (
SELECT 1 CN, month FROM Table2
) B ON A.CN = B.CN
LEFT JOIN Table1 C
ON A.Name = C.Name
AND B.month = C.Month
You have to create the Cartesian product first (cte ClientMonth)- then join back to the original table to get the data you want...
With ClientMonth as (
select Distinct Name,t2.[Month]
from Table_1 t1
Cross Join Table_2 t2
)
Select cm.Name, cm.[Month], isnull(amt,0)
from ClientMonth cm
Left Join Table_1 t3 on t3.Name = cm.Name and t3.Month = cm.Month
Try it.
SELECT name, m1 as month ,max(cast) as cast from
(
SELECT [id]
,[name]
,b.month as m1
,case when a.month =b.month then [cast] else 0 end as cast
FROM [Table_1] a,[Table_2] b
)c
group by name,m1
order by name
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a two tables:
Employee:
EmployeeID First Name Last Name
------------------------------------
1 ABC XYZ
2 DEF LMN
3 GHI OPQ
Conversation:
ConversationId FromUser ToUser
-----------------------------------
11 1 2
22 1 3
33 2 1
44 3 2
I want output as follows
ConversationId FromUserId ToUserId FromUser ToUser
---------------------------------------------------------------
11 1 2 ABC DEF
22 1 3 ABC GHI
33 2 1 DEF ABC
44 3 2 GHI DEF
JOIN the Employee table twice, once for from user, and once for to user.
select c.*, fu.firstname, tu.firstname
from conversation c
join Employee fu on c.FromUser = fu.EmployeeID
join Employee tu on c.ToUser = tu.EmployeeID
SELECT A.* , B.FIRSTNAME FromUser , C.FIRSTNAME ToUser FROM
conversation A LEFT JOIN EMPLOYEE B ON
A.FromUser = B.EmployeeID
LEFT JOIN
EMPLOYEE C ON A.ToUser = B.EmployeeID
Try like this,
SELECT c.conversationId
,c.Fromuser AS FromUserId
,c.ToUser AS ToUserId
,frm.FirstName AS FromUser
,t.FirstName AS ToUser
FROM conversation c
INNER JOIN employee frm ON c.fromuser = frm.EmployeeId
INNER JOIN employee t ON c.touser = t.EmployeeId
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have two tables (Temp1 & finalTemp)
temp1
AccId Name address city GuarantorId
1 abc xyz a 11
1 qwe asd a 115
2 kk aa t 21
3 t u p 96
now I want my finalTemp like
AccId Name1 address1 city1 Name2 address2 city2 Name3 address3 city3
1 abc xyz a qwe asd a null null null
2 kk aa t null null null null null null
3 t u p
Self LEFT JOIN?
select t1.*,t2.*
from temp t1
left join temp t2 on t1.AccId = t2.AccId and t1.name < t2.name
Or, extended, double self left join:
select t1.*,t2.*,t3.*
from temp t1
left join temp t2 on t1.AccId = t2.AccId and t1.name < t2.name
left join temp t3 on t2.AccId = t3.AccId and t2.name < t3.name
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am using Sql server 2008, i need to order by the following data:
Id PId Name
1 1 A
1 2 G --value to consider
1 3 C
2 1 A
2 2 B --value to consider
2 3 C
3 1 A
3 2 D --value to consider
3 3 C
result should look something like:
Id PId Name
2 1 A
2 2 B
2 3 C
3 1 A
3 2 D
3 3 C
1 1 A
1 2 G
1 3 C
i have tried different combinations in order by clause but of no use like:
order by Name, PId
order by PId, Name, Id
select T1.*
from Table1 as t1
left outer join Table1 as T2 on T2.id = T1.id and T2.Pid = 2
order by T2.Name, T1.Pid
sql fiddle demo
Your question isn't clear about the ordering you want, but if you want to order by the Name associated with the same Id for PId=2, then by Pid, try
select Id, PId, Name
from t
order by (
select Name from t as t2
where t2.Id = t.Id
and t2.PId = 2
), PId