Solved

Assign fields to other names

Posted on 2011-09-21
9
189 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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