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)
 Cross Join Grouping & Totals

Author  Topic 

darius_sutherland
Starting Member

17 Posts

Posted - 2008-08-20 : 10:09:30
Hi.

Pulling my hair out a little. I have 2 tables and I want to do a sum of row in 1 table divided by the count from another table. Tables can be linked on an ID

So.

I'll just send the gobbledegook query...

SELECT SUM(A.Qty) / (SELECT COUNT(B.IssueID)
FROM Issues WHERE (ProductID = 13) AND (DATEPART(year, CoverDate) = 2008) AND (DATEPART(month, CoverDate) = 2) GROUP BY ProductID)

FROM v2Subrun AS A CROSS JOIN Issues AS B WHERE (A.ProductID = 13) AND (DATEPART(year, B.CoverDate) = 2008) AND (DATEPART(month,B.CoverDate) = 2)

Doesn't work. Im all confused

www.state-of-mind.co.uk | www.photographersinbrighton.org.uk | www.sussexweddingphotographers.org.uk

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-20 : 10:18:35
quote:
Originally posted by darius_sutherland

Hi.

Pulling my hair out a little. I have 2 tables and I want to do a sum of row in 1 table divided by the count from another table. Tables can be linked on an ID

So.

I'll just send the gobbledegook query...

SELECT SUM(A.Qty) *1.0/ (SELECT COUNT(B.IssueID)
FROM Issues WHERE (ProductID = 13) AND (DATEPART(year, CoverDate) = 2008) AND (DATEPART(month, CoverDate) = 2) GROUP BY ProductID)
FROM v2Subrun AS A CROSS JOIN Issues AS B WHERE (A.ProductID = 13) AND (DATEPART(year, B.CoverDate) = 2008) AND (DATEPART(month,B.CoverDate) = 2)

Doesn't work. Im all confused...


may be this
Go to Top of Page

darius_sutherland
Starting Member

17 Posts

Posted - 2008-08-20 : 10:24:04
Cheers.

Wasn't really thinking or staring too much. Just did it, maybe not the best way but will keep yours in mind if all goes pear chaped...

SELECT SUM(v2Subrun.Qty) AS Expr1, Issues_1.CoverDate, SUM(v2Subrun.Qty) / (SELECT COUNT(IssueID) AS Expr1
FROM Issues WHERE (ProductID = 13) AND (DATEPART(Year, CoverDate) = 2008) AND (DATEPART(Month, CoverDate) = 2)
GROUP BY ProductID) AS Expr2
FROM v2Subrun INNER JOIN
Issues AS Issues_1 ON v2Subrun.IssueID = Issues_1.IssueID
WHERE (v2Subrun.ProductID = 13) AND (DATEPART(month, Issues.CoverDate) = 2) GROUP BY Issues.CoverDate ORDER BY Issues.CoverDate
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-20 : 10:31:09
quote:
Originally posted by darius_sutherland

Cheers.

Wasn't really thinking or staring too much. Just did it, maybe not the best way but will keep yours in mind if all goes pear chaped...

SELECT SUM(v2Subrun.Qty) AS Expr1, Issues_1.CoverDate, SUM(v2Subrun.Qty) / (SELECT COUNT(IssueID) AS Expr1
FROM Issues WHERE (ProductID = 13) AND (DATEPART(Year, CoverDate) = 2008) AND (DATEPART(Month, CoverDate) = 2)
GROUP BY ProductID)
AS Expr2
FROM v2Subrun INNER JOIN
Issues AS Issues_1 ON v2Subrun.IssueID = Issues_1.IssueID
WHERE (v2Subrun.ProductID = 13) AND (DATEPART(month, Issues.CoverDate) = 2) GROUP BY Issues.CoverDate ORDER BY Issues.CoverDate


this will return wrong results if you're expecting decimal result. the reason is below

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx
Go to Top of Page

darius_sutherland
Starting Member

17 Posts

Posted - 2008-08-20 : 10:46:21
Thanks for the heads up. Long is what Im after...

D
Go to Top of Page
   

- Advertisement -