tableau calculate cumulative value with condition - tableau-api

I have a tableau table with columns like this:
| ID | ww | count_flag |
| 1 | ww1 | 0 |
| 1 | ww2 | 1 |
| 1 | ww3 | 1 |
| 1 | ww4 | 0 |
| 1 | ww5 | 1 |
| 2 | ww1 | 1 |
| 2 | ww2 | 1 |
| 2 | ww3 | 1 |
| 2 | ww4 | 0 |
| 2 | ww5 | 1 |
...
Now I'd like to add a new column to show the consistent status for each ID among all the ww(workweek), the consistent status will be reset every time when the count_flag is 0 or ID changes, so it will look like below:
|ID | ww | count_flag | consistent status|
| 1 | ww1 | 0 | 0 |
| 1 | ww2 | 1 | 1 |
| 1 | ww3 | 1 | 2 |
| 1 | ww4 | 0 | 0 |
| 1 | ww5 | 1 | 1 |
| 2 | ww1 | 1 | 1 |
| 2 | ww2 | 1 | 2 |
| 2 | ww3 | 1 | 3 |
| 2 | ww4 | 0 | 0 |
| 2 | ww5 | 1 | 1 |
...
How should I create the calculating field to add such a parameter to the table column.

Related

Is there any way to group by continious values

I have data from car gps tracker and I need to find parking and movement periods
Now every row have id of parking or movement in table
+------------+-------+----------+---------+------------+
| time | speed | ignition | trip_id | parking_id |
+------------+-------+----------+---------+------------+
| 05:48:50 | 0 | false | 0 | 300 |
| 05:49:20 | 0 | false | 0 | 300 |
| 05:49:20 | 10 | true | 300 | 0 |
| 05:50:01 | 20 | true | 300 | 0 |
| 05:51:20 | 17 | true | 300 | 0 |
| 05:51:20 | 0 | false | 0 | 301 |
| 05:52:40 | 0 | false | 0 | 301 |
| 05:52:40 | 12 | true | 301 | 0 |
| 05:52:50 | 22 | true | 301 | 0 |
| 05:53:00 | 30 | true | 301 | 0 |
| 05:53:30 | 40 | true | 301 | 0 |
| 05:53:30 | 0 | false | 0 | 302 |
| 05:55:00 | 0 | false | 0 | 302 |
+------------+-------+----------+---------+------------+
SELECT min(time) as time_start, max(time) as time_end, trip_id, parking_id
FROM 'tablename' GROUP BY trip_id, parking_id
result is
+------------+----------+---------+------------+
| time start | time end | trip id | parking id |
+------------+----------+---------+------------+
| 05:48:50 | 05:49:20 | 0 | 300|
| 05:49:20 | 05:51:20 | 300| 0 |
| 05:51:20 | 05:52:40 | 0 | 301|
| 05:52:40 | 05:53:30 | 301| 0 |
| 05:53:30 | 05:55:00 | 0 | 302|
+------------+----------+---------+------------+
How should I do group by so as not to use parking_id and trip_id. But generate parking ids and trip ids. And the final result must be:
+------------+----------+---------+------------+
| time start | time end | trip id | parking id |
+------------+----------+---------+------------+
| 05:48:50 | 05:49:20 | 0 | 1 |
| 05:49:20 | 05:51:20 | 1 | 0 |
| 05:51:20 | 05:52:40 | 0 | 2 |
| 05:52:40 | 05:53:30 | 2 | 0 |
| 05:53:30 | 05:55:00 | 0 | 3 |
+------------+----------+---------+------------+

ERROR: column "table.column_name" must appear in the GROUP BY clause or be used in an aggregate function

