How to select data from Database where between months in the Database? - sql

I have i have 2 input fields that have text and the other dropdown (select option) in html. The user types the text and select the month from the dropdown (select option) then its submitted to Database. The dropdown have four months March, June, September and December. I want to create a query from database that selects between months like:
SELECT * FROM `returndocs` WHERE Year='2020' and Month BETWEEN 'March' AND 'June'
My database is:
I want to select all the data in the database posted starting from March to June. How do i do that?

in your drop down put the date instead of month name as value
like
03 => March
06 => June
then make it 2020-03-01 and 2020-06-01
then run your query with
SELECT * FROMreturndocsWHERE Year='2020' and Month BETWEEN '2011-03-01' AND '2011-06-01'

Since you defined month in text type you will need to type all between month manually :
SELECT *
FROM `returndocs`
WHERE Year = 2020 AND
Month IN ('March', 'April', 'May','June');

Related

What's the best way to find if a SQL record ceased to exist in a subsequent time period

How can I generate a list of items that existed in one time period, but don't exist in a following time period. For example, a list of items that existed in January 2019, but did not appear in February 2019. I posted an idea of the sample query below, but I don't know how to approach this.
select period, item
from table
where period = 'February 2019'
and item (exists) in period -1
and item (does not exist) in period
Sample Data would be as follows (if there were three records, for example)
Period Item
January 2019 Bob
January 2019 Steve
January 2019 Phil
February 2019 Steve
February 2019 Phil
Desired results would be as follows:
Period Item
January 2019 Steve
Thank you for the help.
Based on your sample query, you should be able to use aggregation:
select item
from table
where period in ('February 2019', 'January 2019')
group by item
having min(period) = max(period) and
min(period) = 'January 2019';
You can use MONTH(yourdate) to get the month number and use that to get the desired results.
DECLARE #Month INT
SET #Month=1
SELECT * FROM
(SELECT * FROM #table WHERE month_number=#Month)current_period
LEFT JOIN
(SELECT * FROM #table WHERE month_number=#Month+1)following_period
ON
current_period.item=following_period.item
WHERE
following_period.item IS NULL
The SELECT after LEFT JOIN can be modified to Period_ID>#Month to include all the following periods.

How to get year, month and day from seconds in PostgreSql?

I'm trying to create three columns based on date in seconds format.
My user.updated_at = 1521533490
I would like to get year, month and day separately and put these formatted values to columns for example:
year -> 2018, month -> 11, day -> 23
Does someone know how can I do that in pgSQL?
I would like to get year, month and day separately and put these formated values to columns
Don't do that.
Use a single column of type date or timestamp, depending on your application. Not every combination of your three columns will be a valid date. But every value in a single column of type date will be a valid date.
If you need the parts of a date separately, use PostgreSQL's date/time functions.
Try this approche to get differents arguments, then you can do whatever you want:
SELECT to_timestamp(1521533490); //2018-03-20T08:11:30.000Z
SELECT to_char(to_timestamp(1521533490), 'HH'); // 08 Hour
SELECT to_char(to_timestamp(1521533490), 'MI'); // 11 Minutes
SELECT to_char(to_timestamp(1521533490), 'SS'); // 30 Seconds
SELECT to_char(to_timestamp(1521533490), 'DD'); // 20 Day
SELECT to_char(to_timestamp(1521533490), 'Mon'); // MAR Month
SELECT to_char(to_timestamp(1521533490), 'YYYY'); // 2018 Year
Use the EXTRACT function.
SELECT to_timestamp(updated_at) "Date",
EXTRACT(YEAR FROM (to_timestamp(updated_at))) "Year",
EXTRACT(MONTH FROM (to_timestamp(updated_at))) "Month",
EXTRACT(DAY FROM (to_timestamp(updated_at))) "Day"
FROM users
Output
Date Year Month Day
2018-03-20T08:11:30Z 2018 3 20
SQL Fiddle: http://sqlfiddle.com/#!15/afe0e/15/0
More information on the EXTRACT function.

Update statement Syntax (PostgreSQL-Redshift)

In the time dimension we have added additional two columns called Fiscal Year and Fiscal Year Month which are to be populated. In our case Fiscal year starts from
Sep 01 and ends on Aug 31. (For example - Sep 01, 2017 to Aug 31,2018 is Fiscal Year 2018). I am trying to write an update statement in PostgreSQL.
(Logic - case when X.monthname in ('September','October','November','December') then (X.Year+1) else X.Year end)- But looks like I am not able to get the right syntax. Can you help build it ?
We have Date, Year, MonthName columns in our Time table which can be used in update statement.
Thanks.
I think the update would look like:
update x
set Year = Year + 1
where x.monthname in ('September', 'October', 'November', 'December') ;

convert month name in varchar to date so as to order by month [duplicate]

This question already has answers here:
Convert month name to month number in SQL Server
(14 answers)
Closed 9 years ago.
A table exists which stores month name in the form of string..
I want to order by year and month so as to get proper result.
year(string) Month(string) data
------------ ------------ ----
2012 August bigbox
2012 December samllbox
2013 April samll box
2012 September larg
I want to order by year and month. as in 2012,2013...
Jan,feb,march....
I am aware of the method of
case statement when
Month = 'january' THEN 1
Month - FEB THEN 2
But i do'nt want to use this as the procedure will be too big..
Your best option is to use the proper date type. Otherwise, create a table (inline or physical) to map your string months.
SELECT 1 AS month, 'January' AS strMonth
UNION ALL
SELECT 2, 'February'
UNION ALL
SELECT 3, 'March'
...
SELECT 12, 'December'
Then map this your table. See a demo
SELECT *
FROM TABLE
ORDER BY [year] DESC,
DATEPART(month,[Month] + ' 01 ' + CONVERT(VARCHAR(4),[YEAR]))
The above code will give you what you want , but i would strongly suggest you reconsider your design.
Right now you are reserving a string type field which would be at least 15 characters long. This field does not have any value than for display reasons. You could have a DATETIME field that would be much easier to short by (not having to do calculations there) and if you would like to display the name of the month you could use:
DATENAME ( month, DateField )

Counting the number of occurrences of a certain month in SQL

I have a complex problem to solve with SQL query. I need to generate a report that gives a numerical value which represents a number of actions that users performed in each month of the year. For instance, if User1 performed the specified action in January and July, the query needs to return numbers 1 in column called 'January' and 'July' and 0 in columns that represent other month's. The table that I'm pulling the data from has only one column that contains the date and time on which the certain action was taken (this is the only column relevant to this specific problem). I need to make a SQL query that will return this informations for each user in a certain period.
Please help, and if you need any more informations please tell me and I will provide it.
Thanks
The structure of the result should be like this:
User Jan Feb ..... Dec
UID 1 0 1
I need this for every user that appears in the selected period. My application is using SQL server 2005.
select datepart(month, dateColumn) as Month
, count(*) as NumberOfActions
from Actions
group by
datepart(month, dateColumn)
The above query is correct, Though this will only return a number for the month.
I would do the following.
SELECT dateparth(month, dateColumn) as Month, count(*) as NumberOfActions
FROM Actions
GROUP BY Month
And then (if using php) consider mktime() and date() functions. Like this:
/* The year doesn't matter. we are just trying to format this in to a date */
$date = mktime(0, 0, 0, $row->Month, 1, 2012);
/* This will display 'Jan, Feb, Mar ...' respectively */
$dateText = date('M', $date);
If using another language then just google alternatives. It'll be the same principle .. just a different syntax.
Hope this helps.
This will return the data in rows but I think the OP is actually asking for it in columns. You will need a PIVOT statement for this, something like this...
Assuming...
Create Table TestPivot (UserName Varchar(10), DateColumn DateTime);
Then...
Select *
From (Select UserName, DateName(Month, DateColumn) As Month_Name, Count(*) As Month_Count
From TestPivot
Group By UserName, DateName(Month, DateColumn)) As SourcePart
Pivot (Sum(Month_Count)
For Month_Name In (January, February, March, April, May, June,
July, August, September, October, November, December)) As PivotPart