?
Solved

How to create a SQL SERVER view using Max and groping by..

Posted on 2003-03-23
4
Medium Priority
?
458 Views
Last Modified: 2012-05-04
I need to create a view.  Data looks like below.

YEAR     UNITS   ACCOUNT#
2001      200     abc
2005      100     dvf
2003      50     aaa

I need to find out the TOTAL for the most recent year only grouping by ACCOUN#.

when I create view ...

CREATE view vw_test
as select MAX(year), account#, units
FROM table_name
GROUP BY account#

it gives me an error telling that I need to group by UNITS as well.  but it is not a want.  Please help..
0
Comment
Question by:nelum_lk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 34

Expert Comment

by:arbert
ID: 8190548
It's an aggregation query--you have to either group by everything, or include everything in an aggregate function.  

If units is numeric--do this:

CREATE view vw_test
as select MAX(year), account#, sum(units)
FROM table_name
GROUP BY account#


if units is alpha, do this:


CREATE view vw_test
as select MAX(year), account#, max(units)
FROM table_name
GROUP BY account#



Do you always only have on unit per year?

Brett
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 300 total points
ID: 8194925
CREATE VIEW LatestYear
 as Select Data.Account#,Data.Year,Data.Units
      From (select Max(year) as Year,account#
              from table_name
             group by Account#) as LatestYear
     inner join Table_name as Data
        on Data.Account#=LatestYear.Account#
       and Data.Year = LatestYear.Year


if you want to retrieve the data in the latestyear for an
account , otherwise you'll have to sum the units
or whatever is relevant to produce an aggregation in the original group...

hth
       
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10348600
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: Lowfatspread {http:#8194925}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

walterecook
EE Cleanup Volunteer
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

800 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