I have the table below
relID value charge
1 2 5
1 8 2
2 1 10
2 4 6
2 9 2
For the above table i need for a given value ex 10 to find what to charge for each relID
In the above for value<10 i need to get charge=5 for relID=1 and charge=2 for relID=2
I am trying to use 1 sql command to get it and i am kind of lost
Can anyony help
Thanks
Your question isn't very clear but I think this will work for you
SELECT t.relID,
(
SELECT charge
FROM table
WHERE relID = t.relID
AND value < 10
ORDER BY value
LIMIT 1
) AS charge
FROM table AS t
Let me rephrase.
Here is the table
relID value charge
1 2 5
1 8 2
2 1 10
2 4 6
2 9 2
Explain the table :
Lets say that the value and charge are money.
If the user has value 2 then i must charge with 5 using relID 1
If the user has value 8 then i must charge with 2 using relID 1
same for the relID 2
So when a user come with a value 10 i must find what to charge.So for the given value 10 i must find in the table all records with value<10.
In the example the values for value <10 are
For relID=1 are (2,8)
For relID=2 are (1,4,9)
Now for each relID i need to get the max value.
For relID=1 max value is 8 so charge is 2
For relID=2 max value is 9 so charge is 2
I plain english there are value rate
0-2 charge 5
2-8 charge 2
and ...
i hope to be clear now
select *
from Table t
where value =
(select max(value)
from Table
where value <= 10
and relId = t.relId)
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'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'am trying to get total full time and total half time by user, Timing is stored in single column, Integer value of timing should come in full time as sum(timing) and floating value in half time but in count
id user_id timing
1 2 1
2 2 2.5
3 1 1.5
4 1 1
5 3 3
6 2 2.5
I need the result as
user_id fulltime halftime
1 2 1
2 5 2
3 3 0
SELECT user_id
, SUM(FLOOR(timing)) AS fulltime
, SUM((timing % 1) * 2) AS halftime
FROM table
GROUP BY user_id;
This query should help you. please try it on your data
SELECT user_id,
sum(if(ceil(timing)>timing,0,timing)) as fulltime,
sum(if(ceil(timing)>timing,timing,0)) as halftime
FROM rest
GROUP BY user_id
Thanks
Amit
In mysql, I need a query that returns the quantity of repeated values in the field "Info" of my table "Log".
Table Log:
ID_Log User Info
1 1 3
2 1 3
3 1 3
4 1 5
5 1 6
6 1 6
7 1 7
8 1 8
9 1 8
The query should return "4" (Info 3 appears three times, Info 6 appears two times, Info 8 appears two times).
Any suggestions?
You can get the number of values that have already appeared by using a simple subtraction. Subtract the number of distinct values from the total number of rows:
select count(*) - count(distinct info)
from log;
The difference is the number that "repeat".
This should work. Group the values of info together and only keep the results where the number of occurrences minus 1 is greater than 0. Then sum the numbers of occurrences.
select sum(repeats)
from (SELECT Info, count(*) - 1 AS repeats
FROM Log
GROUP BY Info
HAVING repeats > 0)
I have a column in table A. the column name is Sequence number. The Structure of table A is numbers from 1,2,3,4.....3600.
Now on the basis of table A. I want the below output from the SQL select query for SQL server 2008.
seq no dynamic col
1 1
2 1
3 1
4 1
5 1
6 2
7 2
8 2
9 2
10 2
11 2
12 3
13 3
My Second column is getting generated at the run time.
And the business logic is that, if the seq number mod 6 = 0 then increment the value of dynamic column.
Thanks in advance
Try this:
select seqno, (seqno/6) +1 dynamiccol
from t
Fiddle Demo
Take this as pseudo code because I'm not familiar with SQL Server specifically, but it should give you somewhere to go.
SELECT
seq_no,
ROUNDDOWN(seq_no/6)+1 AS dynamic_col
FROM
my_table