Nested session.query using sqlalchemy - sqlalchemy

I am completely new in SQL Alchemy. I am trying to convert below sql into sql alchemy but not getting desire result. My query:
select locationkey
,(select geo.value from locations__geo_coordinates geo where l._id=geo._sdc_source_key__id and geo._sdc_level_0_id=0) as latitude
,(select geo.value from locations__geo_coordinates geo where l._id=geo._sdc_source_key__id and geo._sdc_level_0_id=1) as longitude
from locations l
I am trying to get result by using session.query(. Whatever I found is on SELECT but that also giving error.

Related

Equivalent of SQL Server's CONTAINSTABLE in MySQL

I am converting a Query from SQL Server to MySQL but I have a problem converting the sentence contains table , because it uses rank .
I´ve looked for a similar property but I didn´t find anything , here is my Query at SqlServer
SELECT KEY_TBL.RANK
FROM CATS
INNER JOIN containsTABLE (CATS,(COLOR,CITY),'ORANGE',1000) AS KEY_TBL ON RUP.ID = KEY_TBL.[KEY]
ORDER BY RANK

DATA_READ ERROR in hive+oracle join query in apache drill

I tried a join query in hive & oracle:
select x.net_profit, y.city from hive.testdb.`catalog_sales` x inner join oracle.USER.`customer_address` y on y.address_id = x.bill_add_id
Data types for these fields in Drill (I checked using describe table) :
address_id : Decimal
city : CHARACTER VARYING
bill_addr_id : Double
net_profit : Double
Above query worked & I got desired output.
I tried :
select y.city, sum(x.net_profit) from hive.testdb.`catalog_sales` x inner join oracle.USER.`customer_address` y on y.address_id = x.bill_addr_id group by y.city
I got the following Exception:
Error: DATA_READ ERROR: The JDBC storage plugin failed while trying setup the SQL query.
sql SELECT "CA_CITY", CAST("ADDRESS_ID" AS DOUBLE) "$f13"
FROM "USER"."CUSTOMER_ADDRESS"
plugin oracle
Fragment 0:0
[Error Id: 7a2106da-1326-4de1-81e4-338a37acd7f9 on 192.168.145.151:31010] (state=,code=0)
Since joined columns(address_id,bill_addr_id) has different datatypes ie Decimal & Double but these are similar. So, I think this should be handled.
Is casting from Decimal to Double not possible? or is this a bug?

Select Query throwing Invalid number error

Hello first time using Oracle SQL Developer and I'm trying to do a select join on two tables and I get the error "ORA-01722: invalid number". What is causing this error?
All of the Google results point to creates/inserts that are invalid.
Here is my query, both CUSTNO and ARCUSTO_ID are of type NUMBER(15,0)
SELECT
ARCUSTO.COMPANY, SHIP_TO.ADDR1
FROM
ARCUSTO
INNER JOIN
SHIP_TO ON ARCUSTO.CUSTNO = SHIP_TO.ARCUSTO_ID;

Convert MS Access "TOP" to MySQL "LIMIT" in subquery

I am trying to convert an MS Access query to MySQL and the problem is converting MS Access top to MySQL limit to get the same result. When I change query to limit I get the error that this version of MySQL does not support limit in subquery.
This is the MS Access query:
SELECT a.FK_CONTRIBUTOR_ID
FROM tPUBLISHERS
INNER JOIN (tCONTRIBUTORS AS b
INNER JOIN tCLIPS AS a ON b.CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID)
ON tPUBLISHERS.PUBLISHER_ID = b.FK_PUBLISHER_ID
WHERE ((a.CLIP_ID) In
(select top 5 CLIP_ID
from tCLIPS
where FK_CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID
AND SUSPEND = a.SUSPEND))
AND ((a.FK_CONTRIBUTOR_ID) In (1922,2034,2099))
Previously answered at:
MySQL Subquery LIMIT
basically change the subquery to a Join
Google for more with "mysql limit on subquery"

mySQL Nested Query Syntax

I am trying to use a nested query approach to build a query-on-query for my mySQL database and failing to correctly generate output. I am able to import my table into Microsoft Access and build Query1 and then build Query2 on Query1 to get the correct output I'm looking for so I feel like I'm close, I just can't get the right syntax to get the output I'm looking for using a mySQL query approach.
Query1, here is the SQL statement from Access for Query1.
SELECT DISTINCT MediaBuys.DistrictID, MediaBuys.SpenderID, MediaBuys.PG, MediaBuys.SupportType, MediaBuys.PriSupportType
FROM MediaBuys
WHERE MediaBuys.PG ="P";
Query2, if I have built Query1 in Access as above and I run this SQL statement in Access as a separate query built on the first I can generate the output I'm looking for.
SELECT Query1.DistrictID, Query1.SpenderID, Query1.PG, Query1.SupportType, Query1.PriSupportType, Count(Query1.SupportType) AS CountOfSupportType
FROM Query1 INNER JOIN Query1 AS Query1_1 ON Query1.PG = Query1_1.PG AND Query1.SpenderID = Query1_1.SpenderID AND Query1.DistrictID = Query1_1.DistrictID
GROUP BY Query1.DistrictID, Query1.SpenderID, Query1.PG, Query1.SupportType, Query1.PriSupportType
HAVING Count(Query1.SupportType) > 1;
I'd like to be able to produce the same output from a query in mySQL. Since I have the SQL statements of these two queries I feel like this should be doable, I've attempted to build a nested query in a number of different ways and each attempt fails, it seems I can't put together the correct syntax. The most common error I receive is "Error Code: 1146. Table 'Query1' doesn't exist".
Is this doable in mySQL and if so can anyone help me with the correct syntax?
Just like you created the query Query1 in Access, create a view View1 in MySql:
CREATE VIEW View1 AS
SELECT DISTINCT DistrictID, SpenderID, PG, SupportType, PriSupportType
FROM MediaBuys
WHERE PG ='P';
and your query will be:
SELECT
View1.DistrictID, View1.SpenderID, View1.PG, View1.SupportType, View1.PriSupportType,
Count(View1.SupportType) AS CountOfSupportType
FROM View1 INNER JOIN View1 AS View1_1
ON View1.PG = View1_1.PG AND View1.SpenderID = View1_1.SpenderID
AND View1.DistrictID = View1_1.DistrictID
GROUP BY View1.DistrictID, View1.SpenderID, View1.PG, View1.SupportType, View1.PriSupportType
HAVING Count(View1.SupportType) > 1;