I want the query results to be grouped by day of the week .. like into 7 groups Monday, Tuesday, Wednesday... etc one for each
the column in the table is a timestamp field like 12-Mar-2011 .....
so I don't care which dates exist in the table I just need to get 7 groupings and if there is a count column which doesnt exist for like thursday it should return 0 and for wednesday the count should the total counts for all wednesday and like wise for each day of the week.
How do I mplement this query with HQL/criteria?
Is there a built in clause like this that I can use in Hibernate (HQL/Criteria)?
Thanks in advance.
Try this one:
select dayofweek(dateField), count(*)
from PersistentClass
group by 1
Did you try 'order by' clause?
Smth like this:
FROM tableName as table
ORDER BY table.dayOfWeek ASC
In what form you store your data?
Related
I have a table of sales grouped by week. I want to write a query that creates a new table giving the sales of the week in question AND the sales of that item from this time last year, but my attempts either give blank cells for the this-time-last-year (TTLY) values or duplicates.
I've tried writing a subquery that takes the date, subtracts 52 weeks, and shows the value for that week, then joining that subquery to my main query.
However, that subquery isn't working: the query shows the date of a year ago correctly, but doesn't then pull the SALES for that TTLY week, only the current week.
with ttyl as
(select
date::date as date,
(sales.date - interval '52 weeks') as date_ttly,
ID,
value
from sales
where country = 'uk' and date > '2019-08-01' and ID = '12345678')
In this example the subquery generates the previous year's date in the date_ttly column but pulls 2019 data in the value column.
All the WHERE conditions are just temporary so as to make building the query easier.
Thank you!
Assuming that the sales are grouped by date and by country only, a join on the same table should work:
SELECT sales1.id,
sales1.date,
sales1.value,
sales2.date,
sales2.value
FROM sales AS sales1
JOIN sales AS sales2 ON sales1.date - interval '52 weeks' = sales2.date
AND sales1.country = sales2.country
However, this also assumes that your date is always the same day of the week, e.g. Monday.
I am using this query for fetching the data day wise
SELECT
(count( server_time::timestamp::date)) ,
server_time::timestamp::date
FROM
complaint_details_v2
WHERE
server_time between '2018/10/03' and '2018/10/11'
GROUP BY
server_time::timestamp::date
ORDER BY
server_time ASC
but I want to alter the above query week wise instead of day wise between two dates.
you can group the dates into sevens by doing date arithmetic.
SELECT
(count( server_time::timestamp::date)) ,
min(server_time::timestamp::date) as "week starting"
FROM
complaint_details_v2
WHERE
server_time between '2018/10/03' and '2018/10/11'
GROUP BY
floor((server_time::timestamp::date - '2018/10/03'::date)/7)
ORDER BY
2 ASC
another alternative is grouping expression date_trunc(week,server_time) but that binds you to ISO weeks
I want to use Redshift to count the number of Mondays in a given time range. I've tried using date_part, which returns the day of the week. I can't use a simple count as there are multiple instances on the same day.
if you have dates table reference you can use the following code
select count(distinct my_table.date)
from my_table
where
date_part(dow,my_table.date)=1
and my_table.date between '2015-01-01' and '2016-01-01'
in this case the query will count all Mondays during 2015,
you can change the dates range the the day week .
date_part(dow,my_table.date)=1 -- Monday
date_part(dow,my_table.date)=2 -- Tuesday
and so on
if you don't have dates table , you should create Cartesian product
This might be a simple one but I haven't got a solution yet. I have a create_date field which is a date type, and a revenue number. I want to see weekly break down of revenue.
I can get the numbers easily in tableau because of built in functionality but doing it in PostgreSQL is where I need some help.
If you want the revenue by week, you'll need to group and aggregate:
select extract (week from create_date) as week, sum(revenue) from table group by week
I want to group results by day: max value for each of the the last 30 days. I came up with :
select max(value), DATE(time) from table where time>DATE('now', '-30 days') group by DATE(time);
But this only gives results for dates with data. I want null or 0 for the dates without data. Is that possible?