Solved

Best way to do query

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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