Sort and Group by Time - mysql

I have the following table:
Line Date Time Order Qty
3 1140531 24737 S215037 1
3 1140531 24737 S215037 1
2 1140531 14955 S215058 1
2 1140531 14955 S215058 1
2 1140531 24055 S215059 1
2 1140531 24055 S215060 1
3 1140530 25319 S215099 1
3 1140530 25319 S215099 1
I need to be able to display the Line, work order, sum of qty (I need to show the order that was completed first on desc based on date and time). My end result should look like this:
Line Order Sum of Qty
2 S215058 2
2 S215059 1
2 S215060 1
3 S215099 2
3 S215037 2
Attempt:
I have a query that updates the data into a table where it's ordered by Line, Date, Time
then I apply the following query:
SELECT Line, Order, Sum(Qty)
From MyTable
Group by Line, Order
When I group the items, it groups my Orders based on alphabetical order and not the time. Please help!

I feel some contradiction between your description and expected result as you mention the word desc. Anyway the following query will give you the end result.
SELECT `Line`, `Order`, Sum(Qty)
From MyTable
Group by `Line`, `Order`
order by `Line`, `Date`, `Time`, `Order`;
SQL Fiddle here: http://www.sqlfiddle.com/#!2/456bf/8

Related

How to show all MIN of SUM(column) that has the same min value?

I've been trying to get MIN of SUM of a column by doing subqueries, however in a case where I have two same SUM values, my query only returns me one of them. Is there a way to get all of them to be shown?
So for example this table is called quantity,
date product_id quantity_start quantity_end
1/1/2020 1 10 5
1/1/2020 2 10 5
1/1/2020 3 12 1
2/2/2020 1 10 5
2/2/2020 2 11 6
2/2/2020 3 14 1
my query would be
SELECT product_id, MIN(Total) as Minimum
FROM (SELECT product_id, SUM(quantity_start - quantity_end) as Total
FROM quantity
GROUP BY product_id)T
But this will return me only one min value while there are two since both product 1 and 2 will have total of 10 from my subquery. Is there a way to write it such that it shows me both ?
Thanks!
You are correctly grouping by your sub query, but the outer query also needs a group by so:
SELECT product_id, MIN(Total) as Minimum
FROM (SELECT product_id, SUM(quantity_start - quantity_end) as Total
FROM quantity
GROUP BY product_id)T
GROUP BY product_id;
See it working here: http://sqlfiddle.com/#!9/97d7724/1

SQl:how to write a query for least times id is presnt in the table

I have a table called "UserPlay" which as values like this
th_id route_id
1 1
1 2
1 2
1 3
1 3
I just want least time rout_id is used
I have to get output as this
th_id route
1 1
If I understand correctly, you want the route_id with the lowest count:
select route_id, count(*)
from UserPlay u
group by route_id
order by count(*) asc
limit 1;
You can get the list of the_id on it by including group_concat(the_id).

Suggest the query to fetch the required result in one query with performance

I am able to fetch the results using the temp tables or looping over this table but I want to translate the following table into the required result in one query with great performance.
Order Table
OrdNbr LineNbr ItemName Qty
1 1 Pen 1
1 2 Pencil 2
1 3 Scale 2
2 5 Bottle 2
3 3 Pen 10
3 1 Pencil 5
Required Result:
OrdNbr OrdNbrFirstLineNbr ItemName Qty AllLineNumbers
1 1-1 Pen 1 1,2,3
2 2-5 Bottle 2 5
3 3-1 Pencil 5 1,3
OrdNbr and LineNbr are primary key for order table. I want to fetch only first record for the same OrdNbr.
Logic to get the result:
Find the distinct order number and get the lowest line number for the individual order number. Now display the order number, lowest line number and details for that lowest line number in that order. I want two extra derived fields OrdNbrFirstLineNbr and AllLineNumbers.
select OrdNbr,
LineNbr as OrdNbrFirstLineNbr,
ItemName,
Qty
from (
select *,
row_number() over (partition by OrdNbr order by LineNbr) as rn
from "Order" -- need to quote this, because order is a reserved word
) t
where rn = 1
Try this:
SELECT OrdNbr, LineNbr, ItemName, Qty
FROM Order
GROUP BY OrdNbr
ORDER BY LineNbr;

Get the last 2 rows of a table while grouping one of the column. MySQL

Consider Facebook. Facebook displays the latest 2 comments of any status. I want to do something similar.
I have a table with e.g. status_id, comment_id, comment and timestamp.
Now I want to fetch the latest 2 comments for each status_id.
Currently I am first doing a GROUP_CONCAT of all columns, group by status_id and then taking the SUBSTRING_INDEX with -2.
This fetches the latest 2 comments, however the GROUP_CONCAT of all the records for a status_id is an overhead.
SELECT SUBSTRING_INDEX(GROUP_CONCAT('~', comment_id,
'~', comment,
'~', timestamp)
SEPARATOR '|~|'),
'|~|', -2)
FROM commenttable
GROUP BY status_id;
Can you help me with better approach?
My table looks like this -
status_id comment_id comment timestamp
1 1 xyz1 3 hour
1 2 xyz2 2 hour
1 3 xyz3 1 hour
2 4 xyz4 2 hour
2 6 xyz6 1 hour
3 5 xyz5 1 hour
So I want the output as -
1 2 xyz2 2 hour
1 3 xyz3 1 hour
2 4 xyz4 2 hour
2 6 xyz6 1 hour
3 5 xyz5 1 hour
Here is a great answer I came across here:
select status_id, comment_id, comment, timestamp
from commenttable
where (
select count(*) from commenttable as f
where f.status_id = commenttable.status_id
and f.timestamp < commenttable.timestamp
) <= 2;
This is not very efficient (O(n^2)) but it's a lot more efficient than concatenating strings and using substrings to isolate your desired result. Some would say that reverting to string operations instead of native database indexing robs you of the benefits of using a database in the first place.
After some struggle I found this solution -
The following gives me the row_id -
SELECT a.status_id,
a.comments_id,
COUNT(*) AS row_num
FROM comments a
JOIN comments b
ON a.status_id = b.status_id AND a.comments_id >= b.comments_id
GROUP BY a.status_id , a.comments_id
ORDER BY row_num DESC
The gives me the total rows -
SELECT com.status_id, COUNT(*) total
FROM comments com
GROUP BY com.status_id
In the where clause of the main select -
row_num = total OR row_num = total - 1
This gives the latest 2 rows. You can modify the where clause to fetch more than 2 latest rows.

MySql Result Set Combine Columns

I have this result set from my query:
OrderId CustomerId ProducerId CustomerPayment ProducerPayment
1 1 3 10 5
1 1 4 10 5
1 2 3 10 5
1 2 4 10 5
I need to return this result into this:
OrderId UserId Payment
1 1 20
1 2 20
1 3 10
1 4 10
Just combining the CustomerId and ProducerId into UserId. Same with the Payment Columns.
Is there any way to achieve this with using just a simple select and group by? I'm avoiding temp tables, calling multiple same queries and like for optimization. I hope this is possible.
Thanks a lot
SELECT
OrderId,
CustomerID AS UserId,
SUM (CustomerPayment) As Payment
FROM orders
UNION ALL
SELECT
OrderId,
ProducerId AS UserId,
SUM (ProducerPayment) As Payment
FROM orders
Try something like this:
select
OrderId,
CustomerId,
sum(CustomerPayment) Payment,
group_concat(OrderId separator ',') listOrders /* list all OrderID's from the user and separates these with a , */
from your_table
group by CustomerId
Dont know how you query looks like atm?