GTFS get last stop on trip - mysql

Using a standard GTFS database, I'm trying to add the name of the last stop on a trip to my current query which returns the following:
| trip_id | service_id | departure_stop | departure_time | arrival_stop | arrival_time | end_departure |
|---------------------------------|------------|----------------|----------------|-----------------|--------------|---------------|
| 15693832.T6.2-EPP-E-mjp-1.11.R | T6_1 | Clifton Hill | 04:56:00 | Flinders Street | 05:07:00 | 05:07:00 |
and so on.
My current query is:
select `t`.`trip_id`,
`c`.`service_id`,
`start_s`.`stop_name` as `departure_stop`,
`start_st`.`departure_time`,
`end_s`.`stop_name` as `arrival_stop`,
`end_st`.`arrival_time`,
`end_st`.`departure_time` as `end_departure`
from `trips` as `t`
inner join `calendar` as `c` on `t`.`service_id` = `c`.`service_id`
inner join `routes` as `r` on `t`.`route_id` = `r`.`route_id`
inner join `stop_times` as `start_st` on `t`.`trip_id` = `start_st`.`trip_id`
inner join `stops` as `start_s` on `start_st`.`stop_id` = `start_s`.`stop_id`
inner join `stop_times` as `end_st` on `t`.`trip_id` = `end_st`.`trip_id`
inner join `stops` as `end_s` on `end_st`.`stop_id` = `end_s`.`stop_id`
where `start_st`.`departure_time` > '00:00:00'
and `start_st`.`departure_time` < '23:59:59'
and `start_s`.`stop_id` = 19974
and `end_s`.`stop_id` = 19854
and start_st.departure_time < end_st.arrival_time
order by arrival_time asc
I'm stuck trying to figure out how I can get the last stop in the stop_times table for the trip_id that my query returns for each row.
So in addition to what I have currently, I'd like to:
get the last stop_id for that trip_id in the stop_times table
get the stop_name from the stops table for the corresponding stop_id
display it as the last column in my output as last_stop
Update:
I've tried selecting s.stop_name and adding the following inner join:
inner join (
SELECT s.stop_name, trip_id
FROM stop_times
INNER JOIN stops as s on `s`.`stop_id` = `stop_times`.`stop_id`
ORDER BY stop_sequence DESC
) s on `t`.`trip_id` = `s`.`trip_id`
However, this adds extra rows for every single stop in trip, where I only want the last one and adding LIMIT 1 returns no results.

One caution, arrival and departure times may be later than midnight in the GTFS spec (hour value could be 24, 25, etc.)
select "t"."trip_id",
"c"."service_id",
"start_s"."stop_name" as "departure_stop",
"start_st"."departure_time",
"end_s"."stop_name" as "arrival_stop",
"end_st"."arrival_time",
"end_st"."departure_time" as "end_departure",
"last_st"."arrival_time" as "last_arrival",
"last_s"."stop_name" as "last_stop"
from "trips" as "t"
inner join "calendar" as "c" on "t"."service_id" = "c"."service_id"
inner join "routes" as "r" on "t"."route_id" = "r"."route_id"
inner join "stop_times" as "start_st" on "t"."trip_id" = "start_st"."trip_id"
inner join "stops" as "start_s" on "start_st"."stop_id" = "start_s"."stop_id"
inner join "stop_times" as "end_st" on "t"."trip_id" = "end_st"."trip_id"
inner join "stops" as "end_s" on "end_st"."stop_id" = "end_s"."stop_id"
inner join "stop_times" as "last_st" on "t"."trip_id" = "last_st"."trip_id"
inner join "stops" as "last_s" on "last_st"."stop_id" = "last_s"."stop_id"
where "start_s"."stop_id" = '245' -- my data's stop id
and "end_s"."stop_id" = '762' -- my data's stop id
and "last_st"."stop_sequence" = (select max("stop_sequence") from "stop_times" where "t"."trip_id" = "trip_id")
and start_st.departure_time < end_st.arrival_time
order by arrival_time asc

