0

我不确定为什么以下子查询在 SQL Server 中不起作用,当我自己运行子查询时它工作得很好,但是只要我添加

Select * from ([subquery]) as table1

查询没有运行,SQL Server 返回一个错误,说我在关键字“if”附近有不正确的语法,这里存在什么语法错误,否则它怎么会正常工作?

任何指针?

select * from (

    if datepart(MONTH, getdate()) >= MONTH('1/February/0000') 
    --after february we want the next years semesters, 

        begin
        select top 3 sem_name from semester_dates
         where datepart(year, start_date) < datepart(YEAR, getdate()) and sem_name not like 'summer%'
        end

    else 
        begin
        select top 3 sem_name from semester_dates  sd
        where datepart(year, start_date) >= datepart(YEAR, getdate()) and sem_name not like 'summer%' 
        end

    ) table1
4

7 回答 7

4

您可能需要考虑将 if/else 逻辑移动到表值函数中。可能是这样的。注意,我没有检查你的逻辑,我只是​​重新使用了这个。您可能需要相应地调整以下代码:

CREATE FUNCTION SemesterNames
(
  @ReferenceDate datetime
)
RETURNS @dates table 
(
  sem_name nvarchar(50) --check the data type
)
AS
BEGIN
  if datepart(MONTH, @ReferenceDate) >= MONTH('1/February/0000') 
    --after february we want the next years semesters, 
    INSERT INTO @dates (sem_name)
    select top 3 sem_name from semester_dates
     where datepart(year, start_date) > datepart(YEAR, @ReferenceDate) and sem_name not like 'summer%'
    end
else 
    INSERT INTO @dates (sem_name)
    select top 3 sem_name from semester_dates  sd
    where datepart(year, start_date) >= datepart(YEAR, @ReferenceDate) and sem_name not like 'summer%' 
END;
GO

SELECT * FROM SemesterNames(GETDATE());
于 2012-12-18T20:39:25.047 回答
2

使用 CASE 而不是 IF 尝试以下操作。

SELECT *
FROM
(SELECT 
    CASE 
        WHEN datepart(MONTH, getdate()) >= MONTH('1/February/0000')
            THEN
                (SELECT TOP 3 sem_name 
                 FROM semester_dates
                 WHERE datepart(year, start_date) > datepart(YEAR, getdate()) and sem_name not like 'summer%')
        ELSE
                (SELECT TOP sem_name 
                FROM semester_dates
                WHERE datepart(year, start_date) >= datepart(YEAR, getdate()) and sem_name not like 'summer%')
)
于 2012-12-18T20:40:54.013 回答
1

据我所知,您的条件逻辑的唯一影响是 datepart(year, start_date) 严格大于或大于或等于。所以你可以用这样一个简单的查询来替换整个事情:

    select top 3 sem_name from semester_dates
    where datepart(year, start_date) >= datepart(YEAR, getdate()) 
    and sem_name not like 'summer%'
    and (
         datepart(MONTH, getdate()) > MONTH('1/February/0000') or 
         datepart(year, start_date) = datepart(YEAR, getdate()
    ) 

换句话说, start_date 的年份严格 > year(getdate()) 除非,如果月份 > 二月,那么年份可以等于它。

于 2012-12-18T20:49:21.690 回答
0

对于这个特定的查询,您能否在 WHERE 子句中使用带有 February 条件的单个 SELECT 来获得所需的结果?

SELECT TOP 3
    Sem_Name
FROM
    semester_dates
WHERE
    sem_name not like 'summer%'
    and 
    (
        (datepart(month, getdate()) >= 2 AND datepart(year, start_date) < datepart(YEAR, getdate()))
        OR
        (datepart(month, getdate()) < 2 AND datepart(year, start_date) >= datepart(YEAR, getdate()))
    )
于 2012-12-18T20:49:15.137 回答
0

If/else 结构不应该在 Select 查询中起作用。

于 2012-12-18T20:32:15.883 回答
0
select * from
(select datepart(MONTH, getdate()) >= MONTH('1/February/0000'))
    as department,
(select top 3 sem_name from semester_dates
    where datepart(year, start_date) < datepart(YEAR, getdate()) 
        and sem_name not like 'summer%') 
    as sem_name,
(select top 3 sem_name from semester_dates sd
    where datepart(year, start_date) >= datepart(YEAR, getdate())
        and sem_name not like 'summer%') 
    as top 3 sem_name 
) table
于 2018-12-01T07:02:10.833 回答
0

CASE WHEN batch.PropertyId 为 NULL THEN (SELECT count( ) FROM vw_UtilMan_InspectionBatchAreaPropertyMeterStatistic WHERE BatchId = batch.Id) ELSE (SELECT COUNT( ) AS NumberOfMeters FROM UtilMan_Properties prop INNER JOIN AssMan_Assets Meter ON Meter.UtilMan_PropertyId = prop.Id and Meter.IsDeleted = 0 WHERE prop.IsDeleted = 0 和 prop.Id = batch.PropertyId) END

于 2022-03-05T07:43:12.377 回答