I have the following table:
SELECT * FROM trips_motion_xtics
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| session_id | trip_id | lat_start | lat_end | lon_start | lon_end | alt | distance | segments_length | speed | acceleration | track | travel_mode |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 74.208 | 0 | foot |
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 74.154 | 0 | foot |
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 68.226 | 0 | foot |
| 656 | 303637 | 41.14454009 | 41.1631127 | -8.56292593 | -8.5870161 | 0 | 5921.07030809987 | 2785.6088546142 | 0 | 99.028 | 0 | car |
| 656 | 303637 | 41.14454009 | 41.1631127 | -8.56292593 | -8.5870161 | 0 | 5921.07030809987 | 2785.6088546142 | 0 | 109.992 | 0 | car |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
Now would like to compute the average value for columns alt, distance, speed ... for unique value of session_id, trip_id, lat_start,...
Query:
SELECT DISTINCT(session_id, trip_id, lat_start, lat_end, lon_start, lon_end, travel_mode), AVG(alt) AS avg_alt, AVG(distance) AS avg_disntance, AVG(speed) AS avg_speed, AVG(acceleration) AS avg_acc FROM akil.trips_motion_xtics;
ERROR: column "trips_motion_xtics.session_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT DISTINCT(session_id, trip_id, lat_start, lat_end, lon...
Required result:
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| session_id | trip_id | lat_start | lat_end | lon_start | lon_end | alt | distance | segments_length | speed | acceleration | track | travel_mode |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 72.196 | 0 | foot |
| 656 | 303637 | 41.14454009 | 41.1631127 | -8.56292593 | -8.5870161 | 0 | 5921.07030809987 | 2785.6088546142 | 0 | 104.51 | 0 | car |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
You want aggregation. You will get a unique record for each combination of the column listed in the GROUP BY clause, and you can apply aggregate functions (such as AVG()) on other columns:
SELECT
session_id,
trip_id,
lat_start,
lat_end,
lon_start,
lon_end,
travel_mode,
AVG(alt) AS avg_alt,
AVG(distance) AS avg_disntance,
AVG(speed) AS avg_speed,
AVG(acceleration) AS avg_acc
FROM akil.trips_motion_xtics
GROUP BY
session_id,
trip_id,
lat_start,
lat_end,
lon_start,
lon_end,
travel_mode

count continuously postgresql data

i need help with counting some data
this what i want
| user_id | action_id | count |
-------------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 3 | 3 |
| 7 | 4 | 1 |
| 8 | 5 | 1 |
| 9 | 5 | 2 |
| 10 | 6 | 1 |
this is what i have
| user_id | action_id | count |
-------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 1 |
| 6 | 3 | 1 |
| 7 | 4 | 1 |
| 8 | 5 | 1 |
| 9 | 5 | 1 |
| 10 | 6 | 1 |
i really need it for create some research about second action from users
how do i do it?
thank you
Using ROW_NUMBER should work here:
SELECT
user_id,
action_id,
ROW_NUMBER() OVER (PARTITION BY action_id ORDER BY user_id) count
FROM yourTable
ORDER BY
user_id;
Demo

group by breaks order by

