How to solve this SQL query identifier error in alias name? - sql

I am trying to get count of event plan in table, but showing identifier error in count alias name event plan count.
IN ORACLE DATABASE(SQL)
This is my table
SELECT
EventRequest.eventNo,
EventRequest.dateHeld,
count(*) AS eventPlanCount
FROM EventRequest,eventplan
where EventRequest.eventNo = Eventplan.eventNo and
Eventplan.workDate BETWEEN '01-DEC-2018' AND '31-DEC-2018'
GROUP BY EventRequest.eventNo
HAVING eventPlanCount > 1;
I am getting this error:
Error starting at line : 1 in command -
SELECT
EventRequest.eventNo,
EventRequest.dateHeld,
count(*) AS eventPlanCount
FROM EventRequest
where EventRequest.eventNo = Eventplan.eventNo and
Eventplan.workDate BETWEEN '01-DEC-2018' AND '31-DEC-2018'
GROUP BY EventRequest.eventNo
HAVING eventPlanCount > 1
Error at Command Line : 9 Column : 8
Error report -
SQL Error: ORA-00904: "EVENTPLANCOUNT": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
i am new to this please help me!
THANKS IN ADVANCE!!..

Here:
HAVING eventPlanCount > 1;
You can't use the alias here. Instead, you have to repeat the expression:
HAVING COUNT(*) > 1
Your query has other issues, namely:
column dateHeld must be either repeated in the group by clause, or belong to an aggrgate function in the select clause
you would rather use legitimate date literals rather than relying on implicit conversion of strings to date (also, half-open intervals are safer than between)
you should use explicit joins instead of old-school implicit joins
So:
select r.eventno, r.dateheld, count(*) as eventplancount
from eventrequest r
inner join eventplan p on p.eventno = r.eventno
where p.workdate >= date '2018-01-01' and p.workdate < date '2019-01-01'
group by r.eventno, r.dateheld
having count(*) > 1;

A few things about your query:
You should use modern join syntax (SQL-92) instead of old joins. They are less error prone, easier to read, more powerful, and easier to debug. Fixed it in the query.
It's easier to use table aliases. It help readability and it makes it easier to debug. I defined r an p.
Columns not present in the GROUP BY clause should be aggregated. I added r.dateHeld to the GROUP BY clause.
It's better to ensure you are using correct date comparisons. I forced Oracle to convert the strings to dates. Sometimes Oracle [wrongly] normalizes everything to VARCHAR2s when the operands are of a different type.
In the HAVING clause I used count(*).
With all of that your query can look like:
SELECT
r.eventNo,
r.dateHeld,
count(*) AS eventPlanCount
FROM EventRequest r
JOIN eventplan p ON r.eventNo = p.eventNo
WHERE p.workDate BETWEEN date '2018-12-01' AND date '2018-12-31'
GROUP BY r.eventNo, r.dateHeld
HAVING count(*) > 1

Related

SQL left join with grouped table

