如果DB包含当前日期数据,则SELECT top 1降序,否则选择top 1升序

时间:2016-05-05 15:31:56

标签: sql sql-server selenium

我有一个填充到我的数据库中的月份列表。然后,这些月份将填充到Web应用程序的下拉列表中。客户希望当前月份成为下拉列表中的默认选项。但是,当前月份的数据并不总是可用(或者数据库中的ActiveFlag = Y)。因此,在这种情况下,客户希望默认为一年中的第一个月。

所以我测试下拉列表和数据库表使用Selenium自动化与C#匹配。我有一个SQL查询返回以降序排序的前1(这可以返回当年的最后一个活跃月份,因为BroadcastMonthofYearNbr包含整数1到12个月的数字):

SELECT top 1 CONCAT(RTRIM(left(M.Name, CHARINDEX(',',M.Name)-1)), ' (', STUFF(REPLACE('/'+CONVERT( VARCHAR(10), M.StartDate, 101), '/0', '/'), 1, 1, ''), '-', STUFF(REPLACE('/'+CONVERT(VARCHAR(10), M.EndDate, 101), '/0', '/'), 1, 1, ''), ')') 
            FROM [AudienceProjections].[dbo].[BroadcastCalendarQuarterMonthMap] QMP,
                AudienceProjections.dbo.BroadcastCalendarYear Y,
                AudienceProjections.dbo.BroadcastCalendarMonth M,
                AudienceProjections.dbo.BroadcastCalendarQuarter Q
            WHERE y.BroadcastCalendarYearUuid = q.BroadcastCalendarYearUuid
                AND q.BroadcastCalendarQuarterUuid = qmp.BroadcastCalendarQuarterUuid
                AND qmp.BroadcastCalendarMonthUuid = m.BroadcastCalendarMonthUuid
                AND M.ActiveStatus = 'ACTIVE'
                AND y.ActiveStatus = 'ACTIVE'
                AND q.BroadcastQuarterType = 'STANDARD'
                AND y.BroadcastCalendarYear = YEAR(GETDATE())
            ORDER BY qmp.BroadcastMonthofYearNbr DESC 

上面的查询返回:

No Column Name April (3/28/2016-4/24/2016)

我发布此问题的当前月份是5月5日。请注意,上面的查询结果返回四月,而不是五月。这是因为May数据尚未导入数据库(请参阅下表,以区分是否存在数据。即活动与非活动)。所以我的查询在这种情况下不起作用。如果发生这种情况,我现在需要CASE THEN ELSEIF EXISTS类型的函数来返回1月

以下是您可以看到May表为INACTIVE的月表:

Name           StartDate    EndDate     ActiveStatus
January, 2016  2015-12-28   2016-01-31  ACTIVE
February, 2016 2016-02-01   2016-02-28  ACTIVE
March, 2016    2016-02-29   2016-03-27  ACTIVE
April, 2016    2016-03-28   2016-04-24  ACTIVE
May, 2016      2016-04-25   2016-05-29  INACTIVE

注意:我忘记了当前日期是1月份的情况,如果1月份没有,那么年份也不会存在。因此查询中的条件需要考虑Year。年表(BroadcastCalendarYear)也有一个可以使用的ActiveStatus列。

因此,如果年份也处于非活动状态,那么请返回一个字符串,上面写着“年度数据库中没有数据”。

4 个答案:

答案 0 :(得分:0)

我假设您将能够将以下内容调整到您的特定架构,但这是一个示例,因为它可以工作。请注意,您可能不需要并且应该在不知道数据集的情况下删除一些难以理解的where语句。

如果当月的1月份没有任何活动,那么应该返回哪个月?您可能必须根据答案调整订单。

SELECT TOP 1 *
FROM
    @Months
WHERE
    ActiveStates = 'ACTIVE'
    AND ????
ORDER BY
    CASE WHEN YEAR(GETDATE()) = YearInt THEN 9999 ELSE YearInt END DESC -- Assumes you want to order by most recent to oldest and that youcould potentially have a future year loaded in the table.  Othwerwuse simply use YearInt DESC
    ,CASE WHEN MONTH(GETDATE()) = MonthInt THEN 0 ELSE 1 END --Puts precedence on current month
    ,MonthInt DESC

