here is my result set but when i am doing query on the result set then no data is coming but i am not being able to understand what is the wrong in my query.
call Start Caller direction Is_Internal continuation call duration party1name
------------------ ------------ --------- ----------- ------------ ------------------ -----------------
1/15/2014 8:47 346241552 I 0 0 0:00:18 VM Kanaal 1
1/15/2014 9:56 252621028 I 0 0 0:00:17 Kanaal 1
1/15/2014 9:58 252621028 I 0 0 0:00:17 Kanaal 1
1/15/2014 9:01 252621028 I 0 1 0:00:08 Kanaal 1
1/15/2014 9:01 252621028 I 0 0 0:01:57 Coen
1/15/2014 9:06 302 O 0 0 0:01:53 Coen
1/15/2014 9:07 306 O 0 0 0:01:33 koos de Bruijn
1/15/2014 9:11 644686793 I 0 0 0:00:08 VM Kanaal 1
1/15/2014 9:11 644686793 I 0 0 0:01:46 Coen
1/15/2014 9:27 306 O 0 0 0:00:43 koos de Bruijn
1/15/2014 9:25 302 O 0 0 0:06:46 Coen
1/15/2014 9:46 455426194 I 0 1 0:00:07 VM Kanaal 1
1/15/2014 9:46 455426194 I 0 0 0:00:50 Coen
1/15/2014 9:57 725716251 I 0 1 0:00:10 VM Kanaal 1
1/15/2014 10:00 0 I O 1 0:00:00 Voicemail
my query is here
SELECT Convert(varchar,[call Start],101) Date,[Caller] [Phone No], Count(*) [Total Incomming calls] FROM tridip
where direction='I' and
CAST([call Start] AS datetime) >= CONVERT(datetime,'09:00:00') and
CAST([call Start] AS datetime) <= CONVERT(datetime,'17:30:00')
AND Is_Internal=0 and continuation=0 AND
CONVERT(datetime,CAST([call duration] AS DATETIME),108) <> CAST('00:00:00' AS DATETIME)
and party1name not in ('Voice Mail') and party1name not like 'VM %'
and party1name not like 'Line%'
group by [Caller],Convert(varchar,[call Start],101)
just tell me what is wrong my query. it should show date & phone number and count may be 1 or more than 1. please guide me what to alter.
at the second row there is phone no 252621028 which is repeated and it's count should be greater than 1.
thanks
UPDATE
i figured out the problem and rectified sql as follow
SELECT [Caller] [Phone No], Count(*) [Total Incomming calls] FROM tridip
where direction='I' and
CONVERT(VARCHAR,[call Start],108) >= '09:00:00' and
CONVERT(VARCHAR,[call Start],108) <= '17:30:00'
AND Is_Internal=0 and continuation=0 AND
[call duration] <> '00:00:00'
and party1name not in ('Voice Mail') and party1name not like 'VM %'
and party1name not like 'Line%' and [Caller]>0
group by [Caller]
You are not getting results most probably because of this condition -
CAST([call Start] AS datetime) <= CONVERT(datetime,'17:30:00')
Reason -
Try this -
SELECT CONVERT(datetime,'17:30:00')
The result is - 1900-01-01 17:30:00.000
I believe none of your records have [call start] time less than this and hence no results.
Are your trying to get all the records between 2 times ?
As has been previously mentioned, you are getting a DateTime with the value of 1900-01-01 09:00, which is obviously lower than any of your dates.
You can also use the DATEPART function of SQL Server to compare dates and times easier:
where direction='I'
and DATEPART(hour, CAST([call Start] AS datetime)) >= 9
and DATEPART(hour, CAST([call Start] AS datetime)) <= 17
(You will need to modify this slightly to work for being before 17:30, but it gives you the general idea).
Related
I have data formatted like this:
ID
Date Created
Date Ready to Start
Date In Progress
Date Dev Complete
Date Test Complete
Date Accepted
1
2021-11-01 12:01:15
2021-11-02 14:01:15
2021-11-04 05:01:15
2021-11-04 12:01:15
2021-11-05 12:01:15
2021-11-06 12:01:15
2
2021-11-01 12:01:45
NULL
2021-11-03 12:01:15
NULL
NULL
2021-11-05 12:01:15
3
2021-11-03 11:11:05
2021-11-04 12:01:15
NULL
NULL
NULL
NULL
4
2021-11-05 19:31:45
2021-11-05 12:01:15
2021-11-06 12:01:15
NULL
NULL
NULL
5
2021-11-04 13:21:25
NULL
NULL
NULL
NULL
NULL
and I need it formatted like this:
Date
Created
Ready to Start
In Progress
Dev Complete
Test Complete
Accepted
2021-11-01
2
0
0
0
0
0
2021-11-02
0
1
0
0
0
0
2021-11-03
1
0
1
0
0
0
2021-11-04
0
1
1
1
0
0
2021-11-05
1
1
0
0
1
1
2021-11-06
0
0
1
0
0
1
with rolling dates going back 3 months from current date.
I am unsure of how to start this... Would I union a recursive table creating the rolling calendar to the existing data or would I do a series of custom selects for counts and then group by date?
Try something like this:
DECLARE #Today date = GetDate();
DECLARE #MinDate date = DateAdd(month, -3, #Today);
DECLARE #DayCount int = 1 + DateDiff(day, #MinDate, #Today);
WITH cteNumbers As
(
/* Use a tally-table here if you have one: */
SELECT TOP (#DayCount)
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) As N
FROM
sys.all_columns
),
cteCalendar As
(
SELECT
DateAdd(day, N, #MinDate) As [Date],
DateAdd(day, N + 1, #MinDate) As NextDay
FROM
cteNumbers
)
SELECT
C.[Date],
(
SELECT Count(1)
FROM YourTable As T
WHERE T.[Date Created] >= C.[Date]
And T.[Date Created] < C.NextDay
) As [Created],
(
SELECT Count(1)
FROM YourTable As T
WHERE T.[Date Ready to Start] >= C.[Date]
And T.[Date Ready to Start] < C.NextDay
) As [Ready to Start],
...
FROM
cteCalendar As C
;
I have a #TMP table filled with dates with its corresponding remarks. It looks like this:
Date isworkdays isweekdays
2019-08-16 1 1
2019-08-17 0 0
2019-08-18 0 0
2019-08-19 1 1
2019-08-20 1 1
2019-08-21 1 1
2019-08-22 1 1
2019-08-23 1 1
2019-08-24 0 0
2019-08-25 0 0
2019-08-26 1 1
2019-08-27 1 1
2019-08-28 1 1
2019-08-29 1 1
2019-08-30 1 1
2019-08-31 0 0
2019-09-01 0 0
2019-09-02 1 1
2019-09-03 1 1
2019-09-04 1 1
2019-09-05 1 1
2019-09-06 1 1
2019-09-07 0 0
2019-09-08 0 0
2019-09-09 1 1
2019-09-10 1 1
2019-09-11 1 1
2019-09-12 1 1
2019-09-13 1 1
2019-09-14 0 0
2019-09-15 0 0
2019-09-16 1 1
2019-09-17 1 1
2019-09-18 1 1
2019-09-19 1 1
2019-09-20 1 1
2019-09-21 0 0
2019-09-22 0 0
2019-09-23 1 1
2019-09-24 1 1
2019-09-25 1 1
2019-09-26 1 1
2019-09-27 1 1
2019-09-28 0 0
2019-09-29 0 0
2019-09-30 1 1
Date column are obviously a series of dates from 2019-08-16 to 2019-10-16
isworkingdays column indicates "1" if the date is from Monday to Friday, and "0" if its Saturday and Sunday. isweekdays column indicates "1" if the date is not holiday and "0" if its holiday.
I want to count the days betweem 2019-08-16 to 2019-09-16 only to those dates with isworkdays = 1, and for Saturday and Sunday count it as 1
This is what I have done so far
declare #userinput date
SELECT SUM(IsWorkDay) AS DayCount FROM AMIFIN..PDC T
LEFT JOIN dbo.#TMP c ON c.[Date] <= T.Check_Date
WHERE CAST(#userinput AS Date) <= CAST((c.[Date]) as date) AND c.IsWorkDay = 1
but it returns 3952743 days
Conditional aggregation should work here:
SELECT
SUM(CASE WHEN isworkdays = 1 THEN 1 ELSE 0.5 END) AS DayCount
FROM AMIFIN..PDC T
LEFT JOIN dbo.#TMP c
ON c.[Date] <= T.Check_Date
WHERE
#userinput < c.[Date];
This idea here is to count 1 for a weekday, and 1/2 for a weekend day. The half counting logic is necessary, because it could be that we end up counting an odd number of weekend days.
I have figured it out. Thanks to all who posted an answer it gives me a lot of idea.
Here is my solution:
Declare #cnt INT = 0
DECLARE #postponedate date = cast('2019-08-20' as date); --Will be the current date of postponement
DECLARE #checkdate date = cast('2019-08-27' as date); --Date of the check
Declare #fdate date = (select [Date] FROM #TMP WHERE [Date] = #checkdate); --dates to be compared
WHILE #postponedate <= #fdate
BEGIN
DECLARE #IsWorkDay INT = (select IsWorkDay FROM #TMP WHERE cast([Date] as date) = #postponedate)
DECLARE #IsWeekDay INT = (select IsWeekDay FROM #TMP WHERE cast([Date] as date) = #postponedate)
SET #cnt = CASE WHEN #IsWorkDay = 1 THEN
#cnt + 1
ELSE
#cnt + 0.5
END
SET #postponedate = DATEADD(Day, 1, #postponedate)
PRINT #postponedate
PRINT #cnt
END
I think your query will be like this:
SELECT SUM(DATEDIFF(DAY, date, date) + 1) AS Total
FROM tbl_TMP
WHERE date >= '2019-08-16' AND date <= '2019-09-30'
AND isworkdays=1 AND isweekdays=1
GROUP BY isworkdays,isweekdays
Note: "+1" because DATEDIFF returns No. of days in INT DataType
I have a following table called lobby
QueueID FkBranch IsActive Status AddedLocalTime CompletedTime FkAssistTypeID
553279 16 1 5 7/12/2019 20:06 7/12/2019 21:10 2
553278 16 1 5 7/12/2019 20:07 7/12/2019 21:11 1
553277 16 1 5 7/12/2019 20:08 7/12/2019 21:10 1
553276 16 1 5 7/12/2019 20:09 7/12/2019 21:11 1
553275 16 1 5 7/13/2019 20:10 7/13/2019 21:10 2
553274 16 1 5 7/13/2019 20:11 7/13/2019 21:11 2
553278 17 1 5 7/14/2019 20:07 7/14/2019 21:11 1
553277 17 1 5 7/14/2019 20:08 7/14/2019 21:10 1
553276 18 1 5 7/14/2019 20:09 7/14/2019 21:11 2
553275 18 1 5 7/15/2019 20:10 7/15/2019 21:10 2
553274 18 1 5 7/15/2019 20:11 7/15/2019 21:11 2
And Branch table and Its data as follows
BranchID BranchName IsActive
16 Delhi 1
17 Karnataka 1
18 Telangana 1
Now I need to get a count of FkAssistTypeID of each location between AddedLocalTime and also need to take summation of the time difference of AddedLocalTime and CompletedTime.
I have a function to get the time Difference of two dates and it as follows
dbo.fnTimetoSeconds(AddedLocalTime, CompletedTime, NULL)
CREATE FUNCTION [dbo].[fnTimetoSeconds]
(
#dateOne DATETIME,#dateTwo DATETIME,#dateToConvert DATETIME
)
RETURNS INT
AS
BEGIN
DECLARE #date DATETIME
DECLARE #retValue INT
IF(#dateToConvert IS NULL)
BEGIN
SET #dateToConvert = CASE WHEN(#dateTwo>#dateOne) THEN #dateTwo-#dateOne ELSE #dateOne-#dateTwo END
END
SET #date = DATEADD(D, 0, DATEDIFF(D, 0, #dateToConvert))
IF(DATEPART(yy,#dateToConvert) = 1900)
BEGIN
SET #retValue = DATEDIFF(s,#date,#dateToConvert) + CASE WHEN DATEDIFF(D, 0, #dateToConvert) > 0 THEN DATEDIFF(D, 0, #dateToConvert) ELSE 0 END * 3600 * 24
END
ELSE
BEGIN
SET #retValue = DATEDIFF(s,#date,#dateToConvert)
END
RETURN #retValue
END
My expected output is,
* Please be noted, This Average column need to calculate, Suppose
when FkAssistTypeID = 1 and AddedLocalTime between 7/12/2019 and 7/14/2019 then by passing that row's AddedLocalTime and CompletedTime values fnTimetoSeconds taking time differance and take the summation of each time diffrences and divide it by count .
I need to add the above output to a temporary table. How can I do this?
I just tried this, but this is not the expected
select
b.BranchId AS ID,
b.BranchName,
count(case l.FkAssistTypeId when 1 then 1 end) as CountOf1,
SUM(CASE WHEN (l.FkAssistTypeId = 1) THEN COALESCE((dbo.fnTimetoSeconds(CompletedTime, AssistedTime, NULL)),0) ELSE 0 END) AS Average
from Branch b left join Lobby l
on b.BranchId = l.FkBranchId
where l.IsActive = 1 AND b.IsTestBranch = 0 AND CAST(l.AddedLocalTime as DATE) = '2019-07-12'
group by b.BranchId, b.BranchName
How about something like this :
SELECT Vals.ID,
Vals.BranchName ,
Vals.CountOf1,
CASE WHEN (Vals.CountOf1 = 0) THEN 0 ELSE Vals.mySum/Vals.CountOf1 END as AveSecs
INTO tmpTbl
FROM
(SELECT
b.BranchId AS ID,
b.BranchName,
count(l.FkAssistTypeI) as CountOf1,
SUM(DATEDIFF(second, CompletedTime, AssistedTime)) AS mySum
FROM Branch b
LEFT OUTER JOIN Lobby l
ON b.BranchId = l.FkBranchId
WHERE l.IsActive = 1
AND b.IsTestBranch = 0
AND l.FkAssistTypeI = 1
AND l.AddedLocalTime >= '2019/07/12'
AND l.AddedLocalTime < DATEADD(day, 1, '2019/07/15')
GROUP BY b.BranchId, b.BranchName) as Vals
The query first gets the count and the sum between 2019/07/12 00:00 and 2019/07/15 00:00 (not including). Then it inserts into a temp table (as you indicated) while calculating the average value. Note that you will need to test it and adjust it a bit since I have not run it. Also, I just used date diff to calculate the time in seconds, but you can use your function if needed. They should come up with the same value most times.
Not sure why you use the scalar function to get the date difference, even with the current logic, it's possible to do it in the query without the need of the function.
SELECT
b.BranchID
, b.BranchName
, ISNULL(l.CountOF1,0) CountOF1
, ISNULL(l.Average ,0) Average
FROM
#Branch b
LEFT JOIN (
SELECT
FkBranch
, IsActive
, COUNT(*) CountOF1
, AVG(DATEDIFF(SECOND, AddedLocalTime, CompletedTime)) Average
FROM #lobby l
WHERE
FkAssistTypeID = 1
AND AddedLocalTime BETWEEN '7/12/2019 00:00:00' AND '7/14/2019 23:59:59'
GROUP BY FkBranch, IsActive
) l ON l.FkBranch = b.BranchID AND l.IsActive = b.IsActive
WHERE
b.IsActive = 1
I have this record in my sql server..
RecId RefJobTicket TimeStart TimeEnd VerByStart VerByEnd Cargo PreTrip Transit LoadingUnloading WaitForAdvice MealBreak Breakdown PostTrip Refuel Remarks
2 1 12:00 1:00 NULL NULL NULL 0 0 0 0 0 0 NULL 0 NULL
49 1 3:00 4:00 NULL NULL NULL 0 0 0 0 0 0 NULL 0 NULL
50 1 5:00 8:00 NULL NULL NULL 0 0 0 0 0 0 NULL 0 NULL
In my SSRS REPORT.. I want to put it in a Pre Defined form that look like below according to the available records i have in the sql server:
time start time end Refuel Trip Meal Break
12:01AM 1:00AM 0 Null Null
1:01AM 2:00AM
2:01AM 3:00AM
3:01AM 4:00AM Null 0 Null
4:01AM 5:00AM
5:01AM 6:00AM Null Null 0
6:01AM 7:00AM Null Null 0
7:01AM 8:00AM Null Null 0
8:01AM 9:00AM
9:01AM 10:00AM
10:01AM 11:00AM
11:01AM 12:00PM
12:01PM 1:00PM
1:01PM 2:00PM
2:01PM 3:00PM
3:01PM 4:00PM
4:01PM 5:00PM
5:01PM 6:00PM
6:01PM 7:00PM
7:01PM 8:00PM
8:01PM 9:00PM
9:01PM 10:00PM
10:01PM 11:00PM
11:01PM 12:00AM
Can i do this in ssrs r2?? Can any one help me?
Yes, You can do it. you need to take care of following points:
Step1:
your query should return all the times (12:01AM to 12:00AM). You can do a cross join of following query on your main query. Note that, Report can not generate those times that are not coming from your sql query.
;with Minute_Cycle
as
(
select cast('12:01AM' as time) Mint
Union ALL
Select DATEADD(HOUR,1,cast(Mint as time)) Mint from Minute_Cycle
where convert(varchar(15),cast(Mint as time),100)<>'11:01PM'
)
Select convert(varchar(15),Mint,100) as time_start, convert(varchar(15),DATEADD(MINUTE,59,Mint),100) time_End from Minute_Cycle
Step2
Place a table and design your report as per your choice.
I do not quite understand why those two different codesamples return a different value.
somehow incorrect but working syntax, returns false results, e.g it returns 0 when the comparison is done over two equal values:
(SELECT CASE
WHEN
SUM(V.IsCompatible) OVER
(PARTITION BY ComputerName, UserID) = ApplicationCount
THEN 1 ELSE 0 END
) AS CompatibleUser
The one below returns the correct values, ie. 1 when there are two equal values compared.
(CASE
WHEN
SUM(V.IsCompatible) OVER
(PARTITION BY ComputerName, UserID) = ApplicationCount
THEN 1 ELSE 0 END
) AS CompatibleUser
or even simpler:
(SELECT CASE
WHEN
X = Y
THEN 1 ELSE 0 END
) AS Result
X = 22 AND Y = 22 => Result = 0
(CASE
WHEN
X = Y
THEN 1 ELSE 0 END
) AS Result
X = 22 AND Y = 22 => Result = 1
I understand applying the correct syntax is important, and I am aware of the SELECT CASE syntax in T-SQL, but I do not understand how the first code sample is evaluated and delivering an unexpected result.
update: full query in it's context
select userapplication.username,
computerdetails.computername,
sum(userapplication.iscompatible)
over (partition by computerdetails.computername,
userapplication.userid) as compatiblecount,
userapplication.applicationcount,
( case
when sum(userapplication.iscompatible)
over (partition by
computerdetails.computername,
userapplication.userid) <> userapplication.applicationcount
then 0
else 1
end
) as usercomputeriscompatible
from computerdetails
right outer join usercomputer
on computerdetails.computerid = usercomputer.computerid
right outer join userapplication
on usercomputer.gebruikerid = userapplication.userid
so userComputerIsCompatible is the result in question here
I think the reason for this behavior is the next one: the expressions like (SELECT ...) are considered to be sub-queries even they don't have FROM clause. Is assume the source of data for these (false) "sub-queries" is only the current row. So, (SELECT expression) is interpreted as (SELECT expression FROM current_row) and (SELECT SUM(iscompatible)OVER(...)) is executed as (SELECT SUM(iscompatible)OVER(current_row)).
Argument: analyzing execution plan for (SELECT SUM(IsWeb) OVER(PARTITION BY OrderDate) [FROM current_row]) expression
I see a Constant Scan (Scan an internal table of constants) operator instead of Clustered Index Scan before Segment and Stream Aggregate ([Expr1007] = Scalar Operator(SUM(#OrderHeader.[IsWeb] as [h].[IsWeb]))) operators. This internal table (Constant Scan) is constructed from current row.
Example (tested with SQL2005SP3 and SQL2008):
DECLARE #OrderHeader TABLE
(
OrderHeaderID INT IDENTITY PRIMARY KEY
,OrderDate DATETIME NOT NULL
,IsWeb TINYINT NOT NULL --or BIT
);
INSERT #OrderHeader
SELECT '20110101', 0
UNION ALL
SELECT '20110101', 1
UNION ALL
SELECT '20110101', 1
UNION ALL
SELECT '20110102', 1
UNION ALL
SELECT '20110103', 0
UNION ALL
SELECT '20110103', 0;
SELECT *
,SUM(IsWeb) OVER(PARTITION BY OrderDate) SumExpression_1
FROM #OrderHeader h
ORDER BY h.OrderDate;
SELECT *
,(SELECT SUM(IsWeb) OVER(PARTITION BY OrderDate)) SumWithSubquery_2
FROM #OrderHeader h
ORDER BY h.OrderDate;
Results:
OrderHeaderID OrderDate IsWeb SumExpression_1
------------- ----------------------- ----- ---------------
1 2011-01-01 00:00:00.000 0 2
2 2011-01-01 00:00:00.000 1 2
3 2011-01-01 00:00:00.000 1 2
4 2011-01-02 00:00:00.000 1 1
5 2011-01-03 00:00:00.000 0 0
6 2011-01-03 00:00:00.000 0 0
OrderHeaderID OrderDate IsWeb SumWithSubquery_2
------------- ----------------------- ----- -----------------
1 2011-01-01 00:00:00.000 0 0
2 2011-01-01 00:00:00.000 1 1
3 2011-01-01 00:00:00.000 1 1
4 2011-01-02 00:00:00.000 1 1
5 2011-01-03 00:00:00.000 0 0
6 2011-01-03 00:00:00.000 0 0
I tried your code and I get the same results for both queries. Here's the code I tried:
DECLARE #X INT = 22
DECLARE #Y INT = 22
SELECT (SELECT CASE
WHEN
#X = #Y
THEN 1 ELSE 0 END
) AS Result
SELECT (CASE
WHEN
#X = #Y
THEN 1 ELSE 0 END
) AS Result
Result is 1 and 1
I ran this on SQL Server 2008 R2