I have two tables: Customer and ParkingTransaction. I want to show the top 10 customers who use the lot most often. 'CustomerKey' in the ParkingTrasaction table is the FK that connects ParkingTransaction to Customer. I have written the following code which counts the most used CustomerKey in the ParkingTransaction table, and it works fine...
SELECT TOP 10 CustomerKey
, count(*) as 'Usage'
FROM ParkingTransaction
GROUP BY CustomerKey
HAVING (count(CustomerKey) > 0)
ORDER BY 'Usage' DESC
This is my output
Output
The problem I am facing is this: I want to pull the FirstName and LastName fields from the Customer table, instead of sorting by just the CustomerKey. I have messed around with JOINS, but haven't come up with a solution yet.
Thanks!
MySQL uses LIMIT not TOP.
SELECT a.FirstName, a.LastName,
COUNT(*) as `Usage`
FROM Customer a
INNER JOIN ParkingTransaction b
ON a.CustomerKey = b.CustomerKey
GROUP BY a.FirstName, a.LastName
ORDER BY `Usage` DESC
LIMIT 10
since the query is using INNER JOIN, HAVING COUNT(*) > 0 is unneccessary in this case.
Here is one way:
SELECT CustomerKey, c.firstname, c.lastname, count(*) as 'Usage'
FROM ParkingTransaction pt join
customer c
on pt.customerkey = c.customerkey
GROUP BY pt.CustomerKey, c.firstname, c.lastname,
ORDER BY 'Usage' DESC
limit 10
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;
I have 5 tables connected with each other in the image
[![Image1][1]][1]
I have tried writing query
SELECT lastname,firstname
FROM customer,purchase_order
where customer.customer_id=purchase_order.customer_id
What parameter should I consider for most purchase?
You can solve that by adjusting the select clause (remove order_id).
Then you will need a HAVING clause to implement the condition to only show the customers with the most purchase orders.
SELECT lastname,firstname
FROM customer
INNER JOIN purchase_order
ON customer.customer_id=purchase_order.customer_id
GROUP BY customer_id
ORDER BY COUNT(order_id) DESC LIMIT 1;
select lastname, firstname from customer c
inner join (
select customer_id, count(1) from purchase_order
group by 1
order by 2 desc limit 1) most
/* the above sub-query named as 'most' fetched the customer id with the maximum
number of orders */
on c.customer_id = most.customer_id; -- this is a simple join of customers table with the table generated from sub-query above
SELECT c.firstname,c.lastname
FROM customer c
JOIN purchase_order po
ON c.customer_id = po.customer_id
GROUP BY c.firstname,c.lastname
ORDER BY COUNT(po.order_id) DESC LIMIT 1;
Write a query that returns the names of the top five customers who have purchased the most songs from ATunes, ordered by the number of purchases (descending). I must also limit the results to the top 5 as stated.
SELECT P.CustomerID, A.FirstName, A.LastName, P.DateOfPurchase, COUNT(DateOfPurchase) as NumberOfPurchases
ORDER BY NumberOfPurchases DESC, LIMIT 0,5
FROM Purchases as P
JOIN ATunesCostumers as A on (A.CustomerID = P.CustomerID)
GROUP BY CustomerID;
this is what I am trying, and I am getting a syntax error.
When I get rid of the ORDER BY and LIMIT statements I get everything I need for this, other than the limitation and correct ordering. I'm at a loss, does anyone know what I am doing wrong?
Try this:
SELECT P.CustomerID, A.FirstName, A.LastName, P.DateOfPurchase, COUNT(DateOfPurchase) as NumberOfPurchases
FROM Purchases as P
JOIN ATunesCostumers as A on P.CustomerID = A.CustomerID
GROUP BY CustomerID
ORDER BY NumberOfPurchases DESC LIMIT 0,5;
Below are the two tables, customer and department. I am struggling very hard to get the output.
I want to write the query which shows only the department name which have maximum number of employees.
Answer should be like this ...
Please help me to write the query.
I would suggest using a sub-query, and then selecting from that query. With the outer select, you order by the number of employees and then limit it to 1. This will give you the top department, but also has the flexibility to be modified to give you a list of the x-number of top departments.
SELECT Dep_Name FROM (
SELECT
d.Dep_Name, COUNT(*) AS `count`
FROM
Departments d
JOIN Employees e ON e.Dep_id = d.Dep_id
GROUP BY
d.Dep_id
) AS q
ORDER BY `count` DESC
LIMIT 1
UPDATE
Per a comment by #Dems, you can actually handle this without a sub-query:
SELECT
d.Dep_Name
FROM
Departments d
JOIN Employees e ON e.Dep_id = d.Dep_id
GROUP BY
d.Dep_id
ORDER BY
COUNT(*) DESC
LIMIT 1
SELECT *
FROM
(
SELECT
d.dep_id,
d.dep_name,
count(c.cus_id) cusCount
FROM
cus c,
dep d
WHERE
c.dep_id = d.dep_id
GROUP BY
d.dep_id,d.dep_name
ORDER BY
cusCount desc)
WHERE
ROWNUM = 1;
I created cus & dep tables in Oracle 10g database and tested my code successfully.
What database are you using, and could you post you code.
There error message shows that your "Order by" clause is wrong.