Related

Selecting IDs from a MIN query that includes join

sorry about the title, very bad at titles!
I have these relevant tables:
Times
+--------+----------+-------------+------+---------+
| TimeID | PlayerID | MapCourseID | Mode | RunTime |
+--------+----------+-------------+------+---------+
Checkpoints
+--------------+--------+------------+---------+
| CheckpointID | TimeID | Checkpoint | RunTime |
+--------------+--------+------------+---------+
Maps
+-------+------+
| MapID | Name |
+-------+------+
MapCourses
+-----------+-------+--------+
| MapCourse | MapID | Course |
+-----------+-------+--------+
RunTime stores their "time" as an int
I have a query that selects the fastest times for a specific map MapID on all courses and all modes
SELECT MIN(Times.RunTime), MapCourses.Course, Times.Mode
FROM Times
INNER JOIN MapCourses ON MapCourses.MapCourseID=Times.MapCourseID
INNER JOIN Players ON Players.PlayerID=Times.PlayerID
WHERE Players.Cheater=0 AND MapCourses.MapID=%d
GROUP BY MapCourses.Course, Times.Mode;
Which works fine, but now I want to make another query that selects the checkpoints of the fastest times, since checkpoints are associated with a TimeID
I've tried doing something like this
SELECT * FROM Checkpoints
INNER JOIN (
SELECT
MIN(Times.RunTime),
MapCourses.Course,
Times.Mode,
MapCourses.MapID,
Players.SteamID32
FROM
Times
INNER JOIN MapCourses ON MapCourses.MapCourseID = Times.MapCourseID
INNER JOIN Players ON Players.PlayerID = Times.PlayerID
WHERE
Players.Cheater = 0
AND MapCourses.MapID = %d
GROUP BY
MapCourses.Course,
Times.Mode
) AS wrs ON Checkpoints.TimeID = wrs.TimeID
Also tried something like this:
SELECT
Checkpoints.RunTime,
Checkpoints.Checkpoint,
MapCourses.Course,
Times.Mode,
Times.TimeID,
Players.Alias
FROM
Checkpoints
INNER JOIN Times ON Times.TimeID = Checkpoints.TimeID
INNER JOIN MapCourses ON MapCourses.MapCourseID = Times.MapCourseID
INNER JOIN Players ON Players.PlayerID = Times.PlayerID
WHERE
Players.Cheater = 0
AND MapCourses.MapID = %d
AND Times.RunTime = (
SELECT
MIN(Times.RunTime)
FROM
Times
WHERE
Times.MapCourseID = MapCourses.MapCourseID
AND Mode = Times.Mode
)
Neither seem to really work, any help would be great, thanks!
Basically, if i'm working on map id 50, I already have a query that gets the fastest time on map id 50 for all modes & courses, the query i'm trying to build is a query that gets the checkpoints of the fastest times for every course and mode on map id 50
We could do something like this:
SELECT c.checkpointid
, c.timeid
, c.checkpoint
, c.runtime
, ...
FROM ( SELECT t.mapcourseid
, t.mode
, MIN(r.runtime) AS min_runtime
FROM Times r
JOIN Players p
ON p.playerid = r.playerid
AND p.cheater = 0
JOIN MapCourses s
ON s.mapcourseid = t.mapcourse_id
AND s.mapid = ?
GROUP
BY t.mapcourseid
, t.mode
) q
JOIN Times t
ON t.runtime = q.min_runtime
AND t.mapcourseid = q.mapcourseid
JOIN Checkpoints c
ON c.timeid = t.timeid
The trick here is using a query as an inline view. The result returned from the query inside the parens gets returned as a resultset, which is then used like a table by the outer query. MySQL calls it a "derived table".
In the outer SELECT list, we can include references to columns from q and t as well as c.
Note: If there are two or more rows from t that match the minimumn runtime (returned by q), the query will return all of the matching rows.
The specification is a bit unclear. The query in this answer satisfies one particular interpretation.
If I'm following this correctly, you want the shortest Checkpoints.TimeID for each Checkpoint in a similar format to the above?
SELECT MIN(Checkpoints.TimeID), MapCourses.Course, Checkpoints.Checkpoint, Times.Mode
FROM Checkpoints
INNER JOIN Times ON Checkpoints.TimeID=Times.TimeID
INNER JOIN MapCourses ON MapCourses.MapCourseID=Times.MapCourseID
INNER JOIN Players ON Players.PlayerID=Times.PlayerID
WHERE Players.Cheater=0 AND MapCourses.MapID=%d
GROUP BY MapCourses.Course, Checkpoints.Checkpoint, Times.Mode;
Obviously not having access to your Database I can't test this, but, seems to be what you're asking?
[edit]
Ahh, so you want to keep the min times, but would like to add checkpoints into the returned values:
SELECT MIN(Times.RunTime), MapCourses.Course, Checkpoints.Checkpoint, Times.Mode
FROM Times
INNER JOIN MapCourses ON MapCourses.MapCourseID=Times.MapCourseID
INNER JOIN Players ON Players.PlayerID=Times.PlayerID
INNER JOIN Checkpoints ON Checkpoints.TimeID=Times.TimeID
WHERE Players.Cheater=0 AND MapCourses.MapID=%d
GROUP BY MapCourses.Course, Checkpoints.Checkpoint, Times.Mode;
So this should return your fastest time, the map, checkpoints and then mode.

