I am Unable to Sort the Order By by Month names in MS Access Database
I am Using the Format Function to retrieve Month Name (If I use MonthName(Month(date)) Function The Query is Executing at Access DB(but when I am trying to Use the Same Query from My Application I am getting Errors
My Query is
This is My Query(Code )Used all the Functions of Format But I am Unable to Order By MonthNames
SELECT Format(date ,"mmmm") as MonthName,
Sum(Rojnamchatable.asal1) AS SumOfasal1,
Sum(Rojnamchatable.chaat1) AS SumOfchaat1,
SumOfasal1+SumOfchaat1 AS TotalBiri1,
Sum(Rojnamchatable.asal2) AS SumOfasal2,
Sum(Rojnamchatable.chaat2) AS SumOfchaat2,
SumOfasal2+SumOfchaat2 AS TotalBiri2,
GROUP BY Format(date ,"mmmm")
ORDER BY Format(date ,"mmmm")
I am Getting Data Perfectly But I am unable to get Order By Month Name
(i.e Jan ,Feb ,March)
I Even Used Format(date,"mm") Function If I Use that I am getting Erors
So Please try to Solve My Issue
I Spent 2 days for this
How about adding the month (number) column but not displaying it?
SELECT
Format([date] ,"mmmm") as MonthName,
Sum(Rojnamchatable.asal1) AS SumOfasal1,
Sum(Rojnamchatable.chaat1) AS SumOfchaat1,
SumOfasal1+SumOfchaat1 AS TotalBiri1,
Sum(Rojnamchatable.asal2) AS SumOfasal2,
Sum(Rojnamchatable.chaat2) AS SumOfchaat2,
SumOfasal2 + SumOfchaat2 AS TotalBiri2,
FROM
yourTableName
GROUP BY
Format([date] ,"mmmm"), Month(date)
ORDER BY
Month([date])
Please note I have enclosed date in [], this is because of the fact Date is a reserved word and should not be used as a column/field name.
Related
I work for a gun club and we are trying to find the total number of targets shot at during a specific year. The table contains totals from years 2000-2018 and I am trying to write a query that will look at the string for the date of the shoot which is in a format like this 2010-06-13 00:00:00.000 I just care about the year so I created this query:
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND ShootDate LIKE '2007-%%-%% 00:00:00.000'
If I run the query up to the AND clause it works and returns a total. However, I get a null value if I highlight the whole thing. I have tried variations of the string as well. Such as this '2007%' and this '2007-- %'
Not sure what I am missing. Any help is appreciated.
Don't convert to string to query for a year, use YEAR() function instead:
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND YEAR(ShootDate)=2007 -- MySQL
You could also use a range query
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND ShootDate BETWEEN '2007-01-01' AND '2007-12-01 23:59:59.999'
Note: The above assumes that you do not store dates as strings. The function to use depends on RDBMS. In MS SQL Server you would use DATEPART(year, ShootDate) = 2007
Say I have this .csv file which holds data that describes sales of a product. Now say I want a monthly breakdown of number of sales. I mean I wanna see how many orders were received in JAN2005, FEB2005...JAN2008, FEB2008...NOV2012, DEC2012.
Now one very simply way I can think of is count them one by one like this. (BTW I am using logparser to run my queries)
logparser -i:csv -o:csv "SELECT COUNT(*) AS NumberOfSales INTO 'C:\Users\blah.csv' FROM 'C:\User\whatever.csv' WHERE OrderReceiveddate LIKE '%JAN2005%'
My question is if there is a smarter way to do this. I mean, instead of changing the month again and again and running my query, can I write one query which can produce the result in one excel all at one.
Yes.
If you add a group by clause to the statement, then the sql will return a separate count for each unique value of the group by column.
So if you write:
SELECT OrderReceiveddate, COUNT(*) AS NumberOfSales INTO 'C:\Users\blah.csv'
FROM `'C:\User\whatever.csv' GROUP BY OrderReceiveddate`
you will get results like:
JAN2005 12
FEB2005 19
MAR2005 21
Assuming OrderReceiveDate is a date, you would format the date to have a year and month and then aggregate:
SELECT date_format(OrderReceiveddate, '%Y-%m') as YYYYMM, COUNT(*) AS NumberOfSales
INTO 'C:\Users\blah.csv'
FROM 'C:\User\whatever.csv'
WHERE OrderReceiveddate >= '2015-01-01'
GROUP BY date_format(OrderReceiveddate, '%Y-%m')
ORDER BY YYYYMM
You don't want to use like on a date column. like expects string arguments. Use date functions instead.
I have a table work_history('id','name','work','work_month','work_year').
I need a query to current current month data, but if data is not exist for current month, in this case retrieve last month data so on.....till then data not found.
nly
For Ex- If data found for 11-2012 so retrieve only this data, no need to check for previous month, but if not found so check for last month 10-2012. If again not found check 09-2012.
Thanks in advance
The month that you want is the maximum month that is available in the data. it must be either the current month or the closest to it. so such query should look like:
select ......
from work_history
where work_month = (select max(work_month) from work_history)
note that if the column work_month is a char column, you might need to convert the data to date type in order to allow correct comparison. based on the format seen in the example above:
select ......
from work_history
where str_to_date(work_month,'%m-%Y') = (select max(str_to_date(work_month,'%m-%Y')) from work_history)
EDIT
following all comments, the following query should fix the mistake I had and also give data based on ID of the user:
select wh.*
from work_history wh,
(select id, max(str_to_date(work_month,'%m-%Y')) as mx_month
from work_history
where str_to_date(work_month,'%m-%Y') <= curdate()
group by id
) wh2
where wh.id=wh2.id
and str_to_date(wh.work_month,'%m-%Y') = wh2.mx_month
I would like to include the name of the day in my query in ms access?
I know how to do it from the query designer in the format field as 'dddd', but want to rather add it in the sql editor as part of my statement.
Its part of a select, from, where, group by, order by statement where the date is specified in the where clause but i want it to display as the name of the day ie Tuesday in another column.
Thanks
Format works for queries, too:
SELECT ADate, Format([ADate],"dddd") AS ADay
FROM Table1;
Here is a select statement that returns you the day name from the date time
SELECT datename(dw,GETDATE()) AS 'Day Name'
This gives me the current day name.
so i have a table with hundreds records. And a have a filed name "created" type with a datetime format. Now I want to make and archive with the months. For example January, February.... etc. I need to create query to find all possible months. For example if my records start from 2011/05/01 to now I will need to fetch the months that means months 5,6,7,8,9,10,11,12.
Is there a way to that ???
If you are looking at the list of all Months present in the created field (as I understand your query) then do this:
SELECT DISTINCT(MONTH(created)) FROM posts;
The resulting set would be the list of unique months in the field. If this will complain then try:
SELECT DISTINCT(MONTH(DATE(created))) FROM posts;
You can then substitute MONTH for MONTHNAME and get names instead. I did not add the WHERE clause to these queries but you can limit the dataset you are looking at as you see fit.
For more information take a look at http://dev.mysql.com/doc/refman/5.1/en/functions.html this has a list of quite a few functions that MySQL natively provides.
Yes, use the DATE_FORMAT function and other date and time functions.
More details here
For example, if you want all your records for December 2011:
SELECT * FROM posts WHERE YEAR(created) = 2011 AND MONTH(created) = 12