sql语句问题

时间:2010-12-08 20:08:43

标签: sql sql-server-2005

在我的查询中,我试图获取昨天的日期和所选年份的完全相同的日期。 所以今天是12/08/2010

我想要12/07/2010和12/07/2009

我忘了提到创建数据库的最后一个人将所有预订日期存储为intgers所以基本上

     Date  Year month booked  time
     28Sep09 2009 09 40084 1159
    07Oct09 2009 10 40093 1221
    08Oct09 2009 10 40094 1025
    16Oct09 2009 10 40102 1058
    02Nov09 2009 11 40119 1708
    05Nov09 2009 11 40122 1213
    13Nov09 2009 11 40130 1502
    24Nov09 2009 11 40141 1004
    24Nov09 2009 11 40141 1030
    16Feb10 2010 02 40225 1150
    16Feb10 2010 02 40225 1158
    08Mar10 2010 03 40245 1249
    05Apr10 2010 04 40273 0937
    24May10 2010 05 40322 1559
    25May10 2010 05 40323 1539
    04Jun10 2010 06 40333 1428
    26Jul10 2010 07 40385 1708
    19Aug10 2010 08 40409 1637
    02Sep10 2010 09 40423 0927
    03Sep10 2010 09 40424 1253
    13Sep10 2010 09 40434 1534
    04Oct10 2010 10 40455 1341
    19Oct10 2010 10 40470 0939
    02Nov10 2010 11 40484 0923
    03Nov10 2010 11 40485 0854
    03Nov10 2010 11 40485 1259
    08Nov10 2010 11 40490 1210
    12Nov10 2010 11 40494 1121
    18Nov10 2010 11 40500 1222
    05Aug10 2010 08 40395 1649

我的日期看起来像这样:(

所以这将取代我的上一个查询

Declare @YearToget Integer 
Set @yearToGet = 2008

SELECT TOP (100) PERCENT DIVISION, SDESCR, 
      DYYYY, SUM(APRICE) AS Sales, SUM(PARTY) AS PAX, 
      SUM(NetAmount) AS NetSales, 
      SUM(InsAmount) AS InsSales, 
      SUM(CancelRevenue) AS CXSales, 
      SUM(OtherAmount) AS OtherSales, 
      SUM(CXVALUE) AS CXValue 
FROM  dbo.B101BookingsDetails AS B101BookingsDetails 
WHERE BOOKED <= DateAdd(year, @YearToGet - Year(getdate()), 
                 DateAdd(day, DateDiff(day, 1, getdate()), 0) )
  AND @YearToGet
GROUP BY SDESCR, DYYYY, DIVISION 
HAVING      (DYYYY = @YearToGet) 
ORDER BY DIVISION, SDESCR, DYYYY 

4 个答案:

答案 0 :(得分:1)

一些笔记。首先,你不应该使用“TOP 100 PERCENT”。它没有任何好处。如果您尝试在View中使用它,您应该知道在查询View时使用带有ORDER BY的TOP 100 PERCENT将无法保证顺序。其次,您的整数值似乎有问题,因为与DateDiff(d,0, <date>)相比,它们已经过了两天。我在使用BD.Booked = N.DateInt - 2的Where子句中对此进行了补偿,但您应该查看原因。

Declare @YearToGet int
Set @YearToGet = 2008

;With RawData As
    (
    Select 39503 As Booked
    Union All Select 39509
    Union All Select 39535
    Union All Select 39620
    Union All Select 39791
    Union All Select 39838
    Union All Select 39899
    Union All Select 39134
    Union All Select 39139
    Union All Select 39139
    Union All Select 39140
    Union All Select 39146
    Union All Select 39146
    Union All Select 39146
    )
    , Numbers As 
    (
    Select 0 As Value, Year(GetDate()) As [Year]
        , Cast( DateDiff(d,0,GetDate()) as datetime ) As [Date]
        , DateDiff(d,0,GetDate()) As [DateInt]
    Union All 
    Select Value + 1, [Year] - 1
        , DateAdd(yyyy, -1, [Date])
        , DateDiff(d, 0, DateAdd(yyyy, -1, [Date]))
    From Numbers
    Where Value <= ( Year(GetDate()) - @YearToGet )
    )
Select DIVISION, SDESCR, DYYYY
    , SUM(APRICE) AS Sales
    , SUM(PARTY) AS PAX
    , SUM(NetAmount) AS NetSales
    , SUM(InsAmount) AS InsSales
    , SUM(CancelRevenue) AS CXSales
    , SUM(OtherAmount) AS OtherSales
    , SUM(CXVALUE) AS CXValue
From dbo.B101BookingsDetails As BD
    Join Numbers As N
        On Cast(N.[Year] As char(4)) = BD.DYYYY
Where BD.Booked = N.DateInt - 2
Group By DIVISION, SDESCR, DYYYY

答案 1 :(得分:1)

昨天

convert(varchar, getDate()-1, 101)
一年前从昨天开始

convert(varchar, dateadd(year, -1, getDate()-1), 101)

答案 2 :(得分:1)

尝试:

Declare @YearToget Integer 
Set @yearToGet = 2008

  Select DateAdd(year, @YearToGet - Year(getdate()), 
         DateAdd(day, DateDiff(day, 1, getdate()), 0) )

在您的查询中:

   Where Booked = DateAdd(year, 2008- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) )
      Or Booked = DateAdd(year, 2009- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) )
      Or Booked = DateAdd(year, 2010- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) )
      Or Booked = DateAdd(year, 2011- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) )

   Where Booked In
      (DateAdd(year, 2008- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) ),
       DateAdd(year, 2009- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) ),
       DateAdd(year, 2010- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) ),
       DateAdd(year, 2011- Year(getdate()), 
             DateAdd(day, DateDiff(day, 1, getdate()), 0) ) )

上面的查询,重写:

Declare @YearToget Integer  
Set @yearToGet = 2008  
Select Division, SDESCR,        
    DYYYY, Sum(APRICE) Sales, 
    Sum(PARTY) AS PAX,        
    Sum(NetAmount) NetSales,        
    Sum(InsAmount) InsSales,        
    Sum(CancelRevenue) CXSales,        
    Sum(OtherAmount) OtherSales,        
    Sum(CXVALUE) CXValue  
From dbo.B101BookingsDetails 
Where Booked <= DateAdd(year, @YearToGet - Year(getdate()),                   
                DateAdd(day, DateDiff(day, 1, getdate()), 0) )      
-- AND @YearToGet -- This line appears to be incomplete
Group By SDESCR, DYYYY, Division
Having (DYYYY = @YearToGet)  
Order By Division, SDESCR, DYYYY 

答案 3 :(得分:0)

select getdate() /* Today */
select dateadd(day, -1, getdate()) /* Yesterday */
select dateadd(year, -1, dateadd(day, -1, getdate())) /* 1 year ago yesterday */