[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Queries within queries

Posted on 2006-07-18
12
Medium Priority
?
923 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
[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
  • 7
  • 5
12 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

650 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