Solved

Queries within queries

Posted on 2006-07-18
12
916 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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
 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to share SSIS Package? 6 37
Query Syntax 17 36
What's wrong with this T-SQL Foreign Key? 7 46
SQL Syntax: How to force case sensitive query? 2 30
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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

809 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