I have the following data set:
Item || Date || Client ID || Date difference
A || 12/12/2014 || 102 ||
A || 13/12/2014 || 102 || 1
B || 12/12/2014 || 141 ||
B || 17/12/2014 || 141 || 5
I would like to calculate the difference in years between the two dates when the client ID is the same. What expression can I use in a calculated column to get that value?
UPDATE
Hi
This would be the intended values calculated. My table has approximately 300,000 records in no particular order. Would I have to sort the physical table before using this formula? I used this example from another I found, my actual file has no item column. It is only the client ID, and date of the transaction. Thanks again for the help!
ClientId Date Days
102 2014.12.12 0
102 2014.12.13 1
141 2014.12.12 0
141 2014.12.17 5
123 2014.12.01 0
123 2014.12.02 1
123 2014.12.04 2
I used the following solution to deal with groups that had more than 2 rows/dates.
First create a calculated column to provide a rank order by date within each group:
RankDatePerUnit:
Rank([EventDate],[Group_Name])
Then another calculated column to do the date diff using an over expression to reference the previous date within the group.
TimeSinceLastEvent:
DateDiff("day",
First([EventDate]) OVER (Intersect([Group_Name], Previous([RankDatePerUnit]))),
[EventDate])
Note: Duplicate date could be handled differently by using denserank. The above approach will not calculate a zero date diff between two rows from the same group with a duplicate time. They'll both calculate their delta from an earlier date within the same group if one exists.
EDIT 2015.07.15
got it, so if you want the difference from the last customer-date pair. this expression will give you the table you've listed above. spacing for readability:
DateDiff('day',
First([Date) OVER (Intersect([ClientId], Previous([Date]))),
[Date]
)
EDIT 2015.07.13
if you want to reduce this so that you can accurately aggregate [Days], you can surround the above expression with an If(). I'll add some spacing to make this more readable:
If(
[Date] = Min([Date]) OVER Intersect([ClientId], [Item]),
DateDiff( 'day',
Min([Date]) OVER Intersect([ClientId], [Item]),
Max([Date]) OVER Intersect([ClientId], [Item])
)
, 0
)
in English: "If the value of the [Date] column in this row matches the earliest date for this [ItemId] and [ClientId] combination, then put the number of days difference between the first and last [Date] for this [ItemId] and [ClientId] combination; otherwise, put zero."
it results in something like:
Item ClientId Date Days
A 102 2014.12.12 1
A 102 2014.12.13 0
B 141 2014.12.12 5
B 141 2014.12.17 0
C 123 2014.12.01 2
C 123 2014.12.02 0
C 123 2014.12.03 0
WARNING that filters may break this calculation. for example, if you are filtering based on [Date] and, with the above table as an example, filter OUT all dates before 2014.12.13, Sum([Date]) will be 7 instead of 8 (because the first row has been filtered out).
you can use Spotfire's OVER functions to look at data points with common IDs across rows.
it looks like you've only got two rows per Client ID and Item ID, which helps us out! use the following formula:
DateDiff('day', Min([Date]) OVER Intersect([ClientId], [Item]), Max([Date]) OVER Intersect([ClientId], [Item]))
this will give you a column with the number of days difference between the two dates in each row:
Item ClientId Date Days
A 102 2014.12.12 1
A 102 2014.12.13 1
B 141 2014.12.12 5
B 141 2014.12.17 5
Related
I'm trying to see the difference between the two periods for a column.
For example, we see that sales decreased at the end of the month, and we need to see which products were not sold at the end of the month?
I can create SELECT to see quantity for each product for each period:
SELECT product_id, count(product_id) AS Count
FROM testDB
WHERE
sales_date IS NOT NULL
AND
delivery_date BETWEEN '2021-02-01 00:00:03.0000000' AND '2021-02-14 23:56:00.0000000'
GROUP BY
product_id
and the same SELECT with another period:
delivery_date BETWEEN '2021-02-14 00:00:03.0000000' AND '2021-02-28 23:56:00.0000000'
So, after these queries I see list for first period with 10 products with quantity and in second period I see list with 7 products with quantity. I can't get the difference between the lists of the two SELECTs. I tried to use != and NOT IN but without any results.
I will be very grateful for your help. Thanks
Sorry for the confusion. I meant the difference between the two selects:
The result of the first one (for first period):
Product_ID Count
grapes. 100
lime. 13
lemon. 15
cherry. 222
blueberry. 123
banana. 1
apple. 123
watermelon 56
and second one (for second period):
Product_ID Count
grapes. 10
lime. 1
lemon. 10
cherry. 2
blueberry. 13
banana. 12
and I wand to see difference between these selects:
Product_ID Count
apple. 0
watermelon. 0
So we did not sell any apples and watermelons in second period.
SELECT product_id, count(product_id) AS Count,delivery_date-sales_date as DIFFERENCE
FROM testDB
WHERE
sales_date IS NOT NULL
AND
delivery_date BETWEEN '2021-02-01 00:00:03.0000000' AND '2021-02-14 23:56:00.0000000'
GROUP BY
product_id
This should work for getting the difference between the 2 period columns.
The query I am trying to produce is very similar to this, but instead of counting where it changes, I need to count up to the point the difference exceeds a specific value.
I have tried setting a flag where the date difference is more than 14 from the previous, but that then gives 2 records - one for when the difference is greater than 14, and 1 for the rest. As the ID can appear multiple times with different dates, I cannot then group that result.
e.g.
Data:
ID Date
1A 2020-01-01
1A 2020-01-03
2B 2020-01-05
1A 2020-02-01
Result set to be:
ID Date Count
1A 2020-01-01 2
2B 2020-01-05 1
1A 2020-02-01 1
the criteria in this case being where the difference between the dates is more than 14 days
Tried:
SELECT [ID], [Date],
case when datediff(dd,lag([Date]) over (partition by ID order by [Date]),[Date])>14
then 1 else 0 end as CaseValue
FROM Table
where [Date]>'2020-02-01'
and
declare #CountValue bigint
SELECT [ID], [Date],
#CountValue=#CountValue+case when datediff(dd,lag([Date]) over (partition by ID order by [Date]),[Date])>14
then 1 else 0 end
FROM Table
where [Date]>'2020-02-01'
I am trying to subtract the row values from a column in SSRS report builder to calculate the per day cumulative hourly run hour having first row value as lifetime run hour till yesterday. Could anyone please help me how to proceed. Please refer the image for required report format.
DATE TIME RUNHR COLUMN A(RUN HR in MINUTES) ROW NO
12/12/2018 00:00AM 100 100(TOTAL RUN HR TILL YEST MID-NIGHT11:45PM) 1
12/12/2018 00:30AM 101 1 (I HAVE TO START SUBTRACTING FROM HERE) 2
12/12/2018 01:00AM 105 5 3
---------------------------------------------------------------------------
12/12/2018 11:00PM 130 30 47
12/12/2018 11:30PM 135 35 48
I want to display the value as shown in COLUMN A, where the first row in COLUMN A shows the total RUNHR from yesterday and subsequent rows (2 to 48) showing the cumulative (to that time) RUNHR for that day only (i.e. since the value in the first row).
Given RUNHR is cumulative, you could do this by joining on the minimum value of RUNHR for that period (assuming your dataset is based in Sql):
SELECT PumpId, PumpName, SampleTime, RUNHR, StartingValue, (RUNHR - StartingValue) AS COLUMNA
FROM PumpDataTable A
INNER JOIN
(SELECT PumpId, MIN(RUNHR) AS StartingValue
FROM PumpDataTable
WHERE SampleTime >= #StartTIme AND SampleTime <= #EndTime GROUP BY PumpId) B ON A.PumpId = B.PumpId
WHERE PumpId = #PumpId
AND SampleTime >= #StartTIme AND SampleTime <= #EndTime
ORDER BY SampleTime
My table is having data e.g. empcode designation code and promotion date, I want to get what was an employee's designation on some given date. for eg.
EmpCode DesignationCode PromotionDate
101 50 2010-01-25
101 10 2014-01-01
101 11 2015-01-01
102 10 2009-10-01
103 15 2015-01-01
now if I check designation as on 2014-02-01 it should give result as following
EmpCode DesignationCode PromotionDate
101 10 2014-01-01
102 10 2009-10-01
Can anyone please tell what query should I write ?
Thanks in Advance.
You can try:
SELECT DISTINCT ON (EmpCode) EmpCode, DesignationCode, PromotionDate
FROM mytable
WHERE PromotionDate <= '2014-02-01'
ORDER BY EmpCode, PromotionDate DESC
The query first filters out any records having a PromotionDate that is past given date, i.e. '2014-02-01'.
Using DISTINCT ON (EmpCode) we get one row per EmpCode. This is the one having the most recent PromotionDate (this is achieved by placing PromotionDate DESC in the ORDER BY clause).
Demo here
I have a table with 106 columns. One of those columns is a "Type" column with 16 types.
I want 16 rows, where the Type is distinct. So, row 1 has a type of "Construction", row 2 has a type of "Elevator PVT", etc.
Using Navicat.
From what I've found (and understood) so far, I can't use Distinct (because that looks across all rows), I can't use Group By (because that's for aggregating data, which I'm not looking to do), so I'm stuck.
Please be gentle- I'm really really new at this.
Below is a part of the table (how can I share this normally?)- it's really big so I didn't share the whole thing. Below is a partial result I'm looking for, where the Violation_Type is unique and the rest of the columns display.
Got it.. Sheesh... (took me forever, but got it...)
D_ID B_ID V_ID V_Type S_ID c_f d_y l_u p_s du_p
------ ------ ------- -------------- ------ ----- ------ ------ ----- ------
184 117 V 032 Elevator PVT 2 8 0 0
4 140 V 100 Construction 1 8 0 0
10 116 V 122 Electric 1 8 2005 0 0
11 117 V 033 Boiler Local 1 0 2005 0 0
You can use ROW_NUMBER for this:
SELECT *
FROM(
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY V_Type ORDER BY (SELECT NULL))
FROM tbl
)t
WHERE rn = 1
Modify the ORDER BY depending on what row you want to prioritize.
From the documentation:
Returns the sequential number of a row within a partition of a result
set, starting at 1 for the first row in each partition.
This means that for every row within a partition (specified by the PARTITION BY clause), sql-server assigns a number from 1 depending on the order specified in the ORDER BY clause.
ROW_NUMBER requires an ORDER BY clause. SELECT NULL tells the sql-server that we do not want to enforce a particular order. We just want the rows numbered by partition.
The WHERE rn = 1 obviously filters only rows that has a ROW_NUMBER of 1. This gives you one row for every V_TYPE available.