October 30, 2012 at 6:00 am
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]
October 30, 2012 at 8:02 am
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/[/url]
_______________________________________________________________
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 7, 2012 at 1:35 am
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.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply