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

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;

Related

sql Countrows between date from other table and current_date

All,
To be honest I used to work with Power bi without knowing SQL, so I am learning things backwards.
I started learning SQL two weeks ago for my current (new role) at work.
So probably it is just a simple rookie mistake.
In this case I have 2 tables
events and marketingprofiles
In Table 1 (events) I have columns named
pk_eventtype_id (Which I want to count)
eve.starting (which is the date of the interview)
From the other Table 2 (marketingprofiles) I need the
marketinglivedate (as the new date to count from) Till current_date.
Both tables have a relationship column called mar.pk_marketingprofile_id and eve.marketingprofile_id
I am trying to count the number of interviews someone had in a certain period.
Tried it like this with a sub query, but receive an error at the
ERROR: syntax error at or near "(" LINE 8: (SELECT count(*)
SELECT
eve.femarketingname,
eve.starting,
eve.ending,
eve.pk_event_id,
COUNT(eve.pk_event_id) AS num_int
(SELECT count(*)
FROM marketingprofiles mar
where mar.marketinglivedate >= eve.starting
AND current_date <= eve.ending) AS num_int_by_MLD
FROM events eve
GROUP BY eve.femarketingname,
eve.starting,
eve.ending,
eve.pk_event_id
So I tried with an other method,But here i get the error:
ERROR: operator does not exist: integer >= timestamp with time
zone LINE 8: Where pk_event_id BETWEEN eve.starting and
current_date
Select
*,
Count(pk_event_id)
From
Events eve
Left join marketingprofiles mar
On eve.starting = mar.marketinglivedate
Where pk_event_id BETWEEN eve.starting and current_date
Group by femarketingname

Order an SQL table by year and ranges of years

I would like to be able to order the results in the following way.
There are two columns, one stores years and the other year ranges, and, sometimes, dates, like this:
2017
2016
2014–2016
1980-ongoing
2013
2000 28-27 March
1970
At the moment, I concatenate them and order by DESC, Getting this (showing the concatenated temporary column):
order by CONCAT(IFNULL(CAST(Year_Pub AS VARCHAR(16)) THEN '' ELSE CAST(Year_Pub AS VARCHAR(16))), IFNULL(Date_Freeform THEN '' ELSE Date_Freeform)) DESC
The result is:
2017
2016
2014-2016
2013
200028-27 March
1980-ongoing
1970
However, what I would like to get is this (imagine that this is a list of activities for a CV or similar):
1980-ongoing
2017
2016
2014-2016
2013
2000
1970
That is if there is a span of years, I would like to have the ongoing engagements to appear first, ordered by the start year, then have spans of years ordered by last year and mixed with single years. Dates only occur when Year_Pub is NULL and will have to be removed before concatenation, I imagine.
The separator is an ndash, so I need to split those strings by that somehow as I see from examples that show how to order by the family names in tables that have first name and family name in one column but this is a more complicated situation and I am not really familiar with SQL.
Also, this operation will be performed on a table that comes with an application so I do not want to insert data or columns into their database in case something gets broken.
Using SQL on an ElevateDB database (SQL 2003 standard (ANSI ISO/IEC 9075:2003), but a generic solution will do, I can look up the syntax).
Thank you for the advice.
This is one way to achieve your result:
SELECT
year_pub,
date_freeform,
COALESCE(CAST(year_pub AS VARCHAR(4)), date_freeform) AS year_list
FROM
table_name
ORDER BY
COALESCE(CAST(year_pub AS VARCHAR(4)),
CASE WHEN RIGHT(date_freeform, 4) = 'oing'
THEN '9999'
ELSE RIGHT(date_freeform, 4)
END
) DESC,
date_freeform DESC;

Not a group by function at a cumulative query

I'm making a cumulative query, which shows the evolution of clients in my database. To get these query, I use the year and the week of year they joined in the client database.
I have following query to search for relevant data:
SELECT DD.CAL_YEAR, DD.WEEK_OF_YEAR, SUM(COUNT(DISTINCT FAB.ID)) OVER ( ORDER BY DD.CAL_DATE ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS "Number of account statements"
FROM CLIENT_DATABASE FAB
JOIN DIM_DATE DD ON FAB.BALANCE_DATE_ID = DD.ID
GROUP BY DD.CAL_YEAR, DD.WEEK_OF_YEAR;
But when I compile this query, I get following error:
Error: ORA-00979: not a GROUP BY expression
SQLState: 42000 ErrorCode: 979
How can I fix this?
Since you are grouping by DD.CAL_YEAR, DD.WEEK_OF_YEAR, you can't use DD.CAL_DATE in the order by clause of your cumulative sum function.
It's hard for me to say exactly what you are trying to do without fully understanding your data. But, logically, it does seem like you should be able to simply use DD.CAL_YEAR, DD.WEEK_OF_YEAR in the order by clause instead of DD.CAL_DATE, and still get the results the way you are expecting.
So something like this:
SUM(COUNT(DISTINCT FAB.ID)) OVER ( ORDER BY D.CAL_YEAR, DD.WEEK_OF_YEAR ...

How to get month name out of date field

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

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