Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:08:37+00:00 2026-06-01T16:08:37+00:00

In my journey to learn SQL, I’m writing various queries on an old database

  • 0

In my journey to learn SQL, I’m writing various queries on an old database of mine, but getting into more complex things, I want to make sure I’m not over engineering this. I have a table Agent, with different agents offering different prices for cities. Multiple agents can serve the same city, each with different prices. I wanted to run a query which would return the total cost of hiring all of the agents for any given city, ordered by the most expensive.

WITH orderedPrices AS (
    SELECT SUM(agtFMPrice) 
    OVER (PARTITION BY agtCity) 
    AS IX FROM Agent)
SELECT IX 
FROM orderedPrices 
ORDER BY IX DESC

I found that doing it without the view returned by orderedPrices, it wouldn’t order the prices (I assume because it’s an aggregate function, or whatever they’re called). Did I do this in the best way I could have, or could it be simplified?

Also, if you’re feeling particularly bored, go ahead and give me a new assignment/query to do on this table. I could use the practice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:08:39+00:00Added an answer on June 1, 2026 at 4:08 pm

    What you have written in English doesn’t seem to quite match qhat you have written in SQL.

    English:
    – One record per City
    – One field per record, showing the total cost of all associated agents

    SQL:
    – One record per Agent
    – One field per record, showing the total cost of all agents in the same city

     AgentID | agtCity | agtFMPrice
    ---------+---------+------------
        1    |    1    |     10
        2    |    1    |     20
        3    |    2    |     30
        4    |    2    |     10
        5    |    2    |     25
    
    
     Results of SQL version           Results of English version
    ------------------------         ----------------------------
                30                                30
                30                                65
                65
                65
                65
    

    If you want the English version, I’d do this…

    SELECT
      agtCity,
      SUM(agtFMPrice) AS IX
    FROM
      Agent
    GROUP BY
      agtCity
    ORDER BY
      SUM(agtFMPrice) DESC
    

    To assist performance, the table could (should?) also have an Index on (agtCity)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing my journey into more advanced OO, I'm trying to come up with the
I am just starting my journey into learning databases/SQL and the like. I decided
Ive just started my journey into Source Control ... Im getting the hang of
On my journey into the depths of custom ASP.NET control development I am obviously
On my journey to learning F#, I've run into a problem I cant solve.
I am about to start on a journey writing a windows forms application that
I'm just starting my journey into OOP - and I'm currently trying to roll
I'm learning C, but after that or in the meanwhile, what should I learn
Continuing my journey into the world of variadic templates , I encountered another problem.
i'm new to development and i'm just starting my journey into the daunting world

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.