Solved

How to return a single row with a maximum column value from a sub query

Posted on 2013-12-21
10
391 Views
Last Modified: 2013-12-22
The sub-query in the following statement returns two records as given below:


                  SELECT TMP.TOTAL_ACRES, TMP.YEAR, SD1.*
                  FROM      
                  (SELECT      CA.ACCOUNT_ID, CA.YEAR - 2 AS YEAR, SUM(CA.ACRES) AS TOTAL_ACRES
                         FROM      dbo.Table1 CA INNER JOIN
                              dbo.Table2 A ON
                                    A.ACCOUNT_ID = CA.ACCOUNT_ID
                         WHERE CA.INACTIVE_FLAG IS NULL
                         GROUP BY CA.ACCOUNT_ID, CA.YEAR - 2) tmp
                  INNER JOIN
                  dbo.Table3 SD1 ON
                  tmp.ACCOUNT_ID = SD1.ACCOUNT_ID AND
                  tmp.YEAR = SD1.PROGRAM_YEAR
                  WHERE      SD1.TOTAL_ACRES IS NULL AND SD1.ACREAGE_ESTIMATED IS NULL

Again, the sub-query in the above statement returns two records as given below:

ACCOUNT_ID      YEAR      TOTAL_ACRES
10205467                        2007       1825
10205467                        2008       1825

How can I change the sub-query to only get a single record with the largest number of year, in this case year 2008, i.e. as follows:

ACCOUNT_ID      YEAR      TOTAL_ACRES
10205467                        2008       1825

Please help by changing the above query!
0
Comment
Question by:skaleem1
  • 6
  • 3
10 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39734134
If you mean one per ACCOUNT_ID then :
;WITH MyCTE AS (
 SELECT TMP.TOTAL_ACRES,
        TMP.[YEAR],
        SD1.*,
        ROW_NUMBER() OVER (PARTITION BY TMP.ACCOUNT_ID ORDER BY TMP.[YEAR] DESC) Row
 FROM   (SELECT CA.ACCOUNT_ID,
                CA.[YEAR] - 2 AS [YEAR],
                SUM(CA.ACRES) AS TOTAL_ACRES
         FROM   dbo.Table1 CA
                INNER JOIN dbo.Table2 A ON A.ACCOUNT_ID = CA.ACCOUNT_ID
         WHERE  CA.INACTIVE_FLAG IS NULL
         GROUP BY CA.ACCOUNT_ID,
                CA.YEAR - 2
        ) tmp
        INNER JOIN dbo.Table3 SD1 ON tmp.ACCOUNT_ID = SD1.ACCOUNT_ID
                                     AND tmp.[YEAR] = SD1.PROGRAM_YEAR
 WHERE  SD1.TOTAL_ACRES IS NULL
        AND SD1.ACREAGE_ESTIMATED IS NULL
        )
SELECT  *
FROM MyCTE
WHERE Row = 1

Open in new window

0
 
LVL 8

Expert Comment

by:vr6r
ID: 39734135
Try this:
SELECT TMP.TOTAL_ACRES, TMP.YEAR, SD1.*
FROM      
(SELECT TOP 1 
	CA.ACCOUNT_ID, 
	CA.YEAR - 2 AS YEAR, 
	SUM(CA.ACRES) AS TOTAL_ACRES
	FROM dbo.Table1 CA 
	INNER JOIN dbo.Table2 A ON A.ACCOUNT_ID = CA.ACCOUNT_ID 
	WHERE CA.INACTIVE_FLAG IS NULL 
	GROUP BY CA.ACCOUNT_ID, CA.YEAR - 2
	ORDER BY CA.YEAR DESC) tmp
INNER JOIN
dbo.Table3 SD1 ON
tmp.ACCOUNT_ID = SD1.ACCOUNT_ID AND 
tmp.YEAR = SD1.PROGRAM_YEAR 
WHERE      SD1.TOTAL_ACRES IS NULL AND SD1.ACREAGE_ESTIMATED IS NULL

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39734137
If you mean only one row then replace:
ROW_NUMBER() OVER (PARTITION BY TMP.ACCOUNT_ID ORDER BY TMP.[YEAR] DESC) Row
For:
ROW_NUMBER() OVER (ORDER BY TMP.[YEAR] DESC) Row
0
 
LVL 1

Author Comment

by:skaleem1
ID: 39734148
Acperkins,

I get the following error:

The column 'TOTAL_ACRES' was specified multiple times for 'MyCTE'.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39734179
The answer really depends on what you want to see (you have not told us).  

If just one line then:
;WITH MyCTE AS (
 SELECT TMP.[YEAR],
        TMP.TOTAL_ACRES,
--        SD1.*,
        ROW_NUMBER() OVER (ORDER BY TMP.[YEAR] DESC) Row
 FROM   (SELECT CA.ACCOUNT_ID,
                CA.[YEAR] - 2 AS [YEAR],
                SUM(CA.ACRES) AS TOTAL_ACRES
         FROM   dbo.Table1 CA
                INNER JOIN dbo.Table2 A ON A.ACCOUNT_ID = CA.ACCOUNT_ID
         WHERE  CA.INACTIVE_FLAG IS NULL
         GROUP BY CA.ACCOUNT_ID,
                CA.YEAR - 2
        ) tmp
        INNER JOIN dbo.Table3 SD1 ON tmp.ACCOUNT_ID = SD1.ACCOUNT_ID
                                     AND tmp.[YEAR] = SD1.PROGRAM_YEAR
 WHERE  SD1.TOTAL_ACRES IS NULL
        AND SD1.ACREAGE_ESTIMATED IS NULL
        )
