I have one variable in which i have #totaltime of employee
and I have one variable in which Employee #basictime
I want to get difference between in two variable
if #totaltime is less then #basictime answer will be negative, other wise show in positive
I use sql server 2008 r2
Can any one help me
You need to use DATEDIFF. The below example would give you the difference in seconds.
DECLARE #totaltime TIME
DECLARE #basictime TIME
SET #totaltime = '05:04:30'
SET #basictime = '10:45:31'
SELECT DATEDIFF(ss, #basictime, #totaltime)
Related
I have been using the following code within my SQL queries:
DECLARE #CurrentDate DateTime = Getdate()
I then reference #CurrentDate instead of using the Getdate() function multiple times within the same query. This is useful because the value of #CurrentDate doesn't change during execution of the query.
However, I have started converting some of these variables to parameters set by SSRS. This allows the user to change the parameter #CurrentDate which is useful for various reasons. Typically the default value is set to "=Now()" or some expression containing the Now() function.
At what stage are these parameters calculated and is there a "correct" way to calculate them if I want parameters to be consistent with one another?
I'm trying to figure out if I should have one parameter for the current date and then reference that in the calculation of my other parameters or if this will produce the same inconsistencies (or worse inconsistencies) as simply using Now() within the expression for each parameter.
It all depends on what you mean by "consistent". Take these two scripts for example:
--Method #1
UPDATE tracker.shipment SET delivery_date = GETDATE() WHERE delivery_id = 1;
WAITFOR DELAY '00:00:01';
UPDATE tracker.shipment SET delivery_date = GETDATE() WHERE delivery_id = 2;
--Method #2
DECLARE #current_date DATETIME = GETDATE();
UPDATE tracker.shipment SET delivery_date = #current_date WHERE delivery_id = 1;
WAITFOR DELAY '00:00:01';
UPDATE tracker.shipment SET delivery_date = #current_date WHERE delivery_id = 2;
This is a pseudo script to update a tracking system to show when delivery was made. I would argue that the first version is more "consistent" as it records when the update was actually made, and not an arbitrary date/ time from when the stored procedure was first entered.
Let's pretend that there's another system constantly checking for delivery items that have a delivery date added, then makes the actual delivery, or that the code immediately before each delivery update does something to trigger the actual delivery. In this case it would be much less consistent to use the second method, as this would record deliveries as being made for some time in the past, before they actually were delivered.
As far as I am aware the parameter sent by SSRS will be evaluated to determine the server time on the reporting server at the point where the user clicks the "View Report" button. So every parameter that has Now() will end up with the same date time, but this might be slightly out from the SQL Server if your servers aren't exactly in synch.
Does this actually matter? Well that depends entirely on what you are doing with the date/ time you are passed I guess. When your users enter a custom date/ time are they entering a date or a full date/ time? I imagine they only enter a date, so this is automatically converted to having a 00:00:00.00 time component?
Basically I would need more context to be able to give a fuller answer to this.
Just create a datetime SSRS parameter and set the default value to NOW(). Then it will assume everything, but users can opt for a different date. Note also that SSRS uses DATETIME parameters, but for a lot of my reports I actually use a string parameter field which I CAST to DATE to avoid losing data between hours that the user isn't expecing:
WHERE CAST(StartDate AS DATE) > CAST(#StartDateParam AS DATE)
I want to build a SSRS report that has column as week numbers - 8 weeks for 8 columns starting with current. This report is run every week and current week number is set then. So both column names and their values should change .Is it possible to build something like this in SSRS?
I tried doing this with a dynamic SQL based stored proc in dataset. However for every run I don't even see the columns values updating dynamically
Here's an example :
Also I am trying to avoid these week numbers as row values and then using matrices
My stored proc looks something like this
declare #n tinyint = datepart(wk, getdate())
declare #n1 tinyint = (#n+1), #n2 tinyint =(#n+2), #n3 tinyint =(#n+3), #n4 tinyint =(#n+4), #n5 tinyint =(#n+5), #n6 tinyint =(#n+6)
exec ('Select b.sku, b.['+#n+'], b.['+#n1+'], b.['+#n2+'], b.['+#n3+'], b.['+#n4+'], b.['+#n5+']...
Will appreciate any help in this direction.. many thanks!
When working with SSRS it's generally best to avoid dynamic SQL and pivoting the data in the SQL. Use the SQL to get the raw data you need and then let SSRS do the pivoting and aggregation. This way you take advantage of what they each do best. I know you said you want to avoid matrices, but it is the best way to make the report dynamic.
So you should either return all the data in one dataset and use filters on your matrices OR write two queries and have each one populate a matrix. BTW a matrix is just a table with a column group added, so don't be intimidated by them.
There are 2 ways to do this with a standard tablix.
Calculate the column headers as expressions using concatenation of Wk and some date math to find the correct week number and return the same sort of thing from your query (e.g. columns are current_week, week_minus_1, week_minus_2...)
Return the column headers as additional columns in your query that are the same value for every row (e.g. ColHeader0, ColHeader1...). Your data columns would still be relative weeks (e.g. ValueWeek0, ValueWeek1...). In your report the column header would have an expression like =First(Fields!ColHeader0.Value). This is a more flexible approach since it lets you pick 8 historical weeks instead of only the last 8 weeks if you add a parameter.
EDIT - Clarifications
The reason that you get the blank column Wk48 is approximately that you have created your report looking for that column that won't be there the next time. SSRS looks for exact columns. You should you use relative column names for either of the options I have specified:
exec ('Select b.sku, b.['+#n+'] as Wk0, b.['+#n1+'] as Wk1, b.['+#n2+'] as Wk2, b.['+#n3+'] as Wk3, b.['+#n4+'] as Wk4, b.['+#n5+'] as Wk5...
This will allow you to populate the aliased Wk0 column with the appropriate current week data and still make sure that it can be consistently referenced as the base week by SSRS.
To change the column headers you can:
Independently calculate the week numbers in SSRS in the column header expressions: ="Wk" + CStr(<correct week calculation>).
Return the column headers in the result set and access them in the column header expression:
exec ('Select b.sku, b.['+#n+'] as Wk0, b.['+#n1+'] as Wk1, b.['+#n2+'] as Wk2, b.['+#n3+'] as Wk3, b.['+#n4+'] as Wk4, b.['+#n5+'] as Wk5..., ''Wk'''+#n+' as ColHeader0, ''Wk'''+#n1+' as ColHeader1...
and reference the returned column headers from the SSRS column header expression as =First(Fields!ColHeader0.Value).
Here's a solution that worked for me:
Create parameters (say CurrWk, CurrWk1) ,set as hidden and store 'Default value' and 'Available value' equals to current week number (datepart(wk, now()) and any subsequent week by doing a +1, +2, +3.. etc.
Write a query expression . Click onto fx beside dataset query space and write the select query for your program embedding parameter values in the expression window. For eg ="Select SKU, [" & Parameter!CurrWk.Value & "] as Wk1,
[" & Parameter!CurrWk.Value & "] as Wk1 from Sales_Table"
Before passing this query as a 'command text expression' please ensure this query is working in sql ssms.
Save the expression. Now find 'Fields' tab on the left hand side panel.You need to map the fields manually from the query here. If this is not done, there is a very high chance you seean empty field list and wont be able to access them at all. This may be because ssrs do not store query metadata directly from expressions.
You can avoid part of the issue by having atleast the static fields , for example here SKU listed in the 'Fields' list by first running a sql query with static field(select SKU from Sales_Table ). You can then go back to update dataset- change query to expression and embed the parameterized field names.
Map field names. In this example I chose 'Query Type' fields and set Field names as SKU, CurrentWeek, NextWeek and mapped to source SKU, Wk and Wk1 respectively.
Click on 'Refresh Fields' at the bottom. Now you have a dataset with the complete field list. Use these in charts, tables . Run it every week and note the numbers changing as expected.
In case you are using this dataset in a table, make sure you set headers with Labels of Parameters (for eg here I did =Parameters!CurrWk.Label for col with current week data)
That's it!
In SSRS, I'm trying to calculate the average number of months a client used a program. The programID is the parameter for the whole report. I'm trying to achieve this (not written with real syntax):
=Avg(Fields!length_of_stay.Value, 0))/30.0 WHERE programid = #ProgramID
Using this question, I came up the the following code which is producing an incorrect answer. I tested in SSMS to get the actual values to compare to SSRS results.
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, 0))/30.0
The "/30" is used since the value is in days and I need months. I think the issue is using the parameter value chosen; this is my first report trying to calculate expressions with parameters.
Avg returns the average of all non-null numeric values. 0 is not null so it gets included in the average, distorting your result for every row with a different PragramId. Try using Nothing instead:
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, Nothing))/30.0
Scenario:
I have a lookup table which has a date column, I need to look at this date column and check if its today's date, if not then wait for 5 mins and check the same thing again, and if the date is current send an email and exit the loop, and if after 6 retries if the date is not current execute a SQL task.
I have a ForLoop Container with the following settings:
InitExpression : #[User::Counter] = 0
EvalExpression : #[User::Counter] < 6
AssignExpression : #[User::Counter] = #[User::Counter] + 1
How / Where do I check the date :
SELECT ControlTimeStamp from LOOKUPTABLE
WHERE ControlTimeStamp = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
Note:
I'm using Business Intelligence Development Studio (BIDS) 2008 for SSIS package development.
I think you'll want an approach like this. Execute your SQL Task to determine whether today is your date. From there you'll need to either sleep for N minutes or you'll want to send an email. The trick is to use an Expression on the Precedence Constraint between the Execute SQL Task the children.
My implementation differs slightly from what yours but the concept remains the same. I created two variables, #ActualDate and #ReferenceDate. #ActualDate is today and #ReferenceDate gets set from the Execute SQL Task. I then see whether they are equivalent. For what you have, if you get a result, then you know the send mail condition has been met so change your Expressions to meet that.
What isn't shown is how to terminate the loop early as I'm not quite certain how to do that.
I am trying to use Reporting Services to create a report displaying the call activity of various sales reps. The report will group by extension and then date of call. For each group of call dates (that is, all the calls for a particular date), I want to display some totals. One of the totals I want to display is the total number of calls whose duration greater than 2 minutes. I can see how to use the RunningValue function to keep a running total of ALL calls for the date, but I'm not sure how to make that conditional on the length of call. Any ideas?
UPDATE: The checked answer below did it... I used a case statement in linq like this:
var qry = from Q in c.CallList
select new
{
Q.Extension,
Q.CallDate,
Q.Duration
CallCountOverTwoMinutes = Q.duration > 120 ? 1 : 0,
};
Then I sum the value of CallCountOverTwoMinutes. Thanks for the help, Chris!
The easiest way would be to pass the value as part of the dataset. For example, using SQL:
SELECT Extension, CallDate, Duration,
CASE WHEN Duration > 2 THEN 1 END AS CallsOver2Mins
FROM CallTable
Then just sum on CallsOver2Mins.