从天数中获取天数

时间:2019-06-13 08:28:20

标签: tsql

我正在尝试在一长串日期(TxnDate)中捕获一周中的特定日期。我正在使用的以下命令抛出以下错误:

  

无效的列名“ TxnDay”。

代码

Select DATENAME(dw, ft.[TxnDate]) as TxnDay, ft.[ProductCode], ft.[Site] from dbo.FactTransactions as Ft 
Where ft.[Site] = '1' and TxnDay = 'Tuesday'

编辑:我也尝试了以下错误:

  

无效的列名“ TxnDay”。

Select ft.[TxnDate], ft.[ProductCode], ft.[Site] from 
dbo.FactTransactions as Ft
Where ft.[Site] = '1' and ft.[TxnDate] in (SELECT DATENAME(dw, ft. 
[TxnDate]) as TxnDay where TxnDay = 'Sunday')

2 个答案:

答案 0 :(得分:0)

除非我丢失了某些内容,否则我不确定您是否完全需要WHERE子句中的子查询-您不能这样做吗?

select 
    ft.[TxnDate], 
    ft.[ProductCode], 
    ft.[Site] 
from dbo.FactTransactions as ft
Where 
    ft.[Site] = '1' 
    and DATENAME(dw, ft.[TxnDate]) = 'Sunday'

答案 1 :(得分:0)

嗨,不要在where子句中使用列的别名。解决方案如下。

 Select DATENAME(dw, ft.[TxnDate]) as TxnDay, ft.[ProductCode], ft.[Site] from 
  dbo.FactTransactions as Ft 
  Where ft.[Site] = '1' and DATENAME(dw, ft.[TxnDate])= 'Tuesday'