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 |
mahesh.sanka
Starting Member
18 Posts |
Posted - 2013-05-23 : 02:09:24
|
Hi,I have one table where i am storing the oem details along with the values month wiseOEM(varchar) month(varchar) value(numeric)Avaya Mar-2013 20.65Cisco Feb-2013 0others May-2013 8.02Avaya Feb-2013 0others Feb-2013 10.92Avaya Apr-2013 52.94Cisco Mar-2013 25.61Vernit Mar-2013 19.61others Mar-2013 25.61Cisco Apr-2013 52.94others Apr-2013 52.94To group the results , i have used the following queryselect oem,MONTH,value from OEM_MAIN group by OEM,MONTH,value order by OEM,month The result i am getting isAvaya Mar-2013 20.65Avaya Feb-2013 0Avaya Apr-2013 52.94Cisco Mar-2013 25.61Cisco Feb-2013 0Cisco Apr-2013 52.94others May-2013 8.02others Mar-2013 25.61others Feb-2013 10.92others Apr-2013 52.94Vernit Mar-2013 19.61But i want the o/p likeAvaya Feb-2013 0Avaya Mar-2013 20.65Avaya Apr-2013 52.94Cisco Feb-2013 0Cisco Mar-2013 25.61Cisco Apr-2013 52.94others Feb-2013 10.92others Mar-2013 25.61others Apr-2013 52.94others May-2013 8.02Vernit Mar-2013 19.61what changes i need to make in the query to get above desired resultThanks & Regards,Mahesh Kumar Sanka |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 02:14:31
|
you need to do like thisselect oem,MONTH,value from OEM_MAINorder by OEM,'01-' + [MONTH] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 02:15:13
|
Forgot to add, I dont think you need the GROUP By as you already have data in the same level you want.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mahesh.sanka
Starting Member
18 Posts |
Posted - 2013-05-23 : 02:27:07
|
Hi,Thanks for ur reply. I have used the below mentioned query but i am not getting desired results. Even i have removed the group by conditionquote: Originally posted by visakh16 you need to do like thisselect oem,MONTH,value from OEM_MAINorder by OEM,'01-' + [MONTH] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Thanks & Regards,Mahesh Kumar Sanka |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 02:28:02
|
what about this?select oem,MONTH,value from OEM_MAINorder by OEM,CAST('01-' + [MONTH] AS datetime) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 02:34:30
|
here's the full illustrationdeclare @test table(OEM varchar(30),month varchar(20), value numeric(10,2))insert @testselect 'Avaya','Mar-2013', 20.65 union allselect 'Cisco','Feb-2013' ,0 union allselect 'others','May-2013' ,8.02 union allselect 'Avaya','Feb-2013' ,0 union allselect 'others','Feb-2013' ,10.92 union allselect 'Avaya','Apr-2013' ,52.94 union allselect 'Cisco','Mar-2013' ,25.61 union allselect 'Vernit','Mar-2013' ,19.61 union allselect 'others','Mar-2013' ,25.61 union allselect 'Cisco','Apr-2013' ,52.94 union allselect 'others','Apr-2013' ,52.94 select oem,MONTH,value from @testorder by OEM,CAST('01-' + [MONTH] AS datetime)output------------------------------oem MONTH value------------------------------Avaya Feb-2013 0.00Avaya Mar-2013 20.65Avaya Apr-2013 52.94Cisco Feb-2013 0.00Cisco Mar-2013 25.61Cisco Apr-2013 52.94others Feb-2013 10.92others Mar-2013 25.61others Apr-2013 52.94others May-2013 8.02Vernit Mar-2013 19.61 hope this explains to you why its importatnt to use proper datatype for your fields. Retaining date values as dates would have saved you lot of this unwanted casting effort------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mahesh.sanka
Starting Member
18 Posts |
Posted - 2013-05-23 : 02:35:18
|
Hi, Thank you very much. Problem has solved.quote: Originally posted by visakh16 what about this?select oem,MONTH,value from OEM_MAINorder by OEM,CAST('01-' + [MONTH] AS datetime) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Thanks & Regards,Mahesh Kumar Sanka |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 02:35:53
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|