Sql 从t1中选择类别列表,然后从t2中选择sum,t2为每个数据提供了类别列表
我有两张桌子在用 表1有10个类别,条目不变。除非我改变Sql 从t1中选择类别列表,然后从t2中选择sum,t2为每个数据提供了类别列表,sql,oracle,Sql,Oracle,我有两张桌子在用 表1有10个类别,条目不变。除非我改变 Cat_NO | Cause 1 = Animal 2 = Bird 3 = Bear 4 = Dog 5 = Snake 6 = Human 7 = Cow 8 = Car 9 = Fire 10 = Rain 表2显示了事件发生时随机出现的数据。未知数量的记录/数据 有以下字段 ID Cat_Code DateTime
Cat_NO | Cause
1 = Animal
2 = Bird
3 = Bear
4 = Dog
5 = Snake
6 = Human
7 = Cow
8 = Car
9 = Fire
10 = Rain
表2显示了事件发生时随机出现的数据。未知数量的记录/数据
有以下字段
ID Cat_Code DateTime Location OtherField1 OtherField2
786 7 ... ... ... ...
787 6 ... ... ... ...
789 7 ... ... ... ...
791 1 ... ... ... ...
793 3 ... ... ... ...
794 1 ... ... ... ...
796 4 ... ... ... ...
806 9 ... ... ... ...
我正在尝试编写一个查询,它将给出表1中所有类别的总计
结果应该是
CAUSE Total Hours(I can do this) Field2(this too) Field3(This also)
Animal 2
Bird 0
Bear 1
Dog 1
Snake 0
Human 1
Cow 2
Car 0
Fire 1
Rain 0
到目前为止我已经写过了
SELECT
Cause
from Table1
非常感谢,太棒了。
select t1.cause, count(t2.cat_code) as total
from table1 t1 left outer join table2 t2 on t1.cat_no = t2.cat_code
group by t1.cause