We help IT Professionals succeed at work.
Get Started

Optimize SQL Server 2005 Query

322 Views
Last Modified: 2012-05-12
Is there a way to optimize / re-write the following query:

SELECT name, organization, max(status) as status, sum(total) as total FROM
      (
      SELECT
      dbo.myusers.name,
      dbo.choices.status,
      i2.s1*dbo.rsystem.s1 + i2.s2*dbo.rsystem.s2 + i2.s3*dbo.rsystem.s3 - i1.s1*dbo.rsystem.s1 - i1.s2*dbo.rsystem.s2 - i1.s3*dbo.rsystem.s3 AS total
      FROM dbo.accounts
      INNER JOIN dbo.myusers ON dbo.accounts.id = dbo.myusers.accountid
      INNER JOIN dbo.choices ON dbo.myusers.id = dbo.choices.myuserid
      INNER JOIN dbo.s ON dbo.choices.choiceid = dbo.s.id
      INNER JOIN s_history i1 ON dbo.choices.choiceid = i1.id
      INNER JOIN s_history i2 ON dbo.choices.choiceid = i2.id
      INNER JOIN dbo.rsystem ON dbo.choices.rsystemid = dbo.rsystem.id
      WHERE dbo.accounts.id=39821
      AND i2.stamp = 'November 03 2011' AND i1.stamp = 'October 31 2011 AND choices.startdate <= 'October 31 2011' AND choices.enddate >= 'November 03 2011'
      UNION
      SELECT
      dbo.myusers.name,
      dbo.choices.status,
      i2.s1*dbo.rsystem.s1 + i2.s2*dbo.rsystem.s2 + i2.s3*dbo.rsystem.s3 - i1.s1*dbo.rsystem.s1 - i1.s2*dbo.rsystem.s2 - i1.s3*dbo.rsystem.s3 AS total
      FROM dbo.accounts
      INNER JOIN dbo.myusers ON dbo.accounts.id = dbo.myusers.accountid
      INNER JOIN dbo.choices ON dbo.myusers.id = dbo.choices.myuserid
      INNER JOIN dbo.s ON dbo.choices.choiceid = dbo.s.id
      INNER JOIN s_history i1 ON dbo.choices.choiceid = i1.id
      INNER JOIN s_history i2 ON dbo.choices.choiceid = i2.id
      INNER JOIN dbo.rsystem ON dbo.choices.rsystemid = dbo.rsystem.id
      WHERE dbo.accounts.id=39821
      AND i2.stamp = choices.enddate AND i1.stamp = 'October 31 2011' AND choices.startdate <= 'October 31 2011' AND choices.enddate < 'November 03 2011' AND choices.enddate >= 'October 31 2011'
      UNION
      SELECT
      dbo.myusers.name,
      dbo.choices.status,
      i2.s1*dbo.rsystem.s1 + i2.s2*dbo.rsystem.s2 + i2.s3*dbo.rsystem.s3 - i1.s1*dbo.rsystem.s1 - i1.s2*dbo.rsystem.s2 - i1.s3*dbo.rsystem.s3 AS total
      FROM dbo.accounts
      INNER JOIN dbo.myusers ON dbo.accounts.id = dbo.myusers.accountid
      INNER JOIN dbo.choices ON dbo.myusers.id = dbo.choices.myuserid
      INNER JOIN dbo.s ON dbo.choices.choiceid = dbo.s.id
      INNER JOIN s_history i1 ON dbo.choices.choiceid = i1.id
      INNER JOIN s_history i2 ON dbo.choices.choiceid = i2.id
      INNER JOIN dbo.rsystem ON dbo.choices.rsystemid = dbo.rsystem.id
      WHERE dbo.accounts.id=39821
      AND i2.stamp = 'November 03 2011' AND i1.stamp = choices.startdate AND choices.startdate > 'October 31 2011' AND choices.startdate <= 'November 03 2011' AND choices.enddate >= 'November 03 2011'
      UNION SELECT 0 as       SELECT
      dbo.myusers.name,
      dbo.choices.status,
      i2.s1*dbo.rsystem.s1 + i2.s2*dbo.rsystem.s2 + i2.s3*dbo.rsystem.s3 - i1.s1*dbo.rsystem.s1 - i1.s2*dbo.rsystem.s2 - i1.s3*dbo.rsystem.s3 AS total
      FROM dbo.accounts
      INNER JOIN dbo.myusers ON dbo.accounts.id = dbo.myusers.accountid
      INNER JOIN dbo.choices ON dbo.myusers.id = dbo.choices.myuserid
      INNER JOIN dbo.s ON dbo.choices.choiceid = dbo.s.id
      INNER JOIN s_history i1 ON dbo.choices.choiceid = i1.id
      INNER JOIN s_history i2 ON dbo.choices.choiceid = i2.id
      INNER JOIN dbo.rsystem ON dbo.choices.rsystemid = dbo.rsystem.id
      WHERE dbo.accounts.id=39821
      AND i2.stamp = choices.enddate AND i1.stamp = choices.startdate AND choices.startdate > 'October 31 2011' AND choices.enddate < 'November 03 2011'
      ) as totals GROUP BY name, organization ORDER BY name, total DESC

Thanks in advance!
Comment
Watch Question
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE