Count records in each month using Month(dateField) in SQL - sql

I have a large table containing c650,000 records. They are individuals with email addresses and one of the fields is 'dateOfApplication'. I have been asked for a breakdown of how many people signed up in each month.
I'd like the results to look something like
Month Year Total
1 2017 50763
2 2017 34725
And have made a target table in this format to put the results in. I've been able to use Month(dateOfApplication) to get the month component of the date using
SELECT DISTINCT
(SELECT COUNT(1) FROM [UG_Master]
WHERE MONTH([UG_Master].dateOfApplication) = '6') as Total
To return particular months, but don't really know how to get one row for each month it finds.

but don't really know how to get one row for each month it finds.
You can use GROUP BY :
SELECT MONTH([UG_Master].dateOfApplication), COUNT(1)
FROM [UG_Master]
GROUP BY MONTH([UG_Master].dateOfApplication);
If you want year wise months then include year also :
SELECT YEAR([UG_Master].dateOfApplication), MONTH([UG_Master].dateOfApplication), COUNT(1)
FROM [UG_Master]
GROUP BY YEAR([UG_Master].dateOfApplication), MONTH([UG_Master].dateOfApplication);

Related

Group Sql query by month number and year number

I have written a query which is bring following data:
I want to further group it by month and year to bring total quiz and total pass for each month in the result set, I am stuck and i need help with this,
thank you
You can group by multiple columns
select QuizMont, QuizYear, .. aggregate values here
...
group by QuizMont, QuizYear

Select same account numbers in a new table

Using Teradata SQL Assistant, I want to be able to pull a table a year ahead but only the ones that would match the results in the query from the year before. Here's what I am trying to do. I pulled a table that contains information where the results in a specific column equals 0 for no. I want to pull information from 1 year ahead where the results in that column equals 1 but only include the account numbers that came when I pulled the results for the year before. Like only pull the customer account numbers for the year ahead that are the same from the year before.
Explanation: I pull the one table that has 0 in the column. From that, I want to see which of those accounts became a 1 in the table from a year ahead. The table has millions of accounts and I just have my settings for 10,000 of them so I want to see of those 10,000 in the first year that did not have the product, how many of them became 1 in the second year.
Can I do this? If so, how? I have been googling and I do not think I am explaining what I am trying to do correctly in my google query so I am coming up short with results.
Thanks for clarifying. That makes it a little simpler. I would put the second year data in a subquery and filter the main table on the first year and quantity = 0. This will give you two columns one with the first year and one with the second year. If you're only looking for this information for a single product_id you will need to add this to both WHERE clauses.
SELECT TABLE_NAME.ACCOUNT_ID, TABLE_NAME.QUANTITY AS "2019" , YEAR_TWO.QUANTITY AS "2020"
FROM TABLE_NAME
LEFT JOIN
(
SELECT *
FROM TABLE
WHERE YEAR = 2020
) YEAR_TWO ON TABLE_NAME.ACCOUNT_ID = YEAR_TWO.ACCOUNT_ID
WHERE TABLE_NAME.YEAR = 2019
AND TABLE_NAME.QUANTITY = 0
If you want just the % of accounts that are no longer 0 in the second year you could try something like this (adding up all the 1s and dividing by total count)
SELECT TABLE_NAME.YEAR, SUM(YEAR_TWO.QUANTITY) / COUNT(YEAR_TWO.QUANTITY) AS PERCENTAGE_NOT_ZERO
FROM TABLE_NAME
LEFT JOIN
(
SELECT *
FROM TABLE
WHERE YEAR = 2020
) YEAR_TWO ON TABLE_NAME.ACCOUNT_ID = YEAR_TWO.ACCOUNT_ID
WHERE TABLE_NAME.YEAR = 2019
AND TABLE_NAME.QUANTITY = 0
GROUP BY TABLE_NAME.YEAR

Year wise Average days SQL

Today i have below problem while perform an sql query. Please find below data.
I perform SQL query on my table and get the below resulted output. i perform Group by on ID, Name, Week, Year, Days now i want the Days column as average of All Days based on year column. means there is multiple value of year is exist so i need Avg of Days data in all rows of DAYS for particular row. expected result as per below.
Thanks in Advance!!!
Write in comment if you have any query.
You can use OVER:
SELECT
*,
AVG(Days) OVER (PARTITION BY LEFT(Year, 4)) AvgDays
FROM
Tbl
Note: Just grouped by year (2016)

Working with dates in tsql query

I want to create a report that gives me a total for sales by month so
select customer,month(dated), sum(invtotal)
from salestable
group by customer,dated
gives me the my result but I get multiple lines returned if a customer had three orders for a particular month.
I was expecting the month(dated) to strip out the day part of the date and just return everything for a particular month as one whole, it does not appear to do that.
Any ideas on what I am doing wrong?
select customer, month(dated), sum(invtotal)
from salestable
group by customer, month(dated)

Querying SQLITE DB for Data from One Column Based On Another Column

I hope the title of this post makes sense.
The db in question has two columns that are related to my issue, a date column that follows the format xx/xx/xxxx and price a column. What I want to do is get a sum of the prices in the price column based on the month and year in which they occurred, but that data is in the other aforementioned column. Doing so will allow me to determine the total for a given month of a given year. The problem is I have no idea how to construct a query that would do what I need. I have done some reading on the web, but I'm not really sure how to go about this. Can anyone provide some advice/tips?
Thanks for your time!
Mike
I was able to find a solution using a LIKE clause:
SELECT sum(price) FROM purchases WHERE date LIKE '11%1234%'
The "11" could be any 2-digit month and the "1234" is any 4 digit year. The % sign acts as a wildcard. This query, for example, returns the sum of any prices that were from month 11 of year 1234 in the db.
Thanks for your input!
You cannot use the built-in date functions on these date values because you have stored them formatted for displaing instead of in one of the supported date formats.
If the month and day fields always have two digits, you can use substr:
SELECT substr(MyDate, 7, 4) AS Year,
substr(MyDate, 1, 2) AS Month,
sum(Price)
FROM Purchases
GROUP BY Year,
Month
So, the goal is to get an aggregate grouping by the month?
select strftime('%m', mydate), sum(price)
from mytable
group by strftime('%m', mydate)
Look into group by