How to index a specific column to perform field comparison calculations? - indexing

Given the following table:
| A | B | holdingtemp1 | holdingTemp2 | holdingtemp3 | holdingTemp4 | idx |
| Grits| 7:06:24 AM | 180.199 | - | - | - | 0 |
| | 8:35:56 AM | - | 174.30 | - | - | 1 |
| | 9:25:18 AM | - | - | 182 | - | 2 |
| | 10:02:0 AM | - | - | - | 180 | 3 |
How to properly index column B to perform field comparisons in order to implement conditional logic where each temperature recording must be taken within a two hour interval relative to the previous recording, such that:
if(B[0] + 2 > B[1], green(), red()) // 9:06 > 8:35, turns cell green
if(B[1] + 2 > B[2], green(), red()) // 10:35 > 9:25, turns cell green
Edit:
Found the right function to use for this implementation, but peak() is returning 4:43:21 AM for all records with
Peek('B', 0) as peekB, // 4:43:21 AM
and 5:11:03 AM with
Peek('B', 1) as peekB, // 5:11:03 AM
If so, does peek() fetch the field value of the preloaded or postloaded data?

Related

Loop to find multiple minimum and maximum values

I have a table (tblProduct) with a field (SerialNum).
I am trying to find multiple minimum and maximum values from the field SerialNum, or better put: ranges of sequential serial numbers.
The serial numbers are 5 digits and a letter. Most of the values are sequential, but NOT all!
I need the output for a report to look something like:
00001A - 00014A
00175A - 00180A
00540A - 00549A
12345A - 12349A
04500B - 04503B
04522B - 04529B
04595B
04627B - 04631B
If the values in-between are present.
I tried a loop, but I realized I was using record sets. I need one serial num to be compared to ALL the ranges. Record sets were looking at one range.
I have been able to determine the max and min of the entire series, but not of each sequential group.
| SerialNum |
| -------- |
| 00001A|
| 00002A|
| 00003A|
| 00004A|
| 00005A|
| 00006A|
| 00007A|
| 00008A|
| 00009A|
| 00010A|
| 00011A|
| 00012A|
| 00013A|
| 00014A|
| 00175A|
| 00176A|
| 00177A|
| 00178A|
| 00179A|
| 00180A|
| 00540A|
| 00541A|
| 00542A|
| 00543A|
| 00544A|
| 00545A|
| 00546A|
| 00547A|
| 00548A|
| 00549A|
| 12345A|
| 12346A|
| 12347A|
| 12348A|
| 12349A|
| 04500B|
| 04501B|
| 04502B|
| 04503B|
| 04522B|
| 04523B|
| 04524B|
| 04525B|
| 04526B|
| 04527B|
| 04528B|
| 04529B|
| 04595B|
| 04627B|
| 04628B|
| 04629B|
| 04630B|
| 04631B|
Try to group by the number found with Val:
Select
Min(SerialNum) As MinimumSerialNum,
Max(SerialNum) As MaximumSerialNum
From
tblProduct
Group By
Val(SerialNum)

PowerBI / SQL Query to verify records

