Solved

Group By error (with Subquery)

Posted on 2012-03-14
3
154 Views
Last Modified: 2012-06-27
Experts,
Thank you for taking my question
Objective: I want to return one line grouped by Property (P.scode).
I have specific code (attached) that needs an expert's review.
I'm getting a common error that I can not resolve:  "Column table.feild is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."  I can not seem to resolve, probably due to a subquery and group by function.

Attached find a word document with two T-SQL:
-Group by Tenant - Successful query grouped by a more detailed "Tenant" level
-Group by Property - represents modification to the success query; resulting with the group by error.
I've tried but failed solve.  If this question looks familar, it is directly related to an open Crystal question.  The Crystal Experts help suggests I attempt fix via T-SQL command (which makes perfect sense, execpt I'm failing to solve vial SQL too)

Data set consists of Many Tenants that belong to a single Property.  

Thank you
ExpertExchange-GroupBy-Property.docx
0
Comment
Question by:LFreehauf
  • 2
3 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 37722661
In that construction  charge.hmy must be one of the columns making part of the group by, don't think that will cause a problem

Group By  p.sCode, p.sAddr1, charge.hmy

But see also you use the same subselect  from table detail twice. It will be better to make it an extra left join (and then no need to have charge.hmy in group by).
0
 
LVL 34

Assisted Solution

by:James0628
James0628 earned 100 total points
ID: 37723577
I think he's probably right.  You used charge.hmy in the subquery, but not in the GROUP BY.

 I wonder if you could just use SUM on the subqueries, instead of changing the GROUP BY?  For example

, Sum(charge.sTotalAmount) -
    Sum (select isnull(sum(d.samount),0) from detail d
    where d.hchkorchg = charge.hmy and d.cashpost <= '02/29/2012')


 Don't know that I've ever tried anything like that.  Seems like it might work.

 James
0
 
LVL 25

Accepted Solution

by:
jogos earned 400 total points
ID: 37723679
Select  p.sCode
, p.sAddr1

, Sum(charge.sTotalAmount)
, Sum(charge.sTotalAmount) - isnull(sum(d.samount),0)  -- from join
   
, case when Sum(charge.sTotalAmount) <> 0 then
      (Sum(charge.sTotalAmount) - isnull(sum(d.samount),0) )  -- from join
      / Sum(charge.sTotalAmount) * 100
      else 0 end AS sortCol
From  Property p  Inner Join Tenant t ON p.hMy = t.hProperty  
Inner Join trans charge ON t.hMyPerson = charge.hPerson  
Inner Join acct ON acct.hMy = charge.hOffsetAcct  
Left Outer Join chargtyp ON charge.hRetentionAcct = chargtyp.hMy
-- Introduced left join here
LEFT JOIN  detail d
    ON d.hchkorchg = charge.hmy and d.cashpost <= '02/29/2012'
inner join tenstatus ts on ( ts.istatus = t.istatus )
Where      7 = charge.iType
and chargtyp.sCode='rnt'
and P.scode = 103 and charge.uPostDate ='02/01/2012'
Group By  p.sCode, p.sAddr1
Order By  sortCol desc  -- changed 5 by column alias
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

867 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

22 Experts available now in Live!

Get 1:1 Help Now