Given "my_table":
id other_columns
a ...
a ...
b ...
b ...
b ...
I am trying to get mean, min, max of line count across different IDs. So as a Hive learner, I was trying this:
SELECT avg(line_count), min(line_count), max(line_count)
FROM (SELECT count(*) AS line_count FROM my_table GROUP BY id);
Could someone explain the associated error message of
"cannot recognize input near '<EOF>' '<EOF>' '<EOF>' in subquery source"?
Thanks a lot!
Try giving an alias to the subquery:
SELECT avg(s.line_count), min(s.line_count), max(s.line_count)
FROM (SELECT count(*) AS line_count
FROM my_table GROUP BY id) AS s;
I have a little problem with a piece of SQL code. I have a table Paiements_17_18 and I would like to create a single-line query that calculates:
the total of the Amount field,
the first date of Date_Regulation field,
the last date of Date_Regulation field,
the distinct values of N_Facture field.
All this from a sub request of the style SELECT TOP n FROM ....
I tried this:
SELECT Sum(P.Montant) AS TotalMontant,
First(P.Date_Regulation) AS PremièreDate,
Last(P.Date_Regulation) AS DernièreDate,
First(P.N_Facture) AS PremièreFacture,
Last(P.N_Facture) AS DernièreFacture,
(SELECT Count(N_Facture)
FROM (SELECT DISTINCT N_Facture FROM Paiements_17_18)) AS NombreFactures
FROM (SELECT TOP 5 Paiements_17_18.*
FROM Paiements_17_18
ORDER BY Paiements_17_18.ID_Paiement DESC) AS P;
But I get an error of "P"
(The Microsoft Access database engine cannot find the input table or
query" P" . Make sure it exists and that its name is spelled
correctly)
Can you help me please?
The 2 lines on generating the NombreFacture field is causing the error:
(SELECT Count(N_Facture)
FROM (SELECT DISTINCT N_Facture FROM Paiements_17_18)) AS
NombreFactures
Replaced the two lines. See below.
SELECT Sum(P.Montant) AS TotalMontant,
First(P.Date_Regulation) AS PremièreDate,
Last(P.Date_Regulation) AS DernièreDate,
First(P.N_Facture) AS PremièreFacture,
Last(P.N_Facture) AS DernièreFacture,
(SELECT Count(n.N_Facture_distinct)
FROM (SELECT DISTINCT N_Facture as N_facture_distinct FROM Paiements_17_18 ) AS n)
AS NombreFacture
FROM (SELECT TOP 5 Paiements_17_18.*
FROM Paiements_17_18
ORDER BY Paiements_17_18.ID_Paiement DESC) AS P;
When I run the script below, I got a error message "Cannot perform an aggregate function on an expression containing an aggregate or a subquery" Please provide some advice. Thanks
SELECT
CONVERT(DECIMAL(18,5),SUM(CASE WHEN PATIENT_ACCOUNT_NO IN (
SELECT PATIENT_ACCOUNT_NO
FROM STND_ENCOUNTER
GROUP BY PATIENT_ACCOUNT_NO
HAVING ( COUNT(PATIENT_ACCOUNT_NO) > 1)) THEN 0 ELSE 1 END)) dupPatNo
FROM [DBO].[STND_ENCOUNTER]
I think the error message is pretty clear. You have a sum() function with a subquery in it (albeit within a case, but that doesn't matter).
It seems that you want to choose patients that have more than one encounter, then add 0 if the patients is in the list and 1 if the patient is not. Hmmm. . . sounds like you want to count the number of patients with only one encounter.
Try using this logic instead:
select count(*)
from (select se.*, count(*) over (partition by PATIENT_ACCOUNT_NO) as NumEncounters
from dbo.stnd_encounter se
) se
where NumEncounters = 1;
As a note, the variable you are assigning is called DupPatientNo. This sounds like the number of patients that have duplicates. In that case, the query is:
select count(distinct PATIENT_ACCOUNT_NO)
from (select se.*, count(*) over (partition by PATIENT_ACCOUNT_NO) as NumEncounters
from dbo.stnd_encounter se
) se
where NumEncounters > 1;
(Or use count(*) if you want the number of encounters on duplicate patients.)
If you want to find number of PATIENT_ACCOUNT_NO that does not have any duplicates then use the following
SELECT COUNT(DISTINCT dupPatNo.PATIENT_ACCOUNT_NO)
FROM (
SELECT PATIENT_ACCOUNT_NO
FROM STND_ENCOUNTER
GROUP BY PATIENT_ACCOUNT_NO
HAVING COUNT(PATIENT_ACCOUNT_NO) = 1
) dupPatNo
If you want to find number of PATIENT_ACCOUNT_NO that have atleast one duplicate then use the following
SELECT COUNT(DISTINCT dupPatNo.PATIENT_ACCOUNT_NO)
FROM (
SELECT PATIENT_ACCOUNT_NO
FROM STND_ENCOUNTER
GROUP BY PATIENT_ACCOUNT_NO
HAVING COUNT(PATIENT_ACCOUNT_NO) > 1
) dupPatNo
Use of DISTINCT will make the query not count same item again and again
Though your query looks for first result, its not clear what you want. Hence giving query for both
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
I have the following example code:
create table Details(
name varchar(20),
age int,
weight int,
recordDate Datetime)
--insert data
..query:
SELECT a.name,
a.age,
a.recordDate,
a.weight - (SELECT b.weight
FROM Details
WHERE b.recordDate = dateadd(dd, -1, a.recordDate) as subtable)
FROM Details a
GROUP BY WITH ROLLUP (a.recordDate, a.name, a.age)
I want to see the weight difference between RecordDates for each person and then record total weight different for that person and also for the age group and then grand weight gain/loss. This is not my actual table but just an example.
Problem:
It was complaining about subquery - then I had to use it as table variable: subtable.
Now it is complaining:
Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'as'.
Msg 319, Level 15, State 1, Line 18
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
What am I missing?
Typo:
a.weight - (SELECT b.weight
FROM Details
WHERE b.recordDate = dateadd(dd, -1, a.recordDate)
..."b" is being used as a table alias, but it's not actually defined as one.
Next issue is that your GROUP BY doesn't include a.weight, and there's no aggregate function associated with it. Here's my re-write of your query:
SELECT a.name,
a.age,
a.recordDate,
SUM(a.weight - t.weight) 'weight'
FROM DETAILS a
JOIN (SELECT b.recordDate,
b.weight
FROM DETAILS b) t ON t.recordDate = DATEADD(dd, -1, a.recordDate)
GROUP BY (a.recordDate, a.name, a.age) WITH ROLLUP
Try it like this
SELECT
a.name,
a.age,
a.recordDate,
SUM(a.weight - b.weight) as WeightDiff
FROM Details a
JOIN Details b
ON (b.age = a.age
AND b.name = a.name
AND b.recordDate = dateadd(dd, -1, a.recordDate)
)
GROUP BY a.age, a.name, a.recordDate WITH ROLLUP
Don't use AS keyword. You can just directly write {(select * from blah) a}
OK, so the problem is that WITH ROLLUP isn't really the answer you're looking for. This is for creating subtotals not running totals which is what you're after, so using it will give you the total for different combinations of dates rather than a running total, which is what you're after. In the beginning, the query that you want to just get a total that gives you name, age, date and weight loss compared to yesterday is as follows:
select
a.name
,a.age
,a.recordDate
,(SELECT b.weight from Details b WHERE b.recordDate = dateadd(dd,-1,a.recordDate)) - a.weight as weightLossForToday
from details a
Keep in mind that this query will only work if you have exactly 1 record every day. If you have 2 records for a single day or the entries aren't exactly 1 day apart (ie. they include time), then it won't work. In order to get a running total working, you'll need to follow the approach from a question like this.
That should do the trick.
SELECT a.name,a.age,a.recordDate,a.weight-(SELECT b.weight
FROM Details
WHERE b.recordDate=dateadd(dd,-1,a.recordDate))
FROM Details a
GROUP BY (a.recordDate,a.name,a.age)
WITH ROLLUP