I am working on a PowerBI report that is grabbing information from SQL and I cannot find a way to solve my problem using PowerBI or how to write the required code. My first table, Certifications, includes a list of certifications and required trainings that must be obtained in order to have an active certification.
My second table, UserCertifications, includes a list of UserIDs, certifications, and the trainings associated with a certification.
How can I write a SQL code or PowerBI measure to tell if a user has all required trainings for a certification? ie, if UserID 1 has the A certification, how can I verify that they have the TrainingIDs of 1, 10, and 150 associated with it?
Certifications:
CertificationsTable
UserCertifications:
UserCertificationsTable
This is a DAX pattern to test if contains at least some values.
| Certifications |
|----------------|------------|
| Certification | TrainingID |
|----------------|------------|
| A | 1 |
| A | 10 |
| A | 150 |
| B | 7 |
| B | 9 |
| UserCertifications |
|--------------------|---------------|----------|
| UserID | Certification | Training |
|--------------------|---------------|----------|
| 1 | A | 1 |
| 1 | A | 10 |
| 1 | A | 300 |
| 2 | A | 150 |
| 2 | B | 9 |
| 2 | B | 90 |
| 3 | A | 7 |
| 4 | A | 1 |
| 4 | A | 10 |
| 4 | A | 150 |
| 4 | A | 1000 |
In the above scenario, DAX needs to find out if the mandatory trainings (Certifications[TrainingID]) by Certifications[Certification] is completed by
UserCertifications[UserID ]&&UserCertifications[Certifications] partition.
In the above scenario, DAX should only return true for UserCertifications[UserID ]=4 as it is the only User that completed at least all the mandatory trainings.
The way to achieve this is through the following measure
areAllMandatoryTrainingCompleted =
VAR _alreadyCompleted =
CONCATENATEX (
UserCertifications,
UserCertifications[Training],
"-",
UserCertifications[Training]
) // what is completed in the fact Table; the fourth argument is very important as it decides the sort order
VAR _0 =
MAX ( UserCertifications[Certification] )
VAR _supposedToComplete =
CONCATENATEX (
FILTER ( Certifications, Certifications[Certification] = _0 ),
Certifications[TrainingID],
"-",
Certifications[TrainingID]
) // what is comeleted in the training Table; the fourth argument is very important as it decides the sort order
VAR _isMandatoryTrainingCompleted =
CONTAINSSTRING ( _alreadyCompleted, _supposedToComplete ) // CONTAINSSTRING (<Within Text>,<Search Text>); return true false
RETURN
_isMandatoryTrainingCompleted

PostgreSQL: show trips within a bounding box

I have a trips table containing user's trip information, like so:
select * from trips limit 10;
trip_id | daily_user_id | session_ids | seconds_start | lat_start | lon_start | seconds_end | lat_end | lon_end | distance
---------+---------------+-------------+---------------+------------+------------+-------------+------------+------------+------------------
594221 | 16772 | {170487} | 1561324555 | 41.1175475 | -8.6298934 | 1561325119 | 41.1554091 | -8.6283493 | 5875.39697884959
563097 | 7682 | {128618} | 1495295471 | 41.1782829 | -8.5950303 | 1495299137 | 41.1783908 | -8.5948965 | 5364.81067787512
596303 | 17264 | {172851} | 1578011699 | 41.5195598 | -8.6393526 | 1578012513 | 41.4614024 | -8.717709 | 11187.7956426909
595648 | 17124 | {172119} | 1575620857 | 41.1553116 | -8.6439528 | 1575621885 | 41.1621821 | -8.6383042 | 1774.83365424607
566061 | 8720 | {133624} | 1509005051 | 41.1241975 | -8.5958988 | 1509006310 | 41.1424158 | -8.6101461 | 3066.40306678979
566753 | 8947 | {134662} | 1511127813 | 41.1887996 | -8.5844238 | 1511129839 | 41.2107519 | -8.5511712 | 5264.64026582458
561179 | 7198 | {125861} | 1493311197 | 41.1776935 | -8.5947254 | 1493311859 | 41.1773815 | -8.5947254 | 771.437257541019
541328 | 2119 | {46950} | 1461103381 | 41.1779 | -8.5949738 | 1461103613 | 41.1779129 | -8.5950202 | 177.610819150637
535519 | 908 | {6016} | 1460140650 | 41.1644658 | -8.6422775 | 1460141201 | 41.1642646 | -8.6423309 | 1484.61552373019
548460 | 3525 | {102026} | 1462289206 | 41.177689 | -8.594679 | 1462289843 | 41.1734476 | -8.5916326 | 1108.05119077308
(10 rows)
The task is to filter trips that start and end within the bounding box defined by upper left: 41.24895, -8.68494 and lower right: 41.11591, -8.47569.
If I understand correctly, you can just compare that starting and ending coordinates:
select t.*
from trips t
where lat_start >= 41.11591 and lat_start <= 41.24895 and
lat_end >= 41.11591 and lat_end <= 41.24895 and
long_start >= -8.68494 and long_start <= -8.47569 and
long_end >= -8.68494 and long_end <= -8.47569
Since your coordinates are stored in x,y columns, you have to use ST_MakePoint to create a proper geometry. After that, you can create a BBOX using the function ST_MakeEnvelope and check if start and end coordinates are inside the BBOX using ST_Contains, e.g.
WITH bbox(geom) AS (
VALUES (ST_MakeEnvelope(-8.68494,41.24895,-8.47569,41.11591,4326))
)
SELECT * FROM trips,bbox
WHERE
ST_Contains(bbox.geom,ST_SetSRID(ST_MakePoint(lon_start,lat_start),4326)) AND
ST_Contains(bbox.geom,ST_SetSRID(ST_MakePoint(lon_end,lat_end),4326));
Note: the CTE isn't really necessary and is in the query just for illustration purposes. You can repeat the ST_MakeEnvelope function on both conditions in the WHERE clause instead of bbox.geom. This query also assumes the SRS WGS84 (4326).

