Solved

Best way to do query

Posted on 2012-03-20
4
281 Views
Last Modified: 2012-03-20
What is the best way to do this?

I have a query which groups and counts data. I also need to 'filter' that same data so I can get a percentage between the two.

For instance, query1 would return the total:

Group        Count
Data            95
System       426
Mkt           1560
Sales          264

I then need query1 to be the filtered data:
Group        Count
Data            13
System        17
Mkt             12
Sales            1

So the final results would something like
Data            14%
System        4%
Mkt             1%
Sales            1%
0
Comment
Question by:edrz01
[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
  • 2
4 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 37743303
Select Q1.Group, Q1.Count * 100 / Q2.Count From
(Select Group, Count... this is Query1) Q1
inner join (Select Group, Count... this is Query1) Q2 on Q1.Group = Q2.Group
0
 

Author Comment

by:edrz01
ID: 37743601
amit_g,

Thanks, that worked like a charm.

just one follow up question, I see from my previous results that I might have some Q1 (All) that might not show up because they had nothing in Q2. How can I account for those as well?

For example Q1:
Group        Count
Data            95
System       426
Mkt           1560
Sales          264
Misc           122
Other          97

I then need Q2 to be the filtered data:
Group        Count
Data            13
System        17
Mkt             12
Sales            1

So the final results would something like
Data            14%
System        4%
Mkt             1%
Sales            1%
Misc           No Data <--- Did not show up in Q2
Other         No Data <--- Did not show up in Q2
0
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 37743631
Use Left Outer Join instead of Inner Join
Select Q1.Group, Q1.Count * 100 / Q2.Count From
(Select Group, Count... this is Query1) Q1
left outer join (Select Group, Count... this is Query1) Q2 on Q1.Group = Q2.Group

Open in new window

0
 

Author Closing Comment

by:edrz01
ID: 37743651
Great solution! Worked as expected and I appreciate the rapid response. Thank you!
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

Suggested Solutions

Title # Comments Views Activity
Grid querry results 41 82
Gettg error - Please help Msg 252, Level 16, State 1, Line 1 3 33
Addition to SQL for dynamic fields 6 56
Stored Procedure needs owner to execute 5 44
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…

735 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