Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get the sql result in below format

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

**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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading