I am trying to print the table results on the basis of output generated by another table.
so, lets say my table II produces an integer output of some value then I want to use that value to limit my results for table I.
I am unable to make this query work. I wrote this but cant see the problem.
Is there any other way of writing this?
I read somewhere on SO that subquery in limit is not allowed but I am not sure.
select *
from stage_II_final_suzuki_1648155456 `
limit ( select count(*)*0.80
from tmp.stage_II_final_suzuki_1648155456
);
I want to correct the syntax for compiler
ERROR> Failed validation Syntax error: Unexpected "(" at [31:11]
It appears that Google Ads Data Hub uses BigQuery SQL. The syntax for LIMIT accepts a number (literal constant); you can't use any expression that needs evaluation.
It does support a ROW_NUMBER() function though, and you can try:
select *
from (
select s1.*, row_number() over() as rn
from stage_II_final_suzuki_1648155456 s1 `
) s2
where s2.rn <= ( select count(*)*0.80
from tmp.stage_II_final_suzuki_1648155456
);
Related
As we all know, the ORDER BY clause is processed after the SELECT clause, so a column alias in the SELECT clause can be used.
However, I find that I can’t use the aliased column in a calculation in the ORDER BY clause.
WITH data AS(
SELECT *
FROM (VALUES
('apple'),
('banana'),
('cherry'),
('date')
) AS x(item)
)
SELECT item AS s
FROM data
-- ORDER BY s; -- OK
-- ORDER BY item + ''; -- OK
ORDER BY s + ''; -- Fails
I know there are alternative ways of doing this particular query, and I know that this is a trivial calculation, but I’m interested in why the column alias doesn’t work when in a calculation.
I have tested in PostgreSQL, MariaDB, SQLite and Oracle, and it works as expected. SQL Server appears to be the odd one out.
The documentation clearly states that:
The column names referenced in the ORDER BY clause must correspond to
either a column or column alias in the select list or to a column
defined in a table specified in the FROM clause without any
ambiguities. If the ORDER BY clause references a column alias from
the select list, the column alias must be used standalone, and not as
a part of some expression in ORDER BY clause:
Technically speaking, your query should work since order by clause is logically evaluated after select clause and it should have access to all expressions declared in select clause. But without looking at having access to the SQL specs I cannot comment whether it is a limitation of SQL Server or the other RDBMS implementing it as a bonus feature.
Anyway, you can use CROSS APPLY as a trick.... it is part of FROM clause so the expressions should be available in all subsequent clauses:
SELECT item
FROM t
CROSS APPLY (SELECT item + '') AS CA(item_for_sort)
ORDER BY item_for_sort
It is simply due to the way expressions are evaluated. A more illustrative example:
;WITH data AS
(
SELECT * FROM (VALUES('apple'),('banana')) AS sq(item)
)
SELECT item AS s
FROM data
ORDER BY CASE WHEN 1 = 1 THEN s END;
This returns the same Invalid column name error. The CASE expression (and the concatenation of s + '' in the simpler case) is evaluated before the alias in the select list is resolved.
One workaround for your simpler case is to append the empty string in the select list:
SELECT
item + '' AS s
...
ORDER BY s;
There are more complex ways, like using a derived table or CTE:
;WITH data AS
(
SELECT * FROM (VALUES('apple'),('banana') AS sq(item)
),
step2 AS
(
SELECT item AS s FROM data
)
SELECT s FROM step2 ORDER BY s+'';
This is just the way that SQL Server works, and I think you could say "well SQL Server is bad because of this" but SQL Server could also say "what the heck is this use case?" :-)
I have the following SQL query:
select * from my_table
then the returned results looks like:
my_id field_1 field_2 ...
1 ... ...
1 ... ...
1 ... ...
2
3
3
I only want to keep one record per my_id, perhaps taking the record with minimum value in field_2
Therefore, I am expecting the following query to fail becauseI haven't put a desired aggregation function after select:
select * from my_table group by my_id order by my_id
However, the query went through and returned table have no duplicated my_id. Therefore, I am wondering is there a default method SQL is using if I didn't specify an aggregation function before group by?
I am expecting the following query to fail because I haven't put a
desired aggregation ..
Unfortunately, some SQL Implementations allow GROUP BY without an aggregate function1.
In this case the result is "not defined / implementation defined" - see the specific implementation documentation to see if it provides any guarantees. That is, while it is still guaranteed that the my_id column is unique, values from any row could be returned for other output fields.
(Obviously, if my_id were a key / candidate key - but it is not in this case - then this doesn't make a difference as only one row could be selected..)
For increased compatibility and predictable results, use an aggregate function for every field which is not covered by the GROUP BY - even if the particular SQL/RDBMS does not enforce or "require" the use aggregates.
1 While the original query is "accepted" in MySQL (depending on ONLY_FULL_GROUP_BY), both PostgreSQL and SQL Server would have rejected the original query "as expected".
You can use a correlated subquery:
select t.*
from my_table t
where t.field2 = (select min(t2.field2)
from my_table t2
where t2.my_id = t.my_id
);
I'm trying to developer the Oracle SQL version of the accepted answer here:
Return row of every n'th record
What I have so far is:
SELECT ROW_ID, CUST_ACCT_SITE_ID
FROM
(
SELECT CUST_ACCT_SITE_ID as CUST_ACCT_SITE_ID, ROW_NUMBER() OVER (ORDER BY CUST_ACCT_SITE_ID) AS ROW_ID
FROM XXDMX_VOICE_CUSTOMERS_TBL
) AS t
WHERE ROW_ID % 10000 = 0
ORDER BY CUST_ACCT_SITE_ID;
I get the error
ERROR
ORA-00933: SQL command not properly ended
I've tried lots of variations and can't think of what I am doing wrong. Any ideas, Oracle experts?
Try writing the query like this:
SELECT rn, CUST_ACCT_SITE_ID
FROM (SELECT CUST_ACCT_SITE_ID as CUST_ACCT_SITE_ID,
ROW_NUMBER() OVER (ORDER BY CUST_ACCT_SITE_ID) AS rn
FROM XXDMX_VOICE_CUSTOMERS_TBL
) t
WHERE mod(rn, 10000) = 0
ORDER BY CUST_ACCT_SITE_ID;
The primary difference is removing the as for the table alias. Oracle doesn't allow this syntax. I also changed row_id to something else, because "rowid" means something in Oracle and its use could be confusing (see here).
In PL/SQL (the name for "Oracle SQL"), the modulus operator uses this syntax:
WHERE MOD(ROW_ID, 10000) = 0
I would like to query my database using a HQL query to retrieve the total number of rows having a MY_DATE greater than SOME_DATE.
So far, I have come up with a native Oracle query to get that result, but I am stuck when writing in HQL:
SELECT
(
SELECT COUNT(MY_DATE)
FROM Table1
WHERE MY_DATE >= TO_DATE('2011-09-07','yyyy-MM-dd')
)
+
(
SELECT COUNT(MY_DATE)
FROM Table2
WHERE MY_DATE >= TO_DATE('2011-09-07','yyyy-MM-dd')
)
AS total
I actually have more than 2 tables but I keep having an IllegalArgumentException (unexpected end of subtree).
The working native Oracle basically ends with FROM dual.
What HQL query should I use to get the total number of rows I want?
First of, if you have a working SQL query, why not just use that instead of trying to translate it to HQL? Since you're returning a single scalar in the first place, it's not like you need anything HQL provides (e.g. dependent entities, etc...)
Secondly, do you have 'dual' mapped in Hibernate? :-) If not, how exactly are you planning on translating that?
That said, "unexpected end of subtree" error is usually caused by idiosyncrasies of Hibernate's AST parser. A commonly used workaround is to prefix the expression with '0 +':
select 0 + (
... nested select #1 ...
) + (
... nested select #2 ...
) as total
from <from what exactly?>
Could someone please explain to me why the following query is invalid? I'm running this query against an Oracle 10g database.
select count(test.*) from my_table test;
I get the following error: ORA-01747: invalid user.table.column, table.column, or column specification
however, the following two queries are valid.
select count(test.column) from my_table test;
select test.* from my_table test;
COUNT(expression) will count all rows where expression is not null. COUNT(*) is an exception, it returns the number of rows: * is not an alias for my_table.*.
As far as I know, Count(Table.*) is not officially supported in the SQL specification. Only Count(*) (count all rows returned) and Count(Table.ColumnName) (count all non-null values in the given column). So, even if the DBMS supported it, I would recommend against using it.`
This syntax only works in PostgreSQL and only because it has a record datatype (for which test.* is a meaningful expression).
Just use COUNT(*).
This query:
select count(test.column) from my_table test;
will return you the number of records for which test.column is not NULL.
This query:
select test.* from my_table test;
will just return you all records from my_table.
COUNT as such is probably the only aggregate that makes sense without parameters, and using an expression like COUNT(*) is just a way to call a function without providing any actual parameters to it.
You might reasonably want to find the number of records where test.column is not NULL if you are doing an outer join. As every table should have a PK (which is not null) you should be able to count the rows like that if you want:
select count(y.pk)
from x
left outer join y on y.pk = x.ck
COUNT(*) is no good here because the outer join is creating a null row for the table that is deficient in information.