I need to get the number of holidays between two dates. I have tried using the query below but I'm getting an incorrect count number.
StartDate
EndDate
01/2/2022
03/2/2022
17/2/2022
19/2/2022
SELECT COUNT(*) FROM table
WHERE StartDate <= '02/2/2022' and EndDate >= '19/2/2022'
how I make this date => '02/2/2022' return count of day that between two dates from the first row from the table and get count day from the second row.
The count must be 5 days.
The simplest way of doing it is to have a table that has one row per day for every day from eg year 2000 to year 2099, then you can:
SELECT COUNT(*)
FROM
cal --your dates table
INNER JOIN h. --your holiday table
ON cal.d BETWEEEN h.start AND h.end
WHERE
cal.d BETWEEN '2022-02-02' AND '2022-02-19'
The join will produce 6 rows, the where trims it to 5, there is your count
Generating the calendar table, or its equivalent (an arbitrary series of dates) is an exercise for the reader.. See something like this
Related
I want to insert the date and month (which is in two datetimepicker) along with insert value select query.
I have five columns in my invoice table
Student_id, Amount, Fee_description, issue_date, month
I can insert the values for the first three columns but the remaining two are null for which I don't know where to put the datetimepicker value??
I take a datatimepicker for date and month in the design view of the form
insert into invoice (Student_id, Amount, Fee_description, issue_date, month)
select
student.Student_id,
Fee.Fee_Amount, Fee.fee_type
from
student
join
parent on student.father_nic = parent.father_nic
join
allocate_class on student.Student_id = allocate_class.Student_id
join
class on class.class_id = allocate_class.class_id
join
session on allocate_class.session_id = session.session_id
join
Fee on class.class_id = Fee.class_id
where
session.session_id = 1
and Fee.fee_type = 'Tution Fee'
and student.status = 'active'
Where to add that two that datetimpicker value in the above query?
Sure. It would look something like this:
var cmd = new SqlCommand("insert into invoice (
Student_id,
Amount,
Fee_description,
issue_date,
month
)
select student.Student_id
, Fee.Fee_Amount
, Fee.fee_type
, #issDat
, #mon
from ...", "conn str here");
cmd.Parameters.AddWithValue("#issDat", issueDateDateTimePicker.Value);
cmd.Parameters.AddWithValue("#mon", monthDateTimePicker.Value);
I've used AddWithValue to quickly explain the concept- google for a blog called "can we stop using AddWithValue already" for a long discussion on how to move away from it(it's reasonable in this context as it's not being used for filtering) but for now the concept I'm trying to relate is:
An sql statement can take a fixed value from you:
SELECT 'hello' FROM table
It can also take a fixed parameter you supply:
SELECT #pHello FROM table
Hence adding parameters and filing them with fixed values from your day time pickers (or datetime.Now or whatever) is fine, and what you should be doing to insert fixed values using an INSERT ... SELECT style statement
Side note, it isn't clear if month and issue date are related but if they're the same date then you don't need to store it twice- you can extract the month from a date at any point.
I have 3 columns in table which are Id(int) , Value(int) and Time(datetime). I want query that get data(s) for 1st 10 mins then 2nd set of 10 mins and continued on..
You can use
DATEDIFF ( datepart , startdate , enddate )
Your Query should be like
SELECT id, value, Time FROM Table
WHERE DATEDIFF(MINUTE, Time, GETDATE()) <= 10
first time I ask a question here, I'm coding a reservations application on C# using Visual Studio 2015, but I have a problem trying to show free rooms on a data grid view, here is the query i'm using:
SELECT clientID, cost, arrival, roomNumber, resvNum, departure, size
FROM roomswithresvView
WHERE (roomNumber NOT IN
(SELECT roomNumber
FROM roomswithresvView AS roomswithresvView_1
WHERE (arrival BETWEEN #date1 AND #date2)
OR (departure BETWEEN #date1 AND #date2)))
The problem is that if a room has more than one reservation, the query will show it multiple times, I have tried using DISTINCT but I can only make work with one column and I haven't been able to make GROUP BY work.
Thanks for your attention.
Query Sample
For example, if I test the query with 2016-07-06 as date1 and 2016-07-07 as date2, it will repeat room 1005 because it has two reservations on the database.
Where were you putting the DISTINCT?
You need a table for rooms, and a table for reservations. Then you need a subquery to find reservations that clash with your requested dates. This is where you use DISTINCT. Then you need an outer query to find all rooms not returned in the subquery. Don't forget the case where you have an existing reservation that starts before and ends after your requested dates! Putting that all together, you get this...
insert into room(costPerNight, roomNumber, size)
values
(55, 1, 13),
(65, 2, 15),
(85, 3, 20)
;
create table reservation(
id int identity (1,1) not null,
roomId int not null,
dateIn date not null,
dateOut date not null
)
insert into reservation (roomId, dateIn, dateOut)
values
(1,'2016-07-01','2016-07-03'),
(1,'2016-07-05','2016-07-08'),
(2,'2016-07-01','2016-07-08')
*/
declare #requestedDateIn date = '2016-07-03'
declare #requestedDateOut date = '2016-07-05';
select * from room where id not in(
--find clashing reservations
select distinct roomId from reservation where
(dateOut > #requestedDateIn and dateOut < #requestedDateOut)
or (dateIn > #requestedDateIn and dateIn < #requestedDateOut)
or (dateIn < #requestedDateIn and dateOut > #requestedDateOut)
)
I have a table Sample with some fields
EmpCode=a,OffID=1, FromDate=2015-01-01, ToDate='2015-01-10'
EmpCode=a,OffID=2, FromDate=2015-01-11, ToDate='2015-01-20'
EmpCode=a,OffID=1, FromDate=2015-01-21, ToDate='2015-01-29'
EmpCode=a,OffID=1, FromDate=2015-01-30, ToDate='2015-02-02'
I have 2 dropdown list to select month and year.I want the count of offID in month=1 and year=2015. how can I get it. See last OffID todate is 2015-02-01
I am assuming you need count of offID for the each month and when that offID belongs to 1st month for both todate and fromdate and also same for the year You can do something like this
Select count(offiD)
FROM tablename
WHERE (MONTH(todate) = 1 AND MONTH(fromDate) =1 )
AND (YEAR(fromDate) = 2015 AND YEAR(todate) = 2015)
If you want to filter it for only one date then just remove the second date filter.
Update
You need to put the single quote in your query if you are going to check with hardcoded values
Your Query
Select count(offiD)
FROM
Offdayallocation
WHERE
(MONTH(2015-01-10) = 1 AND MONTH(2015-01-01) =1 )
AND (YEAR(2015-01-01)= 2015 AND YEAR(2015-01-10) = 2015)
and EmpCode='a'
Change the condition like this,
(MONTH('2015-01-10') = 1 AND MONTH('2015-01-01') = 1 )
AND (YEAR('2015-01-01')= 2015 AND YEAR('2015-01-10') = 2015)
and EmpCode='a'
This should resolve your issue.
I have to make in c# a query with linq to sql. I can handle it in sql but in linq to
sql is the result not what I wanted to get.
So there is a table with:
a day, in datetime with date and time
and a kind of id
I have to count the ids for each date, the time isn't important. So the result
should be something like:
day: 2013-11-12 amountIDs: 4
People said to me, I can make a select new query and in this query I can set the day
and could count the ids, or I make a group by day. I read similar question, but it doesn't work in my case.
Could somebody help me?
I tried it with the statement below, but the days have to be grouped, so now the output is foreach datetime, like this
day: 12.12.2013 12:00:00 amountIDs: 1
day: 12.12.2013 12:10:10 amountIDs: 1
In sql I made this statement:
SELECT CONVERT(VARCHAR(20), data.dayandtime, 106) AS day, count(data.amountIds) as ids
FROM data
WHERE ( data.dayandtime >= DATEADD(day, -28, getdate()) AND (data.type = 100) AND (data.isSomething = 0) )
GROUP BY CONVERT(VARCHAR(20), data.dayandtime, 106), data.isSomthing and it works.
I saw similar cases, where people made a : from-select-new xyz statement, than I made a view of it and tried to group just the view. Like this
var query = data.GroupBy(g => g.day.Value).ToList();
var qry = from data in dbContext
group data by data.day into dataGrpd
select new
{
day= dataGrpd.Key,
amountIDs= dataGrpd.Select(x => x.Id).Distinct().Count()
};
Check This