how do i simplify the below sql query

Hi Experts,

how to simplify the below sql query

select t1.name,t1.mid, if(t2.fact_id is null, false, true) as flag from db_meas_comp t1 left join db_comp t2 on t1.fact_id = t2.fact_id and t2.dbview_id = 315 where t1.ID = 65516;

is there any alternative way to check this if condition
if(t2.fact_id is null, false, true) as flag

can some one simplify this query,i have to convert this sql query to hibernate hql query

Thanks,
LVL 2
srikoteshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
Is there an alternative syntax? yes, use a "case expression".

SELECT
      t1.name
    , t1.mid
    , CASE
        WHEN t2.fact_id IS NULL THEN FALSE
        ELSE TRUE
      END AS flag
FROM db_meas_comp t1
LEFT JOIN db_comp t2
      ON t1.fact_id = t2.fact_id
      AND t2.dbview_id = 315
WHERE t1.ID = 65516;

Open in new window


Can it be "optimized"?
Well it is not a complex query, so there isn't any optimization scope in the query code. If you are having performance issues with it then I would suggest looking at the execution plan.

note; I would expect all of these to be indexed:

t1.fact_id
t2.fact_id
t2.dbview_id
0
srikoteshAuthor Commented:
Hi paul,

problem with and condition
if u see the above query after join on condition  immediately i used and condition instead of where

if i use where condition after on condition i am getting single record
if i use and conditioin after on condition i am getting the expected result.

but if i use and condition immediately after on condition i am not able to convert my query to hql


i have modified my query can you please look into it.
i have removed if condition i dont need that condition as well.

select m.name,t1.mid,t2.fact_id from db_meas_comp t1 left join db_comp t2 on t1.fact_id = t2.fact_id and t2.dbview_id = 315 left join measure m on m.measureId=t1.measure_Id where t1.ID = 65516;

here if u see i have use and condition after join
on t1.fact_id = t2.fact_id and t2.dbview_id = 315
0
PortletPaulfreelancerCommented:
absolutely unclear, use examples not just words please.

This is a valid SQL syntax

select *
FROM db_meas_comp t1
LEFT JOIN db_comp t2
      ON t1.fact_id = t2.fact_id
      AND t2.dbview_id = 315
WHERE t1.ID = 65516;

Does this give the the correct result when only using SQL?

if not, please (using a similar example) identify  which FROM & WHERE clause is correct.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

srikoteshAuthor Commented:
the above query is giving expected result.
 this query I have to convert sql query to hibernare hql query.
but in hql after join immediately it will look for where condition

now I want to change my sql query.
0
srikoteshAuthor Commented:
the above query is giving expected result.
 this query I have to convert sql query to hibernare hql query.
but in hql after join immediately it will look for where condition

now I want to change my sql query.
0
srikoteshAuthor Commented:
the above query is giving expected result.
 this query I have to convert sql query to hibernare hql query.
but in hql after join immediately it will look for where condition

now I want to change my sql query.
0
srikoteshAuthor Commented:
the above query is giving expected result.
 this query I have to convert sql query to hibernare hql query.
but in hql after join immediately it will look for where condition

now I want to change my sql query.
0
srikoteshAuthor Commented:
the above query is giving expected result.
 this query I have to convert sql query to hibernare hql query.
but in hql after join immediately it will look for where condition

now I want to change my sql query.
0
srikoteshAuthor Commented:
the above query is giving expected result.
 this query I have to convert sql query to hibernare hql query.
but in hql after join immediately it will look for where condition

now I want to change my sql query.
0
srikoteshAuthor Commented:
the above query is giving expected result.
 this query I have to convert sql query to hibernare hql query.
but in hql after join immediately it will look for where condition

now I want to change my sql query.
0
PortletPaulfreelancerCommented:
You may supply extra join conditions using the HQL with keyword.

from Cat as cat
    left join cat.kittens as kitten
        with kitten.bodyWeight > 10.0
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-from

FROM db_meas_comp t1
LEFT JOIN db_comp t2
      ON t1.fact_id = t2.fact_id
      WITH t2.dbview_id = 315

WHERE t1.ID = 65516
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
K, I will try this and let you know
0
srikoteshAuthor Commented:
Superb
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.