SELECT row COUNT and another row value MYSQL [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to get city and model + order by common model value.
Without city mysql query is preparing successfully but with city is not working.
How can i get two columns info in one query?
SELECT model
FROM cars
GROUP BY model
ORDER BY COUNT(model) DESC
LIMIT 6
not working: SELECT cars,model FROM cars GROUP BY model ORDER BY COUNT(model) DESC LIMIT 6

If you want the model/city combinations with the most rows, then include both in the select and group by:
SELECT model, city
FROM cars
GROUP BY model, city
ORDER BY COUNT(*) DESC
LIMIT 6

Related

make query in mysql to count member [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Write Mysql query with average age counting
Try the following
select
ReportsTo,
count(*) as Members,
avg(age) as Average_Age
from yourTable
where ReportsTo is not null
group by
ReportsTo
This is actually quite easy. This may work for you!
SELECT ReportsTo, COUNT(Members) as Members, AVG(Age) as average
FROM table
GROUP BY ReportsTo
ORDER BY ReportsTo

Display group data along with their last latest message [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to display result having data categorized in the group with their latest message according to date.
I tried grouping and having along with nested queries but no luck.
SELECT groupName,date FROM chat where groupName like '%he%' group by groupName,date having min(date);
I want the two rows to be printed along having the latest message with them.Here it would be row 4 and row 5. this is just an arbitrary data though which I need to impplement on bulk.
You don't need group by, just filtering:
select c.*
from chat c
where c.date = (select max(c2.date) from chat c2 where c2.groupname = c.groupname);
You can do it with NOT EXISTS:
SELECT c.*
FROM chat c
WHERE
groupName LIKE '%he%'
AND
NOT EXISTS (
SELECT 1 FROM chat
WHERE groupname = c.groupname AND date > c.date
)

Difference between the distinct entries and non distinct entries of a column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Find the difference between the total number of records in CITY column of table and the number of distinct CITY records in the table.
City_table:
Field
Type
ID
NUMBER
CITY
VARCHAR
STATE
VARCHAR
LAT_N
NUMBER
LONG_W
NUMBER
select count(*)
from (select CITY
FROM STATION NOT IN (SELECT DISTINCT CITY
FROM STATION))
It is much easier:
SELECT count(city), count(distinct city)
FROM station
You just need to count all the occurrencies of city (count(city)) and count the distinct values (count(distinct city))

Simplify SQL query for me please [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a table of multiple employment records of an employee/s where i want to query for a certain range of date.
Parameters are Company.id,date_hired,date_end
Results must be on the range of the specified date and employee.id must only be one if the employee has multiple employment records result must get the latest employment record..
THIS IS WHAT I CURRENTLY HAVE.
SELECT *
FROM employmentrecords
WHERE employmentrecords.id IN(
SELECT MAX(employmentrecords.id)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22')
GROUP
BY employmentrecords.employee_id)
AND employmentrecords.company_id<>0`
Hope any one would suggest a better approach.Thank you
I am not sure if I got your question all clear, this query below will give you the latest employee record if there are multiple user records -
SELECT * FROM employmentrecords WHERE id IN(SELECT MAX(id) FROM employmentrecords
WHERE ((date_end >='2017-08-22'
OR date_end IS NULL
OR (date_end <='2017-08-22' AND date_end >='2017-08-08'))
AND date_hired <='2017-08-22')
GROUP BY employee_id)
AND company_id<>0
and rownum = 1
order by date_hired desc
JFYI, no need to use the table name as alias if there is only one table you are fetching the data from, it just makes it hard to read the query on go.
SELECT id, max(date_hired)
FROM employmentrecords
WHERE ((employmentrecords.date_end >='2017-08-22'
OR employmentrecords.date_end IS NULL
OR (employmentrecords.date_end <='2017-08-22'
AND employmentrecords.date_end >='2017-08-08'))
AND employmentrecords.date_hired <='2017-08-22') group by id

How to calculating SUM of (quantity*price) from two different tables? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this two following tables:
Order Table
Product Table
I'm trying to calculate the subtotal price for each product (quantity*price) then SUM the TOTAL value for the entire order.
Thanks, Any help would be very appreciated.
Check if table names are correct and try this:
SELECT o.order_number, o.item_quantity, p.price,(o.item_quantity * p.price) AS subtotal, SUM( o.item_quantity * p.price) AS total
FROM order AS o
LEFT JOIN product AS p ON product_number = idproduct_detail;
With this I am connecting each order with the relative product then selecting item_quantity and price and SUM the product of them.