AS not working with COUNT(*) and UNION - mysql

So I have joined 3 queries together using UNIONs and want to count the number of lines in the result, but it's a bit weird. It actually works, and gives the correct answer, but it doesn't assign the "AS" part correctly.
SELECT COUNT(*) FROM (
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
)
AS NoOfTweets";
The outcome is correct, but instead of assigning it to "NoOfTweets" it assigns it to "Count(*)". If I remove the "AS NoOfTweets" it stops working. If I remove some brackets it stops working. I'm running low on ideas after a long day! I can post the whole code if needs be but would rather not as it's quite long and I think that bit works.
Thanks in advance, Jack.
Edit: Fixed with:
SELECT COUNT(*) NoOfTweets FROM (
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
)
AS NoOfTweets";
Thanks guys :)

You aren't putting it in the correct location. The beginning of your query should look like this:
SELECT COUNT(*) AS NoOfTweets
More on Column Alias

SELECT COUNT(*) NoOfTweets FROM
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
or
SELECT COUNT(*) AS NoOfTweets FROM
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)

You have to use AS exactly after the item you are counting:
SELECT COUNT(*) AS `NoOfTweets`
FROM ( ... )
Also be careful with the " you have near the end. Or maybe it comes from a longer string.

The error is Every derived table must have its own alias which is something I didn't know, so thanks for the education :)
http://sqlfiddle.com/#!9/d30f4/4
Nice of MySQL to give an explanation - I tried with MS SQL on SQLFiddle and just got Incorrect syntax near ')'. which isn't so helpful!
So, your 'NoOfTweets' is the name given to the results column, and also to the 'derived table' which is required by the SQL engine but could be a different name ... it's not returned in the results. The point of naming a derived table is in case you wish to JOIN to other tables and reference the fields in the joins.

Related

SQL select: how to alias only 1 column and leave the rest unaliased [duplicate]

I have a table with many columns let say column1,....,column20. I don't want to scroll everytime to the end of the result table to see the value of column20. In mssql I usually do
SELECT column20, * FROM TABLE but apparently this is not valid in MySQL. Any hints? (I also don't want to select all columns explicitly in the select statement)
You have to give the table name in your query, otherwise mysql complains :
SELECT column20, mytable.* FROM mytable
PS: I have absolutely no idea as to why, because SELECT *, column20 FROM mytable works just fine... Strange things happens sometimes ^^

How to SELECT COUNT(DISTINCT) and WHERE conditional on Ms.Access Query

I just wanted to simply count using where conditional and yet this query ask me for parameter instead of automatically execute the query
SELECT COUNT(ActDiscDischargingPort) from(
SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable) WHERE SchLoadVessel LIKE "XB24 - MV. MEMPHIS" AND SchLoadVoyageNo LIKE "0019";
What is the proper way of writing this query?
Found the answer, turn out the query has to be like this
SELECT COUNT(ActDiscDischargingPort) from ( SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable WHERE SchLoadVoyageNo LIKE "XB24 - MV. MEMPHIS" AND SchLoadVessel LIKE "0019" )
Every derived table must have its own alias.
The correct syntax would be
SELECT
COUNT(ActDiscDischargingPort)
from(
SELECT
DISTINCT ActDiscDischargingPort
FROM
SelisihLoadVSActualLoadTable
) AS T
WHERE
SchLoadVessel LIKE "XB24 - MV. MEMPHIS"
AND SchLoadVoyageNo LIKE "0019";
You can further speed up your query by optimizing it a bit.

Why does DISTINCT has to go first in MySQL?

I have a query that works when I do
SELECT DISTINCT(table.field.id), 1 FROM ...
but fails when I do
SELECT 1, DISTINCT(table.field.id) FROM ...
Is this a known behavior?
Why does the first one work while the second doesn't?
Unfortunately I'm not able to add a comment yet.
What #Gordon Linoff has written is exactly right.
You are getting error as DISTINCT in general works as part of SELECT clause or AGGREGATE function. It is used to return unique rows from a result set and it can be used to force unique column values within an aggregate function.
Examples: SELECT DISTINCT * ... COUNT(DISTINCT COLUMN) or SUM(DISTINCT COLUMN).
More information's about DISTINCT in popular DB engines:
PostgreSQL:https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-DISTINCT
SQL Server: https://www.techonthenet.com/sql_server/distinct.php
Oracle: https://www.techonthenet.com/oracle/distinct.php
MySQL:
https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
https://dev.mysql.com/doc/refman/5.7/en/select.html

WSO2 DAS: SPARK SQL query with UNION producing errors

The following query was attempted to be executed when performing batch analytics with WSO2 DAS using Spark SQL. Tables 'First', 'Middle' and 'Third' are required to be combined and written to table 'All_three'.
INSERT OVERWRITE TABLE All_three
SELECT SYMBOL, VOLUME FROM First
UNION
SELECT SYMBOL, VOLUME FROM Middle
UNION
SELECT SYMBOL, VOLUME FROM Third;
Following error is displayed on WSO2 DAS when this query is executed:
ERROR: [1.79] failure: ``limit'' expected but `union' found INSERT OVERWRITE TABLE X1234_All_three SELECT SYMBOL, VOLUME FROM X1234_First UNION SELECT SYMBOL, VOLUME FROM X1234_Middle UNION SELECT SYMBOL, VOLUME FROM X1234_Third ^
Using LIMIT with UNION is not a necessity to the best of my knowledge. Enclosing the SELECT queries in parentheses too was attempted which didn't work. What am I doing wrong here? Thank you in advance!
I had the same issue.
Please make sure you had an whitespace in end of each row.
The reason is it is not considering \n as a whitespace like SSMS or other query editors.
So it reads your query without whitespace.
Hence, it read like SELECT * FROM FirstUNION not like SELECT * FROM First UNION
My issue was resolved and I hope this helps for you too.
There exists a problem with the query you mentioned here. Please change the query as below.
INSERT OVERWRITE TABLE All_three
select * from (
SELECT SYMBOL, VOLUME FROM First
UNION
SELECT SYMBOL, VOLUME FROM Middle
UNION
SELECT SYMBOL, VOLUME FROM Third
) temp;
Actually what we do here is, wrapping the union result into temporary data element called temp and select everything from there. Spark-SQL parser only takes single select element in the insert queries, and at the end of a select query it expects a limit (if available). therefore, you need to wrap the subsequent select statements into one select element. Hope this resolves your issue.

mysql query, reuse columnnames in query

I have a mysql query, something like this:
SELECT users*100 as totalusers, totalusers*90 AS totalsalerys FROM table
As you can see I want to reuse the totalusers when calculating totalsalerys so I son't have to write the same code again. That dosen't seem to work in mysql, is there another easy way to do this?
My query is just an example, change the *100 and *90 to some very long formula and you might see what i mean..
SELECT (#r := users * 100) as totalusers,
#r * 90 AS totalsalerys
FROM table
You can also use a subquery as #Tom Ritter advices, but it's not friendly to ORDER BY ... LIMIT ... clause.
In MySQL, all results of the inner subquery will be fetched before applying any filters in the outer subquery.
I believe you would have to copy/paste the formula or use a subquery. The below would work in T-SQL, but I imagine it'd work in MySQL as well since it's pretty simple.
SELECT
x.totalusers, x.totalusers*90 AS totalsalerys
FROM (
SELECT users*100 as totalusers FROM table
) x