how can i extract the data from excell sheet to mysql in node.js? - mysql

im working on export the import export data from excell sheet to sequlize mysql. im facing the issue during extract the data from one column of excell sheet.
im getting this data
let str = [
'CABBAGE - Approx 800g - 1.3Kg * 1 ₹44.55\r\n' +
"LADY'SFINGER/BHENDI - 1Kg * 1 ₹61.20\r\n" +
'BRINJAL LONG/ BAIGAN - 500g * 1 ₹23.24\r\n' +
'LEMON - 6 pieces * 1 ₹24.00\r\n' +
'KIRA / CUCUMBER GREEN - 1Kg * 1 ₹35.00\r\n' +
'CARROT - 500g * 1 ₹41.65\r\n' +
'Amrutanjan Strong Pain Balm Double Power - 50ml * 1 ₹118.75\r\n' +
'Pediasure 7',
' Specialized Nutrition Drink Powder for Growing Children- Chocolate Flavour - 200g * 1 ₹223.10'
]
let str1 = JSON.parse(str)
console.log( str1)
how can i upload thes all details into mysql defrent defrent column ??

Related

Converting Degrees/Minutes/Seconds to Decimals using MySQL

I'm using MySQL version: 5.7.22
I've got two columns Latitude and Longitude both in Degrees/Minutes/Seconds format that I want to convert to Decimal format ex: 48° 52.250' N to 48.93611111
I have the following script but I'm stuck at how to split the degrees minutes and seconds. I cannot hard-code the values as I've done here left(Latitude,2) since the degrees might have 3 decimals as well
SELECT Latitude,
left(Latitude, 2) +
(TRUNCATE((Latitude - TRUNCATE(Latitude)) * 100) / 60) +
(((Latitude * 100) - TRUNCATE(Latitude * 100)) * 100) / (60 * 60) AS DECIMAL_DEGREES
FROM small_ocean_data
The formula for the conversion is this: D + M/60 + S/3600 * -1 if direction in ['W', 'S'] else 1
Any help would be grateful!
Well if I use this formula of yours: D + M/60 + S/3600
Where I believe D is Degrees and M are Minutes and S are Seconds.
With this select:
select SUBSTRING_INDEX('48° 52.250', '°',1) +
(SUBSTRING_INDEX('48° 52.250',' ',1)/60) +
(SUBSTRING_INDEX('48° 52.250','.',1)/3600);
Or for your database:
select SUBSTRING_INDEX(Latitude, '°', 1) +
(SUBSTRING_INDEX(Latitude ,' ',1)/60) +
(SUBSTRING_INDEX(Latitude ,'.',1)/3600) "DECIMAL_DEGREES"
FROM small_ocean_data;
I get 48.81333333333333
48 + 0.8 + 0.013333333333333334
Here is the DEMO

getting Sum Floor and Group by to work

Ok all i have a question about sum and group by what i have currently is a error and i don't know how to fix basically the error is in regards to this line of code
SELECT PlayerName
,floor(SUM(PlayerScore / 500)) + floor(SUM(PPlayerScore / 1000)) + floor(SUM(S4 / 5)) * 2 + floor(Sum(P7 / 3)) * 2 + SUM(prereg) + SUM(st) + SUM(hb) AS BB
,floor(SUM(PlayerScore / 500)) + floor(SUM(PPlayerScore / 1000)) + floor(SUM(S4 / 5)) * 2 + floor(Sum(P7 / 3)) * 2 + SUM(PlayerBallots) + SUM(prereg) + SUM(st) + SUM(hb) AS TB
,SUM(PlayerBallots) AS PB
FROM player
the same issue is throughout the line and it is this
from this floor sum command
if 4 players have scores of 1100 1300 1800 and 1000 the sum of that will show 5 when the reality is it should only show 4 i tried adding in a group by operator it will only show the total for 1 player
is it possible to do what I'm trying or not
thank you in advance
To help a little i will shorten down the code and explain what i need it to do with an example
floor(SUM(PPlayerScore /1000))
so for each 1000 points a player scores he gets 1 ballot
so if
jane has 1100 gets 1 ballot floor 1.1
joe gets 1300 gets 1 ballot floor of 1.3
jerry gets 1800 1 ballot floor of 1.8
jane gets 1000 gets 1 ballot floor of 1
the issue is with current code i get 5 when in reality there is only 4 ballots

Grouping csv data by reading all lines

