hsqldb count for each day - sql

having table
id|date|somefield
I need to get count of entries for each day of the year
select EXTRACT (DAY_OF_YEAR FROM date) as day, id from table
works fine
but when I try
select EXTRACT (DAY_OF_YEAR FROM date) as day, count(*) from table
fails
select count(*) from table group by EXTRACT (DAY_OF_YEAR FROM date)
fails as well

You need to add a group by expression. Here is some pseudo code, I will work up a SqlFiddle in a moment.
select EXTRACT (DAY_OF_YEAR FROM date) as day,
count(*)
from table
group by EXTRACT (DAY_OF_YEAR FROM date)
SQLFIDDLE (Using MYSQL) http://sqlfiddle.com/#!2/42c9e/10

Related

Get DISTINCT IDs for DISTINCT dates in BigQuery?

I'm trying to get DISTINCT IDs for DISTINCT dates in BigQuery to build a report.
The date field is the kay partition field.
I tried to start with something looking like
SELECT DISTINCT(Id) FROM `project.dataset.table`
WHERE DATE(KeyPartitionDate)
BETWEEN '2017-01-01' AND '2020-06-01'
But this only gives me the different Ids between those 2 dates.
Now when it comes to query and check for every month of the year the DISTINCT(Ids), I have no clue how to do it.
I tried using DISTINCT and GROUP By for the Date but that doesn't seem to be the right path...
Any idea ?
If you are looking for number of distinct IDs for each month then this might help you.
SELECT DATE_TRUNC(DATE(KeyPartitionDate), MONTH) as month,COUNT(DISTINCT Id) as num_of_Ids
FROM `project.dataset.table`
WHERE DATE(KeyPartitionDate) BETWEEN '2017-01-01' AND '2020-06-01'
GROUP BY DATE_TRUNC(DATE(KeyPartitionDate), MONTH)
#standardSQL
SELECT DISTINCT
DATE_TRUNC(DATE(KeyPartitionDate), MONTH) year_month,
id
FROM `project.dataset.table`
WHERE DATE(KeyPartitionDate)
BETWEEN '2017-01-01' AND '2020-06-01'

SQL Server data search with date range

I have a table with the following columns:
Date
Skills,
Customer ID
I want to find out Date(x), Customers, Count of Customers in between Date(x) and Date(x)+6
Can somebody guide me how to make this query, or can I create this function in SQL Server?
If I understand you correctly, you want something like this:
(take care, can be bad syntax, because i "work" only with oracle. But I think that it should work)
select date, customer_id, COUNT(*)
from your_table --add your table
where date between getdate() and DATEADD(day, 6, getdate())
-- between current database system date and +6 day
group by date, customer id
order by COUNT (*) desc -- if you want, you can order your result - ASC||DESC
If you have data on each date, then perhaps this is what you want:
select date, count(*),
sum(count(*)) over (order by date rows between 6 preceding and current row) as week_count
from t
group by date;

Looking to create a query in SQL that states

i am relatively new to SQL and I'm looking to create a query that states how many records were created by those other than a certain "good" group of users (userids). If possible grouped by month as well. Any suggestions? I have some basic logic set out below.
Table is called newcompanies
SELECT COUNT(record_num), userid
FROM Newcompanies
WHERE userID <> (certain group of userIds)
GROUP BY Month
Will i be required to create a second table where the group of "good" userids is held
There are a few ways to do this. Without knowing your exact columns, this will be a rough estimate.
SELECT id,
DATEPART(MONTH, created_date) AS created_month,
COUNT(*)
FROM your_table
WHERE id NOT IN(
--hardcode userID's here
)
GROUP BY
id,
DATEPART(MONTH, created_date)
Or you could have a table with your good id's and then exclude those.
SELECT id,
DATEPART(MONTH, created_date) AS created_month,
COUNT(*)
FROM your_table
WHERE id NOT IN(
SELECT id
from your_good_id_table
)
GROUP BY
id,
DATEPART(MONTH, created_date)
-- if month is not a field in the table you will have to do a function to parse out the month that will depend on the sql database you are using, if it is MS SQL you can do Month(datefield)
SELECT COUNT(record_num), userid, Month
FROM Newcompanies
WHERE userID NOT IN (
Select UserID
from ExcludeTheseUserIDs
)
GROUP BY Month, userid

SQL query for multiple values of a column

I have db with names etc with date of birth. How can I get count of columns for all 12 months of the dates?
Exact code depends on the database you use; you should, somehow, "extract" month from date of birth in order to GROUP BY it.
In Oracle, you might have done it as
select to_char(date_of_birth), 'mon') dob_month,
count(*)
from your_table
group by to_char(date_of_birth, 'mon');
or
select extract(month from date_of_birth) dob_month,
count(*)
from your_table
group by extract(month from date_of_birth);

Count on particular partitions for particular month in hive

I have a have a hive table which is partitioned with the table_date.I want to get the count of individual partition for the particular day for the particular month and particular year.
When I run the following query I am getting a count for an entire month but I want it as individual day.
select count(*) from table where month(table_date)=1 and year(table _date)=2016
If it is partitioned on date, I would expect this to work:
select table_date, count(*)
from table
where table_date >= '2016-01-01' and table_date < '2016-02-01'
group by table_date;
select table_date, count(*)
from table
where table_date between '2016-01-01' and '2016-02-01'
group by table_date;