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


Sum sales for current month


Sum sales for current month

Author
Message
DBayliss
DBayliss
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 157
Hi All,

I'm trying to sum the sales where the date parameter will automatically reference the current day/ month, which I have created (see below query). But I'm having trouble adding an extra line where the sum says, sum the products for the current month (EffectiveStartDateTime), but where the Insertdatetime is less than the current month.

I have enclosed an excel spreadsheet of what I am trying to Achieve.

Any help / advice would be very appreciated



Select top 100

[P].[ProductName] as [Product]
,CAST(ISNULL(SUM(CASE WHEN DATEDIFF(d, GETDATE() - 1, [BT].[EffectiveStartDateTime]) = 0 THEN [BTFB].[Amount] END), 0) AS money) AS [DailySales]
,CAST(ISNULL(SUM(CASE WHEN DATEDIFF(m, GETDATE() - 1, [BT].[EffectiveStartDateTime]) = 0 THEN [BTFB].[Amount] END), 0) AS money) AS [CurrentMonthSales]

FROM
[Product] AS [P]
INNER JOIN [dbo].[BusinessTransaction] AS [BT] ON [P].[ProductID] = [BT].[ProductID]
INNER JOIN [dbo].[BusinessTransactionFinancialBreakdown] AS [BTFB] ON [BT].[BusinessTransactionID] = [BTFB].[BusinessTransactionID]

WHERE
[BTFB].[FinancialFieldID] = 26
AND [BT].[BusinessTransactionTypeID] = 1

GROUP BY
[P].[ProductName]
Attachments
examplesqlcount.xlsx (20 views, 10.00 KB)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26938 Visits: 17557
I am sure that what you posted is very clear for you. Unfortunately we don't know your project or data and we can't see you screen. If you can post ddl (create table scripts), sample data (insert statements) and desired output based on your sample data we can help. Take a look at the first link in my signature for best practices when posting questions.

You might also take a look at this article about dates. http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

_______________________________________________________________

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)
vinu512
vinu512
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1733 Visits: 1625
Attaching an Excel sheet is not called providing readily consumable sample data. Please check the link in my signature and post sample data as shown in the link. This time i'll do it for you so that other Forum members can use it and provide you with a solution:


--Creating Table

Create table Ex
(Product NVarchar(10),
InsertDateTime DateTime,
EffectiveDateTime DateTime,
Value Float )


--Inserting Sample Data

Insert into Ex
Select 'Apples', '1/9/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/9/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/9/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/10/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/10/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/10/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/10/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/10/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/10/2012 5:03', '1/10/2012 5:03', 1.00
Union ALL
Select 'Apples', '1/10/2012 5:03', '1/10/2012 5:03', 1.00


--Query for your Requirement

Select a.Product, a.[SumEffectivedate(currentday)] ,b.[SumEffectivedate(currentmonth)], c.[Sum Effectivedate(currentmonth)where_Insertdatetime_less_than_current_month]
From
(Select Product, SUM(Value) As [SumEffectivedate(currentday)]
From Ex
Group By Product, DATEPART(DD, EffectiveDateTime) ) As a
JOIN
(Select Product, SUM(Value) As [SumEffectivedate(currentmonth)]
From Ex
Group By Product, DATEPART(MM, EffectiveDateTime) ) As b ON a.Product = b.Product
JOIN
(Select Product, SUM(Value) As [Sum Effectivedate(currentmonth)where_Insertdatetime_less_than_current_month]
From Ex
Where DATEDIFF(DD, EffectiveDateTime, InsertDateTime) < 0
Group By Product, DATEPART(MM, EffectiveDateTime) ) As c ON a.Product = c.Product



From what I understood from the Excel sheet you provided....I came up with the above query. If this does not help then please provide the complete query that you are using and then may be someone might come up with a better solution.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
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