I am having the following content where I need to group by the quarter and display the information from quarter starting to end
A|Company1|2008-10-01 - 2008-12-31
A|Company1|2009-01-01 - 2009-03-31
A|Company1|2009-04-01 - 2009-06-30
A|Company1|2009-07-01 - 2009-09-30
A|Company1|2010-01-01 - 2010-03-31
A|Company2|2010-01-01 - 2010-03-31
A|Company1|2010-04-01 - 2010-06-30
A|Company2|2010-04-01 - 2010-06-30
A|Company1|2010-07-01 - 2010-09-30
A|Company2|2010-07-01 - 2010-09-30
A|Company1|2010-10-01 - 2010-12-31
A|Company2|2010-10-01 - 2010-12-31
What I need is
A|Company1|2008-10-01 - 2008-12-31
A|Company1|2009-01-01 - 2009-09-30
A|Company1|2010-01-01 - 2010-12-31
A|Company2|2010-01-01 - 2010-12-31
I need to display all the information between the quarters by separating the data. If multiple companies found I need to display in two rows else in 1 row all the data should be displayed. I tried some code to filter in to two list but not getting how to proceed to get the exact output
using (var reader = new StreamReader(#"D:\requirement.csv"))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.LastIndexOf('|');
string beforeLastIndex = line.Substring(0, values);
listA.Add(beforeLastIndex);
listB.Add(line.Substring(values + 1));
}
}

MySQL convert height format to centimeters

I want to convert feet and inches to centimeters format
Format in my DB is:
4'6" (4 feet, 6 inches)
Formula for converting into centimeters
4*30.48 = 121.92 (convert feet to centimeters = multiply by 30.48)
6*2.54 = 15.24 (convert inches to centimeters = multiply by 2.54)
So Result = 121.92 + 15.24 = 137.16 cm
eg:
Actual Table: inches
SELECT * FROM inches
id height
1 4'6"
2 4'7"
3 5'8"
4 5'9"
I expect the following result as centimeters when I do SQL query
id height
1 137.16
2 139.7
3 172.72
4 175.26
Thanks in advance :)
Probably far easier to do on the application level, but if you really had to, you could do it in SQL like this, using the SUBSTR and INSTR functions, and some basic math:
SET #height = '4''6"';
SELECT
SUBSTR(#height, 1, INSTR(#height, '''') - 1) * 12 * 2.54 +
SUBSTR(#height, INSTR(#height, '''') + 1, INSTR(#height, '"') - INSTR(#height, '''') - 1) * 2.54;
-- yields 137.16
Or, applied to your table structure:
SELECT id,
SUBSTR(height, 1, INSTR(height, '''') - 1) * 12 * 2.54 +
SUBSTR(height, INSTR(height, '''') + 1, INSTR(height, '"') - INSTR(height, '''') - 1) * 2.54 AS height
FROM inches;
Application side process will be better,
However,
SELECT
(CAST(SUBSTR(height,1, LOCATE("'",height)-1) AS UNSIGNED) * 30.48) +
(CAST(SUBSTR(height, LOCATE("'",height)+1) AS UNSIGNED) * 2.54 ) AS cm
FROM
inches;

MySQL weighted average in a single query

I have a MySQL table which looks like this:
id load_transit load_standby hours_transit hours_standby
1 40 20 8 4
2 30 15 10 10
3 50 10 3 9
I need to do the following calculations:
(intermediate calculations)
hours_transit_total = 8+10+3 = 21
hours_standby_total = 4+10+9 = 23
(desired result)
load_transit_weighted_mean = 40*(8/21) + 30*(10/21) + 50*(3/21) = 36.667
load_standby_weighted_mean = 20*(4/23) + 15*(10/23) + 10*(9/23) = 13.913
Is it possible to do this in a single query? What would the best design be?
Note that
40*(8/21) + 30*(10/21) + 50*(3/21) =
(40*8)/21 + (30*10)/21 + (50*3)/21 =
(40*8 + 30*10 + 50*3)/21
and
20*(4/23) + 15*(10/23) + 10*(9/23) =
(20*4)/23 + (15*10)/23 + (10*9)/23 =
(20*4 + 15*10 + 10*9)/23
Which allows you to get the results you want using
SELECT sum(hours_transit * load_transit) / sum(hours_transit),
sum(hours_standby * load_standby) / sum(hours_standby)
FROM your_table
I just had this same question and built this little query I think makes it clear how to find the weighted average in a single query:
select sum(balance), sum(rate * balance / 5200) as weighted_rate, -- what I want
-- what you cannot do: sum(rate * balance / sum(balance))
sum(balance * rate) / sum(balance) as weighted_rate_legit -- ah thank you transitive math properties
from (
select '4600' as balance, '2.05' as rate from dual
union all
select '600' as balance, '2.30' as rate from dual
) an_alias;