[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Assign fields to other names

Posted on 2011-09-21
9
Medium Priority
?
194 Views
Last Modified: 2012-05-12
Hello Experts,

I have 9 separate stored procedures that I would like to combine to just one stored procedure. Each one of the 9 stored procedures perfoms a count function. I would like to add a name to each of the 9 count functions to use later when displaying which count function is for which.

So you will see that the 9 stored procedures below all have "total" which is used to assign the count value to. I need another name to use to describe or list the name of the count funtion.

Is this possible and if so how?


SELECT COUNT(*) AS Total
FROM WellnessChoice_AnnualPhysical

SELECT COUNT(*) AS Total
FROM WellnessChoice_GeneralHealthProgramOne

SELECT COUNT(*) AS Total
FROM WellnessChoice_GeneralHealthProgramThree

SELECT COUNT(*) AS Total
FROM WellnessChoice_GeneralHealthProgramTwo

SELECT COUNT(*) AS Total
FROM WellnessChoice_HealthRiskAssessment

SELECT COUNT(*) AS Total
FROM WellnessChoice_PersonalInfo

SELECT COUNT(*) AS Total
FROM WellnessChoice_PhysicalActivity

SELECT COUNT(*) AS Total
FROM WellnessChoice_Pledge

SELECT COUNT(*) AS Total
FROM WellnessChoice_PreventiveScreenings

Open in new window

0
Comment
Question by:asp_net2
[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
9 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 36574081
maybe I'm misunderstanding what you're asking for,
do you just want to alias them as something different?

for example....

select count(*) as ScreeningTotal
FROM WellnessChoice_PreventiveScreenings
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36574101
Hi sdstuber,

Yes, but I need to add another alias name so that I can use that in my code for the count report..
0
 
LVL 4

Expert Comment

by:jmdl1983
ID: 36574171
select count(*) from table1
union
select count(*) from table2

then each can be called as rstSearch(0), rstSearch(1), etc... where rstSearch is the result of the recordset search
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 4

Author Comment

by:asp_net2
ID: 36574190
Hi jmdl1983,

Ok, but I also need another name for each count. Please see an example as to what I need below.

Table1:
Name of Report 1      Total=8

Table2:
Name of Report 2      Total=22

etc....
0
 
LVL 4

Expert Comment

by:jmdl1983
ID: 36574270
When you do a union, the results are stacked in a single column therefore you cannot have each with a seperate name, however you can reference using their absolute position in the search result.  Example to follow:

SELECT COUNT(*) AS Total FROM WellnessChoice_AnnualPhysical
UNION SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramOne
UNION SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramThree
UNION SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramTwo
UNION SELECT COUNT(*) FROM WellnessChoice_HealthRiskAssessment
UNION SELECT COUNT(*) FROM WellnessChoice_PersonalInfo
UNION SELECT COUNT(*) FROM WellnessChoice_PhysicalActivity
UNION SELECT COUNT(*) FROM WellnessChoice_Pledge
UNION SELECT COUNT(*) FROM WellnessChoice_PreventiveScreenings

Then they may be referenced as:

WellnessChoice_AnnualPhysical = rstSearch(0)
WellnessChoice_GeneralHealthProgramOne = rstSearch(1)
WellnessChoice_GeneralHealthProgramThree = rstSearch(2)
WellnessChoice_GeneralHealthProgramTwo = rstSearch(3)
WellnessChoice_HealthRiskAssessment = rstSearch(4)
WellnessChoice_PersonalInfo = rstSearch(5)
WellnessChoice_PhysicalActivity = rstSearch(6)
WellnessChoice_Pledge = rstSearch(7)
WellnessChoice_PreventiveScreenings = rstSearch(8)
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36574331
@jmdl1983,

Ok, that's not going to work for what I need then. What about adding another name to the following SP below then?

I understand that the Count will be assigned to "total" but is there a way that I can add the PK field and then assign a unique name to the PK?

SELECT COUNT(*) AS Total
FROM WellnessChoice_AnnualPhysical

SOMETHING LIKE THE FOLLOWING BELOW.

SELECT COUNT(*) AS Total, ap_id = Annual Physical Report
FROM WellnessChoice_AnnualPhysical
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 36574370
you can do this
SELECT 
	(SELECT COUNT(*) FROM WellnessChoice_AnnualPhysical) AnnualPhysicalTotal,
	(SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramOne) GeneralHealthProgramOneTotal,
	(SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramThree) GeneralHealthProgramThreeTotal,
	(SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramTwo) GeneralHealthProgramTwoTotal,
	(SELECT COUNT(*) FROM WellnessChoice_HealthRiskAssessment) HealthRiskAssessmentTotal,
	(SELECT COUNT(*) FROM WellnessChoice_PersonalInfo) PersonalInfoTotal,
	(SELECT COUNT(*) FROM WellnessChoice_PhysicalActivity) PhysicalActivityTotal,
	(SELECT COUNT(*) FROM WellnessChoice_Pledge) PledgeTotal,
	(SELECT COUNT(*) FROM WellnessChoice_PreventiveScreenings)PreventiveScreeningsTotal

Open in new window

0
 
LVL 4

Author Comment

by:asp_net2
ID: 36574462
@ewangoya,

Ok, that looks good. BUT, I still need a way to assign a value to each of those so that I know which count is for which table. In other words I need two values. One for the value that will hold the count data and another value to represent the data.
0
 
LVL 4

Expert Comment

by:jmdl1983
ID: 36574602
You can use @ewangoya's solution, and then reference each value by the name ie....

rstSearch("WellnessChoice_AnnualPhysical")
rstSearch("WellnessChoice_GeneralHealthProgramOne")
rstSearch("WellnessChoice_GeneralHealthProgramThree")
rstSearch("WellnessChoice_GeneralHealthProgramTwo")
rstSearch("WellnessChoice_HealthRiskAssessment")
rstSearch("WellnessChoice_PersonalInfo")
rstSearch("WellnessChoice_PhysicalActivity")
rstSearch("WellnessChoice_Pledge")
rstSearch("WellnessChoice_PreventiveScreenings")
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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