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 7070519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:34:34+00:00 2026-05-28T05:34:34+00:00

I have following sql syntax. This works fine but I was wondering if there

  • 0

I have following sql syntax. This works fine but I was wondering if there is a way to write this syntax simpler since MAX(ra0.DescPriority) uses same table join.

SELECT DISTINCT Items_1.ImageID, Items_1.SubcategoryID2, Items_1.AddDate,
                                 (SELECT        MAX(ra0.DescPriority) AS Expr1
                                   FROM            Items INNER JOIN
                                                             Attribs AS ra0 ON Items.ImageID = ra0.ImageID) AS Pri
    FROM            Items AS Items_1 INNER JOIN
                             Attribs AS ra0 ON Items_1.ImageID = ra0.ImageID LEFT OUTER JOIN
                             v_DisplayStockPrice AS v_DisplayStockPrice_1 ON Items_1.ImageID = v_DisplayStockPrice_1.ImageID INNER JOIN
                             Attribs AS ra1 ON ra0.ImageID = ra1.ImageID
    WHERE        (Items_1.deleted NOT IN (1, 2)) AND (Items_1.SubcategoryID2 = 'ORD')
    ORDER BY Pri, Items_1.AddDate DESC

So I came up with this syntax.

SELECT DISTINCT Items_1.ImageID, Items_1.SubcategoryID2, Items_1.AddDate, MAX(ra0.DescPriority) AS Pri
FROM            Items AS Items_1 INNER JOIN
                         Attribs AS ra0 ON Items_1.ImageID = ra0.ImageID LEFT OUTER JOIN
                         v_DisplayStockPrice AS v_DisplayStockPrice_1 ON Items_1.ImageID = v_DisplayStockPrice_1.ImageID INNER JOIN
                         Attribs AS ra1 ON ra0.ImageID = ra1.ImageID
WHERE        (Items_1.deleted NOT IN (1, 2)) AND (Items_1.SubcategoryID2 = 'ORD')
GROUP BY Items_1.ImageID, Items_1.SubcategoryID2, Items_1.AddDate
ORDER BY Pri, Items_1.AddDate DESC

Which SQL syntax is better? Or is there better way to write this Query?

  • 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-05-28T05:34:35+00:00Added an answer on May 28, 2026 at 5:34 am

    The second one looks better and probably performs better, but really you should use Query Analyzer. It will tell you exactly which one is better:
    http://msdn.microsoft.com/en-us/library/aa216945(v=sql.80).aspx

    Debug query performance problems. (Show Execution Plan, Show Server Trace, Show Client Statistics, Index Tuning Wizard)

    Here’s a description of how to use the query execution plan:
    http://www.sql-server-performance.com/2006/query-execution-plan-analysis/

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

Sidebar

Related Questions

I have the following loop, which works fine if i<1 but gives me the
I have an MS SQL function that is called with the following syntax: SELECT
I have the following sql query for transforming data but is it possible to
I have the following SQL Server 2008 query that works for all of my
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
Hi I have following sql statement that gives me the the first_name and total
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following SQL Statement. I need to select the latest record for

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.