Sorry for the long post I'm new to this & want to make sure that I'm fully understood.
I'm trying to make an order by & group query.
I've started with the order by part:
SELECT "tId", "mId","sId","tr", "tg","tp", "date"
FROM table
WHERE "tId" =1
ORDER BY "date" DESC, "mId","sId";
the ouput:
tId | mId | sId | tr | tg | tp | date
-----+-------+------+-----+----+-------+------------------------
1 | 5 | 2 | -73 | 1 | 122 | 2007-01-01 02:03:01+02
1 | 5 | 1 | -72 | 1 | 122 | 2007-01-01 02:02:01+02
1 | 4 | 1 | -70 | 1 | 120 | 2007-01-01 01:01:01+02
1 | 1 | 1 | -30 | 0 | 0 | 2004-10-19 10:23:54+02
1 | 1 | 2 | -31 | 0 | 0 | 2004-10-19 10:23:54+02
1 | 1 | 3 | -32 | 0 | 0 | 2004-10-19 10:23:54+02
1 | 2 | 1 | -40 | 0 | 0 | 2004-10-19 10:23:54+02
1 | 2 | 2 | -41 | 0 | 0 | 2004-10-19 10:23:54+02
1 | 2 | 3 | -42 | 0 | 0 | 2004-10-19 10:23:54+02
1 | 3 | 1 | -50 | 0 | 0 | 2004-10-19 10:23:54+02
1 | 3 | 3 | -50 | 0 | 0 | 2004-10-19 10:23:54+02
The query I would like to do is to group the output of the prev' result and to get:
mId | agg_r | agg_tg | agg_tp | agg_sid | agg_date
-----+--------------+---------+-----------+----------+------------------------------------------------------------------------------
5 | {-73,-72} | {1,1} | {122,122} | {2,1} | {"2007-01-01 02:03:01+02","2007-01-01 02:02:01+02"}
4 | {-70} | {1} | {120} | {1} | {"2007-01-01 01:01:01+02"}
1 | {-30,-31,-32} | {0,0,0} | {0,0,0} | {1,2,3} | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
2 | {-40,-41,-42} | {0,0,0} | {0,0,0} | {1,2,3} | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
3 | {-50,-50} | {0,0} | {0,0} | {1,3} | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
So I've assumed this would work:
SELECT "mId", array_agg("tr") AS agg_r, array_agg("tg") AS agg_tg, array_agg("tp") AS agg_tp, array_agg("sId") AS agg_sid ,array_agg("date") AS agg_date
FROM (
SELECT "tId", "mId","sId","tr", "tg","tp", "date"
FROM table
WHERE "tId" =1
ORDER BY "date" DESC, "mId","sId"
)AS qRes
GROUP BY qRes."mId";
But I'm getting:
mId | agg_r | agg_tg | agg_tp | agg_sid | agg_date
-----+--------------+---------+-----------+----------+------------------------------------------------------------------------------
1 | {-30,-31,-32} | {0,0,0} | {0,0,0} | {1,2,3} | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
4 | {-70} | {1} | {120} | {1} | {"2007-01-01 01:01:01+02"}
2 | {-40,-41,-42} | {0,0,0} | {0,0,0} | {1,2,3} | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
3 | {-50,-50} | {0,0} | {0,0} | {1,3} | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
5 | {-73,-72} | {1,1} | {122,122} | {2,1} | {"2007-01-01 02:03:01+02","2007-01-01 02:02:01+02"}
What am I missing? why does the grouping changes the order?
Like the comment says, there isn't any order on the outer query.
Notice the last line.
SELECT "mId", array_agg("tr") AS agg_r, array_agg("tg") AS agg_tg, array_agg("tp") AS agg_tp, array_agg("sId") AS agg_sid ,array_agg("date") AS agg_date
FROM (
SELECT "tId", "mId","sId","tr", "tg","tp", "date"
FROM table
WHERE "tId" =1
ORDER BY "date" DESC, "mId","sId"
)AS qRes
GROUP BY qRes."mId"
order by max("date") desc;

Ordering and grouping MySQL data

+----+------------------+-----------------+
| id | template_type_id | url |
+----+------------------+-----------------+
| 1 | 1 | text |
| 2 | 2 | text |
| 3 | 1 | text |
| 4 | 1 | text |
| 5 | 1 | text |
| 6 | 1 | text |
| 7 | 1 | text |
| 8 | 1 | text |
| 9 | 2 | text |
| 10 | 2 | text |
+----+------------------+-----------------+
As i am using 1 page template and 2 page template i need to reorder above result as per 1 page and 2 page as below:
+----+------------------+-----------------+
| id | template_type_id | url |
+----+------------------+-----------------+
| 1 | 1 | text |
| 3 | 1 | text |
| 2 | 2 | text |
| 4 | 1 | text |
| 5 | 1 | text |
| 6 | 1 | text |
| 7 | 1 | text |
| 9 | 2 | text |
| 10 | 2 | text |
| 8 | 1 | text |
+----+------------------+-----------------+
+------------------------------------------+
---------------- ------------------
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
---------------- ------------------
+------------------------------------------+
Assuming there's publish_date column in the table that is not shown and the values in it consistent with the ordering of the records in the examples 1 and 2, I suggest:
order by publish_date, template_type_id