How to convert sql subselect query to hql? - sql

hi i want to convert my sql subselect query to hql. my sql query is shown below
select distinct sum(goal_score) from(
select user_id,max(goal_score) goal_score from sc_student_final_results ssfr
where month=8 and year=2013 group by goal_id,user_id) ssfr group by ssfr.user_id
for the abo native sql command i have converted to hql as shown below
select distinct sum(goalScore) FROM (select userId,max(goalScore) goalScore FROM
StudentFinalResults sr where year=:year and month=:month and locationId =:siteid
group by userId,goalId) sr group by sr.userId
but i am getting the error
org.hibernate.hql.PARSER - line 1:37: unexpected token: (
org.hibernate.hql.PARSER - line 1:52: unexpected token: max
unexpected token: ( near line 1, column 37 [select distinct sum(goalScore)
FROM (select userId,max(goalScore) goalScore FROM
net.sankhya.scorecards.model.StudentFinalResults sr where year=:year and
month=:month and locationId =:siteid group by userId,goalId) sr group by sr.userId]

Please update your query with this, it might work out!
select distinct sum(sr3.goalScore) FROM (select userId,max(goalScore) FROM
StudentFinalResults sr2 where year=:year and month=:month and locationId =:siteid
group by userId,goalId) sr3 group by sr3.userId

Related

How to declare and use a variable in PostgreSQL?

I'm new to PostgreSQL, so maybe my question is unconvinced. But what I want is to put the result of one query into a variable and then used it again in another query. I run my query inside pgAdmin4, Below is my query:
Also I tried those solution1, solution2 without achieving my goal.
WITH vars AS (
(select count(*) from employee) AS vars_id
)
select
*
from
employee
where
id=vars.vars_id;
The error is:
ERROR: syntax error at or near "AS"
LINE 2: (select count(*) from employee) AS vars_id
^
SQL state: 42601
Character: 49
The result of a CTE is a table expression. You can't just refer to it as a scalar, you need to query from it:
WITH vars AS (
SELECT COUNT(*) AS vars_id FROM employee
)
SELECT *
FROM employee e
JOIN vars ON e.id = vars.vars_id

How to use distinct in sub select

Hello I have trouble to get distinct count of column.
The result I get right now is total of all not different ones.
This select is as a sub select
select count (distinct tss.Deliverypostadre) from Shipment tss
where tss.DATAAREAID='wh' and tss.PARTITION=1234) as client_count
And if I just go using distinct
distinct s.Deliverypostadre as Client_count
Msg 156, Level 15, State 1, Line 105 Incorrect syntax near the keyword
'distinct'.
can't figure out how to make it work.
Please Check below query
Select ..in (select count(distinct tss.Deliverypostadre) as client_count from Shipment tss
where tss.DATAAREAID='wh' and tss.PARTITION=1234)
Select Code from [dbo].[Codes] where codeId in (select count (distinct Code) as code from [dbo].[Codes])
This should work:
select . . .
(select count(distinct tss.Deliverypostadre)
from Shipment tss
where tss.DATAAREAID = 'wh' and tss.PARTITION = 1234
) as client_count
from . . .
Some databases might have a problem with the space between count and (. but the syntax basically looks correct.

how to get 6 months data

I am running below query in impala but getting error related to unix_timestamp
AnalysisException: No matching function with signature: unix_timestamp(TIMESTAMP, STRING).
Same query while running in hue, I am getting error related to add_months
Error while compiling statement: FAILED: ParseException line 8:28 cannot recognize input near 'select' 'add_months' '(' in expression specification
If I run a below separate add_months query, I am able to get the result:
select add_months(max(period), -6) from ph_com_b_gbl_sales.it_fact_sales
query :
select
product.brand as product_group,
sum(equivalent_units) as equivalent_units ,
sales.data_source,
from_unixtime(unix_timestamp(period,'yyyy-MM-dd'),'yyyy-MM')
from (select sales_line, territory_mapping_key from ph_com_b_gbl_sales.it_territory_mapping where sales_line = 'Linea Cardiometabolica') territory_mapping
inner join
(select territory_mapping_key,equivalent_units, sales_office, product_key, data_source, period from ph_com_b_gbl_sales.it_fact_sales where sales_office = 'ITOS' and data_source like '%DPC%' or data_source like'%Ex Factory%' or data_source like '%Sell-out%') sales
on sales.territory_mapping_key = territory_mapping.territory_mapping_key
inner join
(select brand,product_key from ph_com_b_gbl_sales.it_dim_product where brand like '%ENTRESTO%') product
on sales.product_key = product.product_key
where sales.period between (select add_months(max(period), -6) from ph_com_b_gbl_sales.it_fact_sales) and (select max(period) from ph_com_b_gbl_sales.it_fact_sales)
group by sales.data_source,sales.period,product.brand
My objective is to fetch the 6months data and that wont be the last 6 months data, I j just have to provide the range of 6 months

Why does Hive not like this query?

Ratingshive table is dynamically partitioned on Genre and the table contains movie_titles and rating of movies
select 100 * stars / total
from (select count(rating) as stars
from ratingshive
where rating = 5) t1,
(select count(1) as total
from ratingshive) t2
When I run the above query in Hive, I get this error -
FAILED: ParseException line 1:100 missing EOF at ',' near 't1'
Hive does not like your query most probably because of old version. But you do not need to query the same ratingshive table two times and then cross join scalar results. Use aggregation with case:
select 100 * count(case when rating=5 then 1 end)/count(*) as rating_percent
from ratingshive;

DISTINCT function in ms access

I am running into issues with DISTINCT in MS ACCESS.
Here is what I would like to run and which works in MySQL:
SELECT `orig`.`SONG TITLE`,`orig`.`PUBLISHER`
FROM `Sheet1` AS `orig`
INNER JOIN `Sale type` AS `Sale`
ON orig.`CFG DESCRIPTION`=Sale.`CFG DESC`
GROUP BY orig.`SONG TITLE` , orig.`PUBLISHER`
HAVING COUNT(DISTINCT `Sale type`.`CFG DESC`) > 1
;
The error message I get is:
Syntax error (missing operator) in query expression 'COUNT(DISTINCT Sale type.CFG DESC) > 1'.
Since SELECT DISTINCT is supported in Access, but COUNT(DISTINCT is not, you can use a subquery for SELECT DISTINCT and base GROUP BY, COUNT and HAVING on the subquery.
SELECT sub.`SONG TITLE`, sub.PUBLISHER
FROM
(
SELECT DISTINCT
orig.`SONG TITLE`, orig.PUBLISHER, Sale.`CFG DESC`
FROM
Sheet1 AS orig
INNER JOIN `Sale type` AS Sale
ON orig.`CFG DESCRIPTION`=Sale.`CFG DESC`
) AS sub
GROUP BY sub.`SONG TITLE`, sub.PUBLISHER
HAVING COUNT(sub.`CFG DESC`) > 1;
If the query does work in MySQL, then a passthrough query will allow it to continue to work. See screenshot for area in MS Access for passthrough query..
From w3schools.com:
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but
not with Microsoft Access.