Error in SQL Statement

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Could someone please help me fix this SQL statement; it gives me
Line 57: Incorrect syntax near 'qti' where I've marked it with /*****/
I am using MS Query Analyzer 8.0.


SELECT qti.CUSTNMBR,sum(qti.TRXAMT) as amnt
FROM
(
SELECT DISTINCT rtrim(gl5.actnumbr_1)+'-'+ rtrim(gl5.actnumbr_2) AS
STATE_JOB,c.CUSTNMBR--,
FROM centr.dbo.RM30101 c
inner join (select distinct custnmbr,docnumbr,dstindx,rmdtypal from
centr.dbo.rm30301) ma on
c.custnmbr=ma.custnmbr
and c.docnumbr=ma.docnumbr
and c.rmdtypal=ma.rmdtypal
inner join (select * from centr.dbo.gl00105) gl5
on ma.dstindx=gl5.actindx
WHERE ((case when year(c.VOIDDATE)>2003 then 0 else c.VOIDSTTS end =0) and
(c.GLPOSTDT<='12/31/03')) and gl5.actnumbr_2<>'0000'
and exists (
SELECT qti.CUSTNMBR
FROM
(
SELECT c.CUSTNMBR, c.DOCNUMBR, c.TRXSORCE, c.RMDTYPAL, c.PYMTRMID,
c.DOCDATE,
case when [RMDTYPAL]=1 then [DUEDATE] else [DOCDATE] end AS DUEDATE2,
c.GLPOSTDT,
case when [RMDTYPAL]>6 then -[ORTRXAMT] else [ORTRXAMT] end AS TRXAMT,
c.VOIDSTTS, c.VOIDDATE
FROM centr.dbo.RM30101 c
WHERE ((c.voidstts=0) and (c.GLPOSTDT<='12/31/03'))
union all
--
SELECT d.CUSTNMBR, d.DOCNUMBR, d.TRXSORCE, d.RMDTYPAL, d.PYMTRMID,
d.DOCDATE,
case when [RMDTYPAL]=1 then [DUEDATE] else [DOCDATE] end AS
DUEDATE2,d.GLPOSTDT,
case when [RMDTYPAL]>6 then -[ORTRXAMT] else [ORTRXAMT] end AS TRXAMT,
d.VOIDSTTS, d.VOIDDATE
FROM centr.dbo.RM20101 d
WHERE ( (d.VOIDSTTS=0 )and (d.GLPOSTDT<='12/31/03'))
) qti
LEFT JOIN
(
SELECT a.CUSTNMBR, a.TRXSORCE, a.POSTED, a.APTODCNM, a.APTODCTY,
a.ApplyToGLPostDate,
a.APPTOAMT, a.APFRDCNM, a.APFRDCTY, a.ApplyFromGLPostDate
FROM centr.dbo.RM30201 a
GROUP BY a.CUSTNMBR, a.TRXSORCE, a.POSTED, a.APTODCNM, a.APTODCTY,
a.ApplyToGLPostDate,
a.APPTOAMT, a.APFRDCNM, a.APFRDCTY, a.ApplyFromGLPostDate
HAVING (((a.POSTED)=1)and a.ApplyFromGLPostDate<='12/31/03')
union all
SELECT b.CUSTNMBR, b.TRXSORCE, b.POSTED, b.APTODCNM, b.APTODCTY,
b.ApplyToGLPostDate,
b.APPTOAMT, b.APFRDCNM, b.APFRDCTY, b.ApplyFromGLPostDate
FROM centr.dbo.RM20201 b
GROUP BY b.CUSTNMBR, b.TRXSORCE, b.POSTED, b.APTODCNM, b.APTODCTY,
b.ApplyToGLPostDate,
b.APPTOAMT, b.APFRDCNM, b.APFRDCTY, b.ApplyFromGLPostDate
HAVING (((b.POSTED)=1) and b.ApplyFromGLPostDate<='12/31/03')
)
qtp ON
[qti].DOCNUMBR = [Qtp].APTODCNm
AND [qti].CUSTNMBR = [qtp].CUSTNMBR
GROUP BY [Qti].CUSTNMBR
union all
SELECT d.CUSTNMBR, d.DOCNUMBR, d.TRXSORCE, d.RMDTYPAL, d.PYMTRMID,
d.DOCDATE,
case when [RMDTYPAL]=1 then [DUEDATE] else [DOCDATE] end AS
DUEDATE2,d.GLPOSTDT,
case when [RMDTYPAL]>6 then -[ORTRXAMT] else [ORTRXAMT] end AS TRXAMT,
d.VOIDSTTS, d.VOIDDATE
FROM centr.dbo.RM20101 d
WHERE ( (d.VOIDSTTS=0 )and (d.GLPOSTDT<='12/31/03'))
) qti /***** this is the error line/

group by qti.CUSTNMBR--,
having sum(qti.TRXAMT)<>0
.



Relevant Pages

  • Union of two tables in different databases from within a VB6 application
    ... Since the tables are in different databases ... ... The Union in Query analyzer looks like this: ... Select PartNum, Manu, Available from YYYY2.dbo.PART2 where PartNum like ...
    (microsoft.public.sqlserver.programming)
  • Re: performance of Insert with Partitioned View
    ... As a first guess, if you have clustered indexes on the individual tables, does adding an ORDER BY clause to the SELECT * FROM Temp so that the data is being added in the sequence of the clustered indexes improve performance? ... > As documented in the SQL Server Books Online, ... Using just UNION does not create a partitioned view. ... > can be cut and pasted into Query Analyzer is appreciated. ...
    (microsoft.public.sqlserver.security)
  • Re: Simple merging question...
    ... > UNION ... how do I merge two sql tables using query analyzer? ... >> This seems like it'd be so simple, yet I keep generating errors. ... >> What command or function should I be looking for? ...
    (microsoft.public.sqlserver.programming)
  • SQL Statement
    ... SELECT qti.CUSTNMBR,sumas amnt ... union all ... Prev by Date: ...
    (microsoft.public.vb.database)
  • Re: ordering an int column
    ... Run it in Query Analyzer ... -- not ordered as int ... union all ... Prev by Date: ...
    (comp.databases.ms-sqlserver)