Minimum temp and maximum temp display using SQL script - sql

I have a table in which minimum and maximum temperature is stored per order no and date. I want to pick the minimum temperature and maximum temperature for each day. This should be done using SQL script.

You have to use group by clause, and aggregate functions min, max as below:
select date, min(temperature), max(temperature)
from table
group by date
It will work if your date have only year, month and day (01/11/2012).

In oracle:
SELECT TO_CHAR(DATE_VAL,'DD-MM-YYYY'), MAX(temperature),
MIN(temperature) FROM table_name group by TO_CHAR(DATE_VAL,'DD-MM-YYYY');
In MySQl:
SELECT DATE_FORMAT(DATE_VAL, '%d-%m-%Y'), max(temperature),
min(temperature) from table_name group by DATE_FORMAT(DATE_VAL, '%d-%m-%Y');

Related

Select different min dates BigQuery

I have a table with different values in BigQuery and I want to select the min or the max of these values.
You can use min/max with group by to get min or max date per ID group
SELECT ID, MIN(DATE) AS MIN_DATE, MAX(DATE) AS MAX_DATE
FROM TABLE
GROUP BY ID

How do I count the values in a column by each month?

In my data frame, I am trying to count number of values of a column per month, given by the time column in SQL. I want the output to have a count of the number of values in a column for each month. I know I can use the where function to count for one month and I could do this for all 12 months, but was wondering if there was a more efficient way.
Here's the inefficient example:
SELECT
count(column1) AS Total
FROM DataFrame
WHERE MONTH(date) == 1
GROUP by column1 ORDER by count(column1) DESC LIMIT 10
I am trying to count distinct values of a column per month, given by the time column in SQL. I
You seem to want GROUP BY:
SELECT MONTH(date) as mon, approx_count_distinct(column1, 0.1) AS Total
FROM DataFrame
GROUP by MONTH(date)
ORDER by Total DESC
LIMIT 10;
Note that using MONTH() without YEAR() or a filter on the date is highly suspicious.

Retrieving Date function in sql

I have an employee table with the hire_date column.
I am stuck with one query related to the date function, where I have used data type 'DATE' to insert date of hiring and using DATE_FORMAT fun. to retrieve no. of employees hired in every month, but in SQL-server it is not supporting the date_format function.
I'm using SQL -server
Query: - list of the no.of employee hired every month in ascending order.
select date_format(hire_date,'%b') month, count(*)
from employee
group by DATE_FORMAT(hire_date,'%b')
order by month
date_format(hire_date,'%b') in MySQL will return abbreviated monthname. However, you can still have this functionality by combining MONTHNAME with LEFT in SQL Server.
select LEFT(DATENAME(MONTH,hire_date),3) month, count(*)
from employee
group by LEFT(DATENAME(MONTH,hire_date),3)
order by month
select Month(Hire_Date),count('x') 'count' from employee
group by Month(Hire_Date)
order by Month(Hire_Date) asc
Instead, you can directly use:
select MONTH(hire_date), count(*)
from employee
group by MONTH(hire_date)
order by MONTH(hire_date)
or
select hire_date.MONTH, count(*)
from employee
group by hire_date.MONTH
order by hire_date.MONTH

Oracle SQL Accumulated value for the date

I have a table with 3 columns: id, date and amount, but I would like to get accumulated SUM for each date (Last column).
Do you have an easy solution how to add this column?
I am trying with this:
SELECT date, sum(amount) as accumulated
FROM table group by date
WHERE max(date);
Should I user OVER() for this?
Use a window function to the total for each day:
SELECT date,
amount,
sum(amount) over (partition by date) as accumulated
FROM the_table;
However this will only work, if your dates all have the same time part (in Oracle a DATE column also contains a time). To make sure you ignore the time part, use trunc() to make sure all time parts are normalized to 00:00:00
SELECT date,
amount,
sum(amount) over (partition by trunc(date)) as accumulated
FROM the_table;
Use This:
SELECT T.ID, T.DATE, T.AMOUNT, (SELECT SUM(S.AMOUNT) FROM TABLE S WHERE S.DATE=T.DATE) ACCUMULATED
from
table T
This will give you the records from the table with a sum for all records for the date.

SQL - select max of datetime by day

Using the below example, I want to get a count of records grouped by Person and Product, for each day.
The example on the left is the data in the SQL Server 2008 database. The data on the right is my desired query result.
I could just substring off the time portion of the date values, then just do a Group By... but ideally the Max(date) would contain the full timestamp...
Is this what you want?
select t.person, t.product, count(*) as cnt, max(t.date) as date
from table t
group by t.person, t.product, cast(t.date as date);