Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
mbrady5
mbrady5
SSC-Enthusiastic
SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)SSC-Enthusiastic (129 reputation)

Group: General Forum Members
Points: 129 Visits: 745
If I want to Sum a field where the date has passed, how would I do that?
So my goal would be to Sum the OpenAmountUSD where the dueDate has gone by as a new column called OpenPastDue



SELECT vfact_aptransaction_datepart.openamountusd AS OpenAmtUSD,
dim_site.siteid,
dim_site.sitename,
dim_site.division,
vfact_aptransaction_datepart.duedate,
dim_vendor.vendorname,
dim_vendor.vendorclass,
vfact_aptransaction_datepart.weekdue,
vfact_aptransaction_datepart.yeardue,
dim_site.sitedescription
FROM vfact_aptransaction_datepart
INNER JOIN dim_site
ON vfact_aptransaction_datepart.siteid = dim_site.siteid
INNER JOIN dim_vendor
ON vfact_aptransaction_datepart.vendorid = dim_vendor.vendorid


Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
Can you share the DDL for the tables and some sample data?
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21067 Visits: 18258
Erin Ramsay (3/13/2013)
Can you share the DDL for the tables and some sample data?


And also what the desired output should look like?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Alexander Suprun
Alexander Suprun
Old Hand
Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)

Group: General Forum Members
Points: 341 Visits: 1516
mbrady5 (3/13/2013)
If I want to Sum a field where the date has passed, how would I do that?
Usually using SUM, WHERE and likely GROUP BY.


Alex Suprun
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
Definitely need to provide more details. I would highly recommend that you use aliases in your queries. Your table names are very long and not terribly user friendly. I took a shot in the dark. See if this is somewhat close to what you are looking for.


SELECT v.openamountusd AS OpenAmtUSD,
   SUM(Case when v.duedate > getdate() then v.openamountusd else 0 end) as OpenPastDue,
   s.siteid,
   s.sitename,
   s.division,
   v.duedate,
   ven.vendorname,
   ven.vendorclass,
   v.weekdue,
   v.yeardue,
   s.sitedescription
FROM vfact_aptransaction_datepart v
INNER JOIN dim_site s
ON v.siteid = s.siteid
INNER JOIN dim_vendor ven
ON v.vendorid = ven.vendorid
group by v.openamountusd,
s.siteid,
s.sitename,
s.division,
v.duedate,
ven.vendorname,
ven.vendorclass,
v.weekdue,
v.yeardue,
s.sitedescription



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search