|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Saturday, December 01, 2012 8:20 PM
Points: 2,
Visits: 22
|
|
Hi guys.
Newb in SQL with what i hope is an easy one:
SELECT ACCUM.c1 FROM (SELECT t1.c1, SUM(t1.c2) as adding FROM Table1 t1 GROUP BY t1.c1) AS ACCUM WHERE ACCUM.adding = (SELECT MAX(adding) from ACCUM)
When i do this i get: Invalid object name 'ACCUM'.
If i place a number present in ACCUM table instead of (SELECT MAX(adding) from ACCUM), i get a correct result. That seems to tell that the only problem is that ACCUM is not recognized inside the ().
Any thoughts?
Regards
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 5:30 AM
Points: 2,663,
Visits: 1,680
|
|
Will this help:
SELECT c1 FROM( SELECT c1, SUM(c2) AS 'Adding',(ROW_NUMBER() OVER (ORDER BY SUM(c2) DESC)) AS 'Row' FROM Table1 t1 GROUP BY c1 ) a WHERE Row = 1
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:27 PM
Points: 6,735,
Visits: 11,788
|
|
Ironicster (11/23/2012) Hi guys.
Newb in SQL with what i hope is an easy one:
SELECT ACCUM.c1 FROM (SELECT t1.c1, SUM(t1.c2) as adding FROM Table1 t1 GROUP BY t1.c1) AS ACCUM WHERE ACCUM.adding = (SELECT MAX(adding) from ACCUM)
When i do this i get: Invalid object name 'ACCUM'.
If i place a number present in ACCUM table instead of (SELECT MAX(adding) from ACCUM), i get a correct result. That seems to tell that the only problem is that ACCUM is not recognized inside the ().
Any thoughts?
Regards ACCUM is a derived table so you cannot reference that as if it were a concrete table within a separate derived table query.
I think is logically equivalent to what you are after, namely the c1 values with the largest sum of their associated c2 values:
WITH cte AS (SELECT c1, SUM(c2) as adding FROM Table1 GROUP BY c1), cte2 AS (SELECT c1, RANK() OVER (ORDER BY adding DESC) AS adding_rank FROM cte) SELECT c1 FROM cte2 WHERE adding_rank = 1;
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Saturday, December 01, 2012 8:20 PM
Points: 2,
Visits: 22
|
|
Tks
Didn't know the WITH yer
Rgds
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
Ironicster (11/25/2012) Tks
Didn't know the WITH yer
Rgds
Just to make it easier to find information on the subject, it's the precursor for a thing called "Common Table Expressions" or just "CTE" for short.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
You might want to read some basic articles to get you started.
Data basics: http://www.sqlservercentral.com/stairway/72899/
Database design: http://www.sqlservercentral.com/stairway/72400/
SQL has pretty much the same scoping rules as any block structured language. Looking at your query, where is the Accum derived table created? Now look at where the table is referenced.
SELECT Accum.c1 FROM (SELECT T1.c1, SUM(T1.c2) AS c1_tot FROM Table1 AS T1 GROUP BY T1.c1) AS Accum (c1, c1_tot) WHERE Accum.c1 = (SELECT MAX(c1_tot) FROM Accum);
However, if you create the table at a higher scope, you can make this work. You could put it in a VIEW or CTE.
WITH Accum (c1, c2_tot) AS (SELECT T1.c1, SUM(T1.c2) FROM Table1 AS T1 GROUP BY T1.c1) SELECT Accum.c1 FROM Accum WHERE Accum.c1 = (SELECT MAX(c2_tot) FROM Accum);
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|