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


case statement


case statement

Author
Message
soldout6000
soldout6000
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 146
I'm using two cases separately to display the result as two columns, but I'm getting the result of only first case.
I have a table with a column member_type_id. This column has two sets of values 11 and 99. I want to group my entire table based on those two values.

SELECT CASE
WHEN MEMBER_TYPE_ID = 11 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END emp_count,
CASE
WHEN MEMBER_TYPE_ID = 99 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END sp_count
FROM main
saravanatn
saravanatn
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2608 Visits: 1221
Can you kindly provide more details along with sample data and also what you mean by I'm getting the result of only first case?

Saravanan

Saravanan
Thom A
Thom A
SSC Guru
SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)

Group: General Forum Members
Points: 82523 Visits: 21130
Expected result set is also important. Have a look at the link in my signature on how to post T-SQL questions.

Thanks.


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
soldout6000
soldout6000
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 146

Create table main
(name varchar,
member_type_id integer)


Following are the values in main table.
Name member_type_id
Sara 11
Jack 11
Jill 99
Mary 99
Anna 99

SELECT CASE
WHEN MEMBER_TYPE_ID = 11 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END emp_count,
CASE
WHEN MEMBER_TYPE_ID = 99 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END sp_count
FROM main

When I run the above query in sql server I get following result
emp_count
2
2
sp_count
3
3
3
However, when I run this query in crystal report, I get the result of only emp_count i.e 2. For sp_count nothing shows. I think the issue is with how crystal is reading the case statement. I don't know if this is the right place for me to ask this question. However, any help will be greatly appreciated. Thanks in advance.
saravanatn
saravanatn
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2608 Visits: 1221
soldout6000 - Sunday, December 24, 2017 8:56 AM

Create table main
(name varchar,
member_type_id integer)


Following are the values in main table.
Name member_type_id
Sara 11
Jack 11
Jill 22
Mary 22
Anna 22

SELECT CASE
WHEN MEMBER_TYPE_ID = 11 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END emp_count,
CASE
WHEN MEMBER_TYPE_ID = 99 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END sp_count
FROM main

When I run the above query in sql server I get following result
emp_count
2
2
sp_count
3
3
3
However, when I run this query in crystal report, I get the result of only emp_count i.e 2. For sp_count nothing shows. I think the issue is with how crystal is reading the case statement. I don't know if this is the right place for me to ask this question. However, any help will be greatly appreciated. Thanks in advance.

You mentioned MEMBER_TYPE_ID = 99 for sp_count column in case statement. However in test data it is member_type_id=22.
Can you kindly confirm which is correct?


Saravanan
soldout6000
soldout6000
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 146
While typing I accidentally put 22 instead of 99. I've edited my numbers.
Thom A
Thom A
SSC Guru
SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)

Group: General Forum Members
Points: 82523 Visits: 21130
Out of interest, why are you using a OVER clause, rather than a GROUP BY?


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
soldout6000
soldout6000
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 146
I have a complex query where I'm grouping by different items. To keep the grouping separate, I'm using partition over by clause.
drew.allen
drew.allen
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64189 Visits: 17050
soldout6000 - Sunday, December 24, 2017 8:56 AM

Create table main
(name varchar,
member_type_id integer)


Following are the values in main table.
Name member_type_id
Sara 11
Jack 11
Jill 99
Mary 99
Anna 99

SELECT CASE
WHEN MEMBER_TYPE_ID = 11 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END emp_count,
CASE
WHEN MEMBER_TYPE_ID = 99 THEN COUNT(MEMBER_TYPE_ID) OVER (PARTITION BY MEMBER_TYPE_ID)
END sp_count
FROM main

When I run the above query in sql server I get following result
emp_count
2
2
sp_count
3
3
3
However, when I run this query in crystal report, I get the result of only emp_count i.e 2. For sp_count nothing shows. I think the issue is with how crystal is reading the case statement. I don't know if this is the right place for me to ask this question. However, any help will be greatly appreciated. Thanks in advance.

What makes you think that the problem is with the CASE expression? Something else in your query may be filtering out the rows for the spouses?

Also, this CANNOT be the results that you are getting when you run this, because you should be getting TWO columns per row and you are only showing ONE column per row.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
sgmunson
sgmunson
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93517 Visits: 7084
soldout6000 - Sunday, December 24, 2017 10:32 AM
I have a complex query where I'm grouping by different items. To keep the grouping separate, I'm using partition over by clause.

Unfortunately, when you try to obfuscate what is considered non-essential parts of a query, you often end up with a totally different issue. I'm fairly certain that there's some other problem with what is being done. I suspect that the Crystal Report has it's own grouping somewhere in the report or something else that is inherent to the report, such as accidentally choosing the same column twice. Or perhaps not realizing that you didn't place one of those two columns in the report, and thus only got one in the results. You have to be sure the second column is actually in the report somewhere, and on the same detail line as the other column (at least most of the time, this is how you typically do that)...


Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
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