Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SQL Query

Posted on 2010-01-12
4
Medium Priority
?
327 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

581 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