How to get month name out of date field - sql

I'm trying to build a query that gives me a year month field with month names instead of the numbers. The output should be similar to this:
Year/month field | Sum field
Januari 2014 100
Februari 2014 12300
Maart 2014 3234
April 2014 4964
I've searched for the way to get the the month name but I must be doing something wrong because I constantly get the error that datename is not a function or an sql error 1064. This i what i've tried:
SELECT id,
YEAR(report_date) AS YEAR,
MONTH(report_date) AS MONTH,
SELECT DATENAME(MONTH, (MONTH(report_date))) ,
CONVERT(VARCHAR(3), DATENAME(MONTH, report_date)) SUM(num) AS participants_month
FROM participants
WHERE unit_id = 10
GROUP BY unit_id,
MONTH(report_date)
ORDER BY report_date ASC;
Tried to make this question as clear as possible. If something isn't clear tell me and I'll try to explain it. Really hope someone could point me out in the right direction.

In SQL Server you use DATENAME(month, date):
SQL Server Fiddle
But in MySQL you use MONTHNAME(date):
MySQL Fiddle
From the error you are receiving I guess you do not use SQL Server as your database management system, so in MySQL you should use MONTHNAME(date).

From the error, I suppose, you are using MySQL.But the DATENAME() is Transact-SQL function.
In MySQL, you can use
MONTH(yourdate)
instead

Related

Syntax error in EXTRACT() query, what operator is missing?

I have a table in MS Access that tracks when members of our pool check in with their pool pass that looks like this:
I'm trying to generate a report that displays the data so I can see how many 'Check-Ins' we've had for each year of operation like so:
Season Visits
2021 432
2020 123
2019 354
etc.
My plan is to extract the year from the CHECKINTIME, then count the number of unique ID's in that year.
I'm using the following SQL for my query:
SELECT
EXTRACT(YEAR FROM tblCheckIn.[CHECKINTIME]) AS Season,
COUNT(tblCheckIn.[ID]) AS Visits
FROM tblCheckIn
GROUP BY (YEAR FROM tblCheckIn.[CHECKINTIME]);
Right now I'm getting the following error: Syntax error (missing operator) in query expression 'EXTRACT(YEAR FROM tblCheckIn.[CHECKINTIME]) I've been pouring over the documentation for EXTRACT() but I can't figure out what I'm doing wrong. I'm guessing it's specifically an MS Access thing?
Ms Access uses a YEAR function and not extract
SELECT
YEAR(tblCheckIn.[CHECKINTIME]) AS Season,
COUNT(tblCheckIn.[ID]) AS Visits
FROM tblCheckIn
GROUP BY YEAR(tblCheckIn.[CHECKINTIME]);
or you can use
SELECT DatePart("yyyy",tblCheckIn.[CHECKINTIME]) AS NewDate FROM tblCheckIn;

SQL- HAVING Clause Issue

I'm trying to use the MAX() function as a subquery to filter results of my greater query by the last couple of whole years. I realized this was a little bit more complicated than what I initially thought it would need so I created a much simpler query to test out what I would need to do with the HAVING clause in order to get it to work.
I've checked these two posts but it didn't really help (SQL, HAVING clause explained, SQL: HAVING clause)
I don't think my understanding of the HAVING clause is correct because I'm not sure why it's not working. Would someone be able to help and explain?
Note: [Fiscal Year] is NVARCHAR so I figured converting it to INT might work.
SELECT DISTINCT
D.[FISCAL YEAR]
FROM [Dates] AS D
GROUP BY D.[Fiscal Year]
HAVING CONVERT(INT,D.[Fiscal Year]) >= MAX(CONVERT(INT,D.[FISCAL YEAR])) -2
These are my results:
(No column name)
2015
2014
2013
2012
2016
These are the results I should get:
(No column name)
2015
2014
2016
I think this could be done better in the WHERE clause. This solution isn't optimal, but here's what you can do.
SELECT
DISTINCT D.[FISCAL YEAR]
FROM [Dates] AS D
WHERE CONVERT(INT,d.[Fiscal Year])
>= (SELECT MAX(CONVERT(INT,[Fiscal Year])) -2 from Dates)
The problem you are having is you are grouping by the year, thus the max will always be the same as the year for that group.
Here's one option using window functions:
select distinct fiscalyear
from (
select fiscalyear, max(fiscalyear) over () maxfiscalyear
from dates
) t
where fiscalyear >= maxfiscalyear - 2
I don't see the need to use the convert function here either, but if you need to, you can add it back.
SQL Fiddle Demo

SQL: Can GROUP BY contain an expression as a field?

