• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

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

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
skaleem1
Asked:
skaleem1
  • 6
  • 3
1 Solution
 
Anthony PerkinsCommented:
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
 
vr6rCommented:
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
 
Anthony PerkinsCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
skaleem1Author Commented:
Acperkins,

I get the following error:

The column 'TOTAL_ACRES' was specified multiple times for 'MyCTE'.
0
 
Anthony PerkinsCommented:
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
 
Anthony PerkinsCommented:
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
 
skaleem1Author Commented:
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
 
skaleem1Author Commented:
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
 
Anthony PerkinsCommented:
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
 
Anthony PerkinsCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now