Solved

Queries within queries

Posted on 2006-07-18
12
913 Views
Last Modified: 2008-02-01
Sorry in advance if I'm not asking this correctly or being vague.  I have this query:
SELECT     TOP (100) PERCENT DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) AS Expr1, datOrigSaleDate, folioRecId,
                      fgn_countyRecId, txtCertNum, txtParcelId, txtAcctNum, txtBuyerName, txtBuyerNum, decRate, decFace, decCertBalDue, monthDiff, totalInt, currentInt,
                      bitHomestead, txtStatus, datDateCreated
FROM         dbo.tbl_Folio
WHERE     (0 = 0)
GROUP BY DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)), datOrigSaleDate, folioRecId, fgn_countyRecId, txtCertNum,
                      txtParcelId, txtAcctNum, txtBuyerName, txtBuyerNum, decRate, decFace, decCertBalDue, monthDiff, totalInt, currentInt, txtStatus, datDateCreated,
                      bitHomestead
HAVING      (DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) > 16) AND (txtBuyerName IN (N'Venus 1 LLC', N'Mars 1 LLC',
                      N'Miller Max Or Miller Gary', N'Brown William S', N'Equifunding Inc'))
ORDER BY txtCertNum

Now I want to create another column for output in this query called "Additionals" that would run this query for each row:
SELECT     SUM(decFace) AS totAdds
                  FROM         dbo.tbl_Folio
                  GROUP BY txtParcelId
                  HAVING      (txtParcelId LIKE '@txtParcelId')            

Is this possible?
0
Comment
Question by:lostboyinsofla
  • 7
  • 5
12 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17130413
SELECT     TOP (100) PERCENT DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) AS Expr1, datOrigSaleDate, folioRecId,
                      fgn_countyRecId, txtCertNum, txtParcelId, txtAcctNum, txtBuyerName, txtBuyerNum, decRate, decFace, decCertBalDue, monthDiff, totalInt, currentInt,
                      bitHomestead, txtStatus, datDateCreated
, ( SELECT     SUM(decFace)
               FROM         dbo.tbl_Folio f
         WHERE f.txtParcelId =          txtParcelId    
) AS totAdds
               HAVING      (    
FROM         dbo.tbl_Folio
WHERE     (0 = 0)
GROUP BY DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)), datOrigSaleDate, folioRecId, fgn_countyRecId, txtCertNum,
                      txtParcelId, txtAcctNum, txtBuyerName, txtBuyerNum, decRate, decFace, decCertBalDue, monthDiff, totalInt, currentInt, txtStatus, datDateCreated,
                      bitHomestead
HAVING      (DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) > 16) AND (txtBuyerName IN (N'Venus 1 LLC', N'Mars 1 LLC',
                      N'Miller Max Or Miller Gary', N'Brown William S', N'Equifunding Inc'))
ORDER BY txtCertNum
0
 

Author Comment

by:lostboyinsofla
ID: 17130686
K figured out my own question.  The answer is:

SELECT     DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) AS Expr1, datOrigSaleDate, folioRecId, fgn_countyRecId,
                      txtCertNum, txtParcelId, txtAcctNum, txtBuyerName, txtBuyerNum, decRate, decFace, decCertBalDue, monthDiff, totalInt, currentInt, bitHomestead,
                      txtStatus, datDateCreated,
                          (SELECT     SUM(decFace) AS Expr1
                            FROM          dbo.tbl_Folio AS f2
                            WHERE      (txtParcelId = f.txtParcelId)) AS additionals
FROM         dbo.tbl_Folio AS f
0
 

Author Comment

by:lostboyinsofla
ID: 17130721
Thanks Angel.  I didn't see your solution until after I added mine and it will work too.  I'll give you the points.  Can you help me further with what my end goal is?

I now need to add another column that basically will do these calculations but since it isn't a query I'm not sure how to do it.

                  curInt.numMonths = (dateDiff("m", dateFormat(arguments.saleDate, "mm/1/yyyy"), Now()));
                  curInt.totInt = ((arguments.interestRate / 12)  * curInt.numMonths);
                  curInt.curInterest = (curInt.totInt * arguments.certFaceValue);

I need to my column have the value of curInt.curInterest.  Any suggestions?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17130742
Are you using MS SQL Server or MS Access?
0
 

Author Comment

by:lostboyinsofla
ID: 17130825
SQL Server 2005
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17131779

               curInt.numMonths = dateDiff(month, dateadd( day, 1- datepart(day, arguments.saleDate), arguments.saleDate, getdate()));
               curInt.totInt = ((arguments.interestRate / 12)  * (dateDiff(month, dateadd( day, 1- datepart(day, arguments.saleDate), arguments.saleDate, getdate()))) );
               curInt.curInterest = (((arguments.interestRate / 12)  * (dateDiff(month, dateadd( day, 1- datepart(day, arguments.saleDate), arguments.saleDate, getdate()))) ) * arguments.certFaceValue);

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:lostboyinsofla
ID: 17131843
Angel

