Precise to do a select with inner join that has relationship in more than a field among the tables
Exemple:
DataSet dt = new Select().From(SubConta.Schema)
.InnerJoin(PlanoContabilSubConta.EmpSubContaColumn, SubConta.CodEmpColumn)
.InnerJoin(PlanoContabilSubConta.FilSubContaColumn, SubConta.CodFilColumn)
.InnerJoin(PlanoContabilSubConta.SubContaColumn, SubConta.TradutorColumn)
.Where(PlanoContabilSubConta.Columns.EmpContabil).IsEqualTo(cEmp)
.And(PlanoContabilSubConta.Columns.FilContabil).IsEqualTo(cFil)
.And(PlanoContabilSubConta.Columns.Conta).IsEqualTo(cTrad)
.ExecuteDataSet();
But the generated sql is wrong:
exec sp_executesql N'/* GetDataSet() */ SELECT [dbo].[SubContas].[CodEmp], [dbo].[SubContas].[CodFil], [dbo].[SubContas].[Tradutor], [dbo].[SubContas].[Descricao], [dbo].[SubContas].[Inativa], [dbo].[SubContas].[DataImplantacao]
FROM [dbo].[SubContas]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[CodEmp] = [dbo].[PlanoContabilSubContas].[EmpSubConta]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[CodFil] = [dbo].[PlanoContabilSubContas].[FilSubConta]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[Tradutor] = [dbo].[PlanoContabilSubContas].[SubConta]
WHERE EmpContabil = #EmpContabil0
AND FilContabil = #FilContabil1
AND Conta = #Conta2
',N'#EmpContabil0 varchar(1),#FilContabil1 varchar(1),#Conta2 varchar(1)',#EmpContabil0='1',#FilContabil1='1',#Conta2='1'
What should be made to generate this sql?
exec sp_executesql N'/* GetDataSet() */ SELECT [dbo].[SubContas].[CodEmp], [dbo].[SubContas].[CodFil], [dbo].[SubContas].[Tradutor], [dbo].[SubContas].[Descricao], [dbo].[SubContas].[Inativa], [dbo].[SubContas].[DataImplantacao]
FROM [dbo].[SubContas]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[CodEmp] = [dbo].[PlanoContabilSubContas].[EmpSubConta] AND
[dbo].[SubContas].[CodFil] = [dbo].[PlanoContabilSubContas].[FilSubConta] AND
[dbo].[SubContas].[Tradutor] = [dbo].[PlanoContabilSubContas].[SubConta]
WHERE EmpContabil = #EmpContabil0
AND FilContabil = #FilContabil1
AND Conta = #Conta2
',N'#EmpContabil0 varchar(1),#FilContabil1 varchar(1),#Conta2 varchar(1)',#EmpContabil0='1',#FilContabil1='1',#Conta2='1'
Looks like you can't join on multiple columns in SubSonic 2, you could just create a SQL view that joins as you want it to and then get SubSonic to generate a model for that view.
Related
In Databricks sql while executing SQL with NOT EXISTS operator (using correlated subquery) its not working. Getting Databricks error Error SparkUnsupportedOperationException: [INTERNAL_ERROR] Cannot generate code for expression: outer.
Below is the sql query
SELECT in_cs.COMM_ID AS CUSTOMER_SERVICE_EPIC_ID,
Data.CUR_VALUE_DATETIME AS VALUE_INSTANT,
FROM hive_metastore.RAW_CLARITY.SMRTDTA_ELEM_DATA Data
INNER JOIN hive_metastore.RAW_CLARITY.SMRTDTA_ELEM_VALUE Value
ON Data.HLV_ID = Value.HLV_ID
INNER JOIN hive_metastore.RAW_CLARITY.CLARITY_CONCEPT SmartDataElement
ON Data.ELEMENT_ID = SmartDataElement.CONCEPT_ID
INNER JOIN hive_metastore.RAW_CLARITY.CUST_SERVICE in_cs
ON Data.RECORD_ID_NUMERIC = in_cs.COMM_ID AND NOT EXISTS
( SELECT 1 FROM hive_metastore.RAW_CLARITY.CUST_SERVICE AS cs
LEFT JOIN hive_metastore.RAW_CLARITY.CAL_REFERENCE_CRM AS crc
ON cs.COMM_ID = crc.REF_CRM_ID
LEFT JOIN hive_metastore.RAW_CLARITY.CAL_COMM_TRACKING AS cct
ON crc.COMM_ID = cct.COMM_ID
WHERE cct.COMM_ID IS NULL AND in_cs.COMM_ID = cs.COMM_ID)
I have some sql code like this
select *
from people_work_attendances pwa
inner join people_work_placements pwp on pwa.user_work_id = pwp.id
inner join company_job_profile_r_attendance cjpa on pwp.job_profile = cjpa.profile and pwa.policy_id=cjpa.policy
WHERE
pwa.user_work_id = 71072
and cjpa.profile IS NOT NULL
i want to implement this section to Sequelize
inner join company_job_profile_r_attendance cjpa on pwp.job_profile = cjpa.profile and pwa.policy_id=cjpa.policy
current situation is i can make it like this
if(!module.exports.Placement.associations.JobProfileAttendance){
module.exports.Placement.hasOne(module.exports.JobProfileAttendance, {
as:'JobProfileAttendance',
sourceKey:'jobProfile',
foreignKey:'profile'
})
}
but the result of this part is like this
INNER JOIN "public"."company_job_profile_r_attendance" AS "Placement->JobProfileAttendance" ON "Placement"."job_profile" = "Placement->JobProfileAttendance"."profile"
i want to make like this
INNER JOIN "public"."company_job_profile_r_attendance" AS "Placement->JobProfileAttendance"
ON "Placement"."job_profile" = "Placement->JobProfileAttendance"."profile"
AND "PeopleWorkAttendance"."policy_id" = "Placement->JobProfileAttendance"."policy"
want to add some AND conditions but in Sequelize
AND "PeopleWorkAttendance"."policy_id" = "Placement->JobProfileAttendance"."policy"
You can use on option in include, just keep in mind that you always should do it manually because Sequelize doesn't support composite primary and foreign keys.
See options.include[].on in findAll options
I have to execute the below query using spark. How can I optimize the join.
Each data frame has records in millions
SELECT DISTINCT col1,
col2,
col3...
FROM ool
INNER JOIN ooh ON ool.header_key = ooh.header_key
AND ool.org_key = ooh.org_key
INNER JOIN msib ON ool.inventory_item_key = msib.inventory_item_key
AND ool.ship_from_org_key = msib.o_key
INNER JOIN curr ON curr.from_currency = ooh.transactional_curr_code
AND date_format(curr.date, 'yyyy-mm-dd') = date_format(ooh.date, 'yyyy-mm-dd')
INNER JOIN mtl_parameters mp ON ool.ship_from_org_key = mp.o_key
INNER JOIN ood ON ood.o_key = mp.o_key
INNER JOIN ot ON ooh.order_type_key = ot.transaction_type_key
INNER JOIN csu ON ool.ship_to_org_key = csu.site_use_key
INNER JOIN csa ON csu.site_key = csa._site_key
INNER JOIN ps ON csa.party_key = ps.party_key
INNER JOIN hca ON csa.account_key = hca.account_key
INNER JOIN hp ON hca.party_key = hp.party_key
INNER JOIN hl ON ps.location_key = hl.location_key
INNER JOIN csu1 ON ool.invoice_to_key = csu1.use_key
INNER JOIN csa1 ON ool.invoice_to_key = csu1.use_key
AND csu1.cust_acctkey = csa1.custkey
INNER JOIN ps1 ON csa1.party_key = ps1.party_key
INNER JOIN hca1 ON csa1.cust_key = hca1.cust_key
INNER JOIN hp1 ON hca1.party_key = hp1.party_key
INNER JOIN hl1 ON ps1.loc_key = hl1.loc_key
INNER JOIN hou ON ool.or_key = hou.o_key
How can I optimize this join in pyspark?
ooh and ool are the driver dataframes and their record count will be in hundreds of million range.
TableAlias isn't working with multiple joins.
The query:
var q = Db.From<Blog>(Db.TableAlias("b"))
.LeftJoin<Blog, BlogToBlogCategory>((b,btb)=> b.Id == btb.BlogId, Db.TableAlias("btbc"))
.Join<BlogToBlogCategory, BlogCategory>((bt,bc)=>bt.BlogCategoryId == bc.Id, Db.TableAlias("cats"))
.GroupBy(x => x.Id);
.Select("b.*, json_agg(cats) as BlogCategoriesJson");
var results = Db.Select<BlogQueryResponse>(q);
Generates this SQL:
SELECT b.*, json_agg(cats) as BlogCategoriesJson
FROM "blog" "b" LEFT JOIN "blog_to_blog_category" "btbc" ON ("b"."id" = "btbc"."blog_id") INNER JOIN "blog_category" "cats" ON ("blog_to_blog_category"."blog_category_id" = "cats"."id")
GROUP BY "b"."id"
This causes error because it is referencing "blog_to_blog_category" instead of btbc
The Db.TableAlias() only provides an alias for the target join table, your inner join does not specify the alias to use for the source table so it references the full table name as expected.
You can use Sql.TableAlias() in your LINQ Expression to reference a table alias, e.g:
var q = Db.From<Blog>(Db.TableAlias("b"))
.LeftJoin<Blog, BlogToBlogCategory>((b,btb)=> b.Id == btb.BlogId, Db.TableAlias("btbc"))
.Join<BlogToBlogCategory, BlogCategory>((bt,bc)=>
Sql.TableAlias(bt.BlogCategoryId, "btbc") == bc.Id, Db.TableAlias("cats"))
.GroupBy(x => x.Id);
.Select("b.*, json_agg(cats) as BlogCategoriesJson");
I want to write this raw query using the TypeORM createQueryBuilder. How can I achieve this?
SELECT *
FROM Location l
INNER JOIN LocationType t ON l.TypeId = t.Id
LEFT JOIN Location p ON l.ParentId = p.Id
LEFT JOIN (SELECT locationId, SUM(Units) TotalUnits FROM ItemInventory GROUP BY LocationId) qty ON l.Id = qty.LocationId`