Visual Studio error on valid SQL - sql

I'm getting an annoying error in visual studio for SQL that executes fine.
SELECT InvoiceLines.LineID,
InvoiceLines.InvoiceID,
InvoiceLines.Text,
InvoiceLines.Rate,
InvoiceLines.Count,
InvoiceLines.Rate * InvoiceLines.Count AS LineTotal,
((InvoiceLines.Rate * InvoiceLines.Count) * (1 + Invoices.VatRate / 100)) * (1 - CAST(Invoices.Discount AS money) * InvoiceLines.ApplyDiscount / 100) AS LineTotalIncVat, InvoiceLines.ApplyDiscount
FROM InvoiceLines
LEFT JOIN Invoices ON Invoices.InvoiceID = InvoiceLines.InvoiceID
What LineTotalIncVat is trying to do is compute the total for the invoice item while adding the vat and subtracting the discount, and yes, probably better to do this in code (would if I could)
The error visual studio gives is:
There was an error parsing the query [token line number =1, token line offset =14, token in error = InvoiceLines]
Even though it will validate and execute without a problem in the query builder...

Try bracketing the table and column names, it may be having trouble parsing InvoiceLines.Count because Count is a reserved word. Try [InvoiceLines].[Count].

Solved
Deleted the table from the dataset and added it again with exactly the same SQL.
How strange... although not the first time I've had to do this.

Related

Divide by zero error when converting Access to SQL Server query

I am trying to convert an Access query to one that works in SQL server. The original query in Access works perfectly well (just terribly slow).
I only changed things slightly to make it compatible with SQL server instead of Access, like changing "NOW()" to "GETDATE()" and we can no longer divide aliases.
Running this query in SQL Server:
SELECT batches.[price-group],
[development].verifier,
Count([development].company) AS SENT,
Sum([order] *- 1) AS ORDS,
Count([development].company) / Sum([order] *- 1) AS PCT
FROM [development]
INNER JOIN batches
ON [development].batch = batches.batch
WHERE (( ( [development].[mail-date] ) < Getdate() - 50 ))
GROUP BY batches.[price-group],
[development].verifier
HAVING (( ( batches.[price-group] ) = 'pgb' ))
ORDER BY batches.[price-group],
[development].verifier,
Count([development].company) DESC;
Returns this error:
Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
Only real change, was like I said, in Access we could do this
[ords] / [sent] AS PCT
Any help will be appreciated, I'm not sure exactly why it isn't working! Removing the converted line above, does work in SQL server without any errors.
Thank you!
Use NULLIF():
Count([development].company) / NULLIF(Sum([order] * -1), 0) AS PCT

U-sql error: Expected one of: AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')' ','

I have a following table:
EstimatedCurrentRevenue -- Revenue column value of yesterday
EstimatedPreviousRevenue --- Revenue column value of current day
crmId
OwnerId
PercentageChange.
I am querying two snapshots of the similarly structured data in Azure data lake and trying to query the percentage change in Revenue.
Following is my query i am trying to join on OpportunityId to get the difference between the revenue values:
#opportunityRevenueData = SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue) AS PercentageRevenueChange, optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue, opty.crmId, opty.OwnerId From #opportunityCurrentData AS opty JOIN #opportunityPreviousData AS optyPrevious on opty.OpportunityId == optyPrevious.OpportunityId;
But i get the following error:
E_CSC_USER_SYNTAXERROR: syntax error. Expected one of: AS EXCEPT FROM
GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')'
','
at token 'From', line 40
near the ###:
This expression is having the problem i know but not sure how to fix it.
(((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue)
Please help, i am completely new to U-sql
U-SQL is case-sensitive (as per here) with all SQL reserved words in UPPER CASE. So you should capitalise the FROM and ON keywords in your statement, like this:
#opportunityRevenueData =
SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue) * 100) / opty.EstimatedCurrentRevenue) AS PercentageRevenueChange,
optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue,
opty.crmId,
opty.OwnerId
FROM #opportunityCurrentData AS opty
JOIN
#opportunityPreviousData AS optyPrevious
ON opty.OpportunityId == optyPrevious.OpportunityId;
Also, if you are completely new to U-SQL, you should consider working through some tutorials to establish the basics of the language, including case-sensitivity. Start at http://usql.io/.
This same crazy sounding error message can occur for (almost?) any USQL syntax error. The answer above was clearly correct for the provided code.
However since many folks will probably get to this page from a search for 'AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE', I'd say the best advice to handle these is look closely at the snippet of your code that the error message has marked with '###'.
For example I got to this page upon getting a syntax error for a long query and it turned out I didn't have a casing issue, but just a malformed query with parens around the wrong thing. Once I looked more closely at where in the snippet the ### symbol was, the error became clear.

