0

Say there is a table with columns of such:

date1, date2, date3, val1, val2, val3
2017-02-28, 2017-01-01, 2017-01-02, 100, 200, 300
2016-05-09, 2016-09-09, 2016-05-04, 2, 10, 20

I would like to get the following as result,

2017-01-01, 200
2016-05-04, 20

How to write MS SQL query?

Thanks!

  • take a look at this question https://stackoverflow.com/questions/71022/sql-max-of-multiple-columns – David Lee Jun 20 '17 at 17:22
  • thanks a lot for the link. I am just wondering, what if there are more than three such date values need to be compared, 10, or 50, for example, then the query would become very messy. What is a better way to handle such requirement? Shall I write a program just to create such result (a new table) make it maintainable? – superuserDoHaveStupidQ Jun 21 '17 at 23:39
  • With that number of columns I would question whether or not the table schema is properly setup. That number of columns would leave me to believe it should be just one column and the data would be found as new records. – David Lee Jun 22 '17 at 02:27
  • I was a bit exaggerating. In fact, I am having four date values in four columns. What would you suggest in this case? Thanks in advance! – superuserDoHaveStupidQ Jun 23 '17 at 12:20
  • I would recommend any of the solutions in the link in first comment. I do not believe SQL has aggregate functions across fields so you will have to reference each field individually. – David Lee Jun 23 '17 at 16:43

0 Answers0