Working with dates in tsql query - sql

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)

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

Sum dates with different timestamps and picking the min date?

Beginner here. I want to have only one row for each delivery date but it is important to keep the hours and the minutes. I have the following table in Oracle (left):
As you can see there are days that a certain SKU (e.g SKU A) was delivered twice in the same day. The table on the right is the desired result. Essentially, I want to have the quantities that arrived on the 28th summed up and in the Supplier_delivery column I want to have the earliest delivery timestamp.
I need to keep the hours and the minutes otherwise I know I could achieve this by writing sth like: SELECT SKU, TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD'), SUM(QTY) FROM TABLE GROUP BY SKU , TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD')
Any ideas?
You can use MIN():
SELECT SKU, MIN(SUPPLIER_DELIVERY), SUM(QTY)
FROM TABLE
GROUP BY SKU, TRUNC(SUPPLIER_DELIVERY);
This assumes that SUPPLIER_DELIVERY is a date and does not need to be converted to one. But it would work with TO_DATE() in the GROUP BY as well.

Count records in each month using Month(dateField) in 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);

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

Sql query help, grabbing dates, counts, averages

I have a table called payhistory, and from this table I need to grab the Year, Month when a payment was made. I then need to get an average of payments grouped by Year and then month.
select AVG(totalpaid) from payhistory
This table has a column called datepaid that is where I need to grab the date from. It has a column called totalpaid which is where I need to grab the average from.
I then need a count of every payment within that month.
And finally a column displaying which Year/Month it was from.
Important columns in the table:
number (account number can use this to get a count)
datepaid (contains the date the payment was entered '2009-09-28 00:00:00.000')
totalpaid (contains the specific payment for that date)
This should work:
select AVG(totalpaid),
COUNT(number),
MONTH(datepaid),
YEAR(datepaid)
from payhistory
group by
MONTH(datepaid),
YEAR(datepaid)