?
Solved

SQL Query

Posted on 2010-01-12
4
Medium Priority
?
322 Views
Last Modified: 2012-05-08
I'm using VB.net program to query SQL Server 2005.

This query works fine, except it is incomplete. I also need field PAacct in the Emp$ table which contains text. I want to add it to my select statement, but I do NOT want to group by this field. I can't sum or count it since I want the text.

I can't seem to make this work without adding it to the Group By area. I really only want to Group By C.Comp_ID.  The other groupings are there because the query doesn't work without them but if I could remove them, I would. The new field E.PAacct comes from the $emp table, so grouping would be a problem.

How can I get this field without grouping by it?
SELECT  C.Type, C.Comp_ID, C.Year, C.Comp_Name, C.Phone, 	COUNT(E.CompID) AS EmpCount, SUM(E.Wages) AS Wages, SUM(E.FedTax) AS FedTax

FROM     Com$ AS C INNER JOIN 
               Emp$ AS E ON C.Comp_ID = E.CompID AND C.Year = E.Year

WHERE  (C.Year = @YearParam)

GROUP BY C.Comp_ID, C.Type, C.Year, C.Comp_Name, C.Phone

ORDER BY C.Comp_ID

Open in new window

0
Comment
Question by:TadSter
[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
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 26298069
SELECT  C.Type, C.Comp_ID, C.Year, C.Comp_Name, C.Phone,       COUNT(E.CompID) AS EmpCount, SUM(E.Wages) AS Wages, SUM(E.FedTax) AS FedTax, (SELECT TOP 1 PAacct  FROM Emp$ p where C.Comp_ID = p.CompID AND C.Year = p.Year ) as PAccnt
FROM     Com$ AS C INNER JOIN
               Emp$ AS E ON C.Comp_ID = E.CompID AND C.Year = E.Year

WHERE  (C.Year = @YearParam)
GROUP BY C.Comp_ID, C.Type, C.Year, C.Comp_Name, C.Phone

ORDER BY C.Comp_ID
0
 
LVL 41

Expert Comment

by:Sharath
ID: 26298533
You can try with COUNT or SUM OVER clause. You can check the output of this query and modify it as you want. You can get back to me in case of further assitance.

SELECT C.Type, C.Comp_ID, C.Year, C.Comp_Name, C.Phone,E.PAacct,
       COUNT(E.CompID) OVER (PARTITION BY C.Type, C.Comp_ID, C.Year, C.Comp_Name, C.Phone) AS EmpCount,
       SUM(E.Wages) OVER (PARTITION BY C.Type, C.Comp_ID, C.Year, C.Comp_Name, C.Phone) AS Wages,
       SUM(E.FedTax) OVER (PARTITION BY C.Type, C.Comp_ID, C.Year, C.Comp_Name, C.Phone) AS FedTax
 FROM     Com$ AS C INNER JOIN
               Emp$ AS E ON C.Comp_ID = E.CompID AND C.Year = E.Year
WHERE  (C.Year = @YearParam)
GROUP BY C.Comp_ID, C.Type, C.Year, C.Comp_Name, C.Phone
ORDER BY C.Comp_ID  

0
 
LVL 2

Author Closing Comment

by:TadSter
ID: 31676377
Beautiful! I am ecstatically grateful! So simple and so elegant. Thank you!
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 26299402
You are welcome;)
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

777 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