SELECT 1 ID and all belonging elements

I try to create a json select query which can give me back the result on next way.
1 row contains 1 main_message_id and belonging messages. (Like the bottom image.) The json format is not a requirement, if its work with other methods, it will be fine.
I store the data as like this:
+-----------------+---------+----------------+
| main_message_id | message | sub_message_id |
+-----------------+---------+----------------+
| 1 | test 1 | 1 |
| 1 | test 2 | 2 |
| 1 | test 3 | 3 |
| 2 | test 4 | 4 |
| 2 | test 5 | 5 |
| 3 | test 6 | 6 |
+-----------------+---------+----------------+
I would like to create a query, which give me back the data as like this:
+-----------------+-----------------------+--+
| main_message_id | message | |
+-----------------+-----------------------+--+
| 1 | {test1}{test2}{test3} | |
| 2 | {test4}{test5}{test6} | |
| 3 | {test7}{test8}{test9} | |
+-----------------+-----------------------+--+
You can use json_agg() for that:
select main_message_id, json_agg(message) as messages
from the_table
group by main_message_id;
Note that {test1}{test2}{test3} is invalid JSON, the above will return a valid JSON array e.g. ["test1", "test2", "test3"]
If you just want a comma separated list, use string_agg();
select main_message_id, string_ag(message, ', ') as messages
from the_table
group by main_message_id;

Updating a record using data from the same table

I have a joining table that logs the changes in a File's state (Active/Deleted/Archived etc).
e.g.
+----+---------+------------+-------+
| PK | File_FK | Date | State |
+----+---------+------------+-------+
| 1 | 100 | 11-9-2015 | 1 |
| 2 | 200 | 14-09-2015 | 2 |
| 3 | 300 | 14-07-2015 | 0 |
| 4 | 300 | 12-9-2015 | 2 |
| 5 | 200 | 14-09-2015 | 2 |
| 6 | 300 | 14-09-2015 | 0 |
| 7 | 300 | 13-09-2015 | 1 |
+----+---------+------------+-------+
There are a number of records which are were not inserted accordingly, on a certain date e.g. July 15.
Is there a way how to automate the updating of certain records?
I want to change the state to the last option it was (latest date), if the state is 0 and if there are no previous 0 as states (for the same file foreign key) without amending previous states which had a zero.
To try and simplify (taking File_FK = 300 as example):
I want:
The state of the file to update to be 1.
State '0' is invalid since it was made on 14-09-2015 (the last one)
Prevent updating the '0' that was made on 14-07-2015.
Basically I want to change the state of a file to the state it was before it was '0' without changing the 0s that where done earlier.