Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Sum sales for current month Expand / Collapse
Author
Message
Posted Tuesday, October 30, 2012 6:00 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 24, 2015 4:41 AM
Points: 16, Visits: 155
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]


  Post Attachments 
examplesqlcount.xlsx (10 views, 10.76 KB)
Post #1378711
Posted Tuesday, October 30, 2012 8:02 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 14,385, Visits: 14,129
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 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)
Post #1378789
Posted Wednesday, November 7, 2012 1:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 17, 2015 7:10 AM
Points: 1,127, Visits: 1,610
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
Post #1381822
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse