Joining Cart entry and Product Table in Hybris - sap-commerce-cloud

I am new to floexible search query in hybris
I need to delete the cart entries with specific product id- I am using the below query to get the entries
SELECT {products.PK} FROM {Product AS products JOIN CartEntry AS carts ON {products.PK} = {CartEntry.PRODUCT} } Where {products.PK} ='<PK of the product>'
I keep getting the below exception message.Is there anything i am missing
Exception message: cannot find (visible) type for alias CartEntry within [carts:CartEntry, products:Product]

Try with the following query :
SELECT {products.PK} FROM {Product AS products JOIN CartEntry AS carts ON {products.PK} = {carts.product} } Where {products.PK} ='<PK of the product>'
Hope this helps

The problem is that you are mixing both, alias and actual name of the itemtype i.e. {products.PK} = {CartEntry.PRODUCT} where products is an alias while CartEntry is an actual name of the itemtype. The following will work:
SELECT {products.PK} FROM {Product AS products JOIN CartEntry AS carts ON {products.PK} = {carts.product} } WHERE {products.PK} ='<PK of the product>'
You can also use any of the following:
SELECT {Product.PK} FROM {Product JOIN CartEntry ON {Product.PK} = {CartEntry.product} } WHERE {Product.PK} ='<PK of the product>'
SELECT {PK} FROM {Product}, {CartEntry} WHERE {Product.PK} = {CartEntry.product} AND {Product.PK} ='<PK of the product>'
SELECT {PK} FROM {Product AS products}, {CartEntry AS carts} WHERE {products.PK} = {carts.product} AND {products.PK} ='<PK of the product>'
SELECT {products.PK} FROM {Product AS products}, {CartEntry AS carts} WHERE {products.PK} = {carts.product} AND {products.PK} ='<PK of the product>'

Related

Adding OR condition with other than entity column in typeorm gives error: could not determine data type of parameter $2