received some great help on a previous question on this site.
Im new to sql and trying to get my head around this.
From table Tourenstatistik I will get unique rows of data with a date & id.
I am then looking at another table Vtsbreak and summing up the duration for each id on separate dates as there can be more than one entry for the same id on the same date.
I then want to return that sum in a column called duration for the unique id & its date.
I also need to specify the start and finish dates for my query.
I have found something similar but I cant get it to work
coming up with error ORA-00933: SQL command not properly ended.
Any suggestions please?
Select
Tourenstatistik.DATUM "Date",
Tourenstatistik.MITARBEITER "ID",
VtsPause.Duration
From
Tourenstatistik
Where Tourenstatistik.DATUM >= TO_DATE('2017/05/15', 'yyyy/mm/dd') AND Tourenstatistik.DATUM <= TO_DATE('2017/05/16','yyyy/mm/dd')
Left Join
(Select
Vtsbreak.MITARBEITER_NR "ID",
sum(Vtsbreak.DAUER) "Duration",
Vtsbreak.datum "Date"
From
Vtsbreak
Where DATUM >= TO_DATE('2017/04/01', 'yyyy/mm/dd') AND DATUM <= TO_DATE('2017/05/27','yyyy/mm/dd')
group by MITARBEITER_NR, datum;) as VtsPause
On (Tourenstatistik.MITARBEITER = VtsPause.MITARBEITER_NR and Tourenstatistik.DATUM = VtsPause.DATUM)
order by Tourenstatistik.DATUM, Tourenstatistik.MITARBEITER
You are giving the columns aliases in the subquery. You then need to use those aliases. Also, the WHERE clause is misplaced.
Select ts.DATUM as "Date", ts.MITARBEITER as "ID", b."Duration"
From Tourenstatistik ts Left Join
(Select b.MITARBEITER_NR, b.datum, sum(b.DAUER) as "Duration"
From Vtsbreak b
Where b.DATUM >= DATE '2017-04-01' AND
b.DATUM <= DATE '2017-05-27'
group by MITARBEITER_NR, datum
) b
On ts.MITARBEITER = b.MITARBEITER_NR and ts.DATUM = b.DATUM
Where ts.DATUM >= DATE '2017-05-15' and
ts.DATUM <= DATE '2017-05-16'
order by ts.DATUM, ts.MITARBEITER;
You have numerous things wrong with the query, plus a few suggestions:
You had a semicolon in the middle of the query.
You gave columns aliases in the subquery, and then used the original column names.
You had a misplaced WHERE clause.
Recommendations:
Use table abbreviations as table aliases, so the query is easier to write and to read.
Use the date keyword for defining date literals.
Try taking the "as" off of "as VtsPause". Also, get rid of the semicolon after "datum;". I'd also not give column aliases in double quotes on the inline query, especially since you refer to the actual column name in your ON clause.
Also, your first WHERE clause is out of place.
Perhaps this will work:
Select
Tourenstatistik.DATUM
Tourenstatistik.MITARBEITER,
VtsPause.Duration
From
Tourenstatistik
Left Join
(Select
Vtsbreak.MITARBEITER_NR,
sum(Vtsbreak.DAUER) duration,
Vtsbreak.datum
From
Vtsbreak
Where DATUM >= TO_DATE('2017/04/01', 'yyyy/mm/dd')
AND DATUM <= TO_DATE('2017/05/27','yyyy/mm/dd')
group by MITARBEITER_NR, datum) VtsPause
On (Tourenstatistik.MITARBEITER = VtsPause.MITARBEITER_NR
and Tourenstatistik.DATUM = VtsPause.DATUM)
Where Tourenstatistik.DATUM >= TO_DATE('2017/05/15', 'yyyy/mm/dd')
AND Tourenstatistik.DATUM <= TO_DATE('2017/05/16','yyyy/mm/dd')
order by Tourenstatistik.DATUM, Tourenstatistik.MITARBEITER

ORA-00907: missing right parenthesis using count with expression inside it

SELECT Hotel_Name, COUNT(H_CHECK.Hotel_checkIn >= 'JUL-1-2016' AND H_CHECK.Hotel_checkIn <= 'JUL-31-2016') FROM HOTEL, H_CHECK
GROUP BY Hotel_Name
ORA-00907: missing right parenthesis
I have tried putting Parenthesis in many ways, but I couldn't find the solution. I'm using Oracle Application Express 11G.
This is the query:
Display the hotel name that has more than 2 customers checked in on July 2016.
Once you fix your immediate syntax problem, you need proper JOIN syntax.
One way to fix the problem is simply to move the conditions to a WHERE clause, resulting in a query like this:
SELECT Hotel_Name, COUNT(hc.hotel_id)
FROM HOTEL h LEFT JOIN
H_CHECK hc
ON h.hotel_id = hc.hotel_id -- I don't know what the right join condition is
WHERE hc.Hotel_checkIn >= DATE '2016-07-01' AND
hc.Hotel_checkIn <= DATE '2016-07-31'
GROUP BY Hotel_Name;
You cannot count based on condition in Select statement of your sql query.
COUNT (
H_CHECK.Hotel_checkIn >= 'JUL-1-2016'
AND H_CHECK.Hotel_checkIn <= 'JUL-31-2016')
This is wrong. You can do it like>
SELECT Hotel_Name,
COUNT (1)
FROM HOTEL
join H_CHECK
ON H_CHECK.Hotel_checkIn >= 'JUL-1-2016'
AND H_CHECK.Hotel_checkIn <= 'JUL-31-2016'
GROUP BY Hotel_Name
having count(1) > 2;
You're missing the CASE ... END from your conditions inside your count. You're after something like:
SELECT Hotel_Name,
COUNT(case when H_CHECK.Hotel_checkIn >= 'JUL-1-2016'
AND H_CHECK.Hotel_checkIn <= 'JUL-31-2016'
then 1
end)
FROM HOTEL, H_CHECK
GROUP BY Hotel_Name;
However, there are a number of concerns I have regarding your query:
if your hotel_checkin column is of DATE datatype, then you should be comparing it to DATEs not strings. I.e. H_CHECK.Hotel_checkIn >= to_date('07-01-2016', 'mm-dd-yyyy') - this way, you avoid relying on implicit conversion of the string into a date, which relies on your NLS_DATE_FORMAT parameter setting. This could be changed and may cause your query to fail.
FROM HOTEL, H_CHECK Don't use the old-style method of joining; instead, use the ANSI style method: FROM hotel cross join h_check
Did you really mean the join to be a cross join or did you forget to add the join conditions?
You should alias the hotel_name column to aid maintainability.
You should also give your count column a name.
Since you're only counting rows with a checkin between 1st and 31st July 2016, you should move this condition into the where clause (as XING has shown in their answer) *unless* you need to also show hotels that don't have any checkins within that time period.
Your condition assumes that there are no time elements to the hotel_checkin column - ie. everything is set to midnight. If, however, you could have a date with a time, bear in mind that your count will ignore all rows with a checkin date of 31st July 2016 that are after midnight. In which case, your upper bound needs to change to: H_CHECK.Hotel_checkIn < to_date('08-01-2016', 'mm-dd-yyyy')

