In my query, I want to use the sequelize with left join and subquery.
select category.id, category.name, images.tag_id
from table.category
left outer join(select tag_id, category_id,s3_image_url,alt_tag from table.images images.tag_id = 23 and images.name ILIKE '%fresh%' ) as images on category.id = images.category_id
where category.name in ('fruit', 'leaf','tree')
I want to convert this query into sequelize.
After my conversion also it's not working. Is it possible to use left join and subquery on the same sequelize query? if possible please some example
Related
I have a Sequelize view that uses joins like this:
select * from tableA left join (select * from tableB where someKey={some input}) newTableB on tableA.someId = newTableB.someId
Now, when I create a view from this query, I can create a sequelize model for it. But, I want to execute the query in sequelize... like -
models.tableView.findAll({ some clause })
In the above where clause, I want to pass the value of someKey (referring to someKey={some input} in the sample view). How do I do that?
I don't want to fetch all data and then apply the tableB.someKey=something (as that will take far more time)
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 need to get data on daily/weekly/monthly basis. So i used date_trunc() function to get this type of record. I made psql query but i need to convert it into typeorm code as i'm new to typeorm stack. Below is the query
select date_trunc('day', e."createdAt") as production_to_month, count(id) as count from events e
where e."createdAt" between '2021-05-10' and '2021-05-17' and e."type" = 'LOGIN'
group by date_trunc('day', e."createdAt")
order by date_trunc('day', e."createdAt") asc
need to convert this
You need to use the query builder to achieve the desired result.
The query would be -
return await getRepository(Events)
.createQueryBuilder("e")
.select("date_trunc('day',`e`.`createdAt`)","production_to_month")
.addSelect("count(`id`)","count")
.where("`e`.`createdAt` between '2021-05-10' and '2021-05-17'")
.andWhere("`e`.`type` = 'LOGIN'")
.groupBy("date_trunc('day',`e`.`createdAt`)").
.orderBy("date_trunc('day',`e`.`createdAt`)","ASC")
.printSql()
.getRawMany()
I searched a lot about sorting elements by sum of votes (in another model), like I do in SQL here :
SELECT item.* FROM item
LEFT JOIN (
SELECT
vote.item,
SUM(vote.value) AS vote.rating
FROM vote
GROUP BY vote.item
) AS res ON item.id = vote.item
ORDER BY res.rating DESC
Is there a way to do it via waterline methods ?
I think you can't do the left join with simple waterline methods, but you can use the .query method to execute your raw SQL syntax.
Sails MySQL adapter makes sum('field') conflict with sort('field'). It will generate SQL query like:
SELECT SUM(table.field) AS field FROM table ORDER BY table.field;
But I want:
SELECT SUM(table.field) AS field FROM table ORDER BY field;
It same as:
SELECT SUM(table.field) AS f FROM table ORDER BY f;
My solution is using lodash.sortBy() to process results. https://lodash.com/docs/4.16.4#sortBy
Is it possible to do a join to a nested select statement in SubSonic 2.2? I am having trouble with this because I have to do a group by...
I.E.
select conn_company.company_name, SUM(t.quote_grand_total) as 'SUM' from conn_company
inner join
(SELECT *
FROM [dbo].[QuoteDBAll]
where [dbo].[QuoteDBAll].[quote_status_id] = 1
AND [dbo].[QuoteDBAll].[quote_ca_modified_date] BETWEEN '12/1/2000' AND '12/1/2009'
) t
on conn_company.company_id = t.company_id
group by company_name
having company_name = 'JACK'
Unfortunately you're not going to be able to convert that SQL into SubSonic 2 syntax. I would suggest you create a sql view based on your query and get SubSonic to generate a model from that instead.