Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Urgent Query help needed- calculate one field based on other field in SELECT statment Expand / Collapse
Author
Message
Posted Saturday, April 13, 2013 3:55 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, April 10, 2014 1:38 PM
Points: 90, Visits: 181
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
Post #1442057
Posted Saturday, April 13, 2013 4:46 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:25 PM
Points: 23,215, Visits: 31,896
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.



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)
Post #1442065
Posted Sunday, April 14, 2013 2:01 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, April 10, 2014 1:38 PM
Points: 90, Visits: 181
No worries, I was able to resolve this :)

Kind Regards
Dhananjah
Post #1442130
Posted Sunday, April 14, 2013 2:18 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:36 PM
Points: 1,899, Visits: 18,893
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 !
__________________________________________________________________
Post #1442133
Posted Sunday, April 14, 2013 8:51 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, April 10, 2014 1:38 PM
Points: 90, Visits: 181
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
Post #1442155
Posted Monday, April 15, 2013 9:40 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 10, 2014 6:56 AM
Points: 1,176, Visits: 878
The statement is ok, but using identifiers (column names) with spaces and braces usually brings a lot of troubles.
Post #1442374
Posted Monday, April 15, 2013 3:32 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 12:17 PM
Points: 404, Visits: 1,610
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 +'

Post #1442509
Posted Monday, April 15, 2013 3:39 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:25 PM
Points: 23,215, Visits: 31,896
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.



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)
Post #1442513
Posted Monday, April 15, 2013 3:52 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 12:17 PM
Points: 404, Visits: 1,610
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!
Post #1442519
Posted Tuesday, April 16, 2013 4:16 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, April 10, 2014 1:38 PM
Points: 90, Visits: 181
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 :)

thanks
Dhananjay
Post #1442669
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse