2张表A和B A是主表 关联字段 A:visit_id B:visitId
2表数据如下:
A:
visit_id name text
1 张三 a
2 李四 a
3 王五 b
4 张三 a
B:
visitId typeId
1 65
1 14
2 65
2 14
3 65
3 14
2 14
想要得到结果集:
name visit_id次数 14汇总次数 65汇总次数 text汇总次数
张三 2 1 1 2
李四 1 2 1 1
王五 1 1 1 1
2表数据如下:
A:
visit_id name text
1 张三 a
2 李四 a
3 王五 b
4 张三 a
B:
visitId typeId
1 65
1 14
2 65
2 14
3 65
3 14
2 14
想要得到结果集:
name visit_id次数 14汇总次数 65汇总次数 text汇总次数
张三 2 1 1 2
李四 1 2 1 1
王五 1 1 1 1
解决方案 »
- 在group by 之后的Having里如何求均值
- sql server 中如何查询出所有库中的所有对象
- 提一个登录的问题,请大家帮忙看看
- SQL 2000 创建索引视图 失败 请赐教~
- sql的一个问题
- 急需帮助!!!
- 有没有办法使的在欠入式SQL或者存储过程或触发器里面可以调用系统的存储过程
- 请问一下,我想在表里插入一行
- rs.Open "SELECT distinct * FROM tempdb where year='"&choice&"'", cn, adOpenStatic, adLockOptimistic在VB里面这句就是报错,还望指
- 简单问题,请各位救救我。
- 多个表,多个列转换为1列,并消除空白
- MDF文件加入后,连接不上
with a(
visit_id, name, text)as(
select 1, '张三', 'a' union all
select 2, '李四', 'a' union all
select 3, '王五', 'b' union all
select 4, '张三', 'a'),
B(
visitId, typeId)as(
select 1, 65 union all
select 1, 14 union all
select 2, 65 union all
select 2, 14 union all
select 3, 65 union all
select 3, 14 union all
select 2, 14)
select
name,COUNT(distinct visit_id),
COUNT(case when typeId=14 then 1 else null end),
COUNT(case when typeId=65 then 1 else null end),
(select COUNT(text) from a where name=c.name)
from B full join a c on c.visit_id=B.visitId
group by name