Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

select top1 in subquery - part 2

Posted on 2006-07-11
4
Medium Priority
?
1,152 Views
Last Modified: 2007-12-19
Next problem....

I need to join on the items table again, but this time i need to sum on a column but only on certain criteria (look at the last join, is that the correct way of doing it?) - the column in question is EstReplacementCost


declare @date datetime
set @date = '2006-07-01 00:00:00'

SELECT j.JobNo, j.ClaimNo, nab.NatAccBranch, j.CoverID, j.NewForOld, j.AccidentalClaimType, dc.DamageDescription, c.Cause, j.ReportOnly,
                      j.ContactsEntered, j.CustDirectContact, j.CustLicContact, j.VisitedOn, j.FinalReport, cd.CustName, cd.CustPostcode, j.RoomCount, j.Excess,
             sum(ia.chnet) totalInvoice, rc.AccName, replItems.ReplacementLetterSent, SUM(estItems.EstReplacementCost) estCost
FROM         dbo.Jobs j INNER JOIN
                      dbo.LicencesHeld lh ON j.LicID = lh.LicID INNER JOIN
                      dbo.NationalAccountBranches nab ON j.NatBranchID = nab.NatAccBranchID INNER JOIN
                      dbo.DamageCategories dc ON j.DamageCatID = dc.DamageCategoryID INNER JOIN
                      dbo.Causes c ON j.CauseID = c.CauseID INNER JOIN
                      dbo.Items i ON j.JobNo = i.JobNo INNER JOIN
                      dbo.ItemActions ia ON i.ItemID = ia.ItemID INNER JOIN
                      dbo.CustomerDetails cd ON j.CustomerID = cd.CustomerID LEFT JOIN
                  Items replItems ON j.JobNo = replItems.JobNo AND replItems.itemid =
                  (select top 1 t.itemid from items t where j.JobNo = t.JobNo and replacementcompanyid is not null and replacementletter is not null order by Replacementlettersent desc ) LEFT JOIN
                  ReplacementCompanies rc ON replItems.ReplacementCompanyID = rc.ReplCoID LEFT JOIN
                  items estItems ON j.JobNo = estItems.JobNo AND estItems.ItemID in
                  (select t.itemid from items t where j.JobNo = t.JobNo and replacementrequired = 0)

WHERE     (lh.LiveUser = 1) AND (j.BUID = 2759)
GROUP BY j.JobNo, j.ClaimNo, nab.NatAccBranch, j.CoverID, dc.DamageDescription, c.Cause, j.ContactsEntered, j.CustDirectContact, j.CustLicContact,
                      j.VisitedOn, j.FinalReport, cd.CustName, cd.CustPostcode, j.RoomCount, j.Excess,newforold,accidentalclaimtype,reportonly,
                  rc.AccName,replItems.ReplacementLetterSent
HAVING      (j.FinalReport > CONVERT(DATETIME, @date, 102))




0
Comment
Question by:Type25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 9

Author Comment

by:Type25
ID: 17081121
by the way, the query runs, but the results are somewhat random... the EstReplacementCost columns is generally to much.
0
 
LVL 6

Expert Comment

by:maxy88
ID: 17081309

What criteria do you require?

Max.
0
 
LVL 9

Author Comment

by:Type25
ID: 17081317
This join:
LEFT JOIN
               items estItems ON j.JobNo = estItems.JobNo AND estItems.ItemID in
               (select t.itemid from items t where j.JobNo = t.JobNo and replacementrequired = 0)

I need the sum of EstReplacementCost  where replacementrequired = 0

as well as the rest of the query.

For the time being i've done it as a seperate query
0
 
LVL 6

Accepted Solution

by:
maxy88 earned 2000 total points
ID: 17082481

Try this:

LEFT JOIN
               items estItems ON j.JobNo = estItems.JobNo AND estItems.ItemID in
               (select itemid from items where replacementrequired = 0)

Max.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question