Solved

Queries within queries

Posted on 2006-07-18
12
921 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 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

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

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

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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…

705 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