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


Query Help


Query Help

Author
Message
mbrady5
mbrady5
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 792
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
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2161 Visits: 1119
Can you share the DDL for the tables and some sample data?
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66951 Visits: 18570
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
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1247 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
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62899 Visits: 17959
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