Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
pliskin
Starting Member
3 Posts |
Posted - 2011-03-28 : 03:37:40
|
Hi everybody,I need some help here. For example, I have these kind of data :FieldA FieldB VendA W10 VendB W20 VendC W10 VendD W30 VendE W40 VendF W10VendG W50VendH W40VendI W50I want to count the fieldB, but with this condition:W30, W40 and W50 will be count as W30So, the output should be like this :W10 3W20 1W30 5Can anybody help me how to create a sql syntax to produce output like that?Thanks in advance. |
|
Devart
Posting Yak Master
102 Posts |
Posted - 2011-03-28 : 03:57:44
|
Hello,For example:WITH your_table AS(SELECT 'VendA' AS FieldA, 'W10' AS FieldB UNION ALLSELECT 'VendB' AS FieldA, 'W20' AS FieldB UNION ALLSELECT 'VendC' AS FieldA, 'W10' AS FieldBUNION ALLSELECT 'VendD' AS FieldA, 'W30' AS FieldBUNION ALLSELECT 'VendE' AS FieldA, 'W40' AS FieldBUNION ALLSELECT 'VendF' AS FieldA, 'W10' AS FieldBUNION ALLSELECT 'VendG' AS FieldA, 'W50' AS FieldBUNION ALLSELECT 'VendH' AS FieldA, 'W40' AS FieldBUNION ALLSELECT 'VendI' AS FieldA, 'W50' AS FieldB)SELECT FieldB, Count(*)FROM (SELECT CASE WHEN Cast(Replace(FieldB,'W','') AS INT)>=30 THEN 'W30' ELSE FieldB END AS fieldB FROM your_table) tGROUP BY FieldB;Devart,SQL Server Tools:dbForge Data StudiodbForge Schema ComparedbForge Data ComparedbForge SQL Complete |
|
|
pliskin
Starting Member
3 Posts |
Posted - 2011-03-28 : 05:29:32
|
Thanks a lot Devart, your code is work. Now, I can customize it to fulfill my requirment.Once again, thanks a lot. |
|
|
|
|
|
|
|