I've created a scalar function:

CREATE FUNCTION currentInterest
( @numMonths int, @intRate decimal(18,2), @faceVal decimal(18,2) )
RETURNS decimal(18,2)
AS
BEGIN
  RETURN (((@intRate/12)*@numMonths)*@faceVal)
END

which works.

NOW...  how do I implement it in my SQL statement?: The line in question is surrounded by asteriks.  
SELECT     TOP (100) PERCENT
      DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) AS monthDiff,
      DATEDIFF(mm, datOrigSaleDate, (SELECT dbo.FirstMonthDay(GetDate()))) AS monthInt,
      *****SELECT      dbo.currentInterest(f.monthDiff, f.decRate, f.decFace) AS curInt*****
      datOrigSaleDate,
      folioRecId,
      fgn_countyRecId,
      txtCertNum,
      txtParcelId,
    txtAcctNum,
      txtBuyerName,
      txtBuyerNum,
      decRate,
      decFace,
      bitHomestead,
      txtStatus,
      datDateCreated,
    (SELECT     SUM(decFace) AS Expr1
       FROM          dbo.tbl_Folio AS f2
     WHERE      (txtParcelId = f.txtParcelId)) AS additionals
FROM         dbo.tbl_Folio AS f
WHERE     (0 = 0)
AND (DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) > 16)
AND txtBuyerName IN ('Venus 1 LLC', 'Mars 1 LLC', 'Miller Max Or Miller Gary', 'Brown William S', 'Equifunding Inc')
ORDER BY datOrigSaleDate DESC


When I run it I get these errors:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'datOrigSaleDate'.
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'AS'.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17131896

CREATE FUNCTION dbo.currentInterest
( @numMonths int, @intRate decimal(18,2), @faceVal decimal(18,2) )
RETURNS decimal(18,2)
AS
BEGIN
  RETURN (((@intRate/12)*@numMonths)*@faceVal)
END



SELECT     TOP (100) PERCENT
     DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) AS monthDiff,
     DATEDIFF(mm, datOrigSaleDate, (SELECT dbo.FirstMonthDay(GetDate()))) AS monthInt,
     dbo.currentInterest(f.monthDiff, f.decRate, f.decFace) AS curInt,
     datOrigSaleDate,
     folioRecId,
     fgn_countyRecId,
     txtCertNum,
     txtParcelId,
    txtAcctNum,
     txtBuyerName,
     txtBuyerNum,
     decRate,
     decFace,
     bitHomestead,
     txtStatus,
     datDateCreated,
    (SELECT     SUM(decFace) AS Expr1
      FROM          dbo.tbl_Folio AS f2
     WHERE      (txtParcelId = f.txtParcelId)) AS additionals
FROM         dbo.tbl_Folio AS f
WHERE     (0 = 0)
AND (DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) > 16)
AND txtBuyerName IN ('Venus 1 LLC', 'Mars 1 LLC', 'Miller Max Or Miller Gary', 'Brown William S', 'Equifunding Inc')
ORDER BY datOrigSaleDate DESC
0
 

Author Comment

by:lostboyinsofla
ID: 17132143
That got it.  Thanks.
0
 

Author Comment

by:lostboyinsofla
ID: 17132540
Hey Angel,

Once I have this:
SELECT     TOP (100) PERCENT
     DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) AS monthDiff,
     DATEDIFF(mm, datOrigSaleDate, (SELECT dbo.FirstMonthDay(GetDate()))) AS monthInt,
     dbo.currentInterest(f.monthDiff, f.decRate, f.decFace) AS curInt,
     datOrigSaleDate,
     folioRecId,
     fgn_countyRecId,
     txtCertNum,
     txtParcelId,
    txtAcctNum,
     txtBuyerName,
     txtBuyerNum,
     decRate,
     decFace,
     bitHomestead,
     txtStatus,
     datDateCreated,
    *****(SELECT     SUM(decFace) AS Expr1
      FROM          dbo.tbl_Folio AS f2
     WHERE      (txtParcelId = f.txtParcelId)) AS additionals*****
FROM         dbo.tbl_Folio AS f
WHERE     (0 = 0)
AND (DATEDIFF(mm, datOrigSaleDate, CONVERT(DATETIME, '2006-04-01 00:00:00', 102)) > 16)
AND txtBuyerName IN ('Venus 1 LLC', 'Mars 1 LLC', 'Miller Max Or Miller Gary', 'Brown William S', 'Equifunding Inc')
ORDER BY datOrigSaleDate DESC

Can I run another SUM of the line I have in asteriks in the same query?  If so what's the syntax?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17133793
the syntax you show looks fine...
0
 

Author Comment

by:lostboyinsofla
ID: 17134247
Sytactically it's sound but it's not returning the expected result.  For each row I calculate additionals which is working fine.  I now need to calculate all the additionals for a grand sum.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now