Bug Description
TDengine version 2.2.0.0
select count(*) as ss from gw_state group by gid order by ss;
Use the above statement query, prompted the following error, gid is TAG, can not alias order by?
DB error: invalid SQL: invalid column name (0.001565s)
The problem is not about alias name cannot used in order by clause. As you know TDengine is time-series database, the order by can only be applied to primary timestamp column as I know. In your case count(*) as ss, the ss is the aggregation result column but not the primary timestamp column.
Related
I'm using a query to create a new table in SQL Server using a select query.
My query is
create table test as
select id, sum(marks) as marks
from student
group by id
I'm getting error message in SQL Server. In Stackoverflow I found that instead of create table, SQL Server uses insert into. But I'm not getting how to write group by query using insert into. Can you please guide me?
Try this:
select id,sum(marks) as marks
INTO TEST
from student
group by id
Using INTO clause you are allowed to materialized the result in a table (it can be temporary table as well). There are few limitations - columns must have name (alias) and duplicate names are not allowed.
Select (columns Name or Functions (Max , Min , Sum ...) 1 , 2 , ...)
INTO (Destination table)
From (Source table)
GROUP BY (cloumns Name 1 , 2 , ...)
I am trying to do an initial load of data based on order by creationtime column. My sym_trigger_router (for currencies table ) has below initial_load_select :-
initial_load_select = "1 = 1 order by t.CreationTime"
which create query as below :-
select count(*)
from "board"."dbo"."currencies" t
where 1=1
order by t.CreationTime desc;
It works with MySQL but not with SQL Server because SQL Server does not support order by with count(*).
I'm getting this error :
[head-85711c60-84d5-4527-b31f-a861aa8caa9a] - JdbcSqlTemplate - SQL caused exception: [select count(*) from "board"."dbo"."currencies" t where 1=1 order by t.CreationTime desc ]
java.sql.SQLException: Column "board.dbo.currencies.creationtime" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
As per this link
When 0 is specified for initial_load_batch_count, SymmetricDS will
execute a count(*) query on the table during the extract process
but symmetricds 3.9 has removed this column, now what is workaround to ignore count(*) query for SQL Server for initial load?
Any help?
I have this SQL query:
select top(1)
salary
from
(select top(2) salary
from employee
order by salary desc) as b
order by
salary asc
If I don't utilize as b it will give me an error:
Incorrect syntax near ...
Why is mandatory to use as in this query?
You don't need the as keyword. In fact, I advise using as for column aliases but not for table aliases. So, I would write this as:
select top(1) salary
from (select top(2) salary
from employee
order by salary desc
) b
order by salary asc;
You do need the table alias for the subquery, because SQL Server requires that all subqueries in the from clause be named.
This is TSql syntax. Subquery in FROM must have an alias even it's never used. Oracle for example considers this alias optional.
This is because you have a sub-query that, according to the Transact-SQL documentation on FROM, makes the use of an alias mandatory:
When a derived table, rowset or table-valued function, or operator clause (such as PIVOT or UNPIVOT) is used, the required table_alias at the end of the clause is the associated table name for all columns, including grouping columns, returned.
Note that with derived table the kind of sub-query is intended that you use in your SQL statement:
derived_table
Is a subquery that retrieves rows from the database. derived_table is used as input to the outer query.
Because you are using 'salary' twice. Without an alias the interpreter won't know what 'salary' to order the results by. By using an alias it can discern between employee.salary and b.salary.
A different approach to get the 2nd highest salary... as if you need the 3rd or 4th you're approach would get much more challenging...
SELECT *
FROM (SELECT salary, row_number() over (order by salary desc) rn
FROM employee) E
WHERE rn = 2
You are creating two queries. The first one selects the top 2 salaries from employee. You are calling this list "b". Then you are selecting the top salary from "b".
Using SQL Server Management Studio, I am getting some undesired results (looks like a bug to me..?)
If I use (FIELD rather than field for the other_table):
SELECT * FROM main_table WHERE field IN (SELECT FIELD FROM other_table)
I get all results from main_table.
Using the correct case:
SELECT * FROM main_table WHERE field IN (SELECT field FROM other_table)
I get the expected results where field appears in other.
Running the subquery on it's own:
SELECT FIELD FROM other_table
I get an invalid column name error.
Surely I should get this error in the first case?
Is this related to collation?
The DB is binary collation.
The server is case insensitive however.
It seems to me like the server component is saying "this code is OK" and not allowing the DB to say the field is the wrong name..?
What are my options for a solution?
Let's illustrate what is happening using something that doesn't depend on case sensitivity:
USE tempdb;
GO
CREATE TABLE dbo.main_table(column1 INT);
CREATE TABLE dbo.other_table(column2 INT);
INSERT dbo.main_table SELECT 1 UNION ALL SELECT 2;
INSERT dbo.other_table SELECT 1 UNION ALL SELECT 3;
SELECT column1 FROM dbo.main_table
WHERE column1 IN (SELECT column1 FROM dbo.other_table);
Results:
column1
-------
1
2
Why doesn't that raise an error? SQL Server is looking at your queries and seeing that the column1 inside can't possibly be in other_table, so it is extrapolating and "using" the column1 that exists in the outer referenced table (just like you could reference a column that only exists in the outer table without a table reference). Think about this variation:
SELECT [column1] FROM dbo.main_table
WHERE EXISTS (SELECT [column1] FROM dbo.other_table WHERE [column2] = [column1]);
Results:
column1
-------
1
Again SQL Server knows that column1 in the where clause also doesn't exist in the locally referenced table, but it tries to find it in the outer scope. So in an imaginary world you might consider the query to actually be saying:
SELECT m.[column1] FROM dbo.main_table AS m
WHERE EXISTS (SELECT m.[column1] FROM dbo.other_table AS o WHERE o.[column2] = m.[column1]);
(Which is not how I typed it, but if I do type it that way, it still works.)
It doesn't make logical sense in some of the cases but this is the way the query engine does it and the rule has to be applied consistently. In your case (no pun intended), you have an extra complication: case sensitivity. SQL Server didn't find FIELD in your subquery, but it did find it in the outer query. So a couple of lessons:
Always prefix your column references with the table name or alias (and always prefix your table references with the schema).
Always create and reference your tables, columns and other entities using consistent case. Especially when using a binary or case-sensitive collation.
Very interesting find. The unspoken mandate is that you always should alias tables in your subqueries and use those aliases to be explicit about which table your column comes from. Subqueries allow you to make reference to a field from your outer query which is the cause of your issue, but in your scenario I would agree that either the default should be the internal query's field list, or to give you a column ambiguity error. Regardless, this method below is always preferable:
select * from main_table a where a.field in
(select x.field from other_table x)
Hello Am trying my query as belo.
Please see the two different versions of query and its error message.
SELECT first_value(col1) AS 'inv',col2
FROM dbo.table
--first_value' is not a recognized built-in function name.
SELECT dbo.first_value(col1) AS 'inv',col2
FROM dbo.table
--Cannot find either column "dbo" or the user-defined function or aggregate "dbo.first", or the name is ambiguous.
SELECT first_value(col1) AS 'inv',col2
FROM dbo.table
GROUP BY col2
--'first' is not a recognized built-in function name.
SELECT dbo.first_value(col1) AS 'inv',col2
FROM dbo.table
GROUP BY col2
--Cannot find either column "dbo" or the user-defined function or aggregate "dbo.first", or the name is ambiguous.
Please any help!
If you are trying to pull the top / first record from a table, you need to specify the criteria by which a row is defined as the first / top row
SELECT top 1 col1 AS inv,col2
FROM dbo.table
ORDER BY col1 --or whatever criteria you need here
first_value as far as I can tell seems to be a MS SQL Server 2012 function, which also requires an ordering clause - http://msdn.microsoft.com/en-us/library/hh213018.aspx