I'm having these three tables
Table A:
code aname
----------- ----------
1 A
2 B
3 C
Table B:
code bname
----------- ----------
1 aaa
1 bbb
2 ccc
2 ddd
Table C
code cname
----------- ----------
1 xxx
1 yyy
1 zzz
2 www
How can I achieve the output like this using single query ?
code aname bname cname
----------- ---------- ---------- ----------
1 A aaa xxx
1 A bbb yyy
1 A NULL zzz
2 B ccc www
2 B ddd NULL
3 C NULL NULL
Any suggestions ?
Thanks
It looks like you want "lists" to be vertical for tables B and C. This is doable, by using row_number(). However, the trick is getting the third row in where there are no matches.
Here is one method. It uses a full outer join to combine the b and c names together. It then uses left join to bring in the a records.
select a.code, a.name, bc.bname, bc.cname
from a left join
(select coalesce(b.code, c.code) as code, bname, cname
from (select code, bname, NULL as cname,
row_number() over (partition by code order by code) as seqnum
from b
) b full outer join
(select code, NULL as bname, cname,
row_number() over (partition by code order by code) as seqnum
from c
) c
on b.code = c.code and b.seqnum = c.seqnum
) bc
on bc.code = a.code;
Related
I have requirement
sample data :
Table A :
ID name
1 cat
2 Dog
3 Bird
Table B :
ID name
1 aaa
1 bbb
2 ccc
2 ddd
Table C :
ID name
1 xxx
1 yyy
1 zzz
2 www
Required Output :
ID name name name
1 cat aaa xxx
1 cat bbb yyy
1 cat null zzz
2 Dog ccc www
2 Dog ddd www
3 Bird NULL NULL
I have tried with different joins
Select a.ID,a.name,b.name,c.name from #A a
full join #b b
on a.ID = b.ID
full join #c c
on b.ID = c.ID
Can anyone suggest me the best way to Proceed?
You can use window function row_number to assign sequence number within each id in the order of increasing name for table b and c and then do a full join between them. Finally, do a left join with a table:
with b1 as (
select b.*, row_number() over (partition by id order by name) as rn
from b
),
c1 as (
select c.*, row_number() over (partition by id order by name) as rn
from c
)
select a.*, t.b_name, t.c_name
from a
left join (
select coalesce(b1.id, c1.id) as id,
b1.name as b_name,
c1.name as c_name
from b1
full join c1 on b1.id = c1.id
and b1.rn = c1.rn
) t on a.id = t.id;
This assumes that you need to join the tables b and c based on id and the position (in the order of name column).
I have a query where I need to extract data in a regular table and put two rows of data into a single row.
I have rows that consist of
StudentID AUDIT_ACTN Audit_Date .....
aaa A 01/01/2010
aaa A 03/04/2011
aaa A 02/02/2013
aaa D 09/10/2010
aaa D 05/06/2011
aaa D 06/07/2013
aaa A 11/12/2014~
bbb A 01/01/2010
bbb A 03/04/2011
bbb A 02/02/2013
bbb D 09/10/2010
bbb D 05/06/2011
bbb D 06/07/2013
bbb A 11/12/2014~
I want output like this
StudentID AUDIT_ACTN Audit_Date StudentID AUDIT_ACTN Audit_Date
aaa A 01/01/2010 aaa D 09/10/2010
aaa A 03/04/2011 aaa D 05/06/2011
aaa A 02/02/2013 aaa D 06/07/2013
aaa A 11/12/2014 NULL NULL NULL
bbb A 01/01/2010 bbb D 09/10/2010
bbb A 03/04/2011 bbb D 05/06/2011
bbb A 02/02/2013 bbb D 06/07/2013
bbb A 11/12/2014 NULL NULL NULL
The A & D data rows are related, a= add the something to the record and d = delete something from the record (something is an indicator). These is logical in that you must add something before you delete it and you cannot add it twice, without deleting it first.
My current script is probably going down the wrong track but here goes;
select a.StudentId,a.Audit_Date,a.AUDIT_ACTN,d.StudentId,Audit_Date,d.AUDIT_ACTN,
from table a
join
(select *
from
(Select StudentId, Audit_Date,AUDIT_ACTN
from table b
Where b.AUDIT_ACTN='D'
order by Audit_Date
)
where rownum=1
) d on a.StudentId = d.StudentId
and a.AUDIT_ACTN='A'
and Select * from (Select Audit_Date
Order by a.StudentId, a.Audit_Date
I know this is wrong but where do I go from here. If anyone can help and point me in the right direction. It would be appreciated.
My current attempts bring me zero rows, when I take out the rownum it brings me a x join returning 12 rows in this case.
thanks
Roger
I think you need aggregate function like below
select
A.StudentId,A.Audit_Date,A.AUDIT_ACTN,
D.StudentId,D.Audit_Date,D.AUDIT_ACTN
from
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='D'
) D
FULL OUTER JOIN
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='A'
) A
on A.rn = D.rn and A.StudentId = B.StudentId
I hope this will work
I have a table like this
Table A:
Id Count
1 4
1 16
1 8
2 10
2 15
3 18
etc
Table B:
1 sample1.file
2 sample2.file
3 sample3.file
TABLE C:
Count fileNumber
16 1234
4 2345
15 3456
18 4567
and so on...
What I want is this
1 sample1.file 1234
2 sample2.file 3456
3 sample3.file 4567
To get the max value from table A I used
Select MAX (Count) from A where Id='1'
This works well but my problem is when combining data with another table.
When I join Table B and Table A, I need to get the MAX for all Ids and in my query I dont know what Id is.
This is my query
SELECT B.*,C.*
JOIN A on A.Id = B.ID
JOIN C on A.id = B.ID
WHERE (SELECT MAX(COUNT)
FROM A
WHERE Id = <what goes here????>)
To summarise, what I want is Values from Table B, FileNumber from Table c (where the count is Max for ID from table A).
UPDATE: COrrecting table C above. Looks like I need Table A.
I think this is the query you're looking for:
select b.*, c.filenumber from b
join (
select id, max(count) as count from a
group by id
) as NewA on b.id = NewA.id
join c on NewA.count = c.count
However, you should take into account that I don't get why for id=1 in tableA you choose the 16 to match against table C (which is the max) and for id=2 in tableA you choose the 10 to match against table C (which is the min). I assumed you meant the max in both cases.
Edit:
I see you've updated tableA data. The query results in this, given the previous data:
+----+---------------+------------+
| ID | FILENAME | FILENUMBER |
+----+---------------+------------+
| 1 | sample1.file | 1234 |
| 2 | sample2.file | 3456 |
| 3 | sample3.file | 4567 |
+----+---------------+------------+
Here is a working example
Using Mosty’s working example (renaming the keyword count to cnt for a column name), this is another approach:
with abc as (
select
a.id,
a.cnt,
rank() over (
partition by a.id
order by cnt desc
) as rk,
b.filename
from a join b on a.id = b.id
)
select
abc.id, abc.filename, c.filenumber
from abc join c
on c.cnt = abc.cnt
where rk = 1;
select
PreMax.ID,
B.FileName,
C2.FileNumber
from
( select C.id, max( C.count ) maxPerID
from TableC C
group by C.ID
order by C.ID ) PreMax
JOIN TableC C2
on PreMax.ID = C2.ID
AND PreMax.maxPerID = C2.Count
JOIN TableB B
on PreMax.ID = B.ID
This one is support hard for me. I can do inner join with first result only, but if exist I want take 2nd result.
THIS IS MY TABLE A
ID NAME VALUE
1 A 123
2 B 456
3 C 789
4 A 456
TABLE B
BID BNAME BVALUE
1 A ABC
2 A CDE
3 B 845
4 C 1234
MY SELECT SQL:
SELECT * FROM A
CROSS APPLY (
SELECT TOP 1 *
FROM B
WHERE A.Name = B.BName
) BB
It return
1 A 123 1 A ABC
2 B 456 3 B 845
3 C 789 4 C 1234
4 A 456 1 A ABC
Please help, I want this result:
1 A 123 1 A ABC
2 B 456 3 B 845
3 C 789 4 C 1234
4 A 456 2 A CDE
I accept tmp table and any kind of query :(
Following clarification in the comments that both tables will always have matching rows.
WITH A
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY ID) AS RN
FROM TableA),
B
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY BNAME ORDER BY BID) AS RN
FROM TableB)
SELECT A.ID,
A.NAME,
A.VALUE,
B.BID,
B.BNAME,
B.BVALUE
FROM A
JOIN B
ON A.NAME = B.BNAME
AND A.RN = B.RN
Hi I am having a problems using Group By and joins between 3 tables.
I have a project table with various fields and a projectcode fields. I then have an invoice table and an hours table and each can have multiple rows per project. Both of these table have project code also.
The two SUM values are not calculating correctly and I am realy struggling to see where the issue is.
Here the sql I am using:
SELECT dbo.project.projectcode,
dbo.project.client,
dbo.project.project,
dbo.project.budget,
dbo.project.budget * 80 AS value,
SUM(dbo.harvest.hours) AS hourslogged,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.salesforce
RIGHT OUTER JOIN dbo.project
ON dbo.salesforce.projectcode = dbo.project.projectcode
LEFT OUTER JOIN dbo.harvest
ON dbo.project.projectcode = dbo.harvest.projectcode
GROUP BY dbo.project.projectcode,
dbo.salesforce.projectcode,
dbo.harvest.projectcode,
dbo.project.project,
dbo.project.client,
dbo.project.budget
Any help or tips on this would be much appreciated!
Whenever each of the two tables, dbo.salesforce and dbo.harvest, have more than 1 match for every projectcode, a mini-Cartesian product happens. Here's a simple illustration. Suppose there are tables A and B, like this:
Table A:
AID AVALUE
--- -------
1 ValueA1
2 ValueA2
Table B:
BID BVALUE AID
--- ------- ---
1 ValueB1 1
2 ValueB2 1
3 ValueB3 2
Now if we performed this join:
SELECT * FROM A JOIN B ON A.AID = B.AID
the result would be:
AID AVALUE BID BVALUE AID
--- ------- --- ------- ---
1 ValueA1 1 ValueB1 1
1 ValueA1 2 ValueB2 1
2 ValueA2 3 ValueB3 2
Enter table C:
CID CVALUE AID
--- ------- ---
1 ValueC1 1
2 ValueC2 1
3 ValueC3 1
And the join now is this:
SELECT * FROM A JOIN B ON A.AID = B.AID JOIN C ON A.AID = C.AID
What would be the result? Here:
AID AVALUE BID BVALUE AID CID CVALUE AID
--- ------- --- ------- --- --- ------- ---
1 ValueA1 1 ValueB1 1 1 ValueC1 1
1 ValueA1 1 ValueB1 1 2 ValueC2 1
1 ValueA1 1 ValueB1 1 3 ValueC3 1
1 ValueA1 2 ValueB2 1 1 ValueC3 1
1 ValueA1 2 ValueB2 1 2 ValueC3 1
1 ValueA1 2 ValueB2 1 3 ValueC3 1
As you can see, every match from B is repeated three times, for how many matches C has got. And, similarly, every match from C is repeated twice, because that is how many matches there are in B. The 'luckiest', of course, is the row from A, because it is repeated 2 × 3 = 6 times. That is a Cartesian join for you. And that's just what happens in your case too.
Not sure whether it is considered typical, but in such cases I would often group each table separately by the joining expression(s), then join the result sets. Your query would then look like this:
SELECT
p.projectcode,
p.client,
p.project,
p.budget,
p.budget * 80 AS value,
h.hourslogged,
s.invoiced
FROM dbo.project p
LEFT JOIN (
SELECT
projectcode,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.salesforce
GROUP BY projectcode
) s ON p.projectcode = s.projectcode
LEFT JOIN (
SELECT
projectcode,
SUM(dbo.harvest.hours) AS hourslogged
FROM dbo.harvest
GROUP BY projectcode
) h ON p.projectcode = h.projectcode
I'd suggest to avoid mixing right and left outer join.
Your central table is Project, so use it first.
SELECT dbo.project.projectcode,
dbo.project.client,
dbo.project.project,
dbo.project.budget,
dbo.project.budget * 80 AS value,
SUM(dbo.harvest.hours) AS hourslogged,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.project
LEFT OUTER JOIN dbo.salesforce
ON dbo.salesforce.projectcode = dbo.project.projectcode
LEFT OUTER JOIN dbo.harvest
ON dbo.project.projectcode = dbo.harvest.projectcode
GROUP BY dbo.project.projectcode,
dbo.project.project,
dbo.project.client,
dbo.project.budget
But the error come from the GROUP BY. You don't have to group by the two tables on which you are doing the aggregate, else your aggregate will not be good !