连接两个表时内置函数名称错误?

时间:2015-08-24 14:10:57

标签: sql sql-server

  

错误:消息195,级别15,状态10,行6   ' Mem_date'不是公认的内置函数名称。

这是我第一次尝试加入两个表格,这可能是一个简单的答案,但为什么我会收到此错误?

select 
    e.Meme_lev2 as Group#, 
    e.MEME_GRPN as Groupname, 
    mem_date(MONTH),
    COUNT(*)
from 
    Impact.dbo.tbl_mem m
left join 
    Impact.dbo.tbl_meme e on e.MEME_ID1 = m.MEM_ID1
inner join 
    impactwork.dbo.tbl_mmonth x on x.mem_date between convert(varchar(10), e.meme_eff, 101) and convert(varchar(10), e.meme_trm, 101)
                                and x.mem_date between '01/01/2015' and '03/31/2015'
where
    e.MEME_LEV2 = '52024' 
    and GETDATE() between '01/16/2015' and '06/16/2015'
group by 
    mem_date(month)

2 个答案:

答案 0 :(得分:2)

在Mem_Date之后添加(MONTH)使SQL Server认为它是您尝试将参数传递给的函数。纠正它:

select 
    e.Meme_lev2 as Group#, 
    e.MEME_GRPN as Groupname, 
    MONTH(x.mem_date) AS mem_date, -- changed
    COUNT(*)
from Impact.dbo.tbl_mem m
left join Impact.dbo.tbl_meme e 
    on e.MEME_ID1 = m.MEM_ID1
inner join impactwork.dbo.tbl_mmonth x 
    on x.mem_date between convert(varchar(10), e.meme_eff, 101) and convert(varchar(10), e.meme_trm, 101)
    and x.mem_date between '01/01/2015' and '03/31/2015'
where
    e.MEME_LEV2 = '52024' 
    and GETDATE() between '01/16/2015' and '06/16/2015'
group by 
    e.Meme_lev2, 
    e.MEME_GRPN, 
    MONTH(x.mem_date) 

请注意,我还在SELECT语句中添加了GROUP BY语句中的其他字段。

编辑:所以你可以看到@jarlh的建议:

select 
    e.Meme_lev2 as Group#, 
    e.MEME_GRPN as Groupname, 
    MONTH(x.mem_date) AS mem_date, -- changed
    COUNT(*)
from 
    Impact.dbo.tbl_mem m
     left join 
    Impact.dbo.tbl_meme e on 
        e.MEME_ID1 = m.MEM_ID1 AND 
        e.MEME_LEV2 = '52024' 
     inner join impactwork.dbo.tbl_mmonth x on 
            x.mem_date between convert(varchar(10), e.meme_eff, 101) and convert(varchar(10), e.meme_trm, 101) and 
            x.mem_date between '01/01/2015' and '03/31/2015'
--where
--  GETDATE() between '01/16/2015' and '06/16/2015'
group by 
    e.Meme_lev2, 
    e.MEME_GRPN, 
    MONTH(x.mem_date) 

答案 1 :(得分:0)

我想你可能想要MONTH(mem_date)而不是mem_date(MONTH)

MONTH是一个函数,mem_date不是。