MySQL Trouble with a query - mysql

I have a table that for example it contain 1000 records. The query that I'm trying to do, is for get some like this:
substring_part_name number_of_warehose number_of_parts
156 1 50
156 2 140
156 3 300
180 3 130
120 1 80
120 2 300
And so obtain the 1000 records.
The trouble is this, the part_name is something like this: x_156, b_156, d_156, h_120, f_120 and so on. Every part has its corresponding warehouse.
The first column i get it on this way: distinct(substring(part_name,3)) as substring_part_name, I only want the last part of the name, How i can obtain that result??
My query is this:
select distinct(substring(part_name, 3)) as substring_part_name, count(#the number of parts by ware_house), ware_house from ware_houses
group by substring_part_name;

Use a negative integer for the SUBSTRING (-3) to get the last three characters.
select
distinct(substring(part_name, -3)) as substring_part_name,
number_of_warehouse,
number_of_parts
from table
You can also use RIGHT:
distinct(right(part_name, 3)) as substring_part_name

Related

How to get the lowest price from a particular group of users MYSQL

I have been at this for a few days without much luck and I am looking for some guidance on how to get the lowest estimate from a particular group of sullpiers and then place it into another table.
I have 4 supplier estimate on every piece of work and all new estimates go into a single table, i am trying to find the lowest 'mid' price from the 4 newsest entries in the 'RECENT QUOTE TABLE' with a group id of '1' and then place that into the 'LOWEST QUOTE TABLE' as seen below.
RECENT QUOTE TABLE:
suppid group min mid high
1 1 200 400 600
2 1 300 500 700
3 1 100 300 500
[4] [1] 50 [150] 300
5 2 1000 3000 5000
6 2 3000 5000 8000
7 2 2000 4000 6000
8 2 1250 3125 5578
LOWEST QUOTE TABLE:
suppid group min mid high
4 1 50 150 300
Any help on how to structure this would be great as i have been loking for a few days and have not been able to find anything to get me moving again, im using MYSQL and the app is made in Python im open to all suggestions.
Thanks in advance.
If you really want to select only row with group 1, you can do something like
INSERT INTO lowest_quote_table
SELECT * FROM recent_quote_table
WHERE `group` = 1
ORDER BY `mid` ASC
LIMIT 1.
If you want a row with the lowest mid from every group, you can do something like
INSERT INTO lowest_quote_table
SELECT rq.* FROM recent_quote_table AS rq
JOIN (
SELECT `group`, MIN(`mid`) AS min_mid FROM recent_quote_table
GROUP BY `group`
) MQ ON rq.`group` = MQ.`group` AND rq.`mid` = MQ.min_mid

Get the Top Four Value from data table . Then swap on condition

using this command i can get TOP four values but some of the values are duplicate . so then wanted to swap based on serial number of table . using this bellow command to get the top four values now i want to swap any logic.
INPUT
SN Set name columname
1 100 Randy 25
2 100 many 22
3 100 sanny 22
4 100 nanny 35
Output
SN Set name columname
2 100 many 22
3 100 sanny 22
1 100 Randy 25
4 100 nanny 35
select top 4 * from filename where Set=100 order by columname DESC
Sort it based on column name then swap it based on serial number.
Wrap your query returning the wanted 4 rows up in a derived table, then do the opposite ORDER BY to get the wanted order:
select *
from
(
select top 4 * from filename where Set=100 order by columname DESC
) dt
order by columname ASC

mysql - get the average of the output average

I have 3 table. final,milestone and milestonewp consider that the three tables is foreigned key like milestonewp<--FK--milestone<--FK--Final .Then I have a column for determining the average of the milestonewp for a certain foreign key. Then getting that average to be average again to be displayed to the final table.Here is my visual representation
milestonewp
condition | mile_id
20 1
20 1
30 1
21 2
21 2
31 2
40 3
30 3
50 3
How can I average the average that the chart above will produce?
I'm trying to work on this
select avg(milewp_condition)
from logs_pms_r_milestone_wp
where mile_id=1;
but i dont have any idea how it can produce for the other mile_id
EDIT
The above code will produce something like this
avg(milewp_condition)
0
0
0
so then, i also want to average that 3 rows.
If I understand well this should be what you look for:
SELECT AVG(milewp_condition)
FROM logs_pms_r_milestone_wp
GROUP BY mile_id;
If you want to average all, just do:
SELECT AVG(milewp_condition)
FROM logs_pms_r_milestone_wp;
Regards

wildcard for number in MySQL Query?

I have following rows in my database table,
number:
1
2
3
4
100
101
102
103
104
200
201
202
I want to get output of all 100's like[100,101,102,103,104], but i have tried following codes,
SELECT * FROM table WHERE number RLIKE '[[:<:]]1';
it shows [1,100,101,102,103,104]
SELECT * FROM table WHERE number LIKE '1%';
it shows [1,100,101,102,103,104], i only want get number from 100 to 104, i don't want number 1.
to get all numbers between 100 and 199 you can do this:
SELECT * FROM table WHERE number BETWEEN 100 AND 199;
You have to use between clause like following which select range.
SELECT * FROM table WHERE number BETWEEN 100 AND 199;
OR
SELECT *
FROM test
WHERE number LIKE "1%"
AND length(number)=3;
SQL Fiddle

Perform sum, average, and multiplication across all columns in a table?

I have a table that looks like this:
id test1 test2 test3 test4 ... test200
1 90 87 85 86 70
2 100 95 83 92 80
.
.
18000
I know there are standard operations to perform sums and averages on a single column and multiply the value of two columns together but is it possible to do it across all columns in a row with a given id? If its not clear, I want to do something like this across rows instead of across columns. Thanks
You might be better off redesigning the table so that it does not have 200 columns.
e.g.
Id testnum score
1 1 90
1 2 87
...
2 1 100
2 2 95
...
180000
Now you can do a query like this:
select sum(score) as totalscore
from mynewtable
where id=1
How about:
select id, sum(test1 + test2 + ...) as summation
group by id
Is the problem that you have so many columns? This solution doesn't handle many columns smoothly.
There might be some contorted SQL to work on the contents of the whole row (I doubt that), but you still have to specify the column names, otherwise you'd have the ID numbers included in the calculation.