Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Max Group multiple fields

Author  Topic 

hgjoe
Starting Member

3 Posts

Posted - 2009-09-20 : 20:59:34
I've come to the point where i have become cranky with trying to find a solution for this now.

I have 4 tables a supplier table, a department table, a table to associate both departments and suppliers and a fourth table that has exspenditure from each department to each supplier

department
dID department

800 tiles
900 plumbing
1000 paint

supplier
sID supplier

G000 CTM
K000 caroma
Z000 dulux

Associations
aID dID sID

7001 800 G000
7002 900 K000
7003 1000 Z000
7004 800 Z000
7005 900 G000

spenditure
aID total

7001 5000
7002 8500
7003 3200
7004 4650
7005 2100

As you can see from the tables above a department can purchase from the same suppliers.

By using this script :

SELECT s.sID, max(e.total) AS total
FROM supplier AS s, Associations AS a, expenses AS e
WHERE s.sID =a.sID AND a.aID = e.aID
GROUP by s.sID
ORDER BY s.sID

I can retrieve the supplier code and total amount, which is fine and works, but i require further details. the department name to be specific.

In my script above i would prefer to obtain the 'aID', therefore i can use the associations table to gather the other details but i cannot do a group by on this field as i am trying to obtain which department spent the most with each supplier.

In the case above i should only receive three records.

I hope the above makes sense and i will greatly appreciate any assistance with this.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-21 : 10:15:44
Here's one way:

declare @department table (dID int, department varchar(20))
insert @department
select 800, 'tiles' union all
select 900, 'plumbing' union all
select 1000, 'paint'

declare @supplier table (sID varchar(5), supplier varchar(20))
insert @supplier
select 'G000', 'CTM' union all
select 'K000', 'caroma' union all
select 'Z000', 'dulux'

declare @Associations table (aID int, dID int, sID varchar(5))
insert @associations
select 7001, 800, 'G000' union all
select 7002, 900, 'K000' union all
select 7003, 1000, 'Z000' union all
select 7004, 800, 'Z000' union all
select 7005, 900, 'G000'

declare @spenditure table (aID int, total int)
insert @spenditure
select 7001, 5000 union all
select 7002, 8500 union all
select 7003, 3200 union all
select 7004, 4650 union all
select 7005, 2100


select s.sID
,max(e.total) AS total
,max(ca.department) as department --min / max doesn't matter because of group by in cross apply
from @supplier s
join @associations a
on a.sid = s.sid
join @spenditure e
on e.aID = a.aID
cross apply (
select top 1 d1.department
from @supplier s1
join @associations a1
on a1.sID = s1.sID
join @spenditure e1
on e1.aID = a1.aID
join @department d1
on d1.dID = a1.dID
where a1.sID = s.sID
group by d1.department
order by sum(e1.total) desc
) ca
GROUP by s.sID
ORDER BY s.sID

OUTPUT:
sID total department
----- ----------- --------------------
G000 5000 tiles
K000 8500 plumbing
Z000 4650 tiles


Be One with the Optimizer
TG
Go to Top of Page

hgjoe
Starting Member

3 Posts

Posted - 2009-09-22 : 02:47:20
TG you are a genuis!
This has worked for me.

Thanks for taking the time to help
Go to Top of Page
   

- Advertisement -