How to pass WHERE clause to Sequelize generated subqueries? - node.js

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)

Related

Is sequelize support joins with subquery?

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

Need to convert psql query into typeorm

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()

Is it possible to chain subsequent queries's where clauses in Dapper based on the results of a previous query in the same connection?

Is it possible to use .QueryMultiple (or some other method) in Dapper, and use the results of each former query to be used in the where clause of the next query, without having to do each query individually, get the id, and then .Query again, get the id and so on.
For example,
string sqlString = #"select tableA_id from tableA where tableA_lastname = #lastname;
select tableB_id from tableB WHERE tableB_id = tableA_id";
db.QueryMultiple.(sqlString, new {lastname = "smith"});
Is something like this possible with Dapper or do I need a view or stored procedure to accomplish this? I can use multiple joins for one SQL statement, but in my real query there are 7 joins, and I didn't think I should return 7 objects.
Right now I'm just using object.
You can store every previous query in table parameter and then first perform select from the parameter and query for next, for example:
DECLARE #TableA AS Table(
tableA_id INT
-- ... all other columns you need..
)
INSERT #TableA
SELECT tableA_id
FROM tableA
WHERE tableA_lastname = #lastname
SELECT *
FROM #TableA
SELECT tableB_id
FROM tableB
JOIN tableA ON tableB_id = tableA_id

Maximo Conditional expression manager/Sql Query Builder

Can somebody help me in converting below mentioned query in to Maximo's where clause:
select distinct workorder.wonum from workorder inner join [assignment]
On workorder.wonum=[assignment].wonum
inner join amcrew
On amcrew.amcrew=[assignment].amcrew
inner join amcrewlabor
On amcrewlabor.amcrew=amcrew.amcrew
inner join labor
On amcrewlabor.laborcode=labor.laborcode
inner join person
on labor.laborcode=person.personid where amcrewlabor.laborcode='KELLYB'
KELLYB is PERSONID used here for just reference.
If you are using a custom search query in Maximo, you can try prepending your with in (your query)
For example, if you're in Maximo's work order tracking module, the application uses select * from workorder by default. Any time you add a search filter such as work order number (wonum), then the query appends to run a query as select * from workorder where wonum = '123' if 123 is the work order number you entered.
Your where clause might look something like this:
wonum in (
select distinct workorder.wonum
from workorder
join assignment on workorder.wonum=assignment.wonum
join amcrew on amcrew.amcrew=assignment.amcrew
join amcrewlabor on amcrewlabor.amcrew=amcrew.amcrew
join labor on amcrewlabor.laborcode=labor.laborcode
join person on labor.laborcode=person.personid
where amcrewlabor.laborcode='KELLYB'
)
The SQL that is generated in Microsoft Access will not necessarily work in Maximo without some modification.

How to sort by sum of field of a relation on sails.js?

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

Resources