ORA-00923 error: FROM keyword not found where expected

When calculating retention on Oracle DB, I wrote this code:
select
sessions.sessionDate ,
count(distinct sessions.visitorIdd) as active_users,
count(distinct futureactivity.visitorIdd) as retained_users,
count(distinct futureactivity.visitorIdd) / count(distinct sessions.visitorIdd)::float as retention
FROM sessions
left join sessions futureactivity on
sessions.visitorIdd=futureactivity.visitorIdd
and sessions.sessionDate = futureactivity.sessionDate - interval '3' day
group by 3;
but I always get the error: "ORA-00923: mot-clé FROM absent à l'emplacement prévu" (ORA-00923 FROM keyword not found where expected)
Can you help me guys?
Oracle does not recognize :: syntax of Postgres, so it complains of the missing FROM keyword not being found where expected.
Use a cast instead:
count(distinct futureactivity.visitorIdd) / cast(count(distinct sessions.visitorIdd) as float) as retention
Here is a more "Oracle" way of writing the query:
select s.sessionDate ,
count(distinct s.visitorIdd) as active_users,
count(distinct fs.visitorIdd) as retained_users,
count(distinct fs.visitorIdd) / count(distinct s.visitorIdd) as retention
from sessions s left join
sessions fs
on s.visitorIdd = fs.visitorIdd and
s.sessionDate = fs.sessionDate - interval '3' day
group by s.sessionDate
order by s.sessionDate;
Notes:
Oracle does not require conversion with dividing integers.
The group by should contain the column name, and it is actually "1", not "3".
Shorter table aliases make the query easier to write and to read.
You'll probably want an order by, because the results will be an in indeterminate order.
There is probably a better way to write this query using window functions.

SQL getting invalid relational operator and I want this to be used in a subquery

select
size_desc
from
ODS_INPT_ITEM_MASTER
group BY
CREATE_DATE_TIME
having
max(CREATE_DATE_TIME)
The having clause needs to have some operator that lets SQL know how to filter like so :
having max(CREATE_DATE_TIME) > '2016-07-01'
Edit :
Based on your comment ... this is how you would get the max date for each size_desc
select size_desc, max(CREATE_DATE_TIME) as MaxDate
from ODS_INPT_ITEM_MASTERsize_desc
group by size_desc

how make query about some of a field are equal?

I'm trying the code below
SELECT Sum(Price) FROM Faktor WHERE date=date
but it shows total of all price. I want to show the sum of per day like:
date ----- sum
2015/5/1 12345
2015/5/2 54124
I have tried below code too but get error:
SELECT date,Sum(Price) FROM Faktor WHERE date=date
[Err] 42000 - [SQL Server]Column 'Faktor.date' is invalid in the
select list because it is not contained in either an aggregate
function or the GROUP BY clause.
Try
SELECT [date],SUM([Price])
FROM Faktor
GROUP BY [date]
Not sure why you'd use date=date, so I left it out.
Just as the error message tells you, you need to use a group by clause and the date column needs to be in it.
SELECT date, SUM(Price)
FROM Faktor
WHERE date=date -- this looks a bit odd... maybe you want a range of dates or something?
GROUP BY date
With SQL Server all non-aggregated columns from the select statement needs to be grouped (unlike some versions of MySQL for instance).
SELECT
date
,SUM(Price)
FROM
Faktor
WHERE
--add date rules here if you have date criteria i.e. date >= 'someDate'
GROUP BY
date