Home > Back-end >  how to get the sql result in below format
how to get the sql result in below format

Time:11-16

I have data in mysql DB in below format and I am trying to fetch the result in the format mentioned

I have Data in DB in below format:

**name, stockTimeInterval, date, stockTime, stockOpen**
WIPRO, 5_min_stock_data, 2021-01-01 , 09:15:00, 385.05
WIPRO, 5_min_stock_data, 2021-01-01 , 09:20:00, 386.60
WIPRO, 5_min_stock_data, 2021-01-01 , 09:25:00, 387.45

WIPRO, 5_min_stock_data, 2021-02-01 , 09:15:00, 385.05
WIPRO, 5_min_stock_data, 2021-02-01 , 09:20:00, 386.60
WIPRO, 5_min_stock_data, 2021-02-01 , 09:25:00, 387.45

WIPRO, 5_min_stock_data, 2021-03-01 , 09:15:00, 385.05
WIPRO, 5_min_stock_data, 2021-03-01 , 09:20:00, 386.60
WIPRO, 5_min_stock_data, 2021-03-01 , 09:25:00, 387.45

Question: How to get the result in the format of:

**name, stockTimeInterval, date, stockTime, stockOpen**
WIPRO, 5_min_stock_data, 2021-01-01 , 09:15:00, 385.05
WIPRO, 5_min_stock_data, 2021-02-01 , 09:15:00, 385.05
WIPRO, 5_min_stock_data, 2021-03-01 , 09:15:00, 385.05

WIPRO, 5_min_stock_data, 2021-01-01 , 09:20:00, 386.60
WIPRO, 5_min_stock_data, 2021-02-01 , 09:20:00, 386.60
WIPRO, 5_min_stock_data, 2021-03-01 , 09:20:00, 386.60

WIPRO, 5_min_stock_data, 2021-01-01 , 09:25:00, 387.45
WIPRO, 5_min_stock_data, 2021-02-01 , 09:25:00, 387.45
WIPRO, 5_min_stock_data, 2021-03-01 , 09:25:00, 387.45

CodePudding user response:

Use a two-tiered sort:

SELECT name, stockTimeInterval, date, stockTime, stockOpen
FROM yourTable
ORDER BY name, stockTimeInterval, stockTime, date;

Note that stockTime is coming before the date. More typically, date and time are in a single datetime field, rather than in separate fields.

CodePudding user response:

SELECT *
FROM table
ORDER BY stockOpen ASC, `date` ASC

?

  • Related