Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dataset in which i want the minimum value on the top from each column in SQL, but it got sorted rows based on the sorted order.
FOR EXAMPLE:

What I have tried:

SELECT ExtractProfile
         , CountryCode
--       , TEST.STARTTS AS STARTTS
         , SUM(CASE WHEN TEST.STARTTS = CONVERT(DATE,GETDATE()) THEN TEST.StoreSuccess  ELSE 0 END) Sucesses0
         , SUM(CASE WHEN TEST.STARTTS = CONVERT(DATE,GETDATE()) THEN 1 ELSE 0 END) Stores0
         , SUM(CASE WHEN TEST.STARTTS = Dateadd(dd,-1,CONVERT(DATE,GETDATE())) THEN TEST.StoreSuccess  ELSE 0 END) Sucesses1
         , SUM(CASE WHEN TEST.STARTTS = Dateadd(dd,-1,CONVERT(DATE,GETDATE())) THEN 1                                ELSE 0 END) Stores1
         , SUM(CASE WHEN TEST.STARTTS = Dateadd(dd,-2,CONVERT(DATE,GETDATE())) THEN TEST.StoreSuccess  ELSE 0 END) Sucesses2
         , SUM(CASE WHEN TEST.STARTTS = Dateadd(dd,-2,CONVERT(DATE,GETDATE())) THEN 1                                ELSE 0 END) Stores2
        
--INTO #TEMP
FROM   (SELECT       d.ExtractProfile
                       , s.StoreNbr
                       , s.CountryCode
                       , CONVERT(DATE,d.StartTS) AS STARTTS
                       --, SUM(CASE WHEN s.Result  = 'Success' THEN 1 ELSE 0 END) Successes
                       --, SUM(CASE WHEN s.Result != 'Success' THEN 1 ELSE 0 END) Failures
                       --, COUNT(DISTINCT StoreNbr) Stores
                       , MAX(CASE WHEN s.Result  = 'Success' THEN 1 ELSE 0 END) AS StoreSuccess
              FROM  TEST s (NOLOCK)
              INNER JOIN TEST1 d (NOLOCK)
                           ON s.ExtractId = d.Id
              WHERE d.StartTS > CONVERT(VARCHAR(11),GETDATE() - 25)
       --and s.storenbr=1000
       --and d.rundate = dateadd(dd,-1,convert(varchar(11),getdate()))
              GROUP BY d.ExtractProfile
                       , s.StoreNbr
                       , s.CountryCode
                       , CONVERT(DATE,d.StartTS)
              ) AS TEST2
GROUP BY ExtractProfile
         , CountryCode
ORDER BY Sucesses0 ASC ,Sucesses1 ASC,Sucesses2 ASC,Sucesses3 ASC
Posted
Updated 11-Jul-18 2:16am

1 solution

You can't do that: SQL sorts rows by criteria, it doesn't sort data by columns, because that doesn't leave any unsorted data in place with it's associated data.

It's an odd request, and not one that SQL can automatically fulfil - you will be a lot better off doing this in your presentation language, if you really must do it. While it is possible in SQL (by SELECTign each column into a separate result, ordered and with a ROWNUMBER; then JOINING the various tables together by ROWNUMBER) it's messy, and probably rather confusing to users.

For example, if you have a table with two columns ID and Deimension, you can get both sorted individually like this:
SQL
SELECT a.ID, b.Dimension 
FROM (SELECT ROW_NUMBER() OVER(ORDER BY ID) AS No, Id FROM TableA) a
JOIN (SELECT ROW_NUMBER() OVER(ORDER BY Dimension) AS no, Dimension FROM TableA) b 
     ON a.No = b.No
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900