| Author |
Topic |
|
jbuttery
Starting Member
4 Posts |
Posted - 2008-02-01 : 15:03:08
|
| I can't seem to get my arms around this. The 1st query works, but whwn I try to use results it fails. I thought varchar to int was implicit.Use ItemsDBSelect DataID, Count(DataID) '# Items' from ItemsTablewhere ItemFlag = 2 Group by DataIDorder by '# Items' descDataID # Items126351 219257618 164053968 120260068 55614618 53857832 50231446 38257795 36158728 304This fails with the error.Use ItemsDBSelect DataID, Count(DataID) '# Items' from ItemsTablewhere ItemFlag = 2 and '# Items' >= 20Group by DataIDorder by '# Items' descConversion failed when converting the varchar value '# Items' to data type int. |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2008-02-01 : 15:21:48
|
| Try this:Select DataID, Count(DataID) '# Items' from ItemsTablewhere ItemFlag = 2 and Count(DataID) >= 20Group by DataIDorder by '# Items' descYour example is getting the error because it's trying to compare the string '# Items' to the number 20 and not the actual number of items to the number 20. In other words it's trying to compare text to an int. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-01 : 23:29:10
|
| You cant use aliases in WHERE conditions. You need to use actual values there. Alises can be used only in ORDER BY clause. |
 |
|
|
jbuttery
Starting Member
4 Posts |
Posted - 2008-02-04 : 12:51:24
|
| You also can't use aggregates in a WHERE clauseAn aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.The following works without syntax errors:Use ItemsDBSelect DataID, Count(DataID) '# Items' from ItemsTablewhere ItemFlag = 2 Group by DataIDhaving Cast(Count(DataID) AS int) >= 20order by '# Items' descBut I need to use the int result from the cast statement for an update operation. I think I need to use the Select in a sub-query. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-04 : 12:55:29
|
| Exactly,it will be likeUPDATE t1SET t1.Field=t2.valueFROM Table1 t1INNER JOIN (your query batch here)t2ON t2.matchingfield=t1.matchingfieldremember to use aliases for all columns used in subquery. |
 |
|
|
thedryden
Starting Member
23 Posts |
Posted - 2008-02-04 : 12:56:39
|
The following would also work:Select DataID , # ItemsFrom (SELECT DataID , Count(DataID) '# Items' FROM ItemsTableWHERE ItemFlag = 2) aWHERE # Items >= 20 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-05 : 01:31:42
|
A COUNT() can only return an integer. Not even NULL.SELECT DataID, COUNT(DataID) AS [# Items]FROM ItemsTableWHERE ItemFlag = 2 GROUP BY DataIDHAVING COUNT(DataID) >= 20ORDER BY COUNT(DataID) DESC E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-05 : 08:18:47
|
quote: Originally posted by jbuttery I can't seem to get my arms around this. The 1st query works, but whwn I try to use results it fails. I thought varchar to int was implicit.Use ItemsDBSelect DataID, Count(DataID) '# Items' from ItemsTablewhere ItemFlag = 2 Group by DataIDorder by '# Items' descDataID # Items126351 219257618 164053968 120260068 55614618 53857832 50231446 38257795 36158728 304This fails with the error.Use ItemsDBSelect DataID, Count(DataID) '# Items' from ItemsTablewhere ItemFlag = 2 and '# Items' >= 20Group by DataIDorder by '# Items' descConversion failed when converting the varchar value '# Items' to data type int.
Avoid having single quotes around the alias names. Use proper alias names like no_of_itemsMadhivananFailing to plan is Planning to fail |
 |
|
|
jbuttery
Starting Member
4 Posts |
Posted - 2008-02-05 : 12:01:03
|
| Thanks for the help everyone. I'll play with those and post back. |
 |
|
|
jbuttery
Starting Member
4 Posts |
Posted - 2008-02-06 : 12:53:46
|
| I thought I had tried an IN sub-query as I'm using two different DB providers, but this is what finally did it.USE ItemsDBUpdate ItemsTable set ItemFlag = 0where DataID in (Select DataID from ItemsTableGroup by DataID having Cast(Count(DataID) AS int) <= 4 and ItemFlag = 2) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-06 : 13:18:02
|
| can use join also:-USE ItemsDBUpdate itset it.ItemFlag = 0FROM ItemsTable itINNER JOIN(Select DataID from ItemsTableGroup by DataID having Cast(Count(DataID) AS int) <= 4 and ItemFlag = 2)tON t.DataID =it.DataID |
 |
|
|
|