calculate the time difference for same column in Spotfire - time

I am a beginner for Spotfire. I have a problem about the difference calculation for the some column value. A sample table could be like this:
id timestamp state
1 7/1/2016 12:00:01 AM 1
2 7/1/2016 12:00:03 AM 0
3 7/1/2016 12:00:04 AM 1
4 7/1/2016 12:00:06 AM 0
5 7/1/2016 12:00:09 AM 1
6 7/1/2016 12:00:10 AM 0
7 7/1/2016 12:00:12 AM 1
I want to calculate the time difference for the timestamp when the state is 1,
the final table I want to have is:
id timestamp state time_diffence
3 7/1/2016 12:00:04 AM 1 3
5 7/1/2016 12:00:09 AM 1 5
7 7/1/2016 12:00:12 AM 1 3
it seems that I should identify an expression for the calculation, but I have not idea for the calculation just for one parameter :(. somebody could help me ?
still one more small question: what if the timestamp column value is just number value, how can i calculate the difference, is there any related function like DateDiff() here? for example:
id times state
1 12 1
2 7 0
3 10 1
4 11 0
5 6 1
6 9 0
7 7 1
the result could be :
id times state diffence
3 10 1 -2
5 6 1 -4
7 7 1 1
after running the code: i have the error as below:
for the row if it has the same time stamp as the last previous row, the difference will keep same as before, but actually the difference for the rows which have same time stamp would be as 0
thanks for your help :)

Assuming your data is sorted in ascending order by [timestamp] before you import it, you can partition using the Previous function with Over where the [state]=1.
Insert a calculated column with this expression:
If([state]=1,DateDiff("ss",Min([timestamp]) OVER (Previous([timestamp])),[timestamp]))
You will see it populated in your table like the below:
Then if you ONLY want to see the rows that have the difference you mentioned, on your table you can...
Right Click > Properties > Data > Limit data using expression >
And insert the expression: [time_difference] > 1
This will result in this table:

Related

Oracle query to loop through different records in the table

Apologies in advance I do not have a question instead a situation to which I need to find the solution. Trying to explain the setup with the example -
I have the oracle table as follows -
ID Name Qty1 Qty2 Qty3 Date
-- ---- ---- ---- ---- -----------
1 ABC 0 0 -10 4th-May
1 ABC 5 0 -5 5th-May
1 ABC 0 0 -5 6th-May
1 ABC 0 0 -5 7th-May
1 ABC 0 0 -10 8th-May
1 ABC 0 5 0 9th May
1 ABC 0 0 0 10th May
1 ABC 1 0 15 11th May
...
1 ABC 0 0 10 (100th Day)
--- So on till 100 day from today's date.
Basically, for a single ID,Name I have three quantities starting from the current date till 100th day.
First, I need to find the row where the first occurrence of non zero Qty1 or Qty2 happens( In this case 5th-May record).
Next, I need to find the row where the next occurrence of non-zero Qty1 or Qty2 happens( In this case 9th May). Then I will have to find the record preceding this date (in this case 8th-May).
Qty3 will be picked up from this preceding record that qty will be inserted for the previous occurrence of the date when Qty1 or Qty2 are non-zero.
The resultant data:
ID Name Qty3 Date
-- ---- ---- --------
1 ABC -10 5th-May
1 ABC -10 9th-May
1 ABC 10 11th-May
The last non-zero record will have the Qty3 from the 100th day if there are no non-zero records from the day till 100th day.
Any leads how can I achieve the requirement. I need to find some solution without using cursors or loop.?
Thanks in Advance and apologies for the bad writeup, but really need an answer to this question.

FIFO inventory aging report using a single query in T-SQL

I've got an inventory transactions table :
Product
Date
Direction
Quantity
A
Date 1
IN
3
B
Date 2
IN
55.7
A
Date 3
OUT
1
B
Date 3
OUT
8
B
Date 3
IN
2
I can easily get the stock for any date with the following query :
SELECT Product,
SUM(CASE Direction WHEN 'IN' THEN Quantity ELSE -1 * Quantity END)
FROM Transactions
WHERE Date <= '#DateValue#'
GROUP BY Product;
Now my purpose is to get stocks aged like this using the FIFO principle :
Product
Total stock
0-30 days
31-60 days
61-90 days
91+ days
A
3
3
0
0
0
B
34.2
10
14.2
7
3
C
25
20
3
1
1
D
10
2
8
0
0
E
1
0
0
1
0
I am using SQL Server 2016 & SSMS 18.
The solution should be fast as it will be working against a table with 3,000,000+ rows.
A single query is preferred since it will be integrated into an ERP system.
I have yet to find a solution based on a single query after weeks of research. Any help is appreciated. Thanks in advance.