Get products without sales for last 7day

I have two tables products and user_sales. I need to get the products which have 0 sales for the last 7 days.
I tried some code which I found in stack overflow with the "HAVING" statement but I found out if I put a limit of 12 products than it doesnt works.
Can anoyone please help ?
table products
id | title | price | images | description
table user_sales
id | product_id | sale_date | user_owner
Well that was the simplified version of the tables. The actual query looks somehow like this.
SELECT `product_store`.`friendly_name` AS `store_friendly_name`,
`api_keys`.`key_data` AS `api_data`,
`ep`.*,
`supplier_store`.`icon` AS `supplier_store_icon`,
`supplier_store`.`internal_name` AS `supplier_store_internal_name`,
`supplier_store`.`friendly_name` AS `supplier_store_friendly_name`,
`supplier_store`.`amazon_type_product` AS
`supplier_store_amazon_type_product`,
`supplier_store`.`handler` AS `supplier_store_handler`,
`sp`.`extra_data` AS `sp_extra_data`,
`sp`.`id` AS `sp_id`,
`sp`.`remote_id` AS `sp_remote_id`,
`sp`.`url` AS `sp_url`,
`sp`.`price` AS `sp_price`,
`sp`.`stock` AS `sp_stock`,
`sp`.`picture` AS `sp_picture`,
`sp`.`store_id` AS `sp_store_id`,
Count(us.date) AS sale_date
FROM `products` `ep`
LEFT JOIN `stores` `product_store`
ON `ep`.`store_id` = `product_store`.`id`
LEFT JOIN `api_keys`
ON `api_keys`.`id` = `ep`.`link_key`
LEFT JOIN `products` `sp`
ON `ep`.`linked_to` = `sp`.`id`
LEFT JOIN `stores` `supplier_store`
ON `supplier_store`.`id` = `sp`.`store_id`
RIGHT JOIN `user_sales` `us`
ON `ep`.`remote_id` = `us`.`remote_id`
WHERE `ep`.`user_owner` = '3992'
AND `ep`.`expired` = 0
AND `us`.`user_id` = '3992'
AND us.date > "2019-02-09 14:21:34"
AND us.date < "2019-05-10 14:21:34"
AND `ep`.`store_id` = 3
GROUP BY `ep`.`id`
HAVING `sale_date` < 1
ORDER BY `ep`.`id` DESC
LIMIT 15
You want to ensure that no sales exist for the product in the last seven days. Use NOT EXISTS for that.
From your query I take it that you want to restrict this to products belonging to user_owner 3992 and to sales of the same user.
WHERE ep.user_owner = 3992
AND ep.expired = 0
AND ep.store_id = 3
AND NOT EXISTS
(
SELECT *
FROM user_sales us
WHERE us.user_id = ep.user_owner
AND us.date > current_date - interval 7 day
)
The complete query:
SELECT product_store.friendly_name AS store_friendly_name,
api_keys.key_data AS api_data,
ep.*,
supplier_store.icon AS supplier_store_icon,
supplier_store.internal_name AS supplier_store_internal_name,
supplier_store.friendly_name AS supplier_store_friendly_name,
supplier_store.amazon_type_product AS
supplier_store_amazon_type_product,
supplier_store.handler AS supplier_store_handler,
sp.extra_data AS sp_extra_data,
sp.id AS sp_id,
sp.remote_id AS sp_remote_id,
sp.url AS sp_url,
sp.price AS sp_price,
sp.stock AS sp_stock,
sp.picture AS sp_picture,
sp.store_id AS sp_store_id
FROM products ep
LEFT JOIN stores product_store ON ep.store_id = product_store.id
LEFT JOIN api_keys ON api_keys.id = ep.link_key
LEFT JOIN products sp ON ep.linked_to = sp.id
LEFT JOIN stores supplier_store ON supplier_store.id = sp.store_id
WHERE ep.user_owner = 3992
AND ep.expired = 0
AND ep.store_id = 3
AND NOT EXISTS
(
SELECT *
FROM user_sales us
WHERE us.user_id = ep.user_owner
AND us.date > current_date - interval 7 day
)
ORDER BY ep.id DESC;