I want to group a set of dated records by year, when the date is to the day. Something like:
SELECT venue, YEAR(date) AS yr, SUM(guests) AS yr_guests
FROM Events
...
GROUP BY venue, YEAR(date);
The above is giving me results instead of an error, but the results are not grouping by year and venue; they do not appear to be grouping at all.
My brute force solution would be a nested subquery: add the YEAR() AS yr as an extra column in the subquery, then do the grouping on yr in the outer query. I'm just trying to learn to do as much as possible without nesting, because nesting usually seems horribly inefficient.
I would tell you the exact SQL implementation I'm using, but I've had trouble discovering it. (I'm working through the problems on http://www.sql-ex.ru/ and if you can tell what they're using, I'd love to know.) Edited to add: Per test in comments, it is probably not SQL Server.
Edited to add the results I am getting (note the first two should be summed):
venue | yr | yr_guests
1 2012 15
1 2012 35
2 2012 12
1 2008 15
I expect those first two lines to instead be summed as
1 2012 50
Works Fine in SQL Server 2008.
See working Example here: http://sqlfiddle.com/#!3/3b0f9/6
Code pasted Below.
Create The Events Table
CREATE TABLE [Events]
( Venue INT NOT NULL,
[Date] DATETIME NOT NULL,
Guests INT NOT NULL
)
Insert the Rows.
INSERT INTO [Events] VALUES
(1,convert(datetime,'2012'),15),
(1,convert(datetime,'2012'),35),
(2,convert(datetime,'2012'),12),
(1,convert(datetime,'2008'),15);
GO
-- Testing, select newly inserted rows.
--SELECT * FROM [Events]
--GO
Run the GROUP BY Sql.
SELECT Venue, YEAR(date) AS yr, SUM(guests) AS yr_guests
FROM Events
GROUP BY venue, YEAR(date);
See the Output Results.
VENUE YR YR_GUESTS
1 2008 15
1 2012 50
2 2012 12
it depends of your database engine (or SQL)
to be sure (over different DB Systems & Versions), make a subquery
SELECT venue, theyear, SUM(guests) from (
SELECT venue, YEAR(date) AS theyear, guest
FROM Events
)
GROUP BY theyear
you make a subtable of
venue, date as theyear, guest
aaaa, 2001, brother
aaaa, 2001, bbrother
bbbb, 2001, nobody
... and so on
and then
count them

how to get data whose expired within 45 days..?

HI all,
i have one sql table and field for that table is
id
name
expireydate
Now i want only those record which one is expired within 45 days or 30 days.
how can i do with sql query .?
I have not much more exp with sql .
Thanks in advance,
If you are using mysql then try DATEDIFF.
for 45 days
select * from `table` where DATEDIFF(now(),expireydate)<=45;
for 30 days
select * from `table` where DATEDIFF(now(),expireydate)<=30;
In oracle - will do the trick instead of datediff and SYSDATE instead of now().[not sure]
In sql server DateDiff is quite different you have to provide unit in which difference to be taken out from 2 dates.
DATEDIFF(datepart,startdate,enddate)
to get current date try one of this: CURRENT_TIMESTAMP or GETDATE() or {fn NOW()}
You can use a simple SELECT * FROM yourtable WHERE expireydate < "some formula calculating today+30 or 45 days".
Simple comparison will work there, the tricky part is to write this last bit concerning the date you want to compare to. It'll depend of your environment and how you stored the "expireydate" in the database.
Try Below:-
SELECT * FROM MYTABLE WHERE (expireydate in days) < ((CURRENTDATE in days)+ 45)
Do not execute directly! Depending of your database, way of obtaining a date in days will be different. Go look at your database manual or please precise what is your database.

How to write a sql query to sum total for each day?

Please help me to write the following sql. I have a table like this, with an amount column and date column
amount date
-----------------------
100 - 2010-02-05
200 - 2010-02-05
50 - 2010-02-06
10 - 2010-02-06
10 2010-02-07
what I want is to write a sql to get total for each day. Ultimately my query should return something like this
amount date
-----------------
300 - 2010-02-05
60 - 2010-02-06
10 - 2010-02-07
Please note that now it has group by dates and amounts are summed for that date.
RESOLVED -
This was my bad, even though I mention the date column as date here, my actual date column in postgres table was 'timestamp'. Once I changed it to get the date only, everything got working
my working sql is like this "select sum(amount), date(date)
from bills
group by date(date)
"
thanks everyone (and I will accept the 1st answer as the correct answer since I can accept only one answer)
thanks again
sameera
Pretty basic group by statement.
SELECT SUM(table.amount), table.date
FROM table
GROUP BY table.date
http://www.w3schools.com/sql/sql_groupby.asp
Look into GROUPING by date, here: http://www.w3schools.com/sql/sql_groupby.asp
and look into SUM(), here: http://www.w3schools.com/sql/sql_func_sum.asp
You will need to use both of them.
Try this:
SELECT SUM(Amount), Date FROM Table
GROUP BY Date
SELECT SUM(amount) as amount_sum, date FROM table GROUP BY date;