Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

SQL - Help building a complex query.

Avatar of josephdaviskcrm
josephdaviskcrmFlag for United States of America asked on
Microsoft SQL Server 2005
3 Comments1 Solution272 ViewsLast Modified:
SELECT StoreID, Date, Score, Visit, AreaDisplay
FROM Structure
WHERE AreaID IN ('3266103') AND Visit IN ('Unannounced')
ORDER BY StoreID, Date ASC

The above SQL produces data output similar to the following

StoreID    Date                    Score    Visit                      AreaDisplay
000171   2005-06-09       37          Unannounced      3266103 BKM-JV CBM - 103
000171   2006-02-11       90          Unannounced      3266103 BKM-JV CBM - 103
...               ...                       ...             ...                            ...

Another query I'm trying to build uses aggregate functions, but it throws an error because AreaDisplay is not an aggregate function. I need the text in AreaDisplay to be returned along with the dataset.

SELECT TOP (6) AVG(Score) AS Score, AreaDisplay
FROM (
        SELECT ranking = dense_rank() over (PARTITION BY StoreID ORDER BY OERDate ASC), *
        FROM dbo.BK_OER_Structure
        WHERE AreaID IN ('3266103') AND (Visit IN ('Unannounced'))
        ) average
GROUP BY ranking

Without the AreaDisplay, the query works fine and displays:

Score
46.85
79.83
74.66
68.33
80.20
76.45

How can I get AreaDisplay to appear on every record in the return dataset as it is found in the query at the top?