Unique rows in join result

I have a tables of delas and curencies look like this
curecnies
id,code
pairs (the available pairs of curencies )
id to_sell to_buy
deals
id
user_id
pair_id
amount_to_sell
amount_to_buy
So I need to get all match deals which can execute , but I am can not get the unique matches.
Here is my sql query
select *
from deals as d1
join deals d2
on d1.sell_amount = d2.buy_amount and d1.buy_amount = d2.sell_amount
i am getting result look like this
id | user_id | pair_id | amount_to_buy | amount_to_sell | id | user_id | pair_id | amount_to_buy | amount_to_sell
1|2|1|1000|3000|2|1|2|3000|1000
2|1|2|3000|1000|1|2|1|1000|3000
You may try using a least/greatest trick here:
SELECT t1.*, t2.*
FROM
(
SELECT DISTINCT
LEAST(d1.id, d2.id) AS d1_id,
GREATEST(d1.id, d2.id) AS d2_id
FROM deals AS d1
INNER JOIN deals d2
ON d1.sell_amount = d2.buy_amount AND
d1.buy_amount = d2.sell_amount
) d
INNER JOIN deals t1
ON d.d1_id = t1.id
INNER JOIN deals t2
ON d.d2_id = t2.id;
The basic idea here is that the subquery labelled d finds a single pair of matched deal IDs, using a least/greatest trick. Then, we join twice to the deals table again to bring in the full information for each member of that deal pair.

How do I perform this join query?

