Joining tables from multiple Projects within Big Query - join

So my requirement is to query data from multiple tables that reside across multiple Projects.
For e.g., I want to select few columns from project1.dataset1.tableA, project1.dataset1.tableB,
project1.dataset1.tableC and then select few columns from project2.datasetX.tableX.
Can I just do the below and would that give me the requisite information
SELECT
count(*)
FROM project1.dataset1.tableA vt
, project1.dataset1.tableB v
, project1.dataset1.tableC tp
, project2.datasetX.tableX si
WHERE vt.column1 = v.column1
AND v.column2 = tp.column2
AND vt.column3 = si.column3
tried the above, did get the result but wanted to cross check if thats the correct way to do it

Related

What is the correct way to query data in SQLAlchemy (async) in case of multiple levels of relationships?

I'm trying to read data from related tables which are x levels deep and which have relationships specified, i.e.:
table A
table B
table C
table ABC
Table ABC has relationships ABC.a = A, ABC.b = B and ABC.c = C,
i.e. foreign keys ABC.aid = A.id, ABC.bid = B.id and ABC.cid = C.id.
aid, bid and cid in ABC are set unique using UniqueContstraint
relationship is using lazy="joined"
When I do select(ABC) I'm able to get all values from ABC and also from related tables, i.e.:
{ABC.a: {A}, ABC.b: {B}, ABC.c: {C}}
I have also table D which has a relationship to ABC (D.abcid = ABC.id) and I struggle to construct a correct select statement which would give me all data also from A, B and C. Actually I'm not sure if this should work or I missed / do not understand something in the documentation as I have tried various loading strategies, specified join_depth for D and ABC tables, etc. No matter what I'm getting:
sqlalchemy.exc.InvalidRequestError: The unique() method must be invoked on this Result, as it contains results that include joined eager loads against collections
I would like to get the data the same way as for 1st level relationship, i.e.:
{D.abc : {ABC.a: {A}, ABC.b: {B}, ABC.c: {C}}}
Is it possible or do I have to change the select query completely and just create multiple joins and manually pick all the values I need?
I'm able to get correct records from the database when I just take the generated select statement and use it directly in a DB shell (MariaDB) so I assume that the only issue is my lack of understanding of how SQL handles/presents these records internally.
The issue was using uselist=True in one of the models, all relationships are working perfectly down to the lowest level now.

SQL query report builder 3.0 table join problems with duplicates

I am having problems joining three tables in report builder 3.0. I have experimented with various joins and can not achieve the desired result.
PLACES
place ref,
address,
place type (place type = 1)
JOBS
place ref,
description,
cost,
job number,
JOBS INFO
job number,
date comp (date comp = 25/12/2015)
I need all places with place type 1.
Then I need to link all jobs completed 25/12/2015 by job number and then link these to places on place ref.
The first problem I have is despite which join I chose to link places to jobs and jobs info all I get is the places which have had jobs. I need to also show the places that haven't.
Also the database sometimes contains duplicate jobs so I only need to display the same job number once.
Any help with this would be much appreciated!
You need a LEFT JOIN when PLACES is your master table :
SELECT * FROM PLACES p
LEFT OUTER JOIN (select distinct * from Jobs j
INNER JOIN Jobs_Info ji ON(j.job_number = ji.job_number)
WHERE ji.date_comp = '25/12/2015') t
ON(p.place_ref = t.place_ref)
WHERE p.place_type = 1

Trying to create one table from four

I'm stuck trying to create a query that pulls results from at least three different tables with many to many relationships.
I want to end up with a table that lists cases, the outcomes and complaints.
All cases may have none, one or multiple outcomes, same relationship applies to the complaints. I want to be able to have the case listed once, then subsequent columns to list all the outcomes and complaints related to that case.
I have tried GROUP_CONCAT to get the outcomes in one column instead of repeating the cases but when I use UNION to combine the outcomes and complaints one column header overwrites the other.
Any help appreciated and here's the link to the fiddle http://sqlfiddle.com/#!2/d111e/2/0
I suggest you START with this this query structure:
SELECT
c.caseID, c.caseTitle, c.caseSynopsis /* if more columns ... add to group by also */
, group_concat(co.concern)
, group_concat(re.resultText)
FROM caseSummaries AS c
LEFT JOIN JNCT_CONCERNS_CASESUMMARY AS JCC ON c.caseID = JCC.caseSummary_FK
LEFT JOIN CONCERNS AS co ON JCC.concerns_FK = co.concernsID
LEFT JOIN JNCT_RESULT_CASESUMMARY AS JRC ON c.caseID = JRC.caseSummary_FK
LEFT JOIN RESULTS AS re ON JRC.result_FK = re.result_ID
GROUP BY
c.caseID, c.caseTitle, c.caseSynopsis /* add more ... here also */
;
Treat the table caseSummaries as the most important and then everything else "hangs off" that.
Please note that although MySQL will allow it, you should place EVERY non-aggregating column that you include in the select clause into the group by clause also.
also see: http://sqlfiddle.com/#!2/2d1a79/7

using SQL aggregate functions with JOINs

I have two tables - tool_downloads and tool_configurations. I am trying to retrieve the most recent build date for each tool in my database. The layout of the DB is simple. One table called tool_downloads keeps track of when a tool is downloaded. Another table is called tool_configurations and stores the actual data about the tool. They are linked together by the tool_conf_id.
If I run the following query which omits dates, I get back 200 records.
SELECT DISTINCT a.tool_conf_id, b.tool_conf_id
FROM tool_downloads a
JOIN tool_configurations b
ON a.tool_conf_id = b.tool_conf_id
ORDER BY a.tool_conf_id
When I try to add in date information I get back hundreds of thousands of records! Here is the query that fails horribly.
SELECT DISTINCT a.tool_conf_id, max(a.configured_date) as config_date, b.configuration_name
FROM tool_downloads a
JOIN tool_configurations b
ON a.tool_conf_id = b.tool_conf_id
ORDER BY a.tool_conf_id
I know the problem has something to do with group-bys/aggregate data and joins. I can't really search google since I don't know the name of the problem I'm encountering. Any help would be appreciated.
Solution is:
SELECT b.tool_conf_id, b.configuration_name, max(a.configured_date) as config_date
FROM tool_downloads a
JOIN tool_configurations b
ON a.tool_conf_id = b.tool_conf_id
GROUP BY b.tool_conf_id, b.configuration_name

Create Select clause within Stored Procedure?

I have two tables coming which has data from two different systems. I need to reconcile the data in these two tables
The column mapping needs to be made configurable.
E.g.:
Table A Table B
Col1A, Col2A Col1B, Col2B
MappingTable
Col1A = Col1B
Col2A= Col2B
So I would need to have two result sets like this based on the mappings in the table.This needs to be decided dynamically. i.e. Select _____ from A. The _____ needs to be filled dynamically.
Select Col1A, Col2A from A
Select Col2B, Col1B from B
Is this possible in MySQL?
Use a UNION clause if the column sets are similar (if they're not you won't be able to combine their results in a meaningful way, anyway).
select col1a, col2a from A
union
select col1b, col2b from b

Resources