Expected SELECT statement - sql

WITH 1_hour_trips as (
SELECT *
from bigquery-public-data.new_york.citibike_trips
where tripduration = 60
)

WITH creates a temporary view, and it then expects you to use a select to actually get the result you want. Something like this:
WITH 1_hour_trips as (
SELECT * from bigquery-public-data.new_york.citibike_trips
where tripduration = 60
)
SELECT * from 1_hour_trips;

Related

Can I use With within another with?

I am trying to create something like this but end up in errors. Not sure if this is the right way to do.
WITH SECONDMAIN AS
(
WITH MAIN AS
(
SELECT
....
)
SELECT
*
FROM MAIN M
JOIN ....
)
SELECT * FROM SECONDMAIN SM;
The syntax would be-
WITH MAIN AS
(
SELECT
....
),
SECONDMAIN AS
(
SELECT
*
FROM MAIN M
JOIN ....
)
SELECT * FROM SECONDMAIN SM;

How to write mysql sub query in CDbCriteria and CActiveDataProvider in yii?

example :
SELECT * FROM (
SELECT *
FROM `servicelist` `t`
WHERE t.is_accept_by_driver = 0 ) as tmp GROUP BY servicenumber
Try this:
\Yii::app()
->db
->createCommand(
"SELECT * FROM (
SELECT *
FROM `servicelist` `t`
WHERE t.is_accept_by_driver = 0 ) as tmp GROUP BY servicenumber"
)
->queryAll()

simple subquery not working DB2

hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks
Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)
Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)

With clause subquery availablility for the entire SQL statement in Oracle

I am trying to write a SQL statement which reuses the subquery of the With clause multiple times in Oracle.
With mySubQ as (
...
)
Select Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where mySubQ.Something = SomeTable.Something
This gives me error - ORA-32034 unsupported use of WITH clause
What am I missing?
You need to join with mySubQ, not just define it.
WITH mySubQ AS (...)
SELECT Something
FROM SomeTable
JOIN mySubQ ON mySubQ.Something = SomeTable.Something
WHERE mySubQ.Something >= 0
If you put the query of mySubQ in a subquery, you can't reference mySubQ in the WHERE clause of the main query. Each level of query can only access tables in its own FROM and JOIN clauses, not those of subqueries.
Here is the error: where mySubQ.Something = SomeTable.Something.
The bottom query selects from SomeTable and from the subquery with alias newSubQ,
so mySubQ.Something is not known in this context.
If something is a real column name, not only a "placeholder in the pseudocode", then there is also another error here: Select Something - the column is ambiguous, because both sometable and the subquery have this column.
Try this query:
With mySubQ as (
SELECT * FROM sometable
)
Select newSubQ.Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where newSubQ.Something = SomeTable.Something
;
Demo --> http://www.sqlfiddle.com/#!4/88855/12
This demo contains also another example of using WITH clause:
WITH mySubQ AS (
SELECT *
FROM sometable
),
mySubQ_1 AS (
SELECT *
FROM mySubQ
WHERE somethingelse = 1
),
mySubQ_2 AS (
SELECT *
FROM mySubQ
WHERE something between 2 AND 5
)
SELECT *
FROM sometable s, mySubQ_1 m1,
(
SELECT * FROM mySubQ_2
WHERE something < 10
) m2
WHERE s.something = m1.something
AND m1.somethingelse = m2.somethingelse

How to create CTE which uses another CTE as the data to further limit?

I have searched this question here but couldn't find it, please redirect me if we already have it on the site.
I'm looking for a way to create CTE which uses another CTE as the data to further limit. I have a CTE which creates a report for me , but I would like to narrow this report with another input using the existing CTE.
I hope my question is clear.
You can chain 2 (or more) CTE's together.
For example
with ObjectsWithA as
(
select * from sys.objects
where name like '%A%'
),
ObjectsWithALessThan100 as
(
select * from ObjectsWithA
where object_id < 100
)
select * from ObjectsWithALessThan100;
Or the same example, with more "spelled out" names/aliases:
with ObjectsWithA (MyObjectId , MyObjectName) as
(
select object_id as MyObjIdAlias , name as MyNameAlias
from sys.objects
where name like '%A%'
),
ObjectsWithALessThan100 as
(
select * from ObjectsWithA theOtherCte
where theOtherCte.MyObjectId < 100
)
select lessThan100Alias.MyObjectId , lessThan100Alias.MyObjectName
from ObjectsWithALessThan100 lessThan100Alias
order by lessThan100Alias.MyObjectName;
A CTE can refer to previous CTEs:
with report as (
<your query here>
),
reportLimited as (
select *
from report
where foo = #bar
)
select *
from reportLimited
The only rule is that the references have to be sequential. No forward references.
Sure, just reference the CTE directly:
WITH Source As
(
SELECT * FROM AllData
),
Filtered AS
(
SELECT * FROM Source WHERE ID = 4
)
SELECT * FROM Filtered
WITH
Source ---------1---------
As
(
SELECT * FROM emp
),
destination----2----------
AS
(
SELECT *
FROM Source
WHERE E_id = 4
)
SELECT * FROM destination