Inner JOIN just last result - mysql

I just edit my old code and try to make nice complex query.
My query looks like:
SELECT axnmrs_cases.claimnumber as claim, axnmrs_cases.vin as vin, axnmrs_cases.date_created as date, axnmrs_calculations.totalcosts as totalcosts, axnmrs_cases.country as country
FROM axnmrs_cases
INNER JOIN axnmrs_calculations ON ( axnmrs_cases.case_id = axnmrs_calculations.case_id
AND axnmrs_cases.country = axnmrs_calculations.country )
WHERE vin = :vin
This works and display results, whats perfect (even I can't belieave it :D), however I need just last calculation not all of them.
INNER JOIN axnmrs_calculations ON ( axnmrs_cases.case_id = axnmrs_calculations.case_id
AND axnmrs_cases.country = axnmrs_calculations.country ) ORDER BY axnmrs_calculations.calculation_id DESC LIMIT 1
However I'm not sure how to limit just INNER JOIN not the whole query, can someone advise me please?
Thanks

If you want the last calculation per case_id and country, then you need to create a derived table (subquery in the from clause) that gets you these and you join the cases and calculations table on this derived table.
You need to have either an auto increment id in the calculations table or a timestamp, or a flag that identifies the last version of the calculations. I'll assume, that you have an autoincrement id field in the calculations table:
SELECT axnmrs_cases.claimnumber as claim, axnmrs_cases.vin as vin, axnmrs_cases.date_created as date, axnmrs_calculations.totalcosts as totalcosts, axnmrs_cases.country as country
FROM axnmrs_cases
INNER JOIN axnmrs_calculations AS ac ON ( axnmrs_cases.case_id = ac.case_id AND axnmrs_cases.country = ac.country )
INNER JOIN (SELECT MAX(id) AS maxid, case_id, country FROM axnmrs_calculations GROUP BY case_id, country) AS T
ON ac.id=T.maxid and ac.case_idóT.case_id and ac.country=T.country
WHERE vin = :vin

Related

Getting the top sales

How to i get the top vendor for each country? I have this code and it shows the connection between the two tables, now I have to get the largest gmv per country.
Here is my working code:
SELECT DISTINCT a.country_name, b.vendor_name,
SUM(a.gmv_local) as total_gmw
from `my-project-67287.order1.order2`a
join `my-project-67287.vendor1.vendor2` b on a.vendor_id = b.id
group by a.country_name, b.vendor_name;
The top 3 should show this:
Assuming you can save that SELECT into a table called vendors, you need to use it as the subquery in the FROM clause.
You could use this:
SELECT vendors.country_name, vendors.vendor_name, MAX(vendors.total_gmw)
FROM
(
SELECT DISTINCT a.country_name, b.vendor_name,
SUM(a.gmv_local) as total_gmw
from `my-project-67287.order1.order2`a
join `my-project-67287.vendor1.vendor2` b on a.vendor_id = b.id
group by a.country_name, b.vendor_name
) AS vendors
GROUP BY vendors.country_name;
I must mention I have not tested your query, since I do not have your tables, so I assumed it's correct.
I only created the vendors table with the required fields and values from your picture. This should print:
SELECT odr.c_name,vdr.v_name,odr.gmv
FROM(
SELECT *
FROM(
SELECT c_name,v_id,gmv
FROM `order`
ORDER BY gmv DESC
)
GROUP BY c_name
)AS odr
LEFT JOIN vender AS vdr ON vdr.id = odr.v_id
GROUP BY odr.c_name
c_name short for country_name

SQL Query for getting maximum value from a column Joining from Another Table

This is a slight variant of the question I asked here
SQL Query for getting maximum value from a column
I have a Person Table and an Activity Table with the following data
-- PERSON-----
------ACTIVITY------------
I have got this data in the database about users spending time on a particular activity.
I intend to get the data when every user has spent the maximum number of hours.
My Query is
Select p.Id as 'PersonId',
p.Name as 'Name',
act.HoursSpent as 'Hours Spent',
act.Date as 'Date'
From Person p
Left JOIN (Select MAX(HoursSpent), Date from Activity
Group By HoursSpent, Date) act
on act.personId = p.Id
but it is giving me all the rows for Person and not with the Maximum Numbers of Hours Spent.
This should be my result.
You have several issues with your query:
The subquery to get hours is aggregated by date, not person.
You don't have a way to bring in other columns from activity.
You can take this approach -- joins and group by, but it requires two joins:
select p.*, a.* -- the columns you want
from Person p left join
activity a
on a.personId = p.id left join
(select personid, max(HoursSpent) as max_hoursspent
from activity a
group by personid
) ma
on ma.personId = a.personId and
ma.max_hoursspent = a.hoursspent;
Note that this can return duplicates for a given person -- if there are ties for the maximum.
This is written more colloquially using row_number():
select p.*, a.* -- the columns you want
from Person p left join
(select a.*,
row_number() over (partition by a.personid order by a.hoursspent desc) as seqnum
from activity a
) a
on a.personId = p.id and a.seqnum = 1
ma.max_hoursspent = a.hoursspent;

mysql counts with group by to show all types per country include where no record is present as 0

I have the following mysql query and attempting to do group by country and type, however for all countries not all types are available but would still like to see all types for every country populated with 0.
select distinct
t1.Country,
t2.sectype,
count(t1.secid) AS SecID
from test.t2
left outer join test.t1 on test.t2.sectype= test.t1.sectype
group by t1.Country, t2.sectype;
t1 has country, sectype and secid fields and have created another table t2 which has all sectype's possible.
I get the following output:
https://i.stack.imgur.com/VAdyj.png
As you can see Germany only has 3 sectype's attached to that country but would like to see all sectype's like Canada - to be like the following output:
https://i.stack.imgur.com/ZC73H.png
Is this possible to do? Thanks
Consider a cross join of your distinct country and sectype tables. Then left join this all possible pairings to your actual data table. Finally, use a SUM condition over COUNT. Below uses table names that should be updated to your actual tables:
select cj.Country,
cj.sectype,
sum(d.secid IS NOT NULL) AS Count_SecID
from
(select n.country, s.sectype
from sectypes_table s
cross join countries_table n) cj
left outer join actual_data d
on d.sectype = cj.sectype AND d.country = cj.country
group by cj.Country,
cj.sectype;
To avoid the cross join should you have many distinct values, create such a table beforehand and replace subquery with this new table:
create table country_sectypes as (
select n.country, s.sectype
from sectypes_table s
cross join countries_table n
);
select cs.Country,
cs.sectype,
sum(d.secid IS NOT NULL) AS Count_SecID
from country_sectypes cs
left outer join actual_data d
on d.sectype = cs.sectype AND d.country = cs.country
group by cs.Country,
cs.sectype;
Rextester Demo (using actual_data for distinct country and sectype)

Link to another table to get name with inner join

The following code shows the max date for an item and all works well.
SELECT
pricing_id, pricing.field, pricing.region, price, max_date
FROM
pricing
INNER JOIN
(SELECT
field, MAX(end_date) AS 'max_date'
FROM
pricing, regions
GROUP BY field) AS tmptable ON tmptable.max_date = pricing.end_date
AND tmptable.field = pricing.field
ORDER BY region, pricing.field
I am trying to pull region name from regions.region_name to replace the pricing.region column which just shows the ID. I have tried the usual where clause to join tables and display the descriptive name but it breaks it.
Can anyone help?
Thanks,
John
You have to select region_name from the regions table
somthing like
SELECT
pricing_id, pricing.field, tmptable.region_name , price, max_date
FROM
pricing
INNER JOIN
(SELECT
region_name , field, MAX(end_date) AS 'max_date'
FROM
pricing, regions
GROUP BY field) AS tmptable ON tmptable.max_date = pricing.end_date
AND tmptable.field = pricing.field
ORDER BY region, pricing.field
should work.
Assuming your table is similar in setup this should work. I am making an assumption that you don't want to try to find it in the sub-select which seems like more work than required. Instead I added it as a LEFT JOIN and changed the rest of the query around to match.
SELECT
pricing_id, pricing.field, r.region_name, price, max_date
FROM
pricing
INNER JOIN
(SELECT
field, MAX(end_date) AS 'max_date'
FROM
pricing
GROUP BY field) AS tmptable ON tmptable.max_date = pricing.end_date
AND tmptable.field = pricing.field
LEFT JOIN regions r ON r.id = pricing.region
ORDER BY r.region_name, pricing.field

SQL max of a count involving 3 tables

I have one table Customers with CustomerID and PhoneNumber, the second table is Orders that has CustomerId and OrderNumber and a third table OrderDetails that has OrderNumber, PriceOfOneUnit and UnitsOrdered. I need to find the PhoneNumber for the customer who placed the largest order (PriceOfOneUnit * UnitsOrdered). Doing a count(PriceOfOneUnit*UnitsOrdered) as A1 and then `Group By CustomerId Order By A1 DESC LIMIT 1 is evidently not working after joining the 3 tables. Can anyone help.
If we take you at your word, and what you want is the biggest single line-item rather than the largest order, you can find the largest line-item and then find the order to which it belongs and then the customer who placed that order. You can use a dummy aggregate function to pull back the order id from orderDetails.
EDIT:
OK, for someone just starting out, I think it can be clearer to think in terms of Venn diagrams and use what are called Inline Views and subqueries:
select customername, phone
from customer
inner join
(
select o.id, customerid from orders o
inner join
(
select od.orderid from orderdetail od
where (od.qty * od.itemprice) =
(
select max(od.qty * od.itemprice)
from orderdetail as od
)
) as biggestorder
on o.id = biggestorder.orderid
) as X
on customer.id = X.customerid
Each of the queries inside parentheses returns a set that can be joined/intersected with other sets.
Give this a try,
SELECT cus.CustomerId, cus.PhoneNumber
FROM Customers cus
INNER JOIN Orders a
ON cus.CustomerId = a.CustomerId
INNER JOIN OrderDetails b
On a.OrderNumber = b.OrderNumber
GROUP BY cus.CustomerId, cus.PhoneNumber
HAVING SUM(b.PriceOfOneUnit * b.UnitsOrdered) =
(
SELECT SUM(b.PriceOfOneUnit * b.UnitsOrdered) totalOrdersAmount
FROM Orders aa
INNER JOIN OrderDetails bb
On aa.OrderNumber = bb.OrderNumber
GROUP BY aa.CustomerId
ORDER BY totalOrdersAmount DESC
LIMIT 1
)