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.
| Author |
Topic |
|
MGrassman
Starting Member
4 Posts |
Posted - 2006-07-05 : 17:00:12
|
| I have the following tables and need help creating a query. Please let me know if you need any other information.ModulesM_RecID intM_Name varcharM_Desc varcharM_Monthly decimal (this is what I need summed)M_Annually decimal (this is what I need summed)M_Deleted date/time (needs to be null)M_Updated date/timeM_OrderBy intOrg_ModulesOM_M_RecID int (refers to the M_RecID)OM_O_RecID int (refers to the OrgID)OM_M_Annually 1 or 0 (if 1 add to the sum of the M_Annually field)OM_M_Monthly 1 or 0 (if 1 add to the sum of the M_Monthly field)OrganizationsOrgID int (This is what I group by)deleted date/time (needs to be null)The ending recordset I need is as follows:OrgID, Monthly_Total, Annual_Total1 113.95 245.002 0.00 500.00 |
|
|
MGrassman
Starting Member
4 Posts |
Posted - 2006-07-05 : 17:09:01
|
| Please Disregard I have figured it out. If any one knows of a better way I would love to hear about it.Thanks,Select OM_C_RecID, isNull((Select sum(M_Monthly) from Modules where M_RecID in (Select OM_M_RecID from charity_modules where OM_M_Monthly = 1 and OM_C_RecID = OM.CM_C_REcID)), 0) as Monthly_Fee, isNull((Select sum(M_Annually) from Modules where M_RecID in (Select OM_M_RecID from charity_modules where OM_M_Annually = 1 and OM_C_RecID = OM.CM_C_RecID)), 0) as Annual_Feefrom Org_Modules OMwhere OM_C_RecID in (Select OrgID from Organizations where deleted is null)Group By OM_C_RecIDorder by OM_C_RecID |
 |
|
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2006-07-06 : 02:27:17
|
| [code]SELECT O.OrgID ,SUM(CASE OM.OM_M_Monthly WHEN 1 THEN M.M_Monthly ELSE 0 END) ,SUM(CASE OM.OM_M_Annually WHEN 1 THEN M_Annually ELSE 0 END)FROM Organizations OLEFT OUTER JOIN Org_Modules OM ON OM.OM_O_RecID = O.OrgIDLEFT OUTER JOIN Modules M ON M.M_RecID = OM_M_RecID AND M.M_Deleted IS NULLWHERE O.Deleted IS NULLGROUP BY O.OrgID[/code]-- This one's tricky. You have to use calculus and imaginary numbers for this. You know, eleventeen, thirty-twelve and all those. |
 |
|
|
|
|
|