首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎样将这两个查询结果 full jion起来?(当两个查询结果中的 UID和ProjectID都相等时 则进行合并)解决办法

2012-04-08 
怎样将这两个查询结果 full jion起来?(当两个查询结果中的 UID和ProjectID都相等时 则进行合并)第一个查询

怎样将这两个查询结果 full jion起来?(当两个查询结果中的 UID和ProjectID都相等时 则进行合并)
第一个查询结果

SQL code
SELECT UID ,ProjectID,      sum([LocalValidCount]) as 本地预约量      ,sum([NoLocalValidCount]) as 外地预约量  FROM DialogueQuantitygroup by  UID,ProjectID


第二个查询结果
SQL code
select   UID,ProjectID,   count(1) as 患者总数,   sum(case IsNative when 'true' then 1 else 0 end) as '本地患者数量',   sum(case IsNative when 'false' then 1 else 0 end) as '外地患者数量',   sum(case DiseaseName when '感冒' then 1 else 0 end) as '感冒',   sum(case DiseaseName when '发烧' then 1 else 0 end) as '发烧',   sum(case DiseaseName when '阑尾炎' then 1 else 0 end) as '阑尾炎'



注:
要full jion起来
合并条件是:两个查询结果中的 UID和ProjectID都相等时 进行合并

[解决办法]
SQL code
select * from(SELECT UID ,ProjectID,      sum([LocalValidCount]) as 本地预约量      ,sum([NoLocalValidCount]) as 外地预约量  FROM DialogueQuantitygroup by  UID,ProjectI) as afull join (select   UID,ProjectID,   count(1) as 患者总数,   sum(case IsNative when 'true' then 1 else 0 end) as '本地患者数量',   sum(case IsNative when 'false' then 1 else 0 end) as '外地患者数量',   sum(case DiseaseName when '感冒' then 1 else 0 end) as '感冒',   sum(case DiseaseName when '发烧' then 1 else 0 end) as '发烧',   sum(case DiseaseName when '阑尾炎' then 1 else 0 end) as '阑尾炎') as bon a.UID=b.UID and a.ProjectID=b.ProjectID
[解决办法]
直接写 select * from
(
SELECT UID ,ProjectID,
sum([LocalValidCount]) as 本地预约量
,sum([NoLocalValidCount]) as 外地预约量
FROM DialogueQuantity
group by
UID,ProjectI
) as a
full join 
(
select
UID,ProjectID,
count(1) as 患者总数,
sum(case IsNative when 'true' then 1 else 0 end) as '本地患者数量',
sum(case IsNative when 'false' then 1 else 0 end) as '外地患者数量',
sum(case DiseaseName when '感冒' then 1 else 0 end) as '感冒',
sum(case DiseaseName when '发烧' then 1 else 0 end) as '发烧',
sum(case DiseaseName when '阑尾炎' then 1 else 0 end) as '阑尾炎'
) as b
on a.UID=b.UID and a.ProjectID=b.ProjectID
[解决办法]
SQL code
-- 方法1select * from ([子查询1]) afull join ([子查询2]) b-- 方法2with a as([子查询1]),b as([子查询2])select *from a full join b
[解决办法]
SQL code
select * from (SELECT UID ,ProjectID,      sum([LocalValidCount]) as 本地预约量      ,sum([NoLocalValidCount]) as 外地预约量  FROM DialogueQuantitygroup by  UID,ProjectID)a full join (select   UID,ProjectID,   count(1) as 患者总数,   sum(case IsNative when 'true' then 1 else 0 end) as '本地患者数量',   sum(case IsNative when 'false' then 1 else 0 end) as '外地患者数量',   sum(case DiseaseName when '感冒' then 1 else 0 end) as '感冒',   sum(case DiseaseName when '发烧' then 1 else 0 end) as '发烧',   sum(case DiseaseName when '阑尾炎' then 1 else 0 end) as '阑尾炎') b on a.UID=b.UID and a.ProjectID and b.ProjectID 

热点排行