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
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have 2 tables A and B.
Table A has 3 rows and one column having values 1, 1, 1.
Table B has 2 rows and one column having values 1, 1.
Upon applying left join, Table A left join Table B
The output I will receive is:
1, 1, 1.
Is it correct?
No, your assumption is not correct. If you join on the columns you mention, you will get 6 rows (3 x 2), because every row matches each other.
Table t1
id_t1
colx
1
1
2
1
3
1
Table t2
id_t2
coly
10
1
20
1
Query
select *
from t1
left outer join t2 on t2.coly = t1.colx
order by t1.id_t1, t2.id_t2;
Result
id_t1
colx
id_t2
coly
1
1
10
1
1
1
20
1
2
1
10
1
2
1
20
1
3
1
10
1
3
1
20
1
You seem to misunderstand what a left outer join does. Each row in table t1 has two matches in table t2. You get these with a left outer join, but you also get them with an inner join. The difference between the two join types is when there is no match. Let's add another row to table t1:
id_t1
colx
4
2
There is no match in t2 for the value 2. An inner join would not show this row in the results. A left outer join in contrast will add the following row to your results:
id_t1
colx
id_t2
coly
4
2
null
null
How to find records which matches certain sequences from the below table:
ID s_id task
1 1 a
1 2 b
1 3 b
1 4 c
1 5 c
1 6 d
1 7 a
2 1 a
2 2 c
2 3 c
3 1 a
3 2 b
3 3 c
3 4 d
1 1 a
1 2 b
1 3 c
1 4 c
1 5 e
1 6 d
How to fetch the records following the below pattern
a
1 or more b
c
Below code will transform your data to a list of site visits (id) followed by a single string of characters representing pages visited (e.g. "abbbcd").
SELECT t2.id, max(tasks) as tasks from
(
SELECT t1.id,
(SELECT '' + Task FROM [Table] WHERE id = t1.id ORDER BY s_Id FOR XML PATH('')) AS tasks
from [Table] t1
) t2
group by t2.id
So the problem is now reduced to searching for a pattern of characters: a--any number of b's--c. You can use LIKE to do this:
SELECT *
FROM (
SELECT t2.id, max(tasks) as tasks from
(
SELECT t1.id,
(SELECT '' + Task FROM [Table] WHERE id = t1.id ORDER BY s_Id FOR XML PATH('')) AS tasks
from [Table] t1
) t2
group by t2.id
) t3
WHERE t3.tasks LIKE '%abc%'
OR t3.tasks LIKE '%abbc%'
OR t3.tasks LIKE '%abbbc%'
OR t3.tasks LIKE '%abbbbc%';
This is a bit crude. You want to say any number of b's, but LIKE does not support that. This is what regular expressions (RegEx) is normally used for. The expression would be "ab+c" which stand for: "a" followed by 1 or more "b"s, followed by a "c".
Unfortunately, SQL server does not support regex (Oracle does), so you have to use CLR to implement it. Others have done this for you, so you can follow instructions here to install it: https://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/
I have a three tables
Table 1
Id Department
1 A
2 B
3 C
4 D
Table 2
Id DepartId Name
1 1 ABC
2 1 DEF
3 1 ASD
4 2 FGH
5 2 HJK
6 3 ZXC
Table 3
Id Depart Area
1 A pp
2 B
3 C nn
4 D oo
I need the result
Id Depart Name Area
1 A ABC pp
2 B FGH Null
3 C ZXC nn
4 D NULL oo
I need one matching entry from table 2 and table 3 to corresponding entry in the table 1
Do a left join to also get t1 rows without any reference in the t2 table. GROUP BY to get only 1 row per Department.
select t1.id, t1.Department, min(t2.Name)
from t1
left join t2 on t1.id = t2.DepartId
group by t1.id, t1.Department
I think I would do this with a correlated subquery:
select t1.*,
(select t2.name
from t2
where t1.id = t2.DepartId and rownum = 1
) as t2name
from t1;
This saves the overhead of an aggregation. An index on t2(DepartId, name) is optimal for this query.
by the way not the answer to your specific question but if instead of just one you want all the names you can use listagg
SELECT t1.id,
department,
LISTAGG (name, ',') WITHIN GROUP (ORDER BY name) names
FROM t1, t2
WHERE t1.id = t2.departId(+)
GROUP BY t1.id, department
ORDER BY 1
ID Department Names
1 A ABC,ASD,DEF
2 B FGH, HJK
3 C ZXC
4 D
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 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