Solved

Best way to do query

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Date from a string 4 70
PERFORMANCE OF SQL QUERY 13 75
SQl Agent job fails--SSIS package looses password 6 54
CONVERT date time to a different time zone. 2 60
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 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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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