I want to generate postgres query as below:
select * from table1 t1 inner join table2 t2 on t1.id = t2.table1_id
where param.name is null or t1.name ilike param.name
Here param is request parameter
I tried with this:
await this.userRepository.createQueryBuilder("t1")
.innerJoinAndSelect("t1.table2","t2")
.where("t1.name ilike :name or :name is null",{ name:`%${request.body.name}%`)
.getMany()
This returns following error:
QueryFailedError: could not determine data type of parameter $2
I just tried the example and got the same error, from my experience I think this is a bug with typeorm
I suggest you change your aproach to the problem, try this:
await this.userRepository.createQueryBuilder("t1")
.innerJoinAndSelect("t1.table2","t2")
.where("t1.name ilike :name",{ name: req.body.name ? `%${request.body.name}%` : '%%')
.getMany()
this will work the same as the provided query.
I used below query to meet my need:
await this.userRepository.createQueryBuilder("t1")
.andWhere(new Brackets(q => {
q.where("d.name ilike :name", { name:`%${request.body.name}%`})
q.orWhere("cast(:param as character varying) is null", { param: request.body.name || null})
}))

Typeorm gives me QueryFailedError: column reference "id" is ambiguous

This is the query I'm firing off:
const updatedUser = await queryRunner.manager
.getRepository(UserEntity)
.createQueryBuilder('user')
.leftJoinAndSelect('user.categories', 'cats')
.where('id = :userId', { userId })
.getOne();
And here is the query that is generated by typeorm:
SELECT "user"."id" AS "user_id",
"user"."first_name" AS "user_first_name",
"user"."last_name" AS "user_last_name",
"user"."phone" AS "user_phone",
"user"."password" AS "user_password",
"user"."password_hint" AS "user_password_hint",
"user"."recovery_email" AS "user_recovery_email",
"user"."created" AS "user_created",
"user"."username" AS "user_username",
"user"."profile_pic" AS "user_profile_pic",
"user"."is2fa" AS "user_is2FA",
"user"."refresh_token" AS "user_refresh_token",
"user"."profile_email" AS "user_profile_email",
"user"."subscriptionid" AS "user_subscriptionId",
"cats"."id" AS "cats_id",
"cats"."template_id" AS "cats_template_id",
"cats"."name" AS "cats_name",
"cats"."entry_count" AS "cats_entry_count",
"cats"."is_base_template" AS "cats_is_base_template",
"cats"."can_rename" AS "cats_can_rename",
"cats"."can_delete" AS "cats_can_delete",
"cats"."userid" AS "cats_userId",
"cats"."position" AS "cats_position",
"cats"."icon_path" AS "cats_icon_path",
"cats"."init_name" AS "cats_init_name",
"cats"."isfortasks" AS "cats_isForTasks",
"cats"."is_locked_by" AS "cats_is_locked_by",
"cats"."is_locked" AS "cats_is_locked",
"cats"."password" AS "cats_password",
"cats"."state" AS "cats_state",
"cats"."password_hint" AS "cats_password_hint",
"cats"."parentcategoryid" AS "cats_parentCategoryId"
FROM "user" "user"
LEFT JOIN "category" "cats"
ON "cats"."userid" = "user"."id"
WHERE id = $1
If I remove the leftJoinAndSelect it doesn't yield that error but I need this join.
What is wrong with the query?
META:
The DBMS I'm using is PostgreSQL v. 12.3.
Well, as you can see there is a user.id and a cats.id available - you even select both. But in the WHERE clause you don't say which id you mean. Change 'id = :userId' to 'user.id = :userId' and it should work.

how to use nested select query in knex

I want to add a sub-query as a column using knex.
Example SQL query:
select
name,
(select count(*) from employee where employee.dept_id = 1) as employees_count
from
department
where
department.id = 1
I tried
knex('department').select('name', knex('employee').count().where({dept_id: 1}))
.where({id: 1})
but it didn't work
To help yourself localize the issue, you can add .debug() to your querybuilder. That allows you to see the actual rendered query. This will not solve your issue, but will give you insight into how Knex sees your query.
To solve your issue, try some combination of select and raw. Like:
let emp = knex('employee').count().where({ dept_id: 1 })
knex('department')
.select('name')
.select(knex.raw('? as employee_count', emp))
//.debug()
use .debug() to clear out minor issues.
const Knex = require('knex');
const knex = Knex({
client: 'pg',
});
knex('department').select(
'name',
knex('employee').count().where({dept_id: 1})
.as('employees_count')
).where({id: 1}).toSQL().sql;
// "select \"name\", (select count(*) from \"employee\" where… \"employees_count\" from \"department\" where \"id\" = ?"
https://runkit.com/embed/q22w8v6z5n61

WikiData SPARQL times out with subquery + labels service + OPTIONAL

While trying to answer this question: How to filter results of wikidata to specific language , I have encountered the following problem:
Some countries have more than one capital. This query randomly chooses only one capital per country:
SELECT ?country (sample(?capital) as ?aCapital) WHERE {
?country wdt:P31 wd:Q3624078.
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} # not a former country
?country wdt:P36 ?capital.
}
GROUP BY ?country
Try it here
However, while trying to add labels and coordinates, the query times out:
SELECT ?country ?countryLabel ?aCapital ?aCapitalLabel ?coords WHERE {
OPTIONAL {?aCapital wdt:P625 ?coords.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
{
SELECT ?country (sample(?capital) as ?aCapital) WHERE {
?country wdt:P31 wd:Q3624078.
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} # not a former country
?country wdt:P36 ?capital.
}
GROUP BY ?country
}
}
ORDER BY ?countryLabel
LIMIT 1000
Try it here
Following the comments By #AKSW Above - OPTIONAL in SPARQL is a left join.
Reordering the subquery and OPTIONAL solves the problem:
SELECT ?country ?countryLabel ?aCapital ?aCapitalLabel ?coords WHERE {
{
{
SELECT ?country (sample(?capital) as ?aCapital) WHERE {
?country wdt:P31 wd:Q3624078.
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} # not a former country
?country wdt:P36 ?capital.
}
GROUP BY ?country
}
OPTIONAL {?aCapital wdt:P625 ?coords.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
}
ORDER BY ?countryLabel
LIMIT 1000
Try it here.
Please note that this requires adding an additional { + } to keep the syntax correct.
See also: SPARQL Optional query

Sequelize query in query

I have 2 models Shop and Beacon. I have to return sum beacons assigned to each shop. I know how to do it in SQL:
SELECT Shops.*, (SELECT COUNT(id) FROM Beacons WHERE shopId = Shops.id) FROM Shops
But i have a problem with Sequelize, i try something like below:
models.Shop.findAll({
attributes: ['id', sqz.fn('count', sqz.col('shopId'))],
include: [
{
model: models.Beacon
}
]
});
This solution work, but return query:
SELECT Shop.*, count(shopId) FROM Shops LEFT OUTER JOIN Beacons ON Shop.id = Beacons.shopId

Resources