Solved

Best way to do query

Posted on 2012-03-20
4
286 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

Industry Leaders: 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

Suggested Solutions

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…

738 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