SAS Sorting within group

I would like to try and sort this data by descending number of events and from latest date, grouped by ID
I have tried proc sql;
proc sql;
create table new as
select *
from old
group by ID
order by events desc, date desc;
quit;
The result I currently get is
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
2 09/11/2015 2
3 01/01/2015 2
2 16/10/2014 2
3 08/12/2013 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Although the dates and events are sorted descending. Those IDs with multiple events are no longer grouped.
Would it be possible to achieve the below in fewer steps?
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
3 01/01/2015 2
3 08/12/2013 2
2 09/11/2015 2
2 16/10/2014 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Thanks
It looks to me like you're trying to sort by descending event, then by either the earliest or latest date (I can't tell which one from your explanation), also descending, and then by id. In your proc sql query, you could try calculating the min or max of the Date variable, grouped by event and id, and then sort the result by descending event, the descending min/max of the date, and id.

Hive Script - How to transform table / find average of certain records according to one columns name?

I want to transform a Hive table by aggregating based on averages. However, I don't want the average value of an entire column, I want the average of the records in that column that have the same type in another column.
Here's an example, easier than trying to explain:
TABLE I HAVE:
Timestamp CounterName CounterValue MaxCounterValue MinCounterValue
00:00 Counter1 3 3 100:00 Counter2 4 5 2
00:00 Counter3 1 4 1
00:00 Counter4 6 6 100:05 Counter1 3 5 200:05 Counter2 2 2 200:05 Counter3 4 5 400:05 Counter4 6 6 5.......
TABLE I WANT:
CounterName AvgCounterValue MaxCounterValue MinCounterValue
Counter1 3 5 1Counter2 3 5 2Counter3 2.5 5 1Counter4 6 6 1
So I have a list of a bunch of counters, which each have multiple records (one per 5 minute time period). Every time each counter is logged, it has a value, a max value during that 5 minutes, and a min value. I want to aggregate this huge table so that it just has one record for each counter, which records the overall average value for that counter from all the records in the table,and then the overall min/max value of the counter in the table.
The reason this is difficult is because all the documentation says is how to aggregate by the average of a column in one table - I don't know how to split it up in groups.
Here's the script I've started with:
FROM HighCounters INSERT OVERWRITE TABLE MdsHighCounters
SELECT
HighCounters.CounterName AS CounterName,
HighCounters.CounterValue AS CounterValue
HighCounters.MaxCounterValue AS MaxCounterValue,
HighCounters.MinCounterValue AS MinCounterValue
GROUP BY HighCounters.CounterName;
And I don't know where to go from there... any ideas? Thanks!!
I think I solved my own problem:
FROM HighCounters INSERT OVERWRITE TABLE MdsHighCounters
SELECT
HighCounters.CounterName AS CounterName,
AVG(HighCounters.CounterValue) AS CounterValue,
MAX(HighCounters.MaxCounterValue) AS MaxCounterValue,
MIN(HighCounters.MinCounterValue) AS MinCounterValue
GROUP BY HighCounters.CounterName;
Does this look right to you?

oracle left outer joins not showing null values but displays same value

Problem is in left outer join, when there are no rows in right side table then it does not display null values, it displays previous values....
Like this....
1 st Table contains
PGMTX_CODE PGMTX_MARKS PGMTX_TOTQSTN
-------------------------------------------
EE 1 5
EE 2 5
EE 3 0
EE 4 0
2 nd Table contains
PGMTX_CODE PGMTX_MARKS PGMTX_ACTUSEDQST
-------------------------------------------
EE 1 5
So I want result like...
PGMTX_MARKS PGMTX_TOTQSTN PGMTX_ACTUSEDQST
--------------------------------------------------
1 5 5
2 5 blank
3 0 blank
4 0 blank
I use query like this...
SELECT m.PGMTX_MARKS,
m.PGMTX_TOTQSTN,
tlm.PGMTX_ACTUSEDQST,
from PAPERGEN_MTL_OEX m
left OUTER JOIN PAPERGEN_TLMTL_OEX tlm
ON m.PGMTX_CODE=tlm.PGMTX_CODE
where m.PGMTX_CODE='EE'
order by m.PGMTX_MARKS
But I got result like
PGMTX_MARKS PGMTX_TOTQSTN PGMTX_ACTUSEDQST
--------------------------------------------------
1 5 5
2 5 5
3 0 5
4 0 5
Your join condition is wrong, should be
ON m.PGMTX_CODE=tlm.PGMTX_CODE AND m.PGMTX_MARKS = tlm.PGMTX_MARKS

Resources