Error when using UNPIVOT in SQL Server - sql

I have the following query:
SELECT STDEV(Value) as Value, TimeOfTest as Date
FROM myTable
unpivot
(
value
for col in (WS1, WS2, WS3, WS4, WS5, WS6, WS7, WS8, WS9, WS10, WS11,
WS12, WS13, WS14, WS15, WS16, WS17, WS18, WS19, WS20)
) un
GROUP BY TimeOfTest
ORDER BY TimeOfTest DESC
but I get an SQL server 2008 error: Incorrect syntax near the keyword 'FOR'
Would anyone know the reason why? The syntax looks correct.

Just use APPLY.
SELECT t.TimeOfTest, STDEV(ws) as Value
FROM myTable t CROSS APPLY(
(VALUES (WS1), (WS2), . . . (WS20)) v(ws)
GROUP BY t.TimeOfTest
ORDER BY t.TimeOfTest DESC;
APPLY implements something called a "lateral join". This is a very powerful (and ANSI-standard) construct, that can be used for many things beyond unpivoting. On the other hand, unpivot is very specific syntax that is used for only one purpose . . . and as you have found, may not work well other constructs such as GROUP BY (I think you could resolve your issue by using a subquery).

Related

Openfire ORA-00907: Missing right parenthesis

I have the following problem retrieving messages from the OpenFire Monitoring Service plugin. I discovered that the error is due to an incorrect query in the database but I still cannot detect the error for which the query is not working correctly.
SELECT
fromjid,
fromjidresource,
tojid,
tojidresource,
sentdate,
body,
stanza,
messageid,
barejid
FROM
(
SELECT
DISTINCT ofmessagearchive.fromjid,
ofmessagearchive.fromjidresource,
ofmessagearchive.tojid,
ofmessagearchive.tojidresource,
ofmessagearchive.sentdate,
ofmessagearchive.body,
ofmessagearchive.stanza,
ofmessagearchive.messageid,
ofconparticipant.barejid
FROM
ofmessagearchive
INNER JOIN ofconparticipant ON ofmessagearchive.conversationid =
ofconparticipant.conversationid
WHERE
(
ofmessagearchive.stanza IS NOT NULL
OR ofmessagearchive.body IS NOT NULL
)
AND ofmessagearchive.messageid IS NOT NULL
AND ofmessagearchive.sentdate >= 0
AND ofmessagearchive.sentdate <= 1602748770287
AND ofconparticipant.barejid = 'usuario3#192.168.0.79'
AND (
ofmessagearchive.tojid = 'usuario4#192.168.0.79'
OR ofmessagearchive.fromjid = 'usuario3#192.168.0.79'
)
ORDER BY
ofmessagearchive.sentdate DESC
LIMIT
100
) AS part
ORDER BY
sentdate
I get an error when doing the following query
ORA-00907: missing right parenthesis
Command line error:32 Column: 9
There is no LIMIT keyword available in Oracle and if you are using Oracle 12c you can use FETCH FIRST 100 ROWS ONLY instead of it.
You cannot use AS to give alias to the sub query and it is not recognised by Oracle. So either you can remove the alias completely as you are not using it anywhere or just remove the AS and keep the alias name part only which should be fine.
Here is a good SO link about the Oracle limiting result set and you can always look into other sites available such as Oracle base or the official document as well. For 11g solution you have to use row_number

Google Cloud Big Query , Github Dataset Syntax Error

I am trying to make a query but google cloud gives a syntax error.
I had coppied this code which written in 2017 .
I have no idea about Sql
Syntax error: Unexpected "[" at [5:6]. If this is a table identifier, escape the name with `, e.g. `table.name` rather than [table.name].
The query is:
SELECT
f.repo_name,
f.path,
c.pkey
FROM
[bigquery-public-data:github_repos.files] f
JOIN (
SELECT
id,
You are probably using Standard SQL -- which is a good thing.
Try writing the table reference as:
FROM `bigquery-public-data.github_repos.files` f

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.

unpivot result from temporary table

i try to get the result in an unpivot resultset but with more column I've never used this technique.
I got a temporary table.. with many field... but focus on listing below.
SELECT
u.IDNode_u,
l.lev_u
from #Slugs
unpivot
(
IDNode_u for node in (IDNodo, IDNodoPadre, [IDNodoPadre-2], [IDNodoPadre-3], [IDNodoPadre-4], [IDNodoPadre-5], [IDNodoPadre-6])
) u
unpivot
(
lev_u for level in (LivelloTop, [LivelloTop-1], [LivelloTop-2], [LivelloTop-3], [LivelloTop-4], [LivelloTop-5], [LivelloTop-6])
) l
The query is ok only with the first unpivot, with second add I got error:
"The multi-part identifier "u.IDNodo_u" could not be bound.", but I think is bound to the second unpivot.
Can someone with me some advise?
Alen Italy.
I think the easiest way to unpivot is to use cross apply:
select u.IDNode_u, l.lev_u
from #slugs s cross apply
(values (IDNodo), (IDNodoPadre), ([IDNodoPadre-2]), ([IDNodoPadre-3]),
([IDNodoPadre-4]), ([IDNodoPadre-5]), ([IDNodoPadre-6])
) u(IDNode_u) cross apply
(values (LivelloTop), ([LivelloTop-1]), ([LivelloTop-2]),
([LivelloTop-3]), ([LivelloTop-4]), ([LivelloTop-5]),
([LivelloTop-6])
) l(lev_u);
EDIT:
I think you intend:
select ul.IDNode_u, ul.lev_u
from #slugs s cross apply
(values (IDNodo, LivelloTop),
(IDNodoPadre, [LivelloTop-1]),
([IDNodoPadre-2], [LivelloTop-2]),
([IDNodoPadre-3], [LivelloTop-3]),
([IDNodoPadre-4], [LivelloTop-4]),
([IDNodoPadre-5], [LivelloTop-5]),
([IDNodoPadre-6], [LivelloTop-6])
) ul(IDNode_u, lev_u) ;

Postgresql "subquery in FROM must have an alias" error

I'm doing a quick query using Postgresql 8.2 and I've done queries like this a thousand times before, but I can't figure out why I'm getting this error. I probably am missing something obvious, but it's saying my "subquery in FROM must have an alias". I do have an alias for my subquery "inner", and I can't figure out why else I would be getting the error.
SELECT "Branch", "Zip_5", "CountofStops", avg("EarlyTime") As
"Average_Arrival_Time"
FROM
(SELECT branch_id as "Branch", substring(stop_zip_postal_code, 1, 5) as
"Zip_5", count(stop_name) as "CountofStops", min(actual_arrival_time) as
"EarlyTime"
FROM distribution_stop_information
WHERE company_no = '001' AND route_date > '3/13/2017'
GROUP BY branch_id, stop_zip_postal_code)
inner
GROUP BY "Branch", "Zip_5"
ORDER BY Zip_5
********** Error **********
ERROR: subquery in FROM must have an alias
SQL state: 42601
Hint: For example, FROM (SELECT ...) [AS] foo.
inner is a reserved keyword. Use another name as alias.
inner . . . think "inner join". You need a better alias than that.
SELECT Branch, Zip_5, CountofStops, avg(EarlyTime) As Average_Arrival_Time
FROM (SELECT branch_id as Branch, left(stop_zip_postal_code, 5) as Zip_5,
count(stop_name) as CountofStops,
min(actual_arrival_time) as EarlyTime
FROM distribution_stop_information
WHERE company_no = '001' AND route_date > '2017-03-13'
GROUP BY branch_id, stop_zip_postal_code
) b
GROUP BY Branch, Zip_5
ORDER BY Zip_5;
Notes:
Don't wrap column names in double quotes unless needed. They are just superfluous.
Use standard formats for date constants.
LEFT() is a convenient shorthand for substring( . . ., 1, . . .)