首页=>开发=>Access=>如何用数据库的输出来集合并不存在的列 |
|
|
如何用数据库的输出来集合并不存在的列 |
www.51study.net 2004-7-2 20:52 |
【字体:大 中 小】【评论】【打印】 【关闭】 |
|
|
我可以将以下的PETS数据库的输出, 其中将宠物类型分类为家养或野生并得出每组的合计, 我的表中列的标题为AnimalID, AnimalName, AnimalType, AnimalDOB, AnimalVisitDate和 ClientID, 希望得到的输出结果是这样的: PetType NumberofAnimals Household Pets 50 Exotic Pets 15 我知道用COUNT(*)来做集合, 但我不能将在两行的'Household Pets' 和'Exotic Pets'放在一个新的列的标题下, 你能告诉我如何做到吗? 谢谢。 答:如果你有一个PetType表,集合你的AnimalType值转为Pet类型,你可以关联Pets图表, 就可以完成任务了。 PetTypes PetType AnimalType Dog Household Pets Cat Household Pets Snake Exotic Pets Tarantula Exotic Pets 如果你没有这样的表,你还是可以做查寻, 但会比较复杂和难以维护,当发现新的宠物类型时,你将需要不断地修改你的查寻语句。而不是PetTypes表中,然后从下拉单中选择,输入新的宠物。 这里是复杂的的查寻 select case when AnimalType in ( 'Dog', 'Cat' ) then 'Household Pets' when AnimalType in ( 'Snake', 'Tarantula' ) then 'Exotic Pets' else 'Unknown' end as PetType , count(*) as NumberofAnimals from Pets group by case when AnimalType in ( 'Dog', 'Cat' ) then 'Household Pets' when AnimalType in ( 'Snake', 'Tarantula' ) then 'Exotic Pets' else 'Unknown' end |
|
|
【字体:大 中 小】【评论】【打印】 【关闭】 |
|
|
|
|
|