Counting Values in a repeated field in BigQuery - sql

I want to select rows that have more thank k values in a repeated field. (consider for example selecting user that have more than 3 email addresses)
In Standard SQL I know I can use
SELECT * FROM dataset.users
WHERE array_length(email_address) > 3
But what is the way to do this in BigQuery legacy SQL?

No need for a subquery; you should be able to filter with OMIT RECORD IF directly:
SELECT *
FROM dataset.users
OMIT RECORD IF COUNT(email_address) <= 3;
Do you mind commenting on why you want to use legacy SQL, though? If you encountered a problem with standard SQL I'd like to understand what it was so that we can fix it. Thanks!

Counting Values in a repeated field in BigQuery
BigQuery Legacy SQL
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
If you want then to count output rows - you can use below
SELECT COUNT(1) AS rows_count
FROM (
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
)
WHERE address_count> 3

Related

Is there any SQL query character limit while executing it by using the JDBC driver [duplicate]

I'm using the following code:
SELECT * FROM table
WHERE Col IN (123,123,222,....)
However, if I put more than ~3000 numbers in the IN clause, SQL throws an error.
Does anyone know if there's a size limit or anything similar?!!
Depending on the database engine you are using, there can be limits on the length of an instruction.
SQL Server has a very large limit:
http://msdn.microsoft.com/en-us/library/ms143432.aspx
ORACLE has a very easy to reach limit on the other side.
So, for large IN clauses, it's better to create a temp table, insert the values and do a JOIN. It works faster also.
There is a limit, but you can split your values into separate blocks of in()
Select *
From table
Where Col IN (123,123,222,....)
or Col IN (456,878,888,....)
Parameterize the query and pass the ids in using a Table Valued Parameter.
For example, define the following type:
CREATE TYPE IdTable AS TABLE (Id INT NOT NULL PRIMARY KEY)
Along with the following stored procedure:
CREATE PROCEDURE sp__Procedure_Name
#OrderIDs IdTable READONLY,
AS
SELECT *
FROM table
WHERE Col IN (SELECT Id FROM #OrderIDs)
Why not do a where IN a sub-select...
Pre-query into a temp table or something...
CREATE TABLE SomeTempTable AS
SELECT YourColumn
FROM SomeTable
WHERE UserPickedMultipleRecordsFromSomeListOrSomething
then...
SELECT * FROM OtherTable
WHERE YourColumn IN ( SELECT YourColumn FROM SomeTempTable )
Depending on your version, use a table valued parameter in 2008, or some approach described here:
Arrays and Lists in SQL Server 2005
For MS SQL 2016, passing ints into the in, it looks like it can handle close to 38,000 records.
select * from user where userId in (1,2,3,etc)
I solved this by simply using ranges
WHERE Col >= 123 AND Col <= 10000
then removed unwanted records in the specified range by looping in the application code. It worked well for me because I was looping the record anyway and ignoring couple of thousand records didn't make any difference.
Of course, this is not a universal solution but it could work for situation if most values within min and max are required.
You did not specify the database engine in question; in Oracle, an option is to use tuples like this:
SELECT * FROM table
WHERE (Col, 1) IN ((123,1),(123,1),(222,1),....)
This ugly hack only works in Oracle SQL, see https://asktom.oracle.com/pls/asktom/asktom.search?tag=limit-and-conversion-very-long-in-list-where-x-in#9538075800346844400
However, a much better option is to use stored procedures and pass the values as an array.
You can use tuples like this:
SELECT * FROM table
WHERE (Col, 1) IN ((123,1),(123,1),(222,1),....)
There are no restrictions on number of these. It compares pairs.

Sql query to select records starting with +

Basically I have a user table where mobile is not stored. I want to run a query to select all records starting with +91.
My current query is
Query - select count(*) from temp_table where cell_phone_no like "+91%"; Results - 0
While I know for a fact that there are certain rows with mobile numbers starting with +91.
Important: you must to know us your DBMS, because the sintax can be different, but I try to answer to your question
If you have really these rows (the row as + 91 is not good), your query can be re-write in this way:
select count(*) from temp_table where cell_phone_no like '+91%'
So, I've changed the " with a single quote '
You should check for other hidden values. I might suggest starting with this:
select cell_phone
from temp_table
where cell_phone_no like '%+%91%'
This should get any number that has a + and a 91 in it somewhere. If you get rows from this query, you need to investigate why yours doesn't work.

select single value into multi rows oracle sql

I want to select one value and retrieve it into multi rows i tried to search about this case but i didn't find the write way to sole it and
and finaly sorry for my English
try select your_value, t.* from table t

Copy data from one table to another without duplicates based on more then one column comparision

I am stuck in the following query. This was working properly on mySQL but it gives error on MSSQL-2005. The main purpose of the query is to copy data from one table to another without duplicates based on multiple columns comparison from both tables.
I can do this to compare one column for duplication, but I can't do when I compare more then one column for duplication.
Here is my query.
INSERT INTO eBayStockTaking (OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID)
SELECT OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID
FROM tempEBayStockTaking WHERE (OrderLineItemID,SubscriberID,eBayUserID)
Not In (SELECT OrderLineItemID,SubscriberID,eBayUserID FROM eBayStockTaking)
Note: I have been through many similar questions but all in vain.
Thanks
Rather try NOT EXISTS
Something like
INSERT INTO eBayStockTaking (OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID)
SELECT OrderLineItemID,
Qty,
SKU,
SubscriberID,
eBayUserID
FROM tempEBayStockTaking t
WHERE Not EXISTS (
SELECT *
FROM eBayStockTaking e
WHERE e.OrderLineItemID = t.OrderLineItemID
AND e.SubscriberID = t.SubscriberID
AND e.eBayUserID = t.eBayUserID)
)
I know MySQL allows Row Subqueries, nut SQL Server does not allow this.

How to Unselect The Field in select Query using sql

hi in my database i am store more than 50 field with primarykey (Auto increment) i am not sure about the fields name but i wants to select the entire data in that table , i am using
SELECT * FROM tablename
i want to select all the fields except that ID but this query populate the entire table so is there is possible to unselect the particular field in the select query. Can anyone have an idea please guide me. Thanks in Advance
The * indicates that you want to select ALL fields from a given table. If you want to select only a few fields, or all but one, then you will need to specify the ones you want manually:
select field1,field2,field3 from tablename
The SQL standard does not offer an "except" notation. It would be neat if we could
select t.* -t.ID
from some_table t
/
but it is not supported.
On the other hand, SELECT * is a dangerous construct. It is always better to explicitly list the columns we want in any given situation.