答案 1 :(得分:0)

我希望这会有所帮助。

IF OBJECT_ID(N'tempdb..#Month') IS NOT NULL
DROP TABLE #Month
CREATE TABLE #Month
(Year INT, Month VARCHAR(3), ActiveStatus CHAR(1))


INSERT INTO #Month(Year, Month, ActiveStatus)
VALUES(2016,'Jan','A')
,(2016,'Feb','A')
,(2016,'Mar','A')
,(2016,'Apr','A')
,(2016,'May','I')

;WITH x AS 
(
SELECT 
*,EOMONTH(CONCAT(Year,'-',Month,'-','01')) As Date
FROM #Month
)
SELECT 
*
FROM X
WHERE 
 -- current month is active or current month is inactive then first month of the current year
 Year = YEAR(GETDATE()) AND 
 (DATEPART(MONTH,Date)=DATEPART(MONTH,GETDATE()) AND ActiveStatus='A') OR     (1=DATEPART(MONTH,Date) AND EXISTS(SELECT 1 FROM x WHERE     DATEPART(MONTH,Date)=DATEPART(MONTH,GETDATE()) AND ActiveStatus='I'))

答案 2 :(得分:0)

我用一个漫长而复杂的解决方案来解决这个问题,但它确实有效。

SELECT CASE WHEN t.month = (SELECT left(M.Name, CHARINDEX(',',M.Name)-1) FROM [AudienceProjections].[dbo].[BroadcastCalendarMonth] M
            WHERE StartDate <= GETDATE() AND EndDate >= GETDATE() ) THEN CONCAT(RTRIM(left(t.Name, CHARINDEX(',',t.Name)-1)), ' (', STUFF(REPLACE('/'+CONVERT( VARCHAR(10), t.StartDate, 101), '/0', '/'), 1, 1, ''), '-', STUFF(REPLACE('/'+CONVERT(VARCHAR(10), t.EndDate, 101), '/0', '/'), 1, 1, ''), ')')  ELSE ( SELECT CONCAT(RTRIM(left(M.Name, CHARINDEX(',',M.Name)-1)), ' (', STUFF(REPLACE('/'+CONVERT( VARCHAR(10), M.StartDate, 101), '/0', '/'), 1, 1, ''), '-', STUFF(REPLACE('/'+CONVERT(VARCHAR(10), M.EndDate, 101), '/0', '/'), 1, 1, ''), ')')  FROM AudienceProjections.dbo.BroadcastCalendarMonth M WHERE  NAME = CONCAT( DATENAME(month,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)), ', ', DATENAME(year,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))  ) ) END month FROM    (                 
         SELECT top 1 left(M.Name, CHARINDEX(',',M.Name)-1) AS month, 
         M.Name, M.StartDate, M.EndDate
         FROM [AudienceProjections].[dbo].[BroadcastCalendarQuarterMonthMap] QMP,
                AudienceProjections.dbo.BroadcastCalendarYear Y,
                AudienceProjections.dbo.BroadcastCalendarMonth M,
                AudienceProjections.dbo.BroadcastCalendarQuarter Q
            WHERE y.BroadcastCalendarYearUuid = q.BroadcastCalendarYearUuid
                AND q.BroadcastCalendarQuarterUuid = qmp.BroadcastCalendarQuarterUuid
                AND qmp.BroadcastCalendarMonthUuid = m.BroadcastCalendarMonthUuid
                AND M.ActiveStatus = 'ACTIVE'
                AND q.BroadcastQuarterType = 'STANDARD'
            AND y.ActiveStatus = 'ACTIVE'
                AND y.BroadcastCalendarYear = YEAR(GETDATE())
            ORDER BY qmp.BroadcastMonthofYearNbr DESC) t

答案 3 :(得分:0)

使用IF EXISTS,ELSE IF EXISTS,ELSE完成所有3个条件:

