2017-09-26 4 views
1

Hallo, ich eine SQL-Abfrage haben, in denen ich Fall bin mit, wann und innere Verknüpfung,mehrteiliger Bezeichner konnte nicht auf SQL-Fälle gebunden werden, wenn

das Problem, das ich bin vor ist Ich bin der Fehler mehrt Kennung bekommen

hier ist meine sQL-Abfrage

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 

und meine Fehler

Msg 4104, Ebene 16, Status 1, Leitung 1 Die mehrteilige Kennung "listinvoice.sonvinid" konnte nicht gebunden werden. Msg 4104, Ebene 16, Status 1, Zeile 1 Die mehrteilige Kennung "listinvoice.date" konnte nicht gebunden werden. Meldung 4104, Ebene 16, Status 1, Zeile 1 Die mehrteilige Kennung "listinvoice.brandname" konnte nicht gebunden werden. Msg 4104, Ebene 16, Status 1, Zeile 1 Die mehrteilige Kennung "listinvoice.venue" konnte nicht gebunden werden. Meldung 4104, Ebene 16, Status 1, Zeile 1 Die mehrteilige Kennung "listinvoice.zone" konnte nicht gebunden werden. Msg 4104, Ebene 16, Status 1, Zeile 1 Die mehrteilige Kennung "listinvoice.location" konnte nicht gebunden sein. Nachricht 4104, Ebene 16, Status 1, Zeile 1 Die mehrteilige Kennung "listinvoice.instructore" konnte nicht gebunden werden. Msg 4104, Ebene 16, Status 1, Zeile 1 Die mehrteilige Kennung "listinvoice.pansionid" konnte nicht gebunden werden. Nachricht 4104, Ebene 16, Status 1, Zeile 1 Der mehrteilige Bezeichner "listinvoice.amount" konnte nicht gebunden werden.

Was werden die möglichen Korrekturen dafür sein?

+0

Versuchen Sie, alle ersten 'SELECT's in' data.columnnames' aus 'listinvoice.columnnames' zu ändern. – Simon

+0

Von dem, was Sie gepostet haben, ist keine Unterabfrage erforderlich. –

+0

Was sind Daten hier? –

Antwort

1

Ihre Anfrage ist korrekt, außer für den Alias ​​der inneren Abfrage und erfordert eine einzige Änderung:

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 
0
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 

Ihre SELECT Anweisung wird von einer abgeleiteten Tabelle ziehen Sie data benannt, so dass Sie benötigen referenziere den Tabellennamen entsprechend.

Verwandte Themen