dbTalk Databases Forums  

Suggestions

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Suggestions in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Faye
 
Posts: n/a

Default Suggestions - 11-05-2007 , 09:49 AM






Hi,

The following SELECT statement works fine and returns what I need as
one of the columns in my query. I have to create more than 10 of the
selection statements like this and then UNION them into a final query.
What I want to do is to have an example to show me how create a
function to pass the column name and the table name to create the
query.

SELECT LN_NR, ROUND(CAST(amountTotal AS DECIMAL(7, 2)) /
positionTotal, 0) AS AverageYearsinPresentPosition
FROM (SELECT SURV_YY_DT, LN_NR, SUM(TOT_PSTN_OFFCR_NR) AS
positionTotal, SUM(TOT_YY_PSTN_NR) AS amountTotal
FROM (SELECT TOP 100 PERCENT
SURV_YY_DT, LN_NR, TOT_PSTN_OFFCR_NR, TOT_YY_PSTN_NR
FROM
dbo.FLT_OFFCR_SLRY_SURV_DTL AS FLT_OFFCR_SLRY_SURV_DTL_1
WHERE
(TOT_YY_PSTN_NR > 0)
ORDER BY LN_NR) AS
presentYears_1
GROUP BY SURV_YY_DT, LN_NR) AS presentYears
WHERE (SURV_YY_DT = '2007')

Thank you.

Faye Larson


Reply With Quote
  #2  
Old   
Roy Harvey (SQL Server MVP)
 
Posts: n/a

Default Re: Suggestions - 11-05-2007 , 10:56 AM






I'm not a big fan of dynamic SQL, which is what you appear to be
asking about, so I'm not a good source of advice on that.

I am a fan of simplified SQL though. I believe the following will
provide the same results as the query posted without any derived
tables or meaningless TOP 100 PERCENT.

SELECT LN_NR,
ROUND(CAST(SUM(TOT_YY_PSTN_NR) AS DECIMAL(7, 2)) /
SUM(TOT_PSTN_OFFCR_NR), 0) AS AverageYearsinPresentPosition
FROM dbo.FLT_OFFCR_SLRY_SURV_DTL AS presentYears_1
WHERE TOT_YY_PSTN_NR > 0
AND SURV_YY_DT = '2007'
GROUP BY LN_NR

Roy Harvey
Beacon Falls, CT

On Mon, 05 Nov 2007 15:49:52 -0000, Faye <fzl20 (AT) yahoo (DOT) com> wrote:

Quote:
Hi,

The following SELECT statement works fine and returns what I need as
one of the columns in my query. I have to create more than 10 of the
selection statements like this and then UNION them into a final query.
What I want to do is to have an example to show me how create a
function to pass the column name and the table name to create the
query.

SELECT LN_NR, ROUND(CAST(amountTotal AS DECIMAL(7, 2)) /
positionTotal, 0) AS AverageYearsinPresentPosition
FROM (SELECT SURV_YY_DT, LN_NR, SUM(TOT_PSTN_OFFCR_NR) AS
positionTotal, SUM(TOT_YY_PSTN_NR) AS amountTotal
FROM (SELECT TOP 100 PERCENT
SURV_YY_DT, LN_NR, TOT_PSTN_OFFCR_NR, TOT_YY_PSTN_NR
FROM
dbo.FLT_OFFCR_SLRY_SURV_DTL AS FLT_OFFCR_SLRY_SURV_DTL_1
WHERE
(TOT_YY_PSTN_NR > 0)
ORDER BY LN_NR) AS
presentYears_1
GROUP BY SURV_YY_DT, LN_NR) AS presentYears
WHERE (SURV_YY_DT = '2007')

Thank you.

Faye Larson

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.