SELECT  [YEAR], TOTAL_ACRES
FROM MyCTE
WHERE Row = 1

Open in new window

0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 39734183
If you want to see one row per account then change it as follows:
;WITH MyCTE AS (
 SELECT TMP.ACCOUNT_ID,
        TMP.[YEAR],
        TMP.TOTAL_ACRES,
--      SD1.*,
        ROW_NUMBER() OVER (PARTITION BY TMP.ACCOUNT_ID ORDER BY TMP.[YEAR] DESC) Row
 FROM   (SELECT CA.ACCOUNT_ID,
                CA.[YEAR] - 2 AS [YEAR],
                SUM(CA.ACRES) AS TOTAL_ACRES
         FROM   dbo.Table1 CA
                INNER JOIN dbo.Table2 A ON A.ACCOUNT_ID = CA.ACCOUNT_ID
         WHERE  CA.INACTIVE_FLAG IS NULL
         GROUP BY CA.ACCOUNT_ID,
                CA.YEAR - 2
        ) tmp
        INNER JOIN dbo.Table3 SD1 ON tmp.ACCOUNT_ID = SD1.ACCOUNT_ID
                                     AND tmp.[YEAR] = SD1.PROGRAM_YEAR
 WHERE  SD1.TOTAL_ACRES IS NULL
        AND SD1.ACREAGE_ESTIMATED IS NULL
        )
SELECT  ACCOUNT_ID, [YEAR], TOTAL_ACRES
FROM MyCTE
WHERE Row = 1

Open in new window

0
 
LVL 1

Author Closing Comment

by:skaleem1
ID: 39734192
AcPerkins,

This is what I was looking for. Thanks for the quick help.

vr6r,

Thanks for your help but unfortunately that was the solution I personally came up with but did not work.
0
 
LVL 1

Author Comment

by:skaleem1
ID: 39734197
AcPerkins,

I apologize but I forgot to mention that I originally want to write the update statement:

UPDATE SD1
                  SET      TOTAL_ACRES = tmp.TOTAL_ACRES,
                        ACREAGE_ESTIMATED = 'Y'
                  --SELECT TMP.TOTAL_ACRES, TMP.YEAR, SD1.*
                  FROM      
                  (SELECT      CA.ACCOUNT_ID, CA.YEAR - 2 AS YEAR, SUM(CA.ACRES) AS TOTAL_ACRES
                         FROM      dbo.Table1 CA INNER JOIN
                              dbo.Table2 A ON
                                    A.ACCOUNT_ID = CA.ACCOUNT_ID
                         WHERE CA.INACTIVE_FLAG IS NULL
                         GROUP BY CA.ACCOUNT_ID, CA.YEAR - 2) tmp
                  INNER JOIN
                  dbo.Table3 SD1 ON
                  tmp.ACCOUNT_ID = SD1.ACCOUNT_ID AND
                  tmp.YEAR = SD1.PROGRAM_YEAR
                  WHERE      SD1.TOTAL_ACRES IS NULL AND SD1.ACREAGE_ESTIMATED IS NULL

How can I use your with statement with this update statement?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39734838
So you want to UPDATE all the years in Table3 with the last value from Table1 and Table2 and where you do not have a previous value for TOTAL_ACRES and ACREAGE_ESTIMATED?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39734858
If so try it like this:
SELECT	SD1.Account, 
		tmp.TOTAL_ACRES
--UPDATE  SD1
--SET     TOTAL_ACRES = tmp.TOTAL_ACRES,
--        ACREAGE_ESTIMATED = 'Y'
FROM    dbo.Table3 SD1
        INNER JOIN (SELECT  CA.ACCOUNT_ID,
                            CA.[YEAR] - 2 AS [YEAR],
                            SUM(CA.ACRES) AS TOTAL_ACRES,
                            ROW_NUMBER() OVER (PARTITION BY CA.ACCOUNT_ID, CA.YEAR ORDER BY CA.YEAR DESC) Row
                    FROM    dbo.Table1 CA
                            INNER JOIN dbo.Table2 A ON A.ACCOUNT_ID = CA.ACCOUNT_ID
                    WHERE   CA.INACTIVE_FLAG IS NULL
                    GROUP BY CA.ACCOUNT_ID,
                            CA.YEAR - 2
                   ) tmp ON SD1.ACCOUNT_ID = tmp.ACCOUNT_ID --AND SD1.PROGRAM_YEAR = tmp.[YEAR]
WHERE   COALESCE(SD1.TOTAL_ACRES, SD1.ACREAGE_ESTIMATED) IS NULL
        AND tmp.Row = 1

Open in new window


If it looks correct, then uncomment the UPDATE and comment out the SELECT.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

13 Experts available now in Live!

Get 1:1 Help Now