KSQLDB table RIGHT JOIN - ksqldb

I have two tables in KSQLDB
Person
------
Id PK
Name
Activity
--------
Id PK
Name
PersonId
Joining "FROM Activity LEFT JOIN Person" works fine.
But I want to "FROM Person LEFT JOIN Activity"
CREATE TABLE Person_Activity AS
SELECT *
FROM Person
LEFT JOIN Activity on Person.Id = Activity.PersonId
EMIT CHANGES;
But I'm getting: Invalid join condition: table-table joins require to join on the primary key of the right input table. Got Person.Id = Activity.PersonId
I then tried full outer join
CREATE TABLE Person_Activity AS
SELECT *
FROM Activity
FULL OUTER JOIN Person on Person.Id = Activity.PersonId
EMIT CHANGES;
But got this instead:
Invalid join type: full-outer join not supported for foreign-key table-table join. Got Activity [FULL] OUTER JOIN Person.
Are there any other way to do it?

Related

LEFT JOIN using _PARTITIONDATE

I'm currently using StandardSQL in BigQuery, I tried to join two sets of table one of which is a pseudo-column table partitioned by day.
I tried to use this query below:
SELECT
DISTINCT DATE(create_time) AS date,
user_id,
city_name,
transaction_id,
price
FROM
table_1 a
LEFT JOIN (SELECT user_id, city_name FROM table_2) b
ON (a.user_id = b.user_id AND DATE(create_time) = _PARTITIONDATE)
I've tried this kind of JOIN (using _PARTITIONDATE) and worked out, but for this particular query I got an error message:
Unrecognized name: _PARTITIONDATE
Can anyone tell me why this happened, and how could I solve this? Thanks in advance.
The issue is that you are not selecting the _PARTITIONDATE field from table_2 when joining it so it can't recognize it:
SELECT user_id, city_name FROM table_2
In order to solve it you can add it as follows:
SELECT
DISTINCT DATE(create_time) AS date,
user_id,
city_name,
transaction_id,
price
FROM
table_1 a
LEFT JOIN (SELECT _PARTITIONDATE AS pd, user_id, city_name FROM table_2) b
ON (a.user_id = b.user_id AND DATE(create_time) = pd)
Note that you'll need an alias such as pd as it's a pseudocolumn
Probably it was working in the past if you were joining two tables directly such as in (you don't get selectivity benefits in that case):
FROM
table_1 a
LEFT JOIN table_2 b
ON (a.user_id = b.user_id AND DATE(create_time) = _PARTITIONDATE)

left_joins the same table twice

Is it possible to join the same table twice?
In our case we want:
polymorphic.left_joins(:course, step: :course)
Problem when polymorphic row has a type "Step" in such case associated course of step does not join
Why do you need to join it twice?
For example we have a Polymorphic model that belongs to either a Course model or a Step model. Step belongs_to course. We should join course or step with course. It means if Polymorphic row has type Course we join course.fields if type Step we need to join steps with course so we can have in one row polymorphic.fields + steps.fields + associated course.fields
⚙ We can join the same table twice manually using alias for table (t1)
⛄
sql = <<-SQL
LEFT JOIN courses
ON courses.id = polymorphicable_id
AND (polymorphicable_type = 'Course')
LEFT JOIN steps
ON steps.id = polymorphicable_id
AND (polymorphicable_type = 'Step')
LEFT JOIN courses AS t1
ON t1.id = steps.course_id
SQL
polymorphic.joins(sql)

Optimizing SQL query using JOIN instead of NOT IN

I have a sql query that I'd like to optimize. I'm not the designer of the database, so I have no way of altering structure, indexes or stored procedures.
I have a table that consists of invoices (called faktura) and each invoice has a unique invoice id. If we have to cancel the invoice a secondary invoice is created in the same table but with a field ("modpartfakturaid") referring to the original invoice id.
Example of faktura table:
invoice 1: Id=152549, modpartfakturaid=null
invoice 2: Id=152592, modpartfakturaid=152549
We also have a table called "BHLFORLINIE" which consists of services rendered to the customer. Some of the services have already been invoiced and match a record in the invoice (FAKTURA) table.
What I'd like to do is get a list of all services that either does not have an invoice yet or does not have an invoice that's been cancelled.
What I'm doing now is this:
`SELECT
dbo.BHLFORLINIE.LeveringsDato AS treatmentDate,
dbo.PatientView.Navn AS patientName,
dbo.PatientView.CPRNR AS patientCPR
FROM
dbo.BHLFORLINIE
INNER JOIN dbo.BHLFORLOEB
ON dbo.BHLFORLOEB.BhlForloebID = dbo.BHLFORLINIE.BhlForloebID
INNER JOIN dbo.PatientView
ON dbo.PatientView.PersonID = dbo.BHLFORLOEB.PersonID
INNER JOIN dbo.HENVISNING
ON dbo.HENVISNING.BhlForloebID = dbo.BHLFORLOEB.BhlForloebID
LEFT JOIN dbo.FAKTURA
ON dbo.BHLFORLINIE.FakturaId = FAKTURA.FakturaId
WHERE
(dbo.BHLFORLINIE.LeveringsDato >= '2017-01-01' OR dbo.BHLFORLINIE.FakturaId IS NULL) AND
dbo.BHLFORLINIE.ProduktNr IN (110,111,112,113,8050,4001,4002,4003,4004,4005,4006,4007,4008,4009,6001,6002,6003,6004,6005,6006,6007,6008,7001,7002,7003,7004,7005,7006,7007,7008) AND
((dbo.FAKTURA.FakturaType = 0 AND
dbo.FAKTURA.FakturaID NOT IN (
SELECT FAKTURA.ModpartFakturaID FROM FAKTURA WHERE FAKTURA.ModpartFakturaID IS NOT NULL
)) OR
dbo.FAKTURA.FakturaType IS NULL)
GROUP BY
dbo.PatientView.CPRNR,
dbo.PatientView.Navn,
dbo.BHLFORLINIE.LeveringsDato`
Is there a smarter way of doing this? Right now the added the query performs three times slower because of the "not in" subquery.
Any help is much appreciated!
Peter
You can use an outer join and check for null values to find non matches
SELECT customer.name, invoice.id
FROM invoices i
INNER JOIN customer ON i.customerId = customer.customerId
LEFT OUTER JOIN invoices i2 ON i.invoiceId = i2.cancelInvoiceId
WHERE i2.invoiceId IS NULL

Solr5.4 indexing using DataImportHandler(joins)

I am using Solr 5.4. Working on indexing entities using Data Import Handler which are having inner joins and left joins as below.
Does solr5.4 supports inner joins and left joins?
I am having a query which has relationship between 7 tables. I am doing inner joins for few and left joins for few tables.
below is my query declared in data-config.xml file.
SELECT rel.*,
PRODUCT_SRC_SYS.PRODUCT_SRC_NME AS sourceName
FROM (SELECT REF.*,
PRODUCT_RLSP.OBJ_ID AS relObjId
FROM (SELECT prdct.*,
PRODUCT_CATEGORY.OBJ_ID AS refObjId
PRODUCT_CATEGORY_MAPG.category_mapng_Name
AS refCategoryMapngName,
PRODUCT_CATEGORY_LIFE_CYL.LIFE_CYL_STAT_TYP
AS refStatus
FROM (SELECT PRODUCT.RGSTRY_ID
AS id
PRODUCT.OBJ_ID
AS obj_id,
PRODUCT.PRODUCT_NAME
As product_name,
PRODUCT_STAT.PRODUCT_STAT_TYP
AS status
FROM PRODUCT
LEFT JOIN PRODUCT_STAT
ON PRODUCT.OBJ_ID =
PRODUCT_STAT.OBJ_ID)
prdct
LEFT JOIN PRODUCT_CATEGORY
ON prdct.product_name =
PRODUCT_CATEGORY.category_name
INNER JOIN PRODUCT_CATEGORY_MAPG
ON PRODUCT_CATEGORY.OBJ_ID =
PRODUCT_CATEGORY_MAPG.OBJ_ID
INNER JOIN PRODUCT_CATEGORY_LIFE_CYL
ON PRODUCT_CATEGORY.OBJ_ID =
PRODUCT_CATEGORY_LIFE_CYL.OBJ_ID)
REF
LEFT JOIN PRODUCT_RLSP
ON REF.obj_id =
PRODUCT_RLSP.OBJ_ID) rel
LEFT JOIN PRODUCT_SRC_SYS
ON rel.obj_id = PRODUCT_SRC_SYS.OBJ_ID
Any help is highly appreciated!!

Performing Left Join in Rhomobile

Is it not possible to perform a left join in Rhomobile?
I have models PriceGroups, PriceLookup which have a 1-many relationship (ie. each PriceGroup and have many PriceLookup records).
I need to do a simple SQL Left Join so I have the required information from the PriceGroups Table
SELECT * FROM PriceLookup
LEFT JOIN PriceGroups ON PriceLookup.price_group_code=PriceGroups.code
I have added this to the price_lookup model:
belongs_to :price_group_code, 'PriceGroups'
The following is what I have tried in Rhomobile
PriceLookup.find_by_sql("SELECT *
FROM PriceLookup
LEFT JOIN PriceGroups on PriceLookup.price_group_code=PriceGroups.code")
But I get error:
Error: could not prepare statement: 1; Message: no such table: PriceGroups
I know I can do two selects and join them myself but this is a very crap way of doing it
You need to create the RhoMobile model as FixedSchema, not using the default PropertyBags.
Otherwise you don't have a real table in SQLite but you're using the special objectValues table that is implementing a Key-Value store:
http://docs.rhomobile.com/rhodes/rhom#fixed-schema
Example:
dbPT = ::Rho::RHO.get_src_db('PriceLookup')
sql = "SELECT * FROM PriceLookup LEFT JOIN PriceGroups ON PriceLookup.price_group_code=PriceGroups.code"
lines = dbPT.execute_sql(sql)
This could happen if the table PriceGroups hasn't been initialized (created) yet. Tables are created when the model class is loaded, if they don't exist.
For your case, simply call the model class, this will load it and create the table if necessary.
PriceGroups; #Only for create the table
PriceLookup.find_by_sql("SELECT * FROM PriceLookup LEFT JOIN PriceGroups on PriceLookup.price_group_code=PriceGroups.code")

Resources