I have a table leave_form which looks like:
type id reporting_id leave_bal from_date_id leave_from to_date_id leave_to number leave_for status applied_dates_id pendays
personal 99 6 10 1023 full day 1313 full day 10 personal yes 1026 null
I have separate table for dates, so that I can refer these dates into leave_form. My leave_date table looks like:
date_id(AI) dates(UK)
1025 2016-02-18
1301 2016-02-20
1218 2016-02-16
This date_id I have inserted into from_date_id, to_date_id, applied_dates_id columns in leave_form table i.e. all dates are inserted into leave_date table and from this table I am only referring the date_id into leave_form table.
There is also a table that keeps the emp_code and emp_name. My personal table is:
id(AI) emp_code(PK) emp_name
99 K0209 Nijo
When I am trying to fetch the date for from_date_id, to_date_id, applied_dates_id column from leave_form table I don't get any values.
My query for fetching the dates is:
select g.type, a.emp_code, h.rm_id, h.rm_name, g.leave_bal, i1.dates as from_date,
g.leave_from, i2.dates as to_date, g.leave_to, g.number, g.leave_for, g.status,
i3.dates as applied_date, g.pendays
from personal a
inner join leave_form g
on a.id = g.id
inner join inform_to_rm h
on h.reporting_id = g.reporting_id
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
where a.emp_code = 'K0209';
It shows me result like:
type, emp_code, rm_id, rm_name, leave_bal, from_date, leave_from, to_date, leave_to, number, leave_for, status, applied_date, pendays
i.e no data gets returned when I am executing this query.
I would agree with one of the comments to the question. I would recommend referencing the date directly in the leave_form table instead of a FK to a table with dates. But back to the question. You haven't described all of your tables completely, so it is possible that there are multiple problems that I can't see, however, there is definitely one problem.
Your query joins on
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
This is incorrect. leave_dates.dates is the actual DATE, while the columns that you are joining on (leave_form.from_date_id, leave_form.to_date_id, leave_form.applied_dates_id) are foreign key references.
For example, 1023 does not equal 2016-02-18 so you get no match. Replacing the above query-snippet with the following would correct this particular problem.
inner join leave_dates i1
on i1.date_id = g.from_date_id
inner join leave_dates i2
on i2.date_id = g.to_date_id
inner join leave_dates i3
on i3.date_id = g.applied_dates_id

mysql sum() return double value using multiple joins

