From: N.M. on
Hello!

I wrote a sql query I want to run in MS SQL Server:
I got the errror "Incorrect syntax near the keyword GROUP, and I don`t know
what generetes this error message:(
Here is the query:

SELECT
tw_Symbol_final tw_Symbol_ost,
SUM(ilosc2007) ilosc2007_final,
(SUM(ilosc2008)/SUM(ilosc2007))*100 procent2007,
SUM(ilosc2008)-SUM(ilosc2007) roznica2007,
SUM(ilosc2008) ilosc2008_final,
(SUM(ilosc2009)/SUM(ilosc2008))*100 procent2008,
SUM(ilosc2009)-SUM(ilosc2008) roznica2008,
SUM(ilosc2009) ilosc2009_final
FROM
(
SELECT
t2007.tw_Symbol tw_Symbol_final,
SUM(o2007.ob_Znak*(o2007.ob_Ilosc)) ilosc2007,
MAX(0) ilosc2008,
MAX(0) ilosc2009
FROM
tw__Towar t2007
LEFT JOIN dok_Pozycja o2007 ON o2007.ob_TowId=t2007.tw_Id
LEFT JOIN dok__Dokument d2007 ON d2007.dok_Id=o2007.ob_DokHanId
WHERE
(year(d2007.dok_DataWyst)=2007) AND
(d2007.dok_Typ IN (
{CHL:faktura sprzeda�y#2#korekta faktury sprzeda�y#6#:Typ dokumentu}
)
) AND
d2007.dok_PlatnikId IN (1133)
GROUP BY t2007.tw_Symbol
UNION
SELECT
t2008.tw_Symbol tw_Symbol_final,
MAX(0) ilosc2007,
SUM(o2008.ob_Znak*(o2008.ob_Ilosc)) ilosc2008,
MAX(0) ilosc2009
FROM
tw__Towar t2008
LEFT JOIN dok_Pozycja o2008 ON o2008.ob_TowId=t2008.tw_Id
LEFT JOIN dok__Dokument d2008 ON d2008.dok_Id=o2008.ob_DokHanId
WHERE
(year(d2008.dok_DataWyst)=2008) AND
(d2008.dok_Typ IN (
{CHL:faktura sprzeda�y#2#korekta faktury sprzeda�y#6#:Typ dokumentu}
)
) AND
d2008.dok_PlatnikId IN (1133)
GROUP BY t2008.tw_Symbol
UNION
SELECT
t2009.tw_Symbol tw_Symbol_final,
MAX(0) ilosc2007,
MAX(0) ilosc2008,
SUM(o2009.ob_Znak*(o2009.ob_Ilosc)) ilosc2009
FROM
tw__Towar t2009
LEFT JOIN dok_Pozycja o2009 ON o2009.ob_TowId=t2009.tw_Id
LEFT JOIN dok__Dokument d2009 ON d2009.dok_Id=o2009.ob_DokHanId
WHERE
year(d2009.dok_DataWyst)=2009 AND
(d2009.dok_Typ IN (
{CHL:faktura sprzeda�y#2#korekta faktury sprzeda�y#6#:Typ dokumentu}
)
) AND
d2009.dok_PlatnikId IN (1133)
GROUP BY t2009.tw_Symbol
)
GROUP BY tw_Symbol_final

Please help me I have been watching at this query for 2 hours without result
netman



From: N.M. on

U�ytkownik "N.M." <netman(a)poczta.onet.pl> napisa� w wiadomo�ci
news:hgqccl$gko$1(a)news.onet.pl...
> Hello!
>
> I wrote a sql query I want to run in MS SQL Server:
> I got the errror "Incorrect syntax near the keyword GROUP, and I don`t
> know what generetes this error message:(
> Here is the query:
>
> SELECT
> tw_Symbol_final tw_Symbol_ost,
> SUM(ilosc2007) ilosc2007_final,
> (SUM(ilosc2008)/SUM(ilosc2007))*100 procent2007,
> SUM(ilosc2008)-SUM(ilosc2007) roznica2007,
> SUM(ilosc2008) ilosc2008_final,
> (SUM(ilosc2009)/SUM(ilosc2008))*100 procent2008,
> SUM(ilosc2009)-SUM(ilosc2008) roznica2008,
> SUM(ilosc2009) ilosc2009_final
> FROM
> (
> SELECT
> t2007.tw_Symbol tw_Symbol_final,
> SUM(o2007.ob_Znak*(o2007.ob_Ilosc)) ilosc2007,
> MAX(0) ilosc2008,
> MAX(0) ilosc2009
> FROM
> tw__Towar t2007
> LEFT JOIN dok_Pozycja o2007 ON o2007.ob_TowId=t2007.tw_Id
> LEFT JOIN dok__Dokument d2007 ON d2007.dok_Id=o2007.ob_DokHanId
> WHERE
> (year(d2007.dok_DataWyst)=2007) AND
> (d2007.dok_Typ IN (
> {CHL:faktura sprzeda�y#2#korekta faktury sprzeda�y#6#:Typ dokumentu}
> )
> ) AND
> d2007.dok_PlatnikId IN (1133)
> GROUP BY t2007.tw_Symbol
> UNION
> SELECT
> t2008.tw_Symbol tw_Symbol_final,
> MAX(0) ilosc2007,
> SUM(o2008.ob_Znak*(o2008.ob_Ilosc)) ilosc2008,
> MAX(0) ilosc2009
> FROM
> tw__Towar t2008
> LEFT JOIN dok_Pozycja o2008 ON o2008.ob_TowId=t2008.tw_Id
> LEFT JOIN dok__Dokument d2008 ON d2008.dok_Id=o2008.ob_DokHanId
> WHERE
> (year(d2008.dok_DataWyst)=2008) AND
> (d2008.dok_Typ IN (
> {CHL:faktura sprzeda�y#2#korekta faktury sprzeda�y#6#:Typ dokumentu}
> )
> ) AND
> d2008.dok_PlatnikId IN (1133)
> GROUP BY t2008.tw_Symbol
> UNION
> SELECT
> t2009.tw_Symbol tw_Symbol_final,
> MAX(0) ilosc2007,
> MAX(0) ilosc2008,
> SUM(o2009.ob_Znak*(o2009.ob_Ilosc)) ilosc2009
> FROM
> tw__Towar t2009
> LEFT JOIN dok_Pozycja o2009 ON o2009.ob_TowId=t2009.tw_Id
> LEFT JOIN dok__Dokument d2009 ON d2009.dok_Id=o2009.ob_DokHanId
> WHERE
> year(d2009.dok_DataWyst)=2009 AND
> (d2009.dok_Typ IN (
> {CHL:faktura sprzeda�y#2#korekta faktury sprzeda�y#6#:Typ dokumentu}
> )
> ) AND
> d2009.dok_PlatnikId IN (1133)
> GROUP BY t2009.tw_Symbol
> )
> GROUP BY tw_Symbol_final
>
> Please help me I have been watching at this query for 2 hours without
> result
> netman
>
>
>


I`ve added alias

> SELECT
> A.tw_Symbol_final tw_Symbol_ost,
> SUM(A.ilosc2007) ilosc2007_final,
> (SUM(A.ilosc2008)/SUM(A.ilosc2007))*100 procent2007,
> SUM(A.ilosc2008)-SUM(A.ilosc2007) roznica2007,
> SUM(A.ilosc2008) ilosc2008_final,
> (SUM(A.ilosc2009)/SUM(A.ilosc2008))*100 procent2008,
> SUM(A.ilosc2009)-SUM(A.ilosc2008) roznica2008,
> SUM(A.ilosc2009) ilosc2009_final
....

> GROUP BY t2009.tw_Symbol
> ) AS A
> GROUP BY tw_Symbol_final

but I get the error: SQL query erry (in Polish: "B��d wykonania zapytania")

netman


From: Erland Sommarskog on
N.M. (netman(a)poczta.onet.pl) writes:
> but I get the error: SQL query erry (in Polish: "B��d wykonania
> zapytania")

You are not using Query Designer are you? That is definitely not going to
help you. In any case, it does not really look like an error message
from SQL Server.

I look at this:

> (d2009.dok_Typ IN (
> {CHL:faktura sprzeda�y#2#korekta faktury sprzeda�y#6#:Typ dokumentu}

That stuff in braces is defnitely nothing I recognize. What is it
supposed to be?
--
Erland Sommarskog, SQL Server MVP, esquel(a)sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 | 
Pages: 1
Prev: Help with date
Next: SSIS Connection...