Get Missing Month [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have result set like
DT_START DT_END
------------- -----------
2013-05-01 2014-04-01
2014-07-01 2015-04-01
2014-11-01 2015-02-01
i have start date and end date.i need missing month of 2014 in both dates.
Like missing month in 2014 is 5 and 6 (it is the difference between first row "DT_END" and second row "DT_START")

One possible solution:
Create a calendar table or make a query that returns all the months between the lowest dt_start and the highest dt_end. How you do this will depend on the actual database you use - some have nice functions that you can use to create sequences, some have number tables etc.
Then use that as source and do a left join with your table and filter out the rows where dt_start is null.
select month from all_months -- this is the table/query with all months
left join your_table on month between dt_start and dt_end
where dt_start is null

Related

Including only one row in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What I'm trying to do is take these records that looks like this:
ID NAME STARTDATE ENDDATE EnrollmentMonth
1 PETER 20200705 20200729 20200701
1 TONY 20200730 99991231 20200701
and change it to look like this:
ID NAME STARTDATE ENDDATE EnrollmentMonth
1 PETER 20190101 20200729 20200701
Including the name that has enrollmented in 15th of the month, Peter enrolled in the 15th of July while Tony did not.
The idea is to pick only one name in July.
I am having difficult time making this work. Any help would be appreciated.
Thanks
You seem to want to filter rows where startdate is earlier than the 15th of the enrollment month:
select t.*
from mytable t
where t.startdate <= trunc(enrollmentmonth, 'month') + interval '15' month
If enrollmentmonth is always the first of a month, we can dispense trunc():
select t.*
from mytable t
where t.startdate <= enrollmentmonth + interval '15' month

SQL Server:sum rows by year and name the row value as 'current' for current year and 'previous' for last year [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to sql server and I want to sum a particular row for this year 2018 and for last year i.e. add data from jan 2018 to jun 2018 similarly for 2017 as well.
This is how I want the output:
Basically I want the year to date calculations for this problem, and the values column should be added for a particular year, I have given the months as it is my requirement to check if it falls within the month we are in, for example june. The data will be populated every month and thus I want the requirement to get it dynamically.
This will get you close:
SELECT row_number() over (order by year desc) as [S.No],
SUM([values]) as Total, year
FROM [table]
GROUP BY Year

Summarizing blocks of contiguous dates into start and end dates [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a database (SQL 2014) that contains the following entries:
Name Date
John 02/02/2017
John 03/02/2017
John 04/02/2017
John 13/03/2017
John 14/03/2017
These entries represent blocks of absences from work - it could be just one day each time, or blocks of several days (with possible month transitions within a block).
I'd like to summarize this data as follows:
Name MinDate MaxDate
John 02/02/2017 04/02/2017
John 13/03/2017 14/03/2017
where the output contains the first and last day of each block (together with the number of days absent, which will be straightforward).
Use YEAR and MONTH
SELECT
T.Name,
MIN(T.Date) MinDate,
MAX(T.Date) MaxDate
FROM
Tbl T
GROUP BY
T.Name,
YEAR(T.Date),
MONTH(T.Date)

Calculate number of days from date represented as integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a column (EOM) in my table(Employee) which gives the last day of the month as an integer and I want to return the number of days in the month. I know we can normally use the built-in functions but since the DATE that I have is stored as an int they won't work. Can anyone help me out with this?
EmpID EmployeeName EOM
123 ABC 20160731
345 XYZ 20150228
I want to know the number of days that say Employee ABC worked for which is 31.
If you have the last day of the month in EOM column then you can simply take the last 2 characters from EOM column, which will give you number of days in the month.
SELECT SUBSTRING(EOM,7,2) FROM EMPLOYEE
OR
SELECT CONVERT(INT,SUBSTRING(EOM,7,2)) FROM EMPLOYEE
This will give you the number which you can work with to get the number of days Employee worked for.

fetch record count such as count the no of records before 12 weeks from current date and count the no of records after 12 weeks from current date [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to retrive data from table with count such as before current year and after cuurent year.
My table is Patient having patientRegDate column such as
P_RegDate
2013-10-22
2013-10-24
2013-05-01
2013-10-28
2013-10-28
2013-10-28
2014-06-06
2013-10-29
2014-10-29
2014-10-30
2013-10-30
2015-10-30
2013-10-30
from this column i want to fetch record count such as count the no of records before 12 weeks from current date and also count the no of records after 12 weeks from current date. Lest say current date is todays date. How can I get the count of records before and after 12 weeks
This should work, but you might have to fine tune the whereclause to fit your needs:
SELECT Count12Before, Count12After
FROM
(SELECT COUNT(P_RegDate) AS Count12Before
FROM table1
WHERE p_regdate < DATEADD('ww', -12, DATE())
) before12,
(SELECT COUNT(P_RegDate) AS Count12After
FROM table1
WHERE p_regdate > DATEADD('ww', 12, DATE())
) after12
select P_RegDate
from sample_table
group by P_RegDate
having datediff(parameters) >= 12
for e.g
SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate
out put : 61