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
Related
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
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;
There are 2 tables - Person(personName, areaId, birthDate) and Area(areaId, areaCode). Now I want a list of persons with their areaCode in a manner such that the all the persons from an areaCode are listed together and the areaCode with the oldest person should be first areaCode to appear. How do I achieve this through mysql query?
I have tried using ORDER BY with multiple columns in vain.
SELECT P.personName, A.areaCode, P.birthDate
FROM Person P
JOIN Area A ON P.areaId = A.areaId
ORDER BY P.birthDate, A.areaCode
Suppose there are Area1 and Area2. Area2 has the oldest and newest born person. Therefore, Area2 records should appear first.
Update: I managed to solve this, but is there any way I can shorten this code.
SELECT *
FROM (SELECT DISTINCT
A.areaCode,
MIN(P.birthDate)
FROM Area A
JOIN Person P on P.areaId = A.areaId
GROUP BY A.areaCode
ORDER BY P.birthDate) T1
JOIN (SELECT DISTINCT
P.personName,
A.areaCode,
P.birthDate
FROM Area A
JOIN Person P on P.areaId = A.areaId
ORDER BY P.createdTimestamp) T2 ON T1.barcode = T2.binId;
You start with a subquery to get the earliest birthdate in each area code.
SELECT A.areaId, MIN(birthDate) earliestBirthDate
FROM Area A
JOIN Person P ON A.areaId = p.areaId
GROUP BY A.areaId
You can try this subquery to convince yourself it yields exactly one row per areaId, showing the needed earliest birth date.
Then you JOIN the subquery to your detail query
SELECT P.personName, A.areaCode, P.birthDate
FROM Person P
JOIN Area A ON P.areaId = A.areaId
JOIN (
SELECT A.areaId, MIN(birthDate) earliestBirthDate
FROM Area A
JOIN Person P ON A.areaId = p.areaId
GROUP BY A.areaId
) EARLY ON P.areaId = EARLY.areaID
ORDER BY EARLY.earliestBirthDate, P.birthDate, A.areaCode
The trick is to use a subquery to make a virtual table so you can order on a column from that table.
Pro tip: If you find yourself using SELECT DISTINCT to retrieve detail data from tables, it's a caution flag. You may be doing something wrong.
Query result
Procurement table
My Query is not giving me what i want to get,
SELECT p.procid
, p.procdate
, p.vendor
, s.sup_name
, p.creditamount
, p.image
FROM procurement as p
, supplier as s
WHERE p.vendor = s.sid
GROUP
BY sid
ORDER
BY p.procid ASC
Query is giving me the first entry in the table for each vendor, while i want to get the last entry for each vendor in the procurement table(the required one's are highlighted in the image), any input will be appreciated, thanks in advance.
You can use a correlated sub-query
select t2.*,s.sup_name from
(
select t.* from procurement t
where t.procid in
(
select max(procid)
from procurement t1
where t1.vendor=t.vendor
)
) as t2 join supplier as s on t2.vendor = s.sid
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