select
a.ClientID,
f.Currency,
a.OrganizationName,
COALESCE(sum(b.GrandTotal),0) as SaleGrandTotal,
COALESCE(sum(g.AmountReceived),0) as AmountReceived,
COALESCE(sum(b.GrandTotal - g.AmountReceived),0) as SaleBalanceRemaining,
COALESCE(sum(d.GrandTotal), 0) as PurchaseGrandTotal,
COALESCE(sum(e.AmountPaid), 0) as AmountPaid,
COALESCE(sum(d.GrandTotal - e.AmountPaid),0) as PurchaseBalanceRemaining,
COALESCE(sum(b.GrandTotal - g.AmountReceived),0) - COALESCE(sum(d.GrandTotal - e.AmountPaid),0) as Total
from na_clients as a
join na_currency as f
left join na_transaction as b
on a.ClientID = b.ClientID and b.CurrencyID = f.CurrencyID and b.IsActive = 1
left join na_recoverylogs as g
on b.TID = g.TID
left join na_purchase as d
on a.ClientID = d.ClientID and d.CurrencyID = f.CurrencyID and d.IsActive = 1
left join na_purchaselogs as e
on e.PID = d.PID
group by a.OrganizationName,f.Currency
order by a.OrganizationName
I am using multiple currency like dollar,CNY,rupees.
It was working fine but today i noticed sum() double value like b.GrandTotal should be 11500 but its return 23000
Table Client:
clientid,name,organizationName
1,client1,OrgName
2,client2,OrgName
Table Currency:
currencyid,cname
1,Dollar
2,Rupees
Table Transaction:
tid,clientid,currencyid,grandTotal,amountReceived,balanceremaining
1,1,1,11000,0,11000
2,1,1,500,0,500
Table recoveryLogs: // Another Error Here
id,tid,amountreceived
1,1,0
2,2,0
3,2,2000 // Again sum() multiply value - because of PID 2 is repeating
Table Purchase:
pid,clientid,currencyid,grandTotal,amountPaid,balanceRemaining
1,1,1,25000,0,25000
1,2,2,2,3000,1000,2000
Now I am using sum(b.grandTotal) instead of 11500 it return 23000
Table PurchaseLogs: // Another Error Here
id,pid,amountpaid
1,1,0
2,2,1000
3,1,1000 // Again sum() multiply value - because of PID 1 is repeating
So result should be:
Client: Client1
SaleGrandTotal: 11500
AmountReceived: 0
SaleBalanceRemaining: 11500
PurchaseGrandTotal: 25000
AmountPaid: 0
PurchaseBalanceRemaining: 25000
Total Amount: -13500
But result i get:
Client: Client1
SaleGrandTotal: 23000
AmountReceived: 0
SaleBalanceRemaining: 23000
PurchaseGrandTotal: 50000
AmountPaid: 0
PurchaseBalanceRemaining: 50000
Total Amount: -27000
If i remove purchase clause(d and e) or transaction(b and g) clause from query it's working fine individually.
The reason data is doubling is your ClientID has different occurrences in Transaction and Purchase tables and hence not a 1-to-1 match. ClientID = 1 and CurrencyID = 1 appears twice in Transaction and only once in Purchase. When you join the tables, a combination set of 1 x 2 = 2 ClientID records result with some fields repeating data. Thus, summing will double for repeat entries. As illustration:
Transaction Data | Purchase Data
row1: 1,1,1,11000,0,11000 | 1,1,1,25000,0,25000
row2: 2,1,1,500,0,500 | 1,1,1,25000,0,25000
Consider separating the aggregation between both tables using derived tables. Then, join the four underlying aggregates (transaction, purchase, recovery log, purchase log) for final query. The join will match 1-to-1 if you aggregate, grouping on ClientID and CurrencyID, TID and PID.
SELECT
transAgg.ClientID, transAgg.Currency, transAgg.OrganizationName,
transAgg.SaleGrandTotal, recovLogAgg.SumOfAmtReceived,
(transAgg.SaleGrandTotal - recovLogAgg.SumOfAmtReceived) as SaleBalanceRemaining,
purchAgg.PurchaseGrandTotal, purchLogAgg.SumOfAmtPaid,
(purchAgg.PurchaseGrandTotal - purchLogAgg.SumOfAmtPaid) as PurchaseBalanceRemaining,
((transAgg.SaleGrandTotal - recovLogAgg.SumOfAmtReceived) -
(purchAgg.PurchaseGrandTotal - purchLogAgg.SumOfAmtPaid)) As [Total]
FROM
(SELECT
a.ClientID, f.CurrencyID, f.Currency, a.OrganizationName,
COALESCE(sum(b.GrandTotal),0) as SaleGrandTotal
FROM na_clients as a
INNER JOIN na_currency as f
LEFT JOIN na_transaction as b
ON a.ClientID = b.ClientID
AND b.CurrencyID = f.CurrencyID
AND b.IsActive = 1
GROUP BY a.ClientID, a.OrganizationName, f.CurrencyID, f.Currency
ORDER BY a.OrganizationName) As transAgg
INNER JOIN
(SELECT
a.ClientID, f.CurrencyID, f.Currency, a.OrganizationName,
COALESCE(sum(d.GrandTotal), 0) as PurchaseGrandTotal
FROM na_clients as a
INNER JOIN na_currency as f
LEFT JOIN na_purchase as d
ON a.ClientID = d.ClientID
AND d.CurrencyID = f.CurrencyID
AND d.IsActive = 1
GROUP BY a.ClientID, a.OrganizationName, f.CurrencyID, f.Currency
ORDER BY a.OrganizationName) As purchAgg
ON transAgg.ClientID = purchAgg.ClientID
AND transAgg.CurrencyID = purchAgg.CurrencyID
INNER JOIN
(SELECT
g.TID, COALESCE(sum(g.AmountReceived),0) As SumOfAmtReceived
FROM na_recoverylogs as g
GROUP BY g.TID) As recovlogAgg
ON transAgg.TID = recovlogAgg.TID
INNER JOIN
(SELECT
e.PID, COALESCE(sum(e.AmountPaid),0) As SumOfAmtPaid
FROM na_purchaselogs as e
GROUP BY e.PID) As purchlogAgg
ON purchAgg.PID = purchlogAgg.PID