Solved

Queries within queries

Posted on 2006-07-18
12
918 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 143

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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 143

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 143

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
 

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 143

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

713 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