Friday, March 23, 2012

is this possible

hey all,
what's the best way to get all the records in a master table and a sum of a
column in a related details table?
thanks,
rodcharThe best way is to post DDL and sample data (
http://www.aspfaq.com/etiquette.asp?id=5006 )
But...
SELECT Master.PKCol, SUM(Detail.SomeCol) SumSomeCol
FROM Master
JOIN Detail ON Master.PKCol = Detail.PKCol
GROUP BY Master.PKCol
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
a
> column in a related details table?
> thanks,
> rodchar|||without seeing the ddl, this is a guess.
select m.id,sum(c.col)
from master m left join child c on m.id=c.fk
group by m.id
-oj
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
> a
> column in a related details table?
> thanks,
> rodchar|||Well...you query it for the things you need and sum the column that gives yo
u
the answer. Once you have it, it will be obvious how it should be assemble t
o
get those things you need.
Vagueness begets vague answers. Post DDL
Thomas
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
a
> column in a related details table?
> thanks,
> rodchar|||SELECT <col_list_from_master_table>,
(SELECT SUM(<col_name> )
FROM details_table AS D
WHERE D.referencing_col = M.referenced_col) AS sumdetail
FROM master_table AS M
BG, SQL Server MVP
www.SolidQualityLearning.com
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
> a
> column in a related details table?
> thanks,
> rodchar

No comments:

Post a Comment