SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
mbrady5
mbrady5
SSC Veteran
SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)

Group: General Forum Members
Points: 230 Visits: 778
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
SSC Eights!
SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)

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

Group: General Forum Members
Points: 32104 Visits: 18551
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
Mr or Mrs. 500
Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)

Group: General Forum Members
Points: 511 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
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25956 Visits: 17519
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 Modens 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