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

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

Related

SQL Why am I getting the invalid identifier error?

I am trying to use columns that I created in this query to create another column.
Let me first my messy query. The query looks like this:
SELECT tb.team, tb.player, tb.type, tb.date, ToChar(Current Date-1, 'DD-MON-YY') as yesterday,
CASE WHEN to_date(tb.date) = yesterday then 1 else 0 end dateindicator,
FROM (
COUNT DISTINCT(*)
FROM TABLE_A, dual
where dateindicator = 1
Group by tb.team
)
What I am trying to do here is:
creating a column with "Yesterday's date"
Using the "Yesterday" column to create another column called dateindicator indicating each row is yesterday's data or not.
then using that dateindicator, I want to count the distinct number of player for each team that has 1 of the dateindicator column.
But I am getting the "invalid identifier" error. I am new to this oracle SQL, and trying to learn here.
You cannot use an Alias in your Select statement.
see here: SQL: Alias Column Name for Use in CASE Statement
you need to use the full toChar(.. in the CASE WHEN.
Also:
Your WHERE-condition (Line 5) doesnt belong there.. it should be:
SELECT DISTINCT .>. FROM .>. WHERE. you have to specify the table first. then you can filter it with where.
If I follow your explanation correctly: for each team, you want to count the number of players whose date column is yesterday.
If so, you can just filter and aggregate:
select team, count(*) as cnt
from mytable
where mydate >= trunc(sysdate) - 1 and mydate < trunc(sysdate)
group by team
This assumes that the dates are stored in column mydate, that is of date datatype.
I am unsure what you mean by counting distinct players; presumably, a given player appears just once per team, so I used count(*). If you really need to, you can change that to count(distinct player).
Finally: if you want to allow teams where no player matches, you can move the filtering logic within the aggregate function:
select team,
sum(case when mydate >= trunc(sysdate) - 1 and mydate < trunc(sysdate) then 1 else 0 end) as cnt
from mytable
group by team

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

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

sql count function with subquery

here is my query
select narr,vocno,count(*)
from KontenLedger
WHERE VOCDT>'2018-07-01'
group by narr,vocno
having count(*)<'3'
actually if i wright as i given above ,the result which calculates two fields ('narr' and 'vocno') if i remove the field ('narr') answer is correct. i need to view the field 'narr' also without counting
Without knowing your database, nor having some limited sample date, nor expected output?
SELECT
vocno,
COUNT(*) AS total,
MAX(narr) AS max_narr
FROM KontenLedger
WHERE vocdt > '2018-07-01'
GROUP BY vocno
HAVING COUNT(*) < 3

Distinct count and group by in HIVE

I am very new to HIVE and have an issue with distinct count and GROUP BY.
I want to calculate maximum temperature from temperature_data table corresponding to those years which have at least 2 entries in the table-
I tried with below query but it is not working
select
SUBSTRING(full_date,7,4) as year,
MAX(temperature) as temperature
from temperature_data
where count(distinct(SUBSTRING(full_date,7,4))) >= 2
GROUP BY SUBSTRING(full_date,7,4);
I am getting an error-
FAILED: SemanticException [Error 10128]: Line 2:0 Not yet supported place for UDAF 'count'
Below is input-
year,zip,temperature
10-01-1990,123112,10
14-02-1991,283901,11
10-03-1990,381920,15
10-01-1991,302918,22
12-02-1990,384902,9
10-01-1991,123112,11
14-02-1990,283901,12
10-03-1991,381920,16
10-01-1990,302918,23
12-02-1991,384902,10
10-01-1993,123112,11
You should use HAVING keyword instead to set a condition on variable you're using for grouping.
Also, you can benefit of using subqueries. See below.
SELECT
year,
MAX(t1.temperature) as temperature
FROM
(select SUBSTRING(full_date,7,4) year, temperature from temperature_data) t1
GROUP BY
year
HAVING
count(t1.year) > 2;
#R.Gold, We can try to simplify the above query without using sub-query as below:
SELECT substring(full_date,7) as year, max(temperature)
FROM your-hive-table
GROUP BY substring(full_date,7)
HAVING COUNT(substring(full_date,7)) >= 2
And, fyi - we can't use aggregate functions with WHERE clause.

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