Solved

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

Posted on 2013-12-21
10
394 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TSQL - How to declare table name 26 42
SQL Server 2008 R2, need a pivot/cross tab query... 4 27
query optimization 6 14
shrink table after huge delete 2 13
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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