I have a table like attached image in SQL server.
I am trying to retrieve sorted data based on SUM(freight)
column. For this i have used the below query.
SELECT ShipCountry
FROM CountryDetails
GROUP BY ShipCountry
ORDER BY SUM(freight) ASC
When i run this i am getting result like below.
If i run the below query i am getting result like below. It's fine.
SELECT ShipCountry, ShipCity
FROM CountryDetails
GROUP BY ShipCountry, ShipCity
ORDER BY SUM(Freight), ShipCity ASC
Instead of this i need a result like below. In order by clause SUM(Freight)
should consider only ShipCountry
. It should not consider both ShipCountry
and ShipCity
. My Expected result is
How to achieve this result through SQL query?
You can try a query like below
select
ShipCountry,
ShipCity
from
(
Select
ShipCountry,
ShipCity,
SUM(Freight) OVER( Partition by ShipCountry order by ShipCountry) NetCountry,
SUM(Freight) OVER( Partition by ShipCountry,ShipCity order by ShipCountry) NetCity
from
CountryDetails
) T
Order by NetCountry asc,ShipCity asc
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加