两张表合并统计

两张表:一张存储设备编号、状态;另一张存储设备编号、所属工地。要求:统计出每个工地设备在线和离线的数目
USE [GPSClient]
GO
/****** 对象:  StoredProcedure [dbo].[StatisticsContruction]    脚本日期: 01/13/2016 10:13:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<主页使用,获取全部工地,及在线离线设备数目>
-- =============================================
ALTER PROCEDURE [dbo].[StatisticsContruction]
	-- Add the parameters for the stored procedure here
    @CustomID varchar(10),
    @recCount int=0 output	
    
AS
BEGIN
-------------------------------------------------------------------------

with temp1
as 
(
	select ##realtimedata.clientserial ,##realtimedata.clientStutas,construction 
	from ##realtimedata 
	full join server_synchro_info on 
           ##realtimedata.clientserial = server_synchro_info.clientserial
	where ##realtimedata.defaultTag2='200023' 
	group by construction,construction,##realtimedata.clientStutas,
                 ##realtimedata.clientserial
)

select 
isNULL(construction,'未指定工地砂浆罐') as  'construction'
,
count(
   case clientStutas
   when 1 then '在线'
   end
)as 'OnLineCount'
,
count(
   case clientStutas
   when 0 then '离线'
   end
)as 'OffLineCount'
from temp1
group by construction

set @Reccount = @@ROWCOUNT 

END

测试方法:
exec statisticsContruction '200023'





 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值