troubleshooting Question

SQL Sub Query

Avatar of Aaron Greene
Aaron GreeneFlag for United States of America asked on
Microsoft SQL Server 2005
6 Comments1 Solution466 ViewsLast Modified:
I am trying to write a sql view that will give me a summary of service meters by type per route.  Currently I have the information in three separate views, but I'm thinking that I can do it in a single view.  My first view gives the total number of meter serial numbers by route.

SELECT     TOP (100) PERCENT dbo.vData_Meter_Data.METER_REGION + '-' + dbo.vData_Meter_Data.METER_ROUTE AS RRC,
                      COUNT(dbo.vData_Meter_Data.METER_SERIAL) AS COUNT
FROM         dbo.vData_Max_Account_String INNER JOIN
                      dbo.vData_Meter_Data ON dbo.vData_Max_Account_String.ACCOUNT = dbo.vData_Meter_Data.ACCOUNT_ID
GROUP BY dbo.vData_Meter_Data.METER_REGION + '-' + dbo.vData_Meter_Data.METER_ROUTE
ORDER BY RRC

The second view displays the number of automated meters by route.
SELECT     TOP (100) PERCENT dbo.vData_Meter_Data.METER_REGION + '-' + dbo.vData_Meter_Data.METER_ROUTE AS RRC,
                      dbo.vData_Meter_Data.METER_TYPE AS TYPE, COUNT(dbo.vData_Meter_Data.METER_SERIAL) AS COUNT
FROM         dbo.vData_Max_Account_String INNER JOIN
                      dbo.vData_Meter_Data ON dbo.vData_Max_Account_String.ACCOUNT = dbo.vData_Meter_Data.ACCOUNT_ID
GROUP BY dbo.vData_Meter_Data.METER_REGION + '-' + dbo.vData_Meter_Data.METER_ROUTE, dbo.vData_Meter_Data.METER_TYPE
HAVING      (dbo.vData_Meter_Data.METER_TYPE = 'AUTOMATED')
ORDER BY RRC

Is there a way to combine the two using a sub query?
ASKER CERTIFIED SOLUTION
Daniel Wilson

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros