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 |
Alain_TV
Starting Member
12 Posts |
Posted - 2013-11-03 : 02:47:22
|
Hi All,I'm starting to use SQL 2008 recently, and I'm just having trouble with the following problem:The following query:SELECT t_Category.Name as [Category]FROM t_Assets, t_Category, t_Priority, t_Location, t_User_AssetsWHERE t_Assets.Asset_ID = t_User_Assets.Asset_IDAND t_Category.Category_ID = t_User_Assets.Category_IDAND t_Priority.Priority_ID = t_User_Assets.Priority_IDAND t_Location.Location_ID = t_User_Assets.Location_IDReturns this result:CategoryBMSBMSWaterBMSBMSAirAnd the following query:SELECT COUNT(t_Category.Category_ID) AS AssetQtyFROM t_Assets, t_Category, t_Priority, t_Location, t_User_AssetsWHERE t_Assets.Asset_ID = t_User_Assets.Asset_IDAND t_Category.Category_ID = t_User_Assets.Category_IDAND t_Priority.Priority_ID = t_User_Assets.Priority_IDAND t_Location.Location_ID = t_User_Assets.Location_IDGROUP BY t_Category.Category_IDReturns this result:AssetQty411I need to have both of those results returned, as a single result. Such as:Category AssetQtyBMS 4WATER 1AIR 1However, I'm not able to, due to the fact, that if I add the "t_Category.Category.Name" in the SELECT clause, it gives me the following error:Column 't_Category.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.And if I try to use the "Name" as part of the count clause, it won't work, as text are not acceptable data types for aggregations.I'm sure, it is something simple, but is driving me crazy.Any help is appreciated.Regards,Alain |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-03 : 03:03:28
|
[code]SELECT t_Category.Name,COUNT(t_Category.Category_ID) AS AssetQtyFROM t_Assets, t_Category, t_Priority, t_Location, t_User_AssetsWHERE t_Assets.Asset_ID = t_User_Assets.Asset_IDAND t_Category.Category_ID = t_User_Assets.Category_IDAND t_Priority.Priority_ID = t_User_Assets.Priority_IDAND t_Location.Location_ID = t_User_Assets.Location_IDGROUP BY t_Category.Name[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Alain_TV
Starting Member
12 Posts |
Posted - 2013-11-03 : 03:11:05
|
quote: Originally posted by visakh16
SELECT t_Category.Name,COUNT(t_Category.Category_ID) AS AssetQtyFROM t_Assets, t_Category, t_Priority, t_Location, t_User_AssetsWHERE t_Assets.Asset_ID = t_User_Assets.Asset_IDAND t_Category.Category_ID = t_User_Assets.Category_IDAND t_Priority.Priority_ID = t_User_Assets.Priority_IDAND t_Location.Location_ID = t_User_Assets.Location_IDGROUP BY t_Category.Name ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Hi visakh16,Thanks for the reply. Unfortunately, this option does not work, as per my comments above.I get the following error, from your code:The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operatorRegards,Alain |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-03 : 03:16:51
|
ah missed that last sentence. Still if its sql 2008 you can use thisSELECT CAST(t_Category.Name AS varchar(max)) AS CategoryName,COUNT(t_Category.Category_ID) AS AssetQtyFROM t_Assets, t_Category, t_Priority, t_Location, t_User_AssetsWHERE t_Assets.Asset_ID = t_User_Assets.Asset_IDAND t_Category.Category_ID = t_User_Assets.Category_IDAND t_Priority.Priority_ID = t_User_Assets.Priority_IDAND t_Location.Location_ID = t_User_Assets.Location_IDGROUP BY CAST(t_Category.Name AS varchar(max)) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Alain_TV
Starting Member
12 Posts |
Posted - 2013-11-03 : 03:33:55
|
quote: Originally posted by visakh16 ah missed that last sentence. Still if its sql 2008 you can use thisSELECT CAST(t_Category.Name AS varchar(max)) AS CategoryName,COUNT(t_Category.Category_ID) AS AssetQtyFROM t_Assets, t_Category, t_Priority, t_Location, t_User_AssetsWHERE t_Assets.Asset_ID = t_User_Assets.Asset_IDAND t_Category.Category_ID = t_User_Assets.Category_IDAND t_Priority.Priority_ID = t_User_Assets.Priority_IDAND t_Location.Location_ID = t_User_Assets.Location_IDGROUP BY CAST(t_Category.Name AS varchar(max)) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Thanks a lot, that actually worked like a treat!!Do you mind explaining something...?I get that, you are casting the name field, to get around the issue with SQL not accepting text fields as part of an aggregate function. Should I be changing those fields to VarChar instead of Text fields? I was reading that should not be used anymore. I guess, there would be not need for casting.Thanks again for your help.Regards,Alain |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-03 : 04:13:09
|
yes.. Indeedtext,ntext etc are deprecated so you should be using varchar(max),nvarchar(max) etc instead of them in newer versions above 2005------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|