Solved

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

Posted on 2013-12-21
10
390 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
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 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Auditing with Temporal Tables 4 19
Caste datetime 2 25
sql query Help 12 34
affinity mask in sql server 1 8
In this article I will describe the Backup & Restore 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

10 Experts available now in Live!

Get 1:1 Help Now