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
Related
I have a table with real estate agent's info and want to pull firstname, fullname, and email from rets_agents.
I want to then get a count of all of their sales from a different table called rets_property_res_mstr.
I created a query that doesn't work yet so I need some help.
SELECT r.firstname, r.fullname, r.email
from rets_agents r
LEFT JOIN rets_property_res_mstr
ON r.email = rets_property_res_mstr.ListAgentEmail
LIMIT 10;
I'm not sure how to get the count in this.
You seem to be looking for aggregation:
SELECT a.firstname, a.fullname, a.email, COUNT(p.ListAgentEmail) cnt
FROM rets_agents a
LEFT JOIN rets_property_res_mstr p ON r.email = p.ListAgentEmail
GROUP BY a.firstname, a.fullname, a.email
ORDER BY ?
LIMIT 10;
Note that, for a LIMIT clause to really make sense, you need a ORDER BY clause so you get a deterministic results (otherwise, it is undefined which records will be shown) - I added that to your query with a question mark that you should replace with the relevant column(s).
I would consider using a CTE for this:
WITH sales as (
SELECT ListAgentEmail, count(*) count_of_sales
FROM rets_property_res_mstr
GROUP BY ListAgentEmail
)
SELECT r.firstname, r.fullname, r.email, count_of_sales
from rets_agents r
LEFT JOIN sales
ON r.email = sales.ListAgentEmail
LIMIT 10;
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)
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
I have used UNION for 2 separate Mysql query, The query is as below.
select country,u_id from
(SELECT regionShortName as country , GROUP_CONCAT( urls_regions.u_id ) AS
u_id
FROM urls_regions
where LENGTH(regionShortName )<=2
GROUP by regionShortName)
UNION
(SELECT countries.name as country,
GROUP_CONCAT( urls_regions.u_id ) AS u_id
FROM `countries` countries
inner join regions on countries.regions_id = regions.id
INNER JOIN urls_regions ON regions.region = urls_regions.regionShortName
GROUP by countries.name, country)
The above query has given me the results as below,
I am not able to achieve as below,
Now I would like know how to group by the "country" by concat(merge or join) "u_id"
Is my query implemented is right approach or is there any other approach to achieve this.
Using below query I am able to get what I am expecting,
select tb.country, GROUP_CONCAT(tb.u_id, ',') from
((SELECT regionShortName as country , GROUP_CONCAT( urls_regions.u_id ) AS
u_id
FROM urls_regions
where LENGTH(regionShortName )<=2
GROUP by regionShortName)
union
(SELECT countries.name as country,
GROUP_CONCAT(urls_regions.u_id ) AS u_id
FROM `countries` countries
inner join regions on countries.regions_id = regions.id
INNER JOIN urls_regions ON regions.region = urls_regions.regionShortName
GROUP by countries.name, country)) tb group by tb.country
I have done 2 things
In my first line of query I have added GROUP_CONCAT(tb.u_id, ',')
In the last line added group by tb.country
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