How to apply avg function on top of select query in Oracle? - sql

Select (SYSDATE - CREATED_DATE_emplogin) as newinfo, USER_ID from emp;
Based on the result of the above query I want to take the average on newinfo column
I am very much new to SQL I don't understand what to do next
We are oracle db.

If it is average per user_id, then apply avg to date difference and include group by clause (which contains all non-aggregated columns):
Select avg(SYSDATE - CREATED_DATE_emplogin) as newinfo,
USER_ID
from emp
group by user_id

Related

How to aggregate in SQL by having clause

The below is the query I'm using, I would like to add two more columns in the select statement which I have and noted them in the group by clause but the total results are different. The below script gives me the correct total count but I want to see the totals also for a column called transaction, and a column called employee. The total count should still be the same.
SELECT SUB.YEAR, SUM(SUB.TOTAL_COUNT), SUM(SUB.TOTAL_SPENT)
FROM (
SELECT YEAR, COUNT(*) AS TOTAL_COUNT, SUM($SPENTX) AS TOTAL_SPENT, CUSTOMERID
FROM TABLE A
WHERE YEAR = 2017
GROUP BY YEAR, CUSTOMERID
HAVING SUM($SPENTX)<=1000
) SUB
GROUP BY SUB.YEAR

Select query to show timestamp (HH:MM:SS) colomn with group by HH (ORACLE QUERY)

I am trying to create select query in oracle to get following result from my_table
table contents timestamp_coloumn and count coloumn, records in timestamp_colomn are getting inserted are by minute bases.
Tried query:- something like this
select to_char(timestamp_coloumn ,'HH24:MI:SS') as TS , count as count
from my_table
group by to_char(timestamp_coloumn ,'HH24');
Error:-
ORA-00979: not a GROUP BY expression
if ,I match select and and group by statement like following it works but, i couldn't achieve my expected result (i dont know if is right to query like that)
select to_char(timestamp_coloumn ,'HH24:MI:SS') as TS , count as count
from my_table
group by to_char(timestamp_coloumn ,'HH24:MI:SS');
Expected result (Hourly timestamp and count is grouped and summed for all records present in that hour):-
timestamp_coloumn count
--------------------------
07:01:23 4
08:01:36 3
09:01:44 6
10:01:10 5
Please help me with this query
You can use MIN():
select min(to_char(timestamp_coloumn ,'HH24:MI:SS')) as TS, count(*)
from my_table
group by to_char(timestamp_coloumn ,'HH24');
Or make the two expressions match:
select to_char(timestamp_coloumn ,'HH24') as TS, count(*)
from my_table
group by to_char(timestamp_coloumn ,'HH24');

how to sum result of count in sql query from one table and one column

I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1

Efficient query for the first result in groups (postgresql 9)

I have a table with 200000 rows and columns: name and date. The dates and names may have repeated values. I would like get the first 300 unique names for the dates sorted in an ascending order and have this run fast as my table may have a million rows.
I am using postgresql 9.
SELECT name, date
FROM
(
SELECT DISTINCT ON (name) name, date
FROM table
ORDER BY name, date
) AS id_date
ORDER BY date
LIMIT 300;
The last query of #jachguate will miss names having two dates on the same date, however this one doesn't.
The query takes about 100 ms in a non-optimized postgresql 9.1 with about 100.000 entries, thus it may not scale to millions of entries.
An upgrade to postgresql 9.2 may help, as according to the release notes there are many performance improvements
use a CTE:
with unique_date_name as (
select date, name, count(*) rcount
from table
group by date, name
having count(*) = 1
)
select name, date
from unique_date_name
order by date limit 300;
Edit
From the comments, this result in poor performance, so try this other:
select date, name, count(*) rcount
from table
group by date, name
having count(*) = 1
order by date limit 300;
or, transforming the original query into a nested subquery in FROM instead of a CTE:
select name, date
from (
select date, name, count(*) rcount
from table
group by date, name
having count(*) = 1
) unique_date_name
order by date limit 300;
unfortunately I don't have a postgreSQL at hand to check if it works, but the optimizer will make a better work.
A Index for (date, name) is a must for optimal performance.

Group by one column and select more than one column Sql query

I need to group by more than one columns but in special case:
I have a table of (Payment_Type,Year,TotalMoney)
I need to get sum of total grouping by payment_type(cash or credit) only and I need to select year in my query how can I do it? my query is:
select Payment_Type,Year,SUM(TotalMoney)
from table
where 1=1
group by Payment_Type,Year
I get an error message as:
Year is not contained in either an aggregate function or the GROUP BY clause
select Payment_Type,Year(YourDateColumn),SUM(TotalMoney)
from table
group by Payment_Type,Year(YourDateColumn)
if your column is named year then
select Payment_Type,[Year],SUM(TotalMoney)
from table
group by Payment_Type,[Year]