SQL that access accepts, but SQL Server Express rejects

Today I wrote this bit of sql:
SELECT COUNT(T0021_werk_naam)
FROM (SELECT Distinct T0021_werk_naam,T0021_jaar,T0021_kwartiel
FROM T0021_offertes
WHERE T0021_status_code = 'G' AND T0021_jaar = 2013 AND (T0021_kwartiel = 3))
This sql runs great when I run it locally in access, however, when I run it through the code that has been used for ages for this and most certainly definetly is not the problem, and send it to SQL Express it gives an error that says there's a problem near ')'
After stripping away all the brackets possible it becomes clear that it detects there's a problem with the last ')' but I don't see the problem.
Any Ideas?
You need to give an alias for the select in the parenthesis:
SELECT COUNT(T0021_werk_naam)
FROM (
SELECT Distinct T0021_werk_naam,
T0021_jaar,
T0021_kwartiel
FROM T0021_offertes
WHERE T0021_status_code = 'G'
AND T0021_jaar = 2013
AND (T0021_kwartiel = 3)
) T
notice the T in the end after the last parenthesis.

Receiving the following error with my query when calculating distance

I'm receiving the following error "An invalid floating point operation occurred." when I run this query:
SELECT PolID, LocID, Address, City, StateCode, OrigGeoLat, OrigGeoLong, NewGeoLat, NewGeoLong,
acos(sin(radians(OrigGeoLat)) * sin(radians(NewGeoLat)) +
cos(radians(OrigGeoLat)) * cos(radians(NewGeoLat)) *
cos(radians(OrigGeoLong - NewGeoLong))) * 6372.8 as Distance
FROM zzGeoDataTMP
All of the *geoLat and *geoLong data is defined as numeric(18,10). When I run the query I start getting data back and then I get errors on specific rows of data. For example the following row throws the above mentioned exception only once the ACOS function is called:
OrigGeoLat|OrigGeoLong|NewGeoLat|NewGeoLong
---------------------------------------------
32.9364620|-80.0411000|32.9364620|-80.0411000
Thanks so much in advance for any insight you may be able to help with!
You're calculation is accumulating some small errors, which mean that the calculated value (passed to ACOS) is slightly higher than 1. Try wrapping it with a ROUND call:
acos(ROUND(sin(radians(OrigGeoLat)) * sin(radians(NewGeoLat)) +
cos(radians(OrigGeoLat)) * cos(radians(NewGeoLat)) *
cos(radians(OrigGeoLong - NewGeoLong)),15)
)
Where we're only keeping 15 decimal places of accuracy.

SQL Error in VBA

I have the following code in SQL (2005) which calculates the avarage user logins on a systm:
with
user_total as
(
select COUNT(distinct ID) as counter
FROM [dbo].[LOG]
where [LOG].DESCRIPTION='Login success.'
AND
Convert(datetime,convert(char(10),[LOG].CREATED_ON,101)) BETWEEN '2009-01- 01' AND '2009-12-31'
),
USER_avg as
(
select counter/365 as Avarage_Daily_Logins
from user_total
)
select *
from USER_avg
Now the problem is when i put this in a VBA macro in excel to get the result in a spcific cell in strSQL = "QUERY SHOWN ABOVE HERE" argument i get the error in excel
incorrect sysntax near the keyword with
Its worth mentioning that i dont break the code in VBA in multiple lines..i have it all in one line.
Use ;WITH ...
WITH usage for a CTE must have ; after the previous statement. To ensure this is the cases, prefix with ;
Check all of your spacings, if there is an error further down the compiler may interpret it as a problem with the with statement.