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
Related
I have a table,
Name Seconds Status_measure
a 0 10
a 10 13
a 20 -1
a 30 15
a 40 20
a 50 12
a 60 -1
Here I want for a particular name a new column which is calculated by, "The number of times the value goes >-1 only after once the -1 is met" . So in this particular data I want a new column for the name "a" which has the value=3 , because once the -1 is reached in Status_measure, we have 3 values (15 and 20 and 12)>-1
Required data frame:
Id Name Seconds Status_measure Value
1 a 0 10 3
2 a 10 13 3
3 a 20 -1 3
4 a 30 15 3
5 a 40 20 3
6 a 50 12 3
7 a 60 -1 3
I tried doing
count(status_measure>-1) over (partition by name order by seconds)
But this is not giving any desired result
You can do it in 2 steps, group data, count entries of the grp = 1.
select *, sum(Status_measure > -1 and grp = 1) over(partition by name) n
from (
select *
, row_number() over(partition by name order by Seconds) - sum(Status_measure > -1 ) over(partition by name order by Seconds) grp
from tbl
) t
An option is using a variable update, which:
starts from 0
increases its value when reaches a -1
decreases its value when reaches a second -1
Once you have this column, you can run a sum over your values.
SET #change = 0;
SELECT *, SUM(CASE WHEN Status_measure = -1
THEN IF(#change=0, #change := #change + 1, #change := #change - 1)
ELSE #change END) OVER() -1 AS Value_
FROM tab
Check the demo here.
Limitations: this solution assumes you have only one range of interesting values between -1s.
Note: there's a -1 decrement from your sum because the first update of the variable will leave 1 in the same row of -1, which you don't want. For better understanding, comment out the application of SUM() OVER and see intermediate output.
More of a clarification to your question first. I want to expand your original data to include another row for the sake of 2 vs 3 entries. Also, is there some auto-increment ID in your data that the sequential consideration is applicable such as
Id Name Seconds Status_measure Value
1 a 0 10 3
2 a 10 13 3
3 a 20 -1 3
4 a 30 15 3
5 a 40 20 3
6 a 50 12 3
7 a 60 -1 3
If sequential, and you have IDs 1 & 2 above the -1 at ID #3. This would indicate two entries. But then for IDs 4-6 above -1 have a count of three entries before ID #7.
So, what "VALUE" do you want to have in your result. The max count of 3 for all rows, or would it be a value of 2 for ID#s 1, 2 and 3? And value of 3 for Ids 4-7? Or, do you want ALL entries to recognize the greatest count before -1 measure to show 3 for all entries.
Please EDIT your question, you can copy/paste this in your original question if need be and provide additional clarification as requested (auto-increment as well as that is an impact of final output / determining break).
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
I'm trying to normalize some data, and can't seem to come up with a solution. What I have is a table like this:
weight position1 position2 position3
1 10 20 30
2 25 35 45
3 17 05 22
and one like this:
location position
6 1
7 1
8 2
9 2
10 2
11 3
12 3
How do I normalize the above so that given a location and a weight, I can find the value for a given position?
I can use Perl, Python, Excel, MySQL or pretty much any tool on the block to do the actual reshuffling of the data; where I'm having a problem is in coming up with a reasonable schema.
The desired outcome here is something like
if location == 11 -> position is 3
therefore,
if weight == 2 -> the value is 45
The only thing to do is "unpivot" your first table to this:
weight position value
1 1 10
1 2 20
1 3 30
2 1 25
2 2 35
2 3 45
3 1 17
3 2 05
3 3 22
The first two columns should contain unique pairs of values. If you have other information that only depends on weight, you would need another table for that. Same for positions.
Converting to the new model
If you already have the tables, then you can create the first table (t1) with this statement:
create table t1_new
select weight, 1 as position, position1 as value
from t1
union all
select weight, 2 as position, position2 as value
from t1
union all
select weight, 3 as position, position3 as value
from t1
Then, after verification of the result, drop t1, and rename t1_new to t1.
Querying from the new model
To query from these tables the value for a given location and weight, you should use a join:
select value
from t1
inner join t2 on t2.weight = t1.weight
where t2.location = 11
and t1.position = 3
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
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