Hibernate distinct clause problem - sql

I searched a lot around but cannot find the answer:
I have the following SQL query:
select distinct l.id_book from wa2011.tb_lending as l where l.id_user = 1
It's a very simple query but I cannot manage to write it. How can I write this in HQL?
Thanks a lot!
Cheers.

I tested the code with Hibernate 3.3.2 and MS SQL Server and it works fine:
select distinct u.id from User u where u.login='admin'
So I think your HQL code should looks almost the same (just rewrite it from SQL to object model mapped to Hibernate).

Related

How to express this with jOOQ "Select alias.*, otherAlias.Column From .."

I am trying to use jOOQ to dynamically construct queries. So far it is going really well, but now I stumbled upon a case that I can not seem to express.
This is a simplified version of the query I want to generate:
Select alias.*, otherAlias.aColumn as aAlias
From table as alias
inner join otherTable as otherAlias
on alias.someColumn = otherAlias.someOtherColumn
Where otherAlias.someOtherColumn in (????????)
My problem is that i cannot seem to express the SELECT part how I need it. If I just use:
.select() -> I get select *
.select(alias.fields()) -> I get Select *
.select((alias.fields() :+ field(name(otherAlias, aColumn)).as(aAlias)):_*) -> I get Select otherAlias.aColumn as aAlias
Is there a way to express this with jOOQ?
The rest of the statement seems to work as expected. I am using jOOQ 3.10.7 in Scala and targeting Postgres at the moment and my statement currently looks like this:
sql
.select()
.from(alias)
.innerJoin(otherAlias)
.on(field(name(alias, someColumn)).eq(field(name(otherAlias, someOtherColumn))))
.where(condition)
Thanks a lot.
Update: I found a way to express this that seems to work by falling back to plain SQL. But I am still wondering whether there is a better way to express this. This works as a select:
.select((field(""""Alias".*"""), field(name(otherAlias, aColumn)).as(aAlias)):_*) -> I get Select "Alias".*, otherAlias.aColumn as aAlias
Assuming that you're using jOOQ 3.11 (which added support for unqualified asterisks and qualified asterisks), you can just write
alias.asterisk()
Or even, using jOOQ's scala extensions
alias.*
This is especially powerful when using the code generator.

Why is MS Access 2010 SQL choking on this query?

The following query will not show up in design view and if you trying to make it show it locks up MS Access and you have to use the Task Manager to stop MS Access. The query actually runs and produces the correct results. If there is a better way I will certainly accept that.
SELECT
log_metric_N.metric_title,
log_metric_N.metric_N
FROM
(
SELECT
tref_log_metrics_weights_and_factors.metric_title,
[metric_base].[metric_count],
[metric_base].[metric_sum],
(([metric_base].[metric_count]*[tref_log_metrics_weights_and_factors].[metric_weight])/[metric_base].[metric_sum]) AS metric_N
FROM
tref_log_metrics_weights_and_factors,
(
SELECT
Count(tref_log_metrics_weights_and_factors.metric_weight) AS metric_count,
Sum(tref_log_metrics_weights_and_factors.metric_weight) AS metric_sum
FROM
tref_log_metrics_weights_and_factors
WHERE (((tref_log_metrics_weights_and_factors.metric_weight)<>0))
) as metric_base
) AS log_metric_N;
#HansUp you were exactly right on. I forgot all about the Domain functions and they work perfectly without the complexity. Below is the resultant SQL statement.
SELECT
tref_log_metrics_weights_and_factors.metric_title,
DCount("[metric_weight]","tref_log_metrics_weights_and_factors","[metric_weight]<>0") AS metric_count,
Dsum("[metric_weight]","tref_log_metrics_weights_and_factors") AS metric_sum,
(([metric_count]*[tref_log_metrics_weights_and_factors].[metric_weight])/[metric_sum]) AS metric_N
FROM
tref_log_metrics_weights_and_factors

Access pass-through query to Oracle using WITH

I tried to run this in TOAD and sqldeveloper is ok, but when I run in Access with passthrough query I got an error:
"pass-through query with returnsRecords property"
hereby the code
With T1 as (select * from baasa),
T2 as (select * from lalala),
T4 as (select * from babab)
select distinct T1.C11 as something, T4.C5 as somewhere, T2.C2 as what
from T1,T2,T4
Where T1.C11=T4.C5 and T1.C10=T2.C2
or in fiddle: http://sqlfiddle.com/#!4/84c06/2
can anyone help me?
Your (amended) query works fine in SQL Server 2008 R2, and it also works fine as an Access pass-through query to same. Verify that the Returns Records property of the query is set to Yes (as seen below) and try running the pass-through query again. As I said in my initial comment to your question, if it works for SQL Server then I see no reason why it shouldn't work for Oracle.
I'm pretty sure your query has a syntax error: there's no SELECT nor FROM clause before the WHERE clause...

Querying another DB while connecting to other Database in SQL

In my SQL script I am using one database(Lets assume MyDb1).
In side that script I need to query extended property of another DB (MyDb2).
I have no Idea to do this.
Please advise me.
You can use this Query :
SELECT * FROM MyDb1.DBO.TableName as tbl1,MyDb2.DBO.TableName as tbl2
WHERE tbl1.ID=tbl2.ID

SQL Query in Hibernate

I'm carrying out a SQL query which looks like:
SELECT thi.*
FROM track_history_items thi
JOIN artists art
ON thi.artist_id = art.id
WHERE thi.type = TrackBroadcast
Group By art.name
ORDER thi.created_at DESC
This works fine when I run it directly on my database from MySql Workbench, but when I run in through Hibernate, I get a No Dialect mapping for JDBC type: -1 error.
Anyone have any ideas what could be causing it?
Probably one or more of the columns in the query is not supported by the mysql dialect... try expanding the * and add one column at a time until you find the offending one.
Then it is just a matter of deciding whether you need that column or not.