IF EXISTS (
SELECT CONCAT(q.DisplayName, ' (', STUFF(REPLACE('/'+CONVERT( VARCHAR(10), q.StartDate, 101), '/0', '/'), 1, 1, ''), '-', STUFF(REPLACE('/'+CONVERT(VARCHAR(10), q.EndDate, 101), '/0', '/'), 1, 1, ''), ')')
FROM AudienceProjections.dbo.BroadcastCalendarYear Y,
     AudienceProjections.dbo.BroadcastCalendarQuarter Q
WHERE y.BroadcastCalendarYearUuid = q.BroadcastCalendarYearUuid
      AND q.BroadcastQuarterType = 'STANDARD'
      AND y.ActiveStatus = 'ACTIVE'
      AND q.ActiveStatus = 'ACTIVE'
      AND y.BroadcastCalendarYear = YEAR(GETDATE()) 
          AND Q.StartDate <= GETDATE()
          and Q.EndDate >= GETDATE()) 
SELECT CONCAT(q.DisplayName, ' (', STUFF(REPLACE('/'+CONVERT( VARCHAR(10), q.StartDate, 101), '/0', '/'), 1, 1, ''), '-', STUFF(REPLACE('/'+CONVERT(VARCHAR(10), q.EndDate, 101), '/0', '/'), 1, 1, ''), ')')
FROM AudienceProjections.dbo.BroadcastCalendarYear Y,
     AudienceProjections.dbo.BroadcastCalendarQuarter Q
WHERE y.BroadcastCalendarYearUuid = q.BroadcastCalendarYearUuid
      AND q.BroadcastQuarterType = 'STANDARD'
      AND y.ActiveStatus = 'ACTIVE'
      AND q.ActiveStatus = 'ACTIVE'
      AND y.BroadcastCalendarYear = YEAR(GETDATE()) 
          AND Q.StartDate <= GETDATE()
          and Q.EndDate >= GETDATE() ELSE IF EXISTS (
SELECT TOP 1 CONCAT(q.DisplayName, ' (', STUFF(REPLACE('/'+CONVERT( VARCHAR(10), q.StartDate, 101), '/0', '/'), 1, 1, ''), '-', STUFF(REPLACE('/'+CONVERT(VARCHAR(10), q.EndDate, 101), '/0', '/'), 1, 1, ''), ')')
FROM [AudienceProjections].[dbo].[BroadcastCalendarQuarterMonthMap] QMP,
     AudienceProjections.dbo.BroadcastCalendarYear Y,
     AudienceProjections.dbo.BroadcastCalendarQuarter Q
WHERE y.BroadcastCalendarYearUuid = q.BroadcastCalendarYearUuid
      AND q.BroadcastCalendarQuarterUuid = qmp.BroadcastCalendarQuarterUuid
      AND q.BroadcastQuarterType = 'STANDARD'
      AND y.ActiveStatus = 'ACTIVE'
      AND q.ActiveStatus = 'ACTIVE'
      AND y.BroadcastCalendarYear = YEAR(GETDATE()) ORDER BY qmp.BroadcastMonthofYearNbr ASC ) SELECT TOP 1 CONCAT(q.DisplayName, ' (', STUFF(REPLACE('/'+CONVERT( VARCHAR(10), q.StartDate, 101), '/0', '/'), 1, 1, ''), '-', STUFF(REPLACE('/'+CONVERT(VARCHAR(10), q.EndDate, 101), '/0', '/'), 1, 1, ''), ')')
FROM [AudienceProjections].[dbo].[BroadcastCalendarQuarterMonthMap] QMP,
     AudienceProjections.dbo.BroadcastCalendarYear Y,
     AudienceProjections.dbo.BroadcastCalendarQuarter Q
WHERE y.BroadcastCalendarYearUuid = q.BroadcastCalendarYearUuid
      AND q.BroadcastCalendarQuarterUuid = qmp.BroadcastCalendarQuarterUuid
      AND q.BroadcastQuarterType = 'STANDARD'
      AND y.ActiveStatus = 'ACTIVE'
      AND q.ActiveStatus = 'ACTIVE'
      AND y.BroadcastCalendarYear = YEAR(GETDATE()) 
ORDER BY qmp.BroadcastMonthofYearNbr ASC; ELSE SELECT 'No Active quarters are in the database for the current year yet';