I have a table with 6 column that lists order lines.
the columns are Order, Orderline, ProductName, Description, UpdateDate, Location.
What I want to do is looking at values for first 4 columns (order, Line, ProductName, description).
If the rows are not identical in these four columns I want to return
Order, Line, Name, and Description and update dates.
If they are identical I want to
return just one of rows back (first or last).
Order LineNumber ProductName description UpdateDate Location
Order1 1 a1 b1 d1 n
Order1 1 a1 b1 d2 m
Order1 1 a3 b3 d5 L
Order2 1 a1 b1 d3 o
Order2 2 a2 b2 d4 m
I want the result to be:
Order LineNumber ProductName description UpdateDate Location
Order1 1 a1 b1 d1 n
Order1 1 a3 b3 d5 L
Order2 1 a1 b1 d3 o
Order2 2 a2 b2 d4 m
For Order1:
line 1 repeated 3 times.
2 times out of three ProductName a1, and description b1 are identical so one of these two will be returned.
1 time out of three productName a3 and description b3 is unique so this line will be returned as well.
For Order2:
all lines are identical unigue in Name and description so both lines will be returned.
Any help appriciated
You can use window function
SELECT *
FROM
(SELECT *,
ROW_NUMBER() OVER (PARTITION BY [Order], LineNumber, ProductName, [DESCRIPTION] ORDER BY UpdateDate) AS RowNum
FROM YourTable) DerivedTable
WHERE RowNum = 1
Related
I have a table like below:
product
country
group
value
p1
c1
g1
5
p1
c1
g2
6
p1
c2
g1
3
p1
c2
g2
22
p1
c3
g1
1
p1
c3
g2
6
I want to rank them considering the sum in value column for every product-country combination. So in this case, the updated table should be like this:
product
country
group
value
rank
p1
c1
g1
5
2
p1
c1
g2
6
2
p1
c2
g1
3
1
p1
c2
g2
22
1
p1
c3
g1
1
3
p1
c3
g2
6
3
p1-c1 combination will have the 2nd seed because 5+6 in the value column is higher than 7 (1+6) and lower than 25 (22+3). I used dense_rank() over (partition by product, country order by value) but it didn't work. How can I create the above ranking using mysql?
Thanks,
First use SUM() window function in a subquery to get the total value for each product, country combination and then DENSE_RANK() to rank the totals:
select product, country, `group`, value,
dense_rank() over (order by total desc) rnk
from (
select *, sum(value) over (partition by product, country) total
from tablename
) t
See the demo.
Given this DB table:
A_column B_column
---------------
A1 1
A1 2
A1 2
A2 1
A2 1
A2 1
A3 2
A3 3
A3 4
A3 5
How do I write SQL SELECT query to print out number of unique values in B_column per value in A_column, so output would be like this:
A1 2
A2 1
A3 4
I tried this, but doesn't seem to work properly:
SELECT A_column, count(B_column) FROM table GROUP BY A_column
Use distinct:
SELECT A_column, count(distinct B_column) FROM table GROUP BY A_column
I have a table with the below values
name symbol current value
a a1 1
a a2 2
a a3 4
a a4 3
a a5 5
b b1 6
b b2 7
b b3 8
c c1 1
c c2 2
c c3 3
c c4 3
c c5 5
d d1 6
d d2 6
Required : To find the average of the current value grouping by name , yet show all results . ie ; the result show be like below ;
name symbol current value Required
a a1 1 =current value/(sum of all 'a' current values)
a a2 2 =current value/(sum of all 'a' current values)
a a3 4 =current value/(sum of all 'a' current values)
a a4 3 =current value/(sum of all 'a' current values)
a a5 5 =current value/(sum of all 'a' current values)
b b1 6 = current value /(sum of all 'b' current values)
b b2 7 = current value /(sum of all 'b' current values)
b b3 8 = current value /(sum of all 'b' current values)
Similarly for all names
Join to a subquery which finds the averages:
SELECT t1.*,
CASE WHEN t2.avg > 0 THEN t1.current_value / t2.avg ELSE 0 END AS avg
FROM yourTable t1
INNER JOIN
(
SELECT name, SUM(current_value) AS avg
FROM yourTable
GROUP BY name
) t2
ON t1.name = t2.name;
The CASE expression is necessary to protect against a possible divide by zero, which could happen if a given name happen to have all zero current values. In that case, I default the average to zero.
try this logic :)
SELECT
*
FROM
table_listing_sample
WHERE
NAME = 'a'
AND NAME = 'b'
ORDER BY
id
Consider an Order Table:
OrderId Item Qty
O1 A1 5
O2 A2 1
O3 A3 3
Can this data be "exploded" into single-unit records like:
OrderId itemId Qty
O1 A1 1
O1 A1 1
O1 A1 1
O1 A1 1
O1 A1 1
O2 A2 1
O3 A3 1
O3 A3 1
O3 A3 1
The best solution here is to use GROUP_CONCAT and CONCAT_WS (concat with separator):
SELECT CONCAT('Order Id Item Qty ',
GROUP_CONCAT(
CONCAT_WS(' ',OrderId,Item,Qty)
SEPARATOR ' '
)
)
FROM Order;
Let me know if that works or if it needs some refinement!
I have a mysql query which returns more than one row for few ID's with different values. In such a case i need to eliminate the ID's with certain data.
Example
I need to elimate A1 for all those ID's which has more than one name and else if it has only one value A1 then i should be able to display it.
Result Should look like:
ID Name Value
1 A1 AA
1 B1 AB
2 C1 CC
3 A1 AA
4 A1 AA
4 E1 AD
4 B1 AB
Please a solution for this
This is probably very ugly, but at least gets the job done:
select id, name, value from (
select f.*, c.count
from <TABLE> f, (select *, count(*) as 'count' from <TABLE> group by id) c
where f.id = c.id
) c
where (name = 'A1' and count = 1) or name != 'A1'
group by id
you will get the 'first' row for remaining (not random) for id = 4