Showing posts with label cast. Show all posts
Showing posts with label cast. Show all posts

Monday, March 26, 2012

RagRe: How to select this ?

SELECT CAST(MONTH(DateCol) AS VARCHAR(2)) + '/' RIGHT(CAST(YEAR(DateCol) AS VARCHAR(2)),2)
SUM(CASE WHEN Work = 'Design' THEN 1 ELSE 0 END) as Design,
SUM(CASE WHEN Work = 'Programming' THEN 1 ELSE 0 END) as Programming
From SomeTable
GROUP BY
CAST(MONTH(DateCol) AS VARCHAR(2)) + '/' RIGHT(CAST(YEAR(DateCol) AS VARCHAR(2)),2)

The above query works fine . but when i try to change to below format :

SUM(CASE WHEN Work = 'Design' THEN 1 ELSE 0 END) as Design

in this query i how to insert this

SUM(CASE WHEN Work = 'Design' THEN (select distinct(act_point) from act_table) where act_id='1000' ELSE 0 END) as Design

when i use this i get the following error.

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

How to solve this problem ?


Raghu:

Is what you are trying to do to add in a value of "the sum of all distinct 'act_point' values" for each 'DESIGN' record?

act_id act_point
-
1001 15
1000 10
1000 10
1002 5
1000 12
1000 7

For this mock-up act_table data you would try to add 10+12+7=19 for each 'DESIGN' record?

Dave

Wednesday, March 7, 2012

quick question

Write a sql that divides 8 by 3 cast 3 as a decimal rounded to 2 places \
???????????
HOW do i do this im confused and new to this!!!At least one of your values must be non-integer, and you must cast your final results to two decimal places of accuracy. Otherwise, SQL Server works with integers by default becuase they can be processed more quickly, and it will thus round off your answer.

All these work:
select cast(8/cast(3 as decimal(10,2)) as decimal(10,2))
select cast(cast(8 as decimal(10,2))/cast(3 as decimal(10,2)) as decimal(10,2))
select cast(8/3.0 as decimal(10,2))

...but this does not:
select cast(8/3 as decimal(10,2))

blindman|||Thanks a lot!!!|||bm: SQL does not choose integers because they are easy to work with...That's funny even to think that way :)

Read BOL on datatype conversion rules!!!|||rdj, you need to give it a rest already.|||Pigeon: I would, but bm never gives up :) Plus, his last answer was really funny, so I couldn't resist :D|||You're welcom, dirtysouthchick!

Glad I could be of help to you.

blindman|||go to bed, bm, tomorrow is just another day :)|||Just curious rdjabarov, why the beef with Blindie?|||Nevermind, I just caught up reading the other posts :)|||I don't have a beef, I just see that sometimes all of us (or some of us) prefer a p***ing contest over the opportunity to hear each other out. And I am as guilty in doing this as the next guy. I just don't want to be a part of a place like this, so I hope that we all re-think the reason why we chose to be here in the first place.

Other than that, - life is good :)