多部分标识符无法绑定到 sql 的情况下

multipart identifier could not be bound on sql with cases when

大家好,我有一个 sql 查询,我在其中使用 case when 和 inner join,

我面临的问题是我收到多部分标识符错误

这是我的 sql 查询

SELECT 
CASE when row_num = 1 THEN bill_id ELSE NULL 
END as bill_id, listinvoice.sonvinid,
listinvoice.date, listinvoice.brandname,listinvoice.venue,listinvoice.zone,
listinvoice.location,
listinvoice.instructore,listinvoice.paymentid,listinvoice.amount
FROM (
select bill_id, row_number()
 over
 (partition by bill_id order by listinvoice.date asc)
  row_num, listinvoice.sonvinid, tid, listinvoice.date
  , listinvoice.brandname,listinvoice.venue,
  listinvoice.zone,listinvoice.location,
  listinvoice.instructore,paymentid,amount
from listinvoice
inner join sonvininsert
on 
sonvininsert.sonvinid=listinvoice.sonvinid
 where 
tid in (select tid from trainerdetails where empname='andrew charles')
and listinvoice.[date] between
'2015-02-02' and '2017-02-02'
)data

还有我的错误

Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.sonvinid" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.date" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.brandname" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.venue" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.zone" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.location" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.instructore" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.paymentid" could not be bound. Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "listinvoice.amount" could not be bound.

可能的修复方法是什么?

除了内部查询的别名外,您的查询是正确的,需要进行一次更改:

SELECT 
    CASE 
        when row_num = 1 
        THEN bill_id 
        ELSE NULL 
    END as bill_id, 
    listinvoice.sonvinid,
    listinvoice.date,
    listinvoice.brandname,
    listinvoice.venue,
    listinvoice.zone,
    listinvoice.location,
    listinvoice.instructore,
    listinvoice.paymentid,
    listinvoice.amount
FROM (
    select 
        bill_id, 
        row_number() over
            (partition by bill_id order by listinvoice.date asc) row_num, 
        listinvoice.sonvinid, 
        tid, 
        listinvoice.date , 
        listinvoice.brandname,
        listinvoice.venue,  
        listinvoice.zone,
        listinvoice.location,
        listinvoice.instructore,
        paymentid,
        amount
    from listinvoice
        inner join sonvininsert
            on 
            sonvininsert.sonvinid=listinvoice.sonvinid
    where 
        tid in 
            (
                select 
                    tid 
                from trainerdetails 
                where empname='andrew charles'
            )
    and listinvoice.[date] 
        between '2015-02-02' and '2017-02-02'
        )listinvoice -- change required here to correct the alias
SELECT 
CASE when row_num = 1 THEN bill_id ELSE NULL 
END as bill_id, data.sonvinid,
data.date, data.brandname,data.venue,data.zone,
data.location,
data.instructore,data.paymentid,data.amount
FROM (
select bill_id, row_number()
 over
 (partition by bill_id order by listinvoice.date asc)
  row_num, listinvoice.sonvinid, tid, listinvoice.date
  , listinvoice.brandname,listinvoice.venue,
  listinvoice.zone,listinvoice.location,
  listinvoice.instructore,paymentid,amount
from listinvoice
inner join sonvininsert
on 
sonvininsert.sonvinid=listinvoice.sonvinid
 where 
tid in (select tid from trainerdetails where empname='andrew charles')
and listinvoice.[date] between
'2015-02-02' and '2017-02-02'
)data -- here is where you named your table

您的 SELECT 语句是从您命名为 data 的派生 table 中提取的,因此您需要适当地引用 table 名称。