I have the below table
Project No
STAGES
completion date
PROJ_001
1
12-MAR-21
PROJ_001
2
14-MAR-21
PROJ_001
3
15-MAR-21
PROJ_001
4
18-MAR-21
PROJ_002
1
18-MAR-21
PROJ_002
2
19-MAR-21
PROJ_002
3
19-MAR-21
PROJ_002
4
23-MAR-21
Assuming the table is ordered by project no and stages.
I have to check whether the date in stage 1 is less than stage 2
similarly date of stage 2 should be less than stage 3
and date of stage 3 should be less than stage 4.
This should happen for all the stages in a project.
Project No
STAGES
completion date
output
PROJ_001
1
12-MAR-21
Correct
PROJ_001
2
14-MAR-21
Correct
PROJ_001
3
15-MAR-21
correct
PROJ_001
4
18-MAR-21
correct
PROJ_002
1
19-MAR-21
incorrect
PROJ_002
2
17-MAR-21
incorrect
PROJ_002
3
16-MAR-21
correct
PROJ_002
4
23-MAR-21
correct
here stage 1 of project 2 is not less than stage 2 so it is incorrect.
by default the stage 4 should be correct.
can anyone guide me with this?
If a stage is "correct" if its time-sequence order equals the stage number, then the problem is trivial. In answer to questions I asked, the OP said that "If stages between two stages are "wrong" then all must not be considered as wrong". I interpret this to mean: A stage is "correct" if its order in the time sequence equals the stage number.
The output column can be populated by comparing the stage number to the sequence number, directly in a case expression. Like so:
select project_no, stage, completion_date,
case row_number() over (partition by project_no
order by completion_date, stage)
when stage then 'correct' else 'incorrect' end as output
from the_table
order by {whatever} -- if needed
;
Note that in the sample data there are sometimes equal values in the completion_date column. In that case, I assume the stages were completed in the correct order (so, the second criterion in the order by clause of the row_number() analytic function).
Related
I want to implement a running distinct count with Amazon Quicksight. Here's an example of what that would look like:
Date
ID
Amount
Running Distinct Count
1/1/1900
a
1
1
1/2/1900
a
3
1
1/2/1900
b
6
2
1/4/1900
a
3
2
1/8/1900
c
9
3
1/22/1900
d
2
4
I've tried runningSum(distinct_count, [Date ASC]), but this returns a sum of the distinct counts for each aggregated date field.
Can this be implemented in QuickSight?
You can use this workaround to get the same functionality as runningDistinctCount() as follows:
runningSum(
distinct_count(
ifelse(
datetime=minOver({Date}, [{ID}], PRE_AGG),
{ID},
NULL
)),
[{Date} ASC],
[]
)
This would give you the runningDistinctCount of ID's over the Date field. It achieves it by considering just the first time the ID appears in the dataset, counting these and finally doing a runningSum on these counts.
I have someting like this
id day descrition
1 1 hi
1 1 today
1 1 is a beautifull
1 1 day
1 2 exemplo
1 2 for
1 2 this case
I need to do a funtion that for each day concatenate the descrtiomn colunm and return the result like this
id day descrition
1 1 hi today is a beautifull thay
1 2 exemplo for this case
Anny ideia about how can i do this usisng a loop in a function in oracle
You need a way of determining which order the values should be aggregated. The snippet below will rely on the implicit order in which Oracle reads the rows from the datafiles - if you have row movement enabled then you may get inconsistent results as the rows can be read in different orders as they are relocated in the underlying datafiles.
SELECT LISTAGG( description, ' ' ) WITHIN GROUP ( ORDER BY ROWNUM ) AS description
FROM your_table
GROUP BY id, day
It would be better to have another column that stores the order within each day.
I have the following situation:
column 1 column 2 column 3
A 1 1
A 1 2
B 3 4
B 3 5
I need to color my letters when the value in column 2 never occurs in column 3. But I need to color all my letters. Does anyone know how to write a case statement for this?
So I'll explain my example: I dont't need to color the letter A because there is a match between column 2 and 3 and the first row.
I do need to color my B's because there is never a match between columns 2 and 3.
I already tried this:
count(distinct(case when "Column 2" != "Column 3" then 1 else 0 end))
but this gives a result for each row and I need a result for the total package.
Thanks!
You can approach this as following:
Create a logical column on your analysis that does a case statement that returns 1 or 0 depending if the values of column2 and column3 are the same (pretty much like the case-when that you provided on your answer but without the count distinct).
Wrap that case statement with a MAX grouped by your column1. This will give you either a consistent 1 or 0 across all your different values of column1. You can use this value for your conditional formatting. The key here is to use the aggregated function with the group by.
You have here some oracle documentation on how to use the logical SQL group by.
Hope that helps! Good luck!
This question already has answers here:
Why does Oracle return specific sequence if 'orderby' values are identical?
(4 answers)
Closed 7 years ago.
All, Let's say the SQL looks like below.
Select a, b ,c from table1 order by c
If all the rows in table1 have the same field value in the field c. I want to know if the result has the same order for each time I executed the SQL.
Let's say data in the table1 looks like below.
a b c
-------------------------------------------
1 x1 2014-4-1
....
100 x100 2014-4-1
....
1000 x1000 2014-4-1
....
How Oracle determine the rows sequence for the same order by value?
Added
Will they be random sequence for each time?
One simple answer is NO. There is no guarantee that the ORDER BY on equal values will return the same sorted result every time. It might seem to you it is always stable, however, there are many reasons when it could change.
For example, the sorting on equal values might defer after:
Gathering statistics
Adding an index on the column
For example,
Let's say I have a table t:
SQL> SELECT * FROM t ORDER BY b;
A B
---------- ----------
1 1
2 1
3 2
4 2
5 3
6 3
6 rows selected.
The sorting on the column having similar values is just like:
SQL> CREATE TABLE t1 AS SELECT * FROM t ORDER BY b, DBMS_RANDOM.VALUE;
Table created.
SQL> SELECT * FROM t1 ORDER BY b;
A B
---------- ----------
1 1
2 1
4 2
3 2
5 3
6 3
6 rows selected.
So, similar data in bot the tables, however, ORDER BY on the column having equal values, dos not guarantee the same sorting.
They must not be random (change each time), but the order is not guaranteed (change sometimes).
I have an MS Access Database table that records communication status of values from several meters. The data is logged directly to the table, but I need to make sure that the table is populating. From the sample data you can see that the Comm columns doesn't read false or 0, so I want to return a log whenever the difference between now and "Date / Time" is greater than 5 minutes.
Date / Time FCB Comm BOF Comm EAF Comm FGP Comm
9/6/2011 10:29:10 1 1 1 1
9/6/2011 10:28:01 1 1 1 1
9/6/2011 10:27:11 1 1 1 1
9/6/2011 10:26:20 1 1 1 1
9/2/2011 08:17:01 1 1 1 1
9/2/2011 08:16:10 1 1 1 1
9/2/2011 08:15:02 1 1 1 1
9/2/2011 08:14:08 1 1 1 1
I wanted to know if anyone could tell me if this could like a reasonable query to run?
SELECT Data.[Date / Time], Data.[Ford Chiller Building Comm Okay],
Data.[Basic Oxygen Furnace Comm Okay], Data.[Electro-Arc Furnace Comm Okay],
Data.[J-9 Shop Comm Okay], Data.[Ford Glass Plant Comm Okay]
FROM Data
where DateDiff("n",now(), Data.[Date / Time] ) < 5;
You need something running continuously that generates a notification whenever expected data doesn't appear, and there's a couple of approaches you can take to do that.
One is to continuously run a query like the one you have above, checking the most recent date in the table against the value of the now() function.
Another approach is to take the latest date in your table, wait (sleep) for 5 minutes, and then check the table again for any newer entries. My expectation is that this approach will generate fewer hits on your table.
You could also just check the most recent date every 5 minutes regardless of the previous time checked and see if data hasn't come in.
You need to set up your notification loop first, then you can experiment with different approaches.
all you should really need to do is return the number of rows in the table whose timestamp is within 5 minutes of now(). You shouldn't need additional row detail, just is the count 0 or not?