I definitely know there is a right syntax for this but can't remember of find what it is: I need to do something like
SELECT
*
FROM
"table"
WHERE
("dep_date","ret_date") IN (('2019-10-10','2019-10-15'),('2019-10-11','2019-10-16'))
Except this syntax doesn't work. What is the correct syntax for defining a static multi-column lookup for the right operand of IN()?
You can try below way -
SELECT
*
FROM
"table"
WHERE ((dep_date='2019-10-10' and ret_date='2019-10-15') or
(dep_date='2019-10-11' and ret_date='2019-10-16'))
Use a subquery with UNION ALL after the IN clause:
SELECT
*
FROM
"table"
WHERE ("dep_date","ret_date") IN (
select '2019-10-10', '2019-10-15'
union all
select '2019-10-11','2019-10-16'
)
You can find more about Row Values in SQLite here: https://www.sqlite.org/rowvalue.html
Related
I have over 40 tables I want to append in BigQuery using standard SQL. I have already formatted them to have the exact same schema. When I try to use the '*' wildcard at the end of table name in my FROM clause, I get the following error:
Syntax error: Expected end of input but got "*" at [95:48]
I ended up manually doing a UNION DISTINCT on all my tables. Is this the best way to do this? Any help would be appreciated. Thank you!
CREATE TABLE capstone-320521.Trips.Divvy_Trips_All AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_*;
--JOIN all 2020-21 trips data
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06
UNION DISTINCT
Syntax error: Expected end of input but got "*"
I think the problem is in missing ticks around the table references. Try below
CREATE TABLE `capstone-320521.Trips.Divvy_Trips_All` AS
SELECT * FROM `capstone-320521.Trips.Divvy_Trips_*`
Note: The wildcard table name contains the special character (*), which means that you must enclose the wildcard table name in backtick (`) characters. See more at Enclose table names with wildcards in backticks
I am unaware of any such UNION DISTINCT syntax. If your intention is to do a union of the 3 tables and remove any duplicate records in the process, then just using UNION should suffice:
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06;
Note that in general it is bad practice to use SELECT * with union queries. The reason has to do with that a union between two (or more) tables is generally only valid if the two queries involved have the number and types of columns. Using SELECT * obfuscates what columns are actually being selected, and so it is preferable to always explicitly list out the columns.
I'm trying to create a query using the SQL SELECT statement but I keep getting a error with my code
This is my code:
SELECT *
FROM tblRegistration
WHERE (tblRegistration.InstanceID IN (
SELECT InstanceID FROM tblRegistration
SELECT tblCourse.InstanceID, tblCourse.HoursPerWeek
FROM tblCourse
WHERE (((tblCourse.HoursPerWeek)=40));
Access throws in far more parentheses in WHERE clause than are needed. Simplify by removing most of them then test. Access will eventually throw them back in if you switch builder between datasheet and design view and save the object. Always make sure brackets, parens, quote marks, and apostrophes are evenly paired.
Also have too many SELECT clauses.
Subquery for IN() must return only one field.
Consider:
SELECT *
FROM tblRegistration
WHERE tblRegistration.InstanceID IN (
SELECT tblCourse.InstanceID
FROM tblCourse
WHERE tblCourse.HoursPerWeek=40);
If you don't want to remove field from subquery, try:
SELECT *
FROM tblRegistration
WHERE EXISTS (
SELECT tblCourse.InstanceID, tblCourse.HoursPerWeek
FROM tblCourse
WHERE tblCourse.HoursPerWeek=40);
I'm trying to compare two table's values for difference (I suspect for two TankSystemIds containing same data)
My query is
SELECT *
FROM [dbo].[vwRawSaleTransaction]
WHERE hdTankSystemId = 2782
MINUS
SELECT *
FROM [dbo].[vwRawSaleTransaction]
WHERE hdTankSystemId = 2380
But I get an error about syntax issues:
Incorrect syntax near 'minus'
But this is right[1]?
[1] https://www.techonthenet.com/sql/minus.php
Quoted in your link.
For databases such as SQL Server, PostgreSQL, and SQLite, use the EXCEPT operator to perform this type of query.
For your case, it seems like you are looking for duplicated data, intersect should be used instead.
Also, INTERSECT statement like
SELECT
EXPRESSION_1, EXPRESSION_2, ..., EXPRESSION_N
FROM
TABLE_A
INTERSECT
SELECT
EXPRESSION_1, EXPRESSION_2, ..., EXPRESSION_N
FROM
TABLE_B
can be written as
SELECT
TABLE_A.EXPRESSION_1, TABLE_A.EXPRESSION_2, ..., TABLE_A.EXPRESSION_N
FROM
TABLE_A
INNER JOIN
TABLE_B
ON
TABLE_A.EXPRESSION_1 = TABLE_B.EXPRESSION_1
AND TABLE_A.EXPRESSION_2 = TABLE_B.EXPRESSION_2
.
.
.
AMD TABLE_A.EXPRESSION_N = TABLE_B.EXPRESSION_N
If you use select * from the same table with a different where condition then intersect them, you are not going to get any rows as they have different value on the specific column used in where condition.
SQL newbie here, but I can't find the solution to something that looks easy:
The following query does not seem to have a valid syntax (ORA-00904: invalid identifier), but its logic should be clear. How can I achieve this in a query that needs to be speedy?
SELECT * FROM table WHERE LEFT(column,4)="abcd"
For this purpose, you should use like rather than left(). First, Oracle doesn't support left() (you need substr() instead). Second, like can make use of indexes because the wildcard is not at the beginning of the string:
SELECT *
FROM table
WHERE column like 'abcd%';
Oracle and some other products have substr.
SELECT * FROM tablename WHERE substr(columnname, 1, 4) = 'abcd'
I.e. single quotes for string literals!
ANSI SQL has substring:
SELECT * FROM tablename WHERE substring(columnname from 1 for 4) = 'abcd'
And others have left:
SELECT * FROM tablename WHERE LEFT(columnname,4) = 'abcd'
I am trying to execute this query using Postgresql 9.2
WITH udetail as (select(regexp_split_to_array('user#domain', E'\\#+')))
select * from udetail[1];
Buy it gives me a syntax error near where the start of '['. This same query is working fine under version 9.3. So I'm wonder if there's an alternative way of writing the query to get the same result.
I think you are looking for something like this:
WITH udetail(x) as (
select(regexp_split_to_array('user#domain', E'\\#+')))
select x[1] from udetail;
I don't think you can index a table expression like udetail in your case. It is the column of the table expression that is of array type. Hence, you can use an array subscript number on the column, not on the table itself.
Demo here
You should use an alias either as the query parameter:
with udetail(arr) as (
select regexp_split_to_array('user#domain', E'\\#+')
)
select arr[1] from udetail;
or as column alias:
with udetail as (
select regexp_split_to_array('user#domain', E'\\#+') as arr
)
select arr[1] from udetail;
You can also do it without aliases:
with udetail as (
select regexp_split_to_array('user#domain', E'\\#+')
)
select regexp_split_to_array[1] from udetail;