HQL map with distinct not work - hql

I have a trouble using HQL (in Groovy):
A simple query works without problems, like below (please note that I am using distinct):
def eqLiquid = liq.Liquidacion.executeQuery(
"""SELECT
distinct b.id ,
l.nombre as name
FROM Liquidacion l JOIN l.detalles ll JOIN ll.bioquimico b
WHERE l.id=:liqid
""", [liqid: liqid])
But I need to return these data as a map, then I have modified the query (only add new map()) :
def eqLiquid = liq.Liquidacion.executeQuery(
"""SELECT new map(
distinct b.id ,
l.nombre as name
)
FROM Liquidacion l JOIN l.detalles ll JOIN ll.bioquimico b
WHERE l.id=:liqid
""", [liqid: liqid])
Then I get an error: "unexpected token: distinct near line 2, column 17 [SELECT new map(
distinct b.id ,"
If I quit the distinct in the last query, it works.
Anyone had this problem?
It seems that MAP and DISTINCT can not work togheter in an HQL query

Related

Linq query not returning expected results even when using DefaultIfEmpty

I have the following query in one of my Entity Framework Core API controllers:
var plotData = await (from nl in _context.BookList
join ql in _context.PlotList on nl.PlotId equals ql.PlotId
join qc in _context.PlotChoices on ql.PlotId equals qc.PlotId
join nk in _context.BookLinks.DefaultIfEmpty() on qc.ChoiceId equals nk.ChoiceId
where nl.Id == ID
select new
{ .. }
I need it to return all rows even if data doesn't exist in the BookLinks table.
However, it's not returning rows if there is no data data in the BookLinks table for that row.
But this SQL query, from which I'm trying to model from, does return data...it returns nulls if there is no data in BookLinks.
select * from BookList bl
left join PlotList pl ON bl.plotId = bl.plotId
left join PlotChoices pc ON pl.plotId = pc.plotId
left join BookLinks bk ON pc.choiceID = bk.choiceID
where nl.caseID = '2abv1'
From what I read online, adding 'DefaultIfEmpty()' to the end of BookLinks should fix that, but it hasn't.
What am I doing wrong?
Thanks!
When using left join , you can try below code sample :
var plotData = (from nl in _context.BookList
join ql in _context.PlotList on nl.PlotId equals ql.PlotId
join qc in _context.PlotChoices on ql.PlotId equals qc.PlotId
join nk in _context.BookLinks on qc.ChoiceId equals nk.ChoiceId into Details
from m in Details.DefaultIfEmpty()
where nl.Id == ID
select new
{
}).ToList();

JPQL query with having oracle db

I am using jpql jpa eclipselink Following query wont work :
SELECT c FROM TableA c WHERE c.forumId = :forumId AND c.isDefault = true HAVING MAX (c.validFrom)
The error im getting "The expression is not a valid conditional expression"
The HAVING clause only works with a GROUP BY expression.
The HAVING clause allows for the results of a GROUP BY to be filtered.
Your question is:
i want o get max validFrom how can i make expression ot of this
But you can make a query without GROUP BY and HAVING to do what you want:
select c from TableA c WHERE c.validFrom = (
SELECT MAX(c2.validFrom)
FROM TableA c2
WHERE c2.Id = c.Id AND c.forumId = :forumId AND c.isDefault = true
)
If you would like to use GROUP BY and HAVING, you can do:
SELECT c FROM TableA c WHERE c.validFROM = (
SELECT MAX(validFROM)
FROM TableA
WHERE forumId = :forumId AND isDefault = true
GROUP BY validFROM
HAVING validFROM=c.validFROM
)

How to query names from a record with multiple IDs in LINQ

I have a table [A] that has columns such as CreatedBy(ID), AuthorizedBy(ID), SentTo(ID) and I need to join them to a table [B] containing user names (UserID, FullName). How can I write a join that connects each record of table A to multiple records in table B to fill in the CreatedBy/AuthorizedBy/SentTo names using LINQ?
can give try as below , basically you have to join B with A three times
form a in A
join b in B on b.Id = a.Createdby
join b1 in B on b1.Id = a.Authrizedby
join b2 in B on b2.Id = a.SentTo
select new {
a.Id,
CreatedBy= b.FullName,
AuthorizedBy = b1.FullName,
SentTo= b2.FullName};
or
from a in A
select new {
a.ID
CreatedBy= b.FirstOrDefault(a.CreatedBy== b.Id).FullName,
AuthorizedBy = b.FirstOrDefault(a.AuthorizedBy== b.Id).FullName,
SentTo= b.FirstOrDefault(a.SentTo== b.Id).FullName
}

Left outer join is null- additional conditions

I'm trying to find all entries in table a, where there is no matching entry in table b for one specific column (order). I'm using the following:
SELECT *
FROM a
LEFT OUTER JOIN b
ON a.id = b.id
WHERE b.order IS NULL
AND a.result>10
However, the last condition for result doesn't seems to work. It simply lists all the entries from table a, regardless whether result is more than 10 or not.
Any way around this?
Shouldn't your query be as below?
SELECT * FROM a LEFT OUTER JOIN b ON a.id = b.id WHERE b.id IS NULL AND a.result>10

LINQ query with Joins and aggregate

I would request someone to please check whether my following LINQ query with 4 tables joins and group by is logically correct? Is it the correct way write LINQ query for 4 tables join and Group by clause?
My second point:
If I comment line TotalTime = tstGroup.Sum(x=>x.Duration), it works fine.
But, If I uncomment this line it say the following exception.
A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll
What is wrong? Please comment
Tables
Table A: Id, FirstName
Table B: Id, AId(Table A FK)
Table C: Id, BId(Table B FK), Duration
Table D: Id, AId(Table A FK). Description
tstData= (from a in _context.A
join b in _context.B on a.Id equals b.AId
join c in _context.C on b.Id equals c.BId
join d in _context.D on a.Id equals d.AId
group c by new { a= a, b= b, d= d} into tstGroup
select new myobj
{
FirstName= tstGroup.Key.a.FirstName,
Description= tstGroup.Key.d.description,
TotalTime = tstGroup.Sum(x=>x.Duration),
}).ToList();
Modified LINQ.
Do you feel it is correct now? Removed multiple group clauses.
tstData= (from a in _context.A
join b in _context.B on a.Id equals b.AId
join c in _context.C on b.Id equals c.BId
join d in _context.D on a.Id equals d.AId
group c by new { c.Id, c.Duration, d.Name, a.FirstName} into tstGroup
select new myobj
{
FirstName= tstGroup.Key.FirstName,
Description= tstGroup.Key.description,
TotalTime = tstGroup.Sum(x=>x.Duration),
}).ToList();

Resources