DB2 SQL Query issue when using Outer Join - join

I am hoping someone can shed some light to this error I am receiving on a Query script ...
SELECT
...
FROM
LEFT OUTER JOIN DATESA ON DAILY_DATES.ID = DATESA.ID
table3,
table4
WHERE
...
I am getting an error message and stuck; can't figure out the issue on my syntax?!

Why do you put table3, table4 after the join? You specified the tables already in the join
SELECT ...
FROM daily_dates
LEFT OUTER JOIN datesa
ON daily_dates.id = datesa.id
WHERE ...
And you forgot to specify your first table after the select (view my example for the syntax)

Related

Suspected alias issue in bigquery join

I am relatively new to bigquery and think I have an aliasing problem but can't work out what it is. Essentially, I have two tables and while the first table has the majority of the required information the second table has a date of birth that I need to join. I have written the below query and the two initial SELECT statements work in isolation and appear to return the expected values. However, when attempting to join the two tables I get an error stating:
Unrecognized name: t1_teams at [10:60]
WITH table_1 AS (SELECT competition_name, stat_season_name,
matchdata_Date, t1_teams.name, t1_players.Position, CAST(REGEXP_REPLACE(t1_players.uID, r'[a-zA-Z]', '') AS NUMERIC) AS Player_ID1, t1_players.First, t1_players.Last
FROM `prod.feed1`,
UNNEST(teams) AS t1_teams, UNNEST(t1_teams.Players) as t1_players),
table_2 AS (SELECT t2_players.uID AS Player_ID2, t2_players.stat_birth_date
FROM `prod.feed2`,
UNNEST(players) AS t2_players)
SELECT competition_name, stat_season_name, matchdata_Date, t1_teams.name, t1_players.Position, t1_players.uID, t1_players.First, t1_players.Last, t2_players.stat_birth_date
FROM table_1
LEFT JOIN table_2
ON Player_ID1 = Player_ID2
WHERE competition_name = "EPL"
AND stat_season_name = "Season 2018/2019"
Any help in steering me in the right direction would be greatly appreciated as my reading of the bigquery documentation and other searches have drawn a blank.
The problem is here:
WITH table_1 AS (
SELECT
competition_name,
stat_season_name,
matchdata_Date,
-- this line
t1_teams.name,
...
You're selecting t1_teams.name, so you end up with just name an an output column from the select list. If you want to refer to t1_teams later, then select that instead:
WITH table_1 AS (
SELECT
competition_name,
stat_season_name,
matchdata_Date,
-- this line
t1_teams,
...

Join / Aggregate Function Query

I have the following code and output:
SELECT CustomerCategoryName, COUNT(a.CustomerID) AS CustomersInThisCategory
FROM Sales.Customers AS a
RIGHT JOIN Sales.CustomerCategories AS b on a.CustomerCategoryID = b.CustomerCategoryID
GROUP BY CustomerCategoryName
ORDER BY CustomersInThisCategory DESC
This generates the following output:
When I add the following COUNT aggregate funcation and Inner Join:
SELECT CustomerCategoryName, COUNT(a.CustomerID) AS CustomersInThisCategory, COUNT(c.OrderID) AS Orders
FROM Sales.Customers AS a
RIGHT JOIN Sales.CustomerCategories AS b on a.CustomerCategoryID = b.CustomerCategoryID
INNER JOIN Sales.Orders AS c ON a.CustomerID = c.CustomerID
GROUP BY CustomerCategoryName
ORDER BY CustomersInThisCategory DESC
The output changes to:
I am not sure as to why the CustomersInThisCategory column is changing to the same as the Orders column? I'm also not sure why the results in the first ouput with 0 values are being removed in the second query as I still have the Right join present.
Any feedback would be much appreciated.
For your first query, count ( distinct a.customerId) should give you the unique customer ids in a category.
Regarding your second question, right join is performed before inner join. So the inner join will splice the records, for which a match is not found.
Hope my answer helps.

INNER JOIN on a table witch do not have any data

I'm trying to use the INNER JOIN functionality in my phpMyAdmin.
My query looks like this:
SELECT * FROM ___Bookings INNER JOIN ___Origins ON ___Bookings.BOO_Origin=___Origins.ORI_Id WHERE BOO_Id=1.
The problem is at this step nothing is populated into the ___Origins table. So my query returns 0 row.
How to change my query to return a row even if I do not have the joined table populated ?
Also what's the difference between JOIN and INNER JOIN ?
Thanks so much.
Basically, you want to join the tables based on the data in the __BOOKINGS table. That's a job for LEFT JOIN, not INNER JOIN (which is the same as JOIN).
Refer here for more information on SQL Joins: http://www.sql-join.com/sql-join-types/
There isn't a difference between Join and Inner Join (you can search for it also if this isn't enough).
For the other part - how can it return a row when there isn't one?
If you need a response from PHP you can set something like
if ($query->rowCount() > 0) {
echo $records;
}
else {echo "N";}
And in you other code just state if the response is "N" (or something else) do something or not do anything.

Apache Spark SQL: Automatic Inner Join?

So I have a weird situation.
Whenever I run a sqlContext.sql with a inner join statement, I actually get an error but when I read the error, it looks like Spark has already automatically joined my two separate tables once it tries to execute the on statement.
Table1:
patient_id, code
Table2:
patient_id, date
Select code, date
from Table1
inner join Table2
on Table1.patient_id = Table2.patient_id <- exception shows the table is joined already by this point.
Any ideas about this behavior?
Error looks like thisish
org.apache.spark.sql.AnalysisException: cannot resolve 'Table2.patient_id' given input columns [patient_id, code, date]
I think that you have a typo in your program.
However, what you can do is the following:
tableOneDF.join(tableTwoDF, tableOneDF("patient_id") === tableTwoDF("patient_id"), "inner").select("code", "date")
whereas tableOneDF and tableTwoDF are two dataframes created on top of the two tables.
Just try it out, and see if it still happens.

Symfony Propel Query

I want to make this query on Symfony/Propel
SELECT
folders.NAME,
COUNT(documents.NAME),
COUNT(files.idfiles),
SUM(files.size)
FROM `folders`
LEFT JOIN `documents_has_folders` ON (documents_has_folders.folders_idfolders = folders.idfolders)
LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS)
LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS)
GROUP BY folders.idfolders
I do this query
$x = FoldersQuery::create()
->addSelectColumn(FoldersPeer::NAME)
->addSelectColumn("COUNT(".DocumentsPeer::IDDOCUMENTS.")")
->addSelectColumn("COUNT(".FilesPeer::IDFILES.")")
->addSelectColumn("SUM(".FilesPeer::SIZE.")")
->addJoin(DocumentsHasFoldersPeer::FOLDERS_IDFOLDERS, FoldersPeer::IDFOLDERS, CRITERIA::LEFT_JOIN)
->addJoin(DocumentsHasFoldersPeer::DOCUMENTS_IDDOCUMENTS, DocumentsPeer::IDDOCUMENTS, CRITERIA::LEFT_JOIN)
->addJoin(DocumentsPeer::IDDOCUMENTS, FilesPeer::DOCUMENTS_IDDOCUMENTS, CRITERIA::LEFT_JOIN)
->addGroupByColumn(FoldersPeer::IDFOLDERS)
->find();
This returns:
500 | Internal Server Error | PropelException
Unable to execute SELECT statement [SELECT folders.NAME, COUNT(documents.IDDOCUMENTS), COUNT(files.IDFILES), SUM(files.SIZE) FROM LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS) LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS) LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS) WHERE folders.REMOVE_DATE IS NULL GROUP BY folders.IDFOLDERS] [wrapped: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS)' at line 1]
Why there is no table on FROM??? Why it creates a wrong query?
Thanks a lot.
EDITED:
Same problem have and if i do the query like this:
$c = new Criteria();
$c->addSelectColumn(FoldersPeer::NAME);
$c->addSelectColumn("COUNT(".DocumentsPeer::IDDOCUMENTS.")");
$c->addSelectColumn("COUNT(".FilesPeer::IDFILES.")");
$c->addSelectColumn("SUM(".FilesPeer::SIZE.")");
$c->addJoin(DocumentsHasFoldersPeer::FOLDERS_IDFOLDERS, FoldersPeer::IDFOLDERS, CRITERIA::LEFT_JOIN);
$c->addJoin(DocumentsHasFoldersPeer::DOCUMENTS_IDDOCUMENTS, DocumentsPeer::IDDOCUMENTS, CRITERIA::LEFT_JOIN);
$c->addJoin(DocumentsPeer::IDDOCUMENTS, FilesPeer::DOCUMENTS_IDDOCUMENTS, CRITERIA::LEFT_JOIN);
$c->addGroupByColumn(FoldersPeer::IDFOLDERS);
Again after FROM there is no table...
Unable to execute SELECT statement [SELECT folders.NAME, COUNT(documents.IDDOCUMENTS), COUNT(files.IDFILES), SUM(files.SIZE) FROM LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS AND folders.remove_date IS NULL ) LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS AND documents.remove_date IS NULL ) LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS AND documents.remove_date IS NULL AND files.remove_date IS NULL ) WHERE folders.remove_date IS NULL AND documents.remove_date IS NULL AND files.remove_date IS NULL GROUP BY folders.IDFOLDERS] [wrapped: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN folders ON (documents_has_folders.FOLDERS_IDFOLDERS=folders.IDFOLDERS ' at line 1]
This is not the Propel way to build queries since Propel 1.6 (maybe before). We don't use Criteria nor Peer classes. The following query:
SELECT
folders.NAME,
COUNT(documents.NAME),
COUNT(files.idfiles),
SUM(files.size)
FROM `folders`
LEFT JOIN `documents_has_folders` ON (documents_has_folders.folders_idfolders = folders.idfolders)
LEFT JOIN documents ON (documents_has_folders.DOCUMENTS_IDDOCUMENTS=documents.IDDOCUMENTS)
LEFT JOIN files ON (documents.IDDOCUMENTS=files.DOCUMENTS_IDDOCUMENTS)
Can be written like that:
$query = FoldersQuery::create())
->joinDocuments('documents')
->joinFiles('files')
->withColumn('COUNT(documents.NAME)', 'CountName')
->withColumn('COUNT(files.IDFILES)', 'CountIdFiles')
->withColumn('SUM(files.size)', 'Sum')
->select(array('Name', 'CountName', 'CountIdFiles', 'Sum'))
;
It may requires adjustments but it's the right way.

Resources