Multiple Condition for a same column in Where clause - sql-server-2008-r2

I need all the task which are not 'Closed' and also the closed task between the selected date range. There are few old task where closeddate is null so I tried as below sample query.
But the result does not include the closed task between the date range.
select *
from task
where id = 5
and ((closedDate is null and staus <>'Closed')
or (closedDate between '7/10/2021 8:00:00 AM' AND '7/16/2021 8:00:00 AM'))`

Related

Find all instances of a date in a date range - SQL Server

I need to find the price for an item for each financial year end date in a date range. In this case the financial year is e.g. 31 March
The table I have for example:
ItemID
Value
DateFrom
DateTo
1
10
'2019/01/01'
'2021/02/28'
1
11
'2021/03/01'
'2021/05/01'
SQL Fiddle
The SQL would thus result in the above table to be:
ItemID
Value
DateFrom
DateTo
1
10
'2019/01/01'
'2019/03/30'
1
10
'2020/03/31'
'2021/02/28'
1
11
'2020/03/01'
'2021/03/30'
1
11
'2020/03/31'
'2021/05/01'
You can solve it, but a prerequisite is the creation of a table called financial_years and filling it with data. This would be the structure of the table:
financial_years(id, DateFrom, DateTo)
Now that you have this table, you can do something like this:
select ItemID, Value, financial_years.DateFrom, financial_years.DateTo
from items
join financial_years
on (items.DateFrom between financial_years.DateFrom and financial_years.DateTo) or
(items.DateTo between financial_years.DateFrom and financial_years.DateTo)
order by financial_years.DateFrom;
The accepted answer is not correct, as it does not split out different parts of the year which have different values.
You also do not need a Year table, although it can be beneficial. You can generate it on the fly using a VALUES table.
Note also a better way to check the intervals overlap, using AND not OR
WITH Years AS (
SELECT
YearStart = DATEFROMPARTS(v.yr, 3, 31),
YearEnd = DATEFROMPARTS(v.yr + 1, 3, 31)
FROM (VALUES
(2015),(2016),(2017),(2018),(2019),(2020),(2021),(2022),(2023),(2024),(2025),(2026),(2027),(2028),(2029),(2030),(2031),(2032),(2033),(2034),(2035),(2036),(2037),(2038),(2039)
) v(yr)
)
SELECT
i.ItemID,
i.Value,
DateFrom = CASE WHEN i.DateFrom > y.YearStart THEN i.DateFrom ELSE y.YearStart END,
DateTo = CASE WHEN i.DateTo > y.YearEnd THEN y.YearEnd ELSE i.DateTo END
FROM items i
JOIN Years y ON i.DateFrom <= y.YearEnd
AND i.DateTo >= y.YearStart;

Number of days in a month in DB2

Is there a way to find the number of days in a month in DB2. For example I have a datetime field which I display as Jan-2020, Feb-2020 and so on. Based on this field I need to fetch the number of days for that month. The output should be something like below table,
I'm using the below query
select reportdate, TO_CHAR(reportdate, 'Mon-YYYY') as textmonth from mytable
Expected output
ReportDate textMonth No of Days
1-1-2020 08:00 Jan-2020 31
1-2-2020 09:00 Feb-2020 29
12-03-2020 07:00 Mar-2020 31
Try this:
/*
WITH MYTABLE (reportdate) AS
(
VALUES
TIMESTAMP('2020-01-01 08:00:00')
, TIMESTAMP('2020-02-01 09:00:00')
, TIMESTAMP('2020-03-12 07:00:00')
)
*/
SELECT reportdate, textMonth, DAYS(D + 1 MONTH) - DAYS(D) AS NO_OF_DAYS
FROM
(
SELECT
reportdate, TO_CHAR(reportdate, 'Mon-YYYY') textMonth
, DATE(TO_DATE('01-' || TO_CHAR(reportdate, 'Mon-YYYY'), 'dd-Mon-yyyy')) D
FROM MYTABLE
);
Db2 has the function DAYS_TO_END_OF_MONTH and several others which you could use. Based on your month input, construct the first day of the month. This should be something like 2020-01-01 for Jan-2020 or 2020-02-01 for Feb-2020. Follow the link for several other conversion functions which allow you to transform between formats and to perform date arithmetics.
convert your column to a proper date and try this: day(last_day(date_column))

How to count events after set date with COUNTIFS?

I'm creating this report in spreadsheet
https://docs.google.com/spreadsheets/d/1U48MybVshKT3eRYE9goRCab9k1KCoXe7Zsr9Ogkkm4Y/edit?usp=sharing
In "BD" you can find the records and in "NB-month" the KPI that I want to analyze; I would like to create DATE filter for the numbers in the columns, ex for the col: "nÂș Contract closed" I'm thinking to use this formula:
=COUNTIFS(BD!R:R;A5;BD!G:G;">"&DATE(B2))
but I get a value: "0" that is incorrect because for this agent "Carla Vaello" the number of contracts closed after 2020-02-01 should be "2".
your usage of DATE formula is wrong (DATE requires 3 parameters). either change DATE to DATEVALUE or use:
=COUNTIFS(BD!R:R; A5; BD!G:G; ">"&B2)

SSRS Count Records expression with date less than group by date

I want to count records that are not marked as completed with a Received date less than the Received date of the row (group by Received date "Details") This will be the Start of Day column showing how many records are in queue.
I have a tablix in VS 2017 SSDT.
Tablix is grouped by Received Date
COLUMNS
Received Date (group by Details) another column same field (Textbox5)
Start of Day
New Tasks
Completed
I'm having an issue with the code logic for Start of Day column field.
I want to count records that are not marked as completed with a Received date less than the Received date of the row (group by Received date "Details")
This code works for New Tasks column.
=COUNT(IIF(Fields!Received.Value < Fields!Received.Value AND
Fields!Completed.Value = "NO", 1,0),"Details")
When I attempt the Start of Day expression I get errors. Textbox5 is the same dataset field used in group by field (Received). I added it to test different approach.
=COUNT(IIF(Fields!Received.Value < ReportItems!Textbox5.Value AND
Fields!Completed.Value = "NO", 1,0),"Details")
Error: rsAggregateReportItemlnBody aggregate functions can be used
only on report items contained in page headers and footers.
Sample data and expected output for Start of Day Column: it should count records in the group by row if they were in queue prior to start of day (yesterday).
You can use running value to calculate the total. To get the prior date total you subtract the group total value
Start Of Day
= RunningValue( 1, SUM, "Tablix1")- SUM( 1)
New Task
= SUM(1) or COUNT(1)
Complete
= RunningValue( Iif(Fields!CompYN.Value = "YES",1,0), SUM, "Tablix1")- SUM( Iif(Fields!CompYN.Value = "YES",1,0))
New Tasks column: =COUNT(IIF(Fields!Received.Value = Fields!Received.Value AND Fields!Completed.Value = "NO", 1,0),"Details")
Start of day column: =COUNT(IIF(Fields!Received.Value < Fields!Received.Value AND Fields!Completed.Value = "NO", 1,0),"Details")

Bulk insert with variable date field for non existing dates?

Attendence
(
Stu_id int
Att_Id int
Att_Date datetime
Att_Num numeric(15,5)
)
This table basically contains attendence records. I am trying to find the logic to enter the rows for missing dates from 1 Jan 2012 till today.
Assume there is a single attendence record for this period row (1,1,'2012-05-06',1.20000). Then I would like to insert rows for each day from 1 Jan 2012 till today except the existing date with the same values for all the fields except the date field which should be the actual date.
I am trying to bulk insert all the rows but don't know how would I adjust the date field
and check for the existing date.
Thanks.
Try a stored procedure which loops through the dates.
Or better yet, make a dates table and select from that. This will be much faster. Something like:
INSERT INTO Attendence (Stu_id, Att_Id, Att_Date, Att_Num)
SELECT a.Stu_id, a.Att_Id, d.TheDate, a.Att_Num
FROM Attendence a
INNER JOIN Dates d ON d.TheDate BETWEEN '2012-01-01' AND GETDATE()
AND d.TheDate <> a.Att_Date
I would use the DateDiff function to figure out the amount of days inbetween the date in the function and the date in question. Make a DateTime variable and give it the value of 1 Jan 2012. Then set up a while loop that loops the amount of days from the DateDiff function.
In psuedo, the while loop would look something like this.
#DaysTill = DateDiff(days, 1/1/2012, #SomeDate)
While #DaysTill>0
##Check = Select Count(Att_Date) From Attendance where AttDate = DateAdd(days, #DaysTill, Jan 1st 2012)
IF ##Check = 0 THEN Insert Into Attendance VALUES #Stu, #Id, DateAdd(days, #DaysTill, Jan 1st 2012), #numeric
#DaysTill = #DaysTill - 1
END
The main things are understanding DateAdd and DateDiff. After that everything becomes relatively simple.