Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mjimenezh
Yak Posting Veteran
81 Posts |
Posted - 2013-11-01 : 20:16:49
|
Hi everyone, I have a problem and I don't found the solution, I need your help with a line of code, I have a sql 2008 view of sales and I need to change the attached line of code because the code shows information of the current day and I need the information but from yesterday, can you please help me? I don't understand this line of code, I need change the "getdate" command to '10/31/2013' and in the dateadd I suppose that the date is '10/31/2012'WHERE (docdate BETWEEN DATEADD(YY, - 1, GETDATE()) AND GETDATE()) Thanks |
|
mjimenezh
Yak Posting Veteran
81 Posts |
Posted - 2013-11-01 : 20:22:00
|
This is the full code :SELECT TOP (100) PERCENT SlsRep, SlsName, SUM(MonthToDate) AS MonthToDate, SUM(LastYearMonth) AS LastYearMonth, CASE WHEN SUM(Sales.LastYearMonth) = 0 AND SUM(Sales.MonthToDate) <> 0 THEN 1 WHEN SUM(Sales.LastYearMonth) <> 0 AND SUM(Sales.MonthToDate) = 0 THEN - 1 WHEN SUM(Sales.LastYearMonth) = 0 AND SUM(Sales.MonthToDate) = 0 THEN 0 ELSE (SUM(Sales.MonthToDate) - SUM(Sales.LastYearMonth)) / SUM(Sales.LastYearMonth) END AS [Current Period Variance], 0 AS CPProfit, SUM(YearToDate) AS YearToDate, SUM(LastYear) AS LastYear, CASE WHEN SUM(Sales.LastYear) = 0 AND SUM(Sales.YearToDate) <> 0 THEN 1 WHEN SUM(Sales.LastYear) <> 0 AND SUM(Sales.YearToDate) = 0 THEN - 1 WHEN SUM(Sales.LastYear) = 0 AND SUM(Sales.YearToDate) = 0 THEN 0 ELSE (SUM(Sales.YearToDate) - SUM(Sales.LastYearMonth)) / SUM(Sales.LastYear) END AS [Last Year Variance], 0 AS LPProfit, GETDATE() AS FECHAFROM dbo.vwSalesbyRep AS SalesWHERE (docdate BETWEEN DATEADD(YY, - 1, GETDATE()) AND GETDATE())GROUP BY SlsRep, SlsNameORDER BY SlsName |
|
|
mjimenezh
Yak Posting Veteran
81 Posts |
Posted - 2013-11-01 : 20:30:10
|
I don't know if the problem comes from the original view that i'm accessing, this is the code :SELECT dbo.gbkmut.res_id AS SlsRep, reshumres.fullname AS SlsName, dbo.gbkmut.docdate, YEAR(dbo.gbkmut.docdate) AS year, MONTH(dbo.gbkmut.docdate) AS month, CASE WHEN YEAR(gbkmut.docdate) = YEAR(getdate()) THEN CASE WHEN month(gbkmut.docdate) = month(getdate()) THEN - bdr_hfl ELSE 0 END ELSE 0 END AS MonthToDate, CASE WHEN YEAR(gbkmut.docdate) = YEAR(getdate()) THEN CASE WHEN month(gbkmut.docdate) <> month(getdate()) THEN - bdr_hfl ELSE 0 END ELSE 0 END AS YearToDate, CASE WHEN YEAR(gbkmut.docdate) <> YEAR(getdate()) THEN CASE WHEN month(gbkmut.docdate) = month(getdate()) THEN - bdr_hfl ELSE 0 END ELSE 0 END AS LastYearMonth, CASE WHEN YEAR(gbkmut.docdate) <> YEAR(getdate()) THEN CASE WHEN month(gbkmut.docdate) <> month(getdate()) THEN - bdr_hfl ELSE 0 END ELSE 0 END AS LastYear, CASE WHEN YEAR(gbkmut.docdate) = YEAR(getdate()) THEN CASE WHEN month(gbkmut.docdate) = month(getdate()) THEN '1 MonthToDate' ELSE '1 YearToDate' END ELSE CASE WHEN month(gbkmut.docdate) = month(getdate()) THEN '2 LastMonthToDate' ELSE '2 LastYearToDate' END END AS Period, - dbo.gbkmut.bdr_hfl AS TotalFROM dbo.gbkmut LEFT OUTER JOIN dbo.humres AS reshumres ON dbo.gbkmut.res_id = reshumres.res_id INNER JOIN dbo.grtbk ON dbo.grtbk.reknr = dbo.gbkmut.reknr LEFT OUTER JOIN dbo.Items ON dbo.gbkmut.artcode = dbo.Items.ItemCodeWHERE (dbo.gbkmut.transtype IN ('N', 'C', 'P')) AND (dbo.gbkmut.ReminderCount <= 15) AND (dbo.grtbk.omzrek IN ('J')) AND (dbo.gbkmut.transsubtype <> 'X') |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-11-02 : 02:21:45
|
quote: Originally posted by mjimenezh Hi everyone, I have a problem and I don't found the solution, I need your help with a line of code, I have a sql 2008 view of sales and I need to change the attached line of code because the code shows information of the current day and I need the information but from yesterday, can you please help me? I don't understand this line of code, I need change the "getdate" command to '10/31/2013' and in the dateadd I suppose that the date is '10/31/2012'WHERE (docdate BETWEEN DATEADD(YY, - 1, GETDATE()) AND GETDATE()) Thanks
For fixed date, try this:where docdate>='20121101' and docdate<'20131101'Using getdate, try this:where docdate>=dateadd(yy,-1,dateadd(dd,-1,getdate())) and docdate<dateadd(dd,-1,getdate())This will get you same period, as the fixed date |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
mjimenezh
Yak Posting Veteran
81 Posts |
|
|
|
|
|
|