SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Urgent Query help needed- calculate one field based on other field in SELECT statment


Urgent Query help needed- calculate one field based on other field in SELECT statment

Author
Message
dhananjay.nagarkar
dhananjay.nagarkar
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 185
Hi Friends,
I have reporting TSQL query- in this query within the select statement I have 2 fields 1) Age (working Days) and 2) Age Group(Working days).

the requirement demands that based on the first Select field - Age(working Days) I, display one of these selections:
0 - 29
30 - 59
60 - 89
90 - 119
120 +
for the Second SELECT list field Age Group(Working Days)

Note- the data Type for both is Varchar.


thanks
Dhananjay
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50945 Visits: 38673
dhananjay.nagarkar (4/13/2013)
Hi Friends,
I have reporting TSQL query- in this query within the select statement I have 2 fields 1) Age (working Days) and 2) Age Group(Working days).

the requirement demands that based on the first Select field - Age(working Days) I, display one of these selections:
0 - 29
30 - 59
60 - 89
90 - 119
120 +
for the Second SELECT list field Age Group(Working Days)

Note- the data Type for both is Varchar.


thanks
Dhananjay


Sorry, but there really isn't enough enough information here to help you. Please take the time to read the first article I reference below in my signature block reading asking for help. It will walk you through everything you need to post and how to post it to get the best possible tested answers.

Also, these sounds like homework as I seem to remember seeing a very similar post just a few days ago. If true, we would be more the happy to help you, but besides the other info we need we will need you to show us what you have tried so far.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dhananjay.nagarkar
dhananjay.nagarkar
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 185
No worries, I was able to resolve this Smile

Kind Regards
Dhananjah
J Livingston SQL
J Livingston SQL
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6792 Visits: 36035
for the benefit of others who may in the future read your post...would you care to share your solution.

many thanks

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

dhananjay.nagarkar
dhananjay.nagarkar
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 185
Sure I would love to share-

Here's what I did-
created 3 temp tables with the filelds I need for each with same order, data type.
then UNION the 3 selects.
then in the UNION i calculated the field as below-

SELECT A.*,
CASE
WHEN [Age (Working Days)] IS NULL
THEN NULL -- DO NOT DISLAY THIS FIELD IF AGE WORKING DAYS IS NULL
ELSE -- DISPLAY AGE GROUP WHEN AGE IS PRESENT
CASE
WHEN CAST([Age (Working Days)] AS INT) < 30
THEN '1 - 29'
WHEN CAST([Age (Working Days)] AS INT) < 60
THEN '30 - 59'
WHEN CAST([Age (Working Days)] AS INT) < 90
THEN '60 - 89'
WHEN CAST([Age (Working Days)] AS INT) < 120
THEN '90 - 119'
ELSE'120 +'
END
END AS [Age Group (Working Days)]
FROM (
SELECT * FROM #tmp_JP
UNION
SELECT * FROM #tmp_WO
UNION
SELECT * FROM #tmp_WOR
)A


Thanks
Dhananjay
Robert-378556
Robert-378556
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1914 Visits: 1010
The statement is ok, but using identifiers (column names) with spaces and braces usually brings a lot of troubles.
sqldriver
sqldriver
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1198 Visits: 2525
dhananjay.nagarkar (4/14/2013)
Sure I would love to share-

Here's what I did-
created 3 temp tables with the filelds I need for each with same order, data type.
then UNION the 3 selects.
then in the UNION i calculated the field as below-

SELECT A.*,
CASE
WHEN [Age (Working Days)] IS NULL
THEN NULL -- DO NOT DISLAY THIS FIELD IF AGE WORKING DAYS IS NULL
ELSE -- DISPLAY AGE GROUP WHEN AGE IS PRESENT
CASE
WHEN CAST([Age (Working Days)] AS INT) < 30
THEN '1 - 29'
WHEN CAST([Age (Working Days)] AS INT) < 60
THEN '30 - 59'
WHEN CAST([Age (Working Days)] AS INT) < 90
THEN '60 - 89'
WHEN CAST([Age (Working Days)] AS INT) < 120
THEN '90 - 119'
ELSE'120 +'
END
END AS [Age Group (Working Days)]
FROM (
SELECT * FROM #tmp_JP
UNION
SELECT * FROM #tmp_WO
UNION
SELECT * FROM #tmp_WOR
)A


Thanks
Dhananjay


Doesn't everything get cast as either <120 or Over 120, or NULL? You didn't set any lower limits on your higher numbers?


WHEN CAST([Age (Working Days)] AS INT) < 30
THEN '1 - 29'
WHEN CAST([Age (Working Days)] AS INT) between 30 and 59
THEN '30 - 59'
WHEN CAST([Age (Working Days)] AS INT) between 60 and 89
THEN '60 - 89'
WHEN CAST([Age (Working Days)] AS INT) between 90 and 119
THEN '90 - 119'
ELSE '120 +'


Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50945 Visits: 38673
erikd (4/15/2013)
dhananjay.nagarkar (4/14/2013)
Sure I would love to share-

Here's what I did-
created 3 temp tables with the filelds I need for each with same order, data type.
then UNION the 3 selects.
then in the UNION i calculated the field as below-

SELECT A.*,
CASE
WHEN [Age (Working Days)] IS NULL
THEN NULL -- DO NOT DISLAY THIS FIELD IF AGE WORKING DAYS IS NULL
ELSE -- DISPLAY AGE GROUP WHEN AGE IS PRESENT
CASE
WHEN CAST([Age (Working Days)] AS INT) < 30
THEN '1 - 29'
WHEN CAST([Age (Working Days)] AS INT) < 60
THEN '30 - 59'
WHEN CAST([Age (Working Days)] AS INT) < 90
THEN '60 - 89'
WHEN CAST([Age (Working Days)] AS INT) < 120
THEN '90 - 119'
ELSE'120 +'
END
END AS [Age Group (Working Days)]
FROM (
SELECT * FROM #tmp_JP
UNION
SELECT * FROM #tmp_WO
UNION
SELECT * FROM #tmp_WOR
)A


Thanks
Dhananjay


Doesn't everything get cast as either <120 or Over 120, or NULL? You didn't set any lower limits on your higher numbers?


WHEN CAST([Age (Working Days)] AS INT) < 30
THEN '1 - 29'
WHEN CAST([Age (Working Days)] AS INT) between 30 and 59
THEN '30 - 59'
WHEN CAST([Age (Working Days)] AS INT) between 60 and 89
THEN '60 - 89'
WHEN CAST([Age (Working Days)] AS INT) between 90 and 119
THEN '90 - 119'
ELSE '120 +'



Actually, no. If working days is less than 30 then the first WHEN is satisfied. If working days is less than 60, then the second WHEN is satisfied. If you flipped the CASE and started with the largest value first, then yes you would need the lower bounds defined as well. The CASE stops evaluating when it hits the first successful condition.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sqldriver
sqldriver
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1198 Visits: 2525
That's really cool. Never would have guessed that would work. I had a fairly similar query based on employee size and SIC codes, that I just changed up a bit and it works correctly. I had been using between previously.

Thanks!
dhananjay.nagarkar
dhananjay.nagarkar
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 185
I'm new to TSQL , but per my requirement I used this logic- feel free to suggest me if anything better .
I'm open to suggestions Smile

thanks
Dhananjay
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search