Having a horrible time with everything today. I am trying to get a list of month, year and then average the order total for each month. I am getting ORA-00907 "missing right parenthesis" and I am not sure why. Again, I very new to this, but the code that I have I referenced from https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm .Thanks in advance.
SELECT EXTRACT (MONTH, YEAR FROM ORDERDATE) "DATE"
AVG (ORDERDATE) "NO. OF ORDERS"
FROM ORDERINFO
GROUP BY EXTRACT (MONTH, YEAR FROM ORDERDATE)
ORDER BY "MONTH" ASC;
The docs you referenced show that only one of the date part specifier can be of the extract function.
For example, extract(month from orderdate) or extract(year from orderdate).
I'm guessing you really want to truncate the orderdate instead. See https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions201.htm.
SELECT trunc(orderdate, 'MONTH') AS "date"
Either:
select
extract(month from orderdate) as year,
extract(year from orderdate) as month,
...
from ...
group by extract(month from orderdate), extract(year from orderdate)
or:
select
to_char(orderdate, 'YYYY-MM') as year_month,
...
from ...
group by to_char(orderdate, 'YYYY-MM')
this will work:
select extract(month from orderdate) "MONTH",
extract(year from orderdate) "YEAR",
from orderdate
group by extract(month from orderdate)
order by extract(month from orderdate) asc;
Related
In CockroachDB, I want to have such this query on a specific month for its every day:
select count(*), sum(amount)
from request
where code = 'code_string'
and created_at >= '2022-07-31T20:30:00Z' and created_at < '2022-08-31T20:30:00Z'
the problem is that I want it on my local date. What should I do?
My goal is:
"month, day, count, sum" as result columns for a month.
UPDATE:
I have found a suitable query for this purpose:
select count(amount), sum(amount), extract(month from created_at) as monthTime, extract(day from created_at) as dayTime
from request
where code = 'code_string' and created_at >= '2022-07-31T20:30:00Z' and created_at < '2022-08-31T20:30:00Z'
group by dayTime, monthTime
Thanks to #histocrat for easier answer :) by replacing
extract(month from created_at) as monthTime, extract(day from created_at) as dayTime
by this:
date_part('month', created_at) as monthTime, date_part('day', created_at) as dayTime
To group results by both month and day, you can use the date_part function.
select month, day, count(*), sum(things)
from request
where code = 'code_string'
group by date_part('month', created_at) as month, date_part('day', created_at) as day;
Depending on what type created_at is, you may need to cast or convert it first (for example, group by date_part('month', created_at::timestamptz)).
I have a table called 'daily_prices' where I have 'sale_date', 'last_sale_price', 'symbol' as columns.
I need to calculate how many times 'last_sale_price' has gone up compared to previous day's 'last_sale_price' in 10 weeks.
Currently I have my query like this for 2 weeks:
select count(*) as "timesUp", sum(last_sale_price-prev_price) as "dollarsUp", 'wk1' as "week"
from
(
select last_sale_price, LAG(last_sale_price, 1) OVER (ORDER BY sale_date) as prev_price
from daily_prices
where sale_date <= CAST('2020-09-18' AS DATE) AND sale_date >= CAST('2020-09-14' AS DATE)
and symbol='AAPL'
) nest
where last_sale_price > prev_price
UNION
select count(*) as "timesUp", sum(last_sale_price-prev_price) as "dollarsUp", 'wk2' as "week"
from
(
select last_sale_price, LAG(last_sale_price, 1) OVER (ORDER BY sale_date) as prev_price
from daily_prices
where sale_date <= CAST('2020-09-11' AS DATE) AND sale_date >= CAST('2020-09-07' AS DATE)
and symbol='AAPL'
) nest
where last_sale_price > prev_price
I'm using 'UNION' to combine the weekly data. But as the number of weeks increase the query is going to be huge.
Is there a simpler way to write this query?
Any help is much appreciated. Thanks in advance.
you can extract week from sale_date. then apply group by on the upper query
select EXTRACT(year from sale_date) YEAR, EXTRACT('week' FROM sale_date) week, count(*) as "timesUp", sum(last_sale_price-prev_price) as "dollarsUp"
from (
select
sale_date,
last_sale_price,
LAG(last_sale_price, 1) OVER (ORDER BY sale_date) as prev_price
from daily_prices
where symbol='AAPL'
)
where last_sale_price > prev_price
group by EXTRACT(year from sale_date), EXTRACT('week' FROM sale_date)
to extract only weekdays you can add this filter
EXTRACT(dow FROM sale_date) in (1,2,3,4,5)
PS: make sure that monday is first day of the week. In some countries sunday is the first day of the week
You can filter on the last 8 weeks in the where clause, then group by week and do conditional aggregation:
select extract(year from sale_date) yyyy, extract(week from saledate) ww,
sum(last_sale_price - lag_last_sale_price) filter(where lag_last_sale_price > last_sale_price) sum_dollars_up,
count(*) filter(where lag_last_sale_price > last_sale_price) cnt_dollars_up
from (
select dp.*,
lag(last_sale_price) over(partition by extract(year from sale_date), extract(week from saledate) order by sale_date) lag_last_sale_price
from daily_price
where symbol = 'AAPL'
and sale_date >= date_trunc('week', current_date) - '8 week'::interval
) dp
group by 1, 2
Notes:
I am asssuming that you don't want to compare the first price of a week to the last price of the previous week; if you do, then just remove the partition by clause from the over() clause of lag()
this dynamically computes the date as of 8 (entire) weeks ago
if there is no price increase during a whole week, the query still gives you a row, with 0 as sum_dollars_up and cnt_dollars_up
I have a table that looks like this
I'm wanting to sum the sales and only show sales from 2010. Using DATE_PART works except when I try to use there WHERE clause
SELECT date_part('year', date) AS year, SUM(weekly_sales) as yearly_sales
FROM sales
GROUP BY year
SELECT date_part('year', date) AS year, SUM(weekly_sales) as yearly_sales
FROM sales
GROUP BY year
WHERE year = 2010;
Is it because the date_part is a double precision?
The WHERE clause goes before the GROUP BY clause, and it cannot access column aliases defined in the SELECT clause.
You could do:
SELECT date_part('year', date) AS year, SUM(weekly_sales) as yearly_sales
FROM sales
WHERE date_part('year', date) = 2010
GROUP BY year
Or, more efficiently:
SELECT 2010 AS year, SUM(weekly_sales) as yearly_sales
FROM sales
WHERE date >= date '2010-01-01' and date < date '2011-01-01'
I am new to SQL and Learning on my own. I was wondering if someone can help guiding me to a write SQL.
I have the below data:
I am using the following query:
SELECT
TIMESTAMP
DATEPART(Year, TIMESTAMP) Year,
DATEPART(Month, TIMESTAMP) Month,
COUNT(*) [Total Rows]
FROM
stage.ACTIVITY_ACCUMULATOR_archive
WHERE
TIMESTAMP BETWEEN '01-Jan-2014' AND '30-June-2014'
GROUP BY
DATEPART(Year, TIMESTAMP), DATEPART(Month, TIMESTAMP)
ORDER BY
Year, Month
What I am trying to achieve is to display the Timestamp with year and month between the certain date and group them by month and year.
I get an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Year'
This should work.There was a extra timestamp column in select list.
SELECT
DATEPART(Year, TIMESTAMP) Year,
DATEPART(Month, TIMESTAMP) Month,
COUNT(*) [Total Rows]
FROM
stage.ACTIVITY_ACCUMULATOR_archive
WHERE
TIMESTAMP BETWEEN '01-Jan-2014' AND '30-June-2014'
GROUP BY
DATEPART(Year, TIMESTAMP), DATEPART(Month, TIMESTAMP)
ORDER BY
Year, Month
I'm trying to pull a few SUMs, but I'm getting stuck on how to narrow it down to a specific year.
I have the following code...
SELECT SITE_ID,
Extract(YEAR FROM DATE_ORDERED) YEAR,
Extract(MONTH FROM DATE_ORDERED) MONTH,
SUM(TOTAL_PRICE),
SUM(TOTAL_PRICE),
SUM(TOTAL_SAVINGS)
FROM DB.ACTUAL_SAVINGS_MVIEW
WHERE SITE_ID = 561
GROUP BY SITE_ID,
Extract(YEAR FROM DATE_ORDERED),
Extract(MONTH FROM DATE_ORDERED)
ORDER BY YEAR DESC,
MONTH DESC
This returns all available years, when I'm only looking for 2016.
Any and all help would be greatly appreciated!
How about adding it to your where clause:
SELECT SITE_ID,
Extract(YEAR FROM DATE_ORDERED) YEAR,
Extract(MONTH FROM DATE_ORDERED) MONTH,
SUM(TOTAL_PRICE),
SUM(TOTAL_PRICE),
SUM(TOTAL_SAVINGS)
FROM DB.ACTUAL_SAVINGS_MVIEW
WHERE SITE_ID = 561
AND Extract(YEAR FROM DATE_ORDERED) = 2016
GROUP BY SITE_ID,
Extract(YEAR FROM DATE_ORDERED),
Extract(MONTH FROM DATE_ORDERED)
ORDER BY YEAR DESC,
MONTH DESC