Hadoop Hive query select and group by from separate tables - mysql

Below are the avg_mileage table and the trucks table.
What I'm trying to do is to compile an query which allows me to select or create a table with avg_mileage.avgmpg and group by the trucks.model in order from highest to lowest of avg_mileage.avg_mpg.
Something like this:

Isn't this a simple join rather than a group by? (sorry can't "comment" because I don't have enough rep yet.)
OK, I think I get your question. You've already done.
SELECT truckid, avg(mpg) avgmpg FROM truck_mileage GROUP BY truckid;
Now you want truck.model rather than truckid, AND you want it sorted?
SELECT model, avgmpg FROM avg_mileage JOIN trucks ON (avg_mileage.truckid = trucks.truckid) ORDER BY avgmpg DESC;
Try something like that.

Related

SQL Temporary Table or Select

I've got a problem with MySQL select statement.
I have a table with different Department and statuses, there are 4 statuses for every department, but for each month there are not always every single status but I would like to show it in the analytics graph that there is '0'.
I have a problem with select statement that it shows only existing statuses ( of course :D ).
Is it possible to create temporary table with all of the Departments , Statuses and amount of statuses as 0, then update it by values from other select?
Select statement and screen how it looks in perfect situation, and how it looks in bad situation :
SELECT utd.Departament,uts.statusDef as statusoforder,Count(uts.statusDef) as Ilosc_Statusow
FROM ur_tasks_details utd
INNER JOIN ur_tasks_status uts on utd.StatusOfOrder = uts.statusNR
WHERE month = 'Sierpien'
GROUP BY uts.statusDef,utd.Departament
Perfect scenario, now bad scenario :
I've tried with "union" statements but i don't know if there is a possibility to take only "the highest value" for every department.
example :
I've also heard about
With CTE tables, but I don't really get how to use it. Would love to get some tips on it!
Thanks for your help.
Use a cross join to generate the rows you want. Then use a left join and aggregation to bring in the data:
select d.Departament, uts.statusDef as statusoforder,
Count(uts.statusDef) as Ilosc_Statusow
from (select distinct utd.Departament
from ur_tasks_details utd
) d cross join
ur_tasks_status uts left join
ur_tasks_details utd
on utd.Departament = d.Departament and
utd.StatusOfOrder = uts.statusNR and
utd.month = 'Sierpien'
group by uts.statusDef, d.Departament;
The first subquery should be your source of all the departments.
I also suspect that month is in the details table, so that should be part of the on clause.

mysql left join and order by

hi i have to different tables m_mp and m_answer
both have m_date and m_mdate which fills with time() function i want to select both entries but order by date
example:
first table: 'some text','6464647776'
second table 'some answer','545454545'
so i want to show first second table and then the first one
this is the code im using:
SELECT r.*,u.*
FROM `mensajes` as r
LEFT JOIN `m_answer` as u on r.id = u.id_m
WHERE r.id = '1'
ORDER BY m_date
and then display the result of each table using while-loop
I guess I get what you want to do.
You may force both grouping and ordering using ORDER BY, like this:
http://sqlfiddle.com/#!9/373d65/9
I know the solution is not optimal in terms of speed, but, given proper indices, I suspect performance to be acceptable, unless you are already aiming for millions of messages; when that time comes, you would like to either properly GROUPBY, or make subsequent queries for just the recently answered questions from the current page.

MySQL: Count then sort by the count total

I know other posts talk about this, but I haven't been able to apply anything to this situation.
This is what I have so far.
SELECT *
FROM ccParts, ccChild, ccFamily
WHERE parGC = '26' AND
parChild = chiId AND
chiFamily = famId
ORDER BY famName, chiName
What I need to do is see the total number of ccParts with the same ccFamily in the results. Then, sort by the total.
It looks like this is close to what you want:
SELECT f.famId, f.famName, pc.parCount
FROM (
SELECT c.chiFamily AS famId, count(*) AS parCount
FROM
ccParts p
JOIN ccChild c ON p.parChild = c.chiId
WHERE p.parGC ='26'
GROUP BY c.chiFamily
) pc
JOIN ccFamily f ON f.famId = pc.famId
ORDER BY pc.parCount
The inline view (between the parentheses) is the headliner: it does your grouping and counting. Note that you do not need to join table ccFamily there to group by family, as table ccChild already carries the family information. If you don't need the family name (i.e. if its ID were sufficient), then you can stick with the inline view alone, and there ORDER BY count(*). The outer query just associates family name with the results.
Additionally, MySQL provides a non-standard mechanism by which you could combine the outer query with the inline view, but in this case it doesn't help much with either clarity or concision. The query I provided should be accepted by any SQL implementation, and it's to your advantage to learn such syntax and approaches first.
In the SELECT, add something like count(ccParts) as count then ORDER BY count instead? Not sure about the structure of your tables so you might need to improvise.

How to reorder row in SELECT query in MYSQL?

A categories table which contains id, category_name,category_type etc.
I am getting all the records with SELECT query.
suppose I have a category name Shared article and I want to get it first position in my records.
of course I am using simple query
SELECT `category_name`,`category_type`,`categories`.`id`,
count(`user_bookmarks`.`id`) as counter FROM `categories`
LEFT JOIN `user_bookmarks`
ON `categories`.`id`=`user_bookmarks`.`category_id`
WHERE `categories`.`user_id`=39 GROUP By `categories`.`category_name`
but I don't know how achieve my task?
Is it possible with MySQL?
Yes you can do as
order by
`categories`.`category_name` = 'Shared article' desc

Efficient SQL query to find overlap between lists

Let's say I have a MySQL table order_items (idorder, iditem, amount) that contains items that people ordered from a web shop. I want to find orders similar to an order X by finding other orders with similar items in similar amounts.
Here is my current approach:
SELECT SQL_CALC_FOUND_ROWS
SUM(GREATEST(1, LEAST(cown.amount, cother.amount))) hits,
cother.`idorder`
FROM order_items cown
LEFT JOIN order_items cother ON (
cother.`idorder` != 1
AND cown.iditem = cother.iditem
)
WHERE cown.`idorder` = 1 AND cother.idorder IS NOT NULL
GROUP BY cother.idorder ASC
ORDER BY hits DESC
This selects all items from a given order and left joins them with items from other orders. Then I group by the other order ID and sum up the amount of overlap between them.
Is there a more efficient way to do this?
It looks like you need a recommendation engine. That would be tricky to implement in plain sql and not sure how reliable. For starters have a look into the Apache Mahout project.
There is a nice example with Mahout and MySQL which you can try for yourself available on github: https://github.com/jasebell/RecommenderDemo and it looks like the thing you want.