I have a table 'new_table' in mysql, I want to store the result into two different tables based on a condition i.e if the percentage is above 70 then the result should be stored in dominant else should be stored in others
from below table the result based on condition(if percentage > 70) should store values 80 and 75 in dominant table and should store 20 , 40 , 60 in others table
kindly help.
thanks in advance.
sku_id new_total percentage
1 8 20
2 12 40
3 14 80
4 10 75
5 13 60
You can use create as select command like this:
CREATE TABLE dominant AS (
SELECT * FROM new_table
WHERE percentage > 70)
And for the second table same logic:
CREATE TABLE others AS (
SELECT * FROM new_table
WHERE percentage < 70)
Related
I am trying to create a new column that has stores the 'Average weight of the field'. For example, the answer for RaceID = 123 would be 54.5. The RaceID's are not organised from smaller to largest and are displayed randomly like the example below.
RaceID
Weight
No. Starters
123
56
2
124
58
2
123
53
2
125
60
2
125
51
2
124
62
2
Try below query, It will display current table data along with average column :
select t.*,
avg(Weight) over(partition by raceID order by raceID ) avg_raceID
from table t;
SELECT RaceID, AVG(Weight) AS val_1
FROM dataset_name
GROUP BY RaceID;
By using above code we can get the average value of weights for every unique RaceID. Check the below image for better understanding.
https://i.stack.imgur.com/kMA68.png
Let me know if there are any modifications or error.
I have a table with data like this:
ID customer_id rewards_points (target)
1 23796 10 4
2 24196 20 7
3 24197 30 10
4 24198 40 14
5 24199 50 17
I want to divide all values under rewards_points column by 3, then round up to the nearest integer. What query can I run to accomplish this?
Thank you!
To get the value
SELECT CEILING(theField/3.0) FROM theTable WHERE ...
The set the value
UPDATE theTable SET theField = CEILING(theField/3.0) WHERE ....;
try this:
select rewards_points DIV 3 as result from table;
I need to store a set of integers in MYSQL. The problem is as : I have an id(integer). Each id is mapped to a set of numbers. The numbers in the set can have values from 1 - 160. So my table should be of structure {id,set}. Also each id'set can have different length (max 40 numbers).
Eg:
id set
1 {2 45 46 67 89 155}
2 {1, 11 12 34 56 67 68 79 80 134 145}
I have tried the SET datatype in MYSQL. I defined it as
CREATE TABLE mytable ( id NUMBER PRIMARY KEY , mycol SET('1','2','3','4'...))
But the problem is the set allows to define only 64 elemnets.
Note: I need options other than creating a mapping table as :
id setno
1 2
1 45
....... and so on
Can anyone suggest another way to store a set of numbers in MYSQL.
Create child table to hold the numbers, one per row:
create table set_number (
set_id int,
num int
);
Then insert your numbers. To get them as one CSV value, use group_concat(), something like:
select t.id, group_concat(s.num) set
from mytable t
join set_nimber s
on t.set_id = s.set_id
group by t.id
The table structure you described is the right way to do.
And its all in the way you present data, create a table where it maps ID into Sento
And ID is a primary key
So that you will have:
ID Sento
1 2
1 45
1 46
...
1 155
And then have an SQL that says:
SELECT ID, GROUP_CONCAT(Sento, SEPARATOR ', ')
FROM Table
GROUP BY ID
Hope this helps
I have a problem that I cannot solve. I work on Microsoft SQL Server 2008 and I have a table with four columns
Id
Date (2013-07, 2013-08, 2011-03, etc)
Amount 1 (100, 150, etc.)
Amount 2 (100, 80, etc.)
If Amount 1 > 150 then I need to create new columns with the values in Date as column names and distribute Amount 2 into 6 (date) periods starting one month after the Date value.
It should look like this:
Id Date Amount 1 Amount 2
----------------------------------
1 2013-07 160 60
2 2013-10 180 80
Id Date Amount 1 2013-08 2013-09 2013-10 2013-11 2013-12 2014-01 ...
--------------------------------------------------------------------------------
1 2013-07 160 10 10 10 10 10 10
2 2013-10 180 20 20 20...
I don't know how to do this and any help is highly appreciated! Thank you!
The table itself should not have these additional columns because that would be a denormalized table structure. That's a poor way to store data in many cases. But you can easily do a query against your existing table that will return the additional columns in the form you want, so that you can display it this way. Check out PIVOT and UNPIVOT.
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.