如何使用客户端明智的方式在sqlserver中获取总记录 [英] How to get the total records in sqlserver with client wise

查看:52
本文介绍了如何使用客户端明智的方式在sqlserver中获取总记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

这是拉梅什.这是My Sql Server 2008表

ClientID EmpName代码
1个访问过的
1 B Ptp
1 A Ptp
1个访问过的
1 B造访了
1个访问过的
1 B ptp

我要如何明智地检索客户端,我仅传递客户端ID.

输出

EmpName访问了Ptp
A 3 1
B 1 2

Hi..

This is Ramesh. This is My Sql Server 2008 Table

ClientID EmpName Code
1 A Visited
1 B Ptp
1 A Ptp
1 A Visited
1 B Visited
1 A Visited
1 B ptp

I want to How to retrieve the client wise I am passing only Client Id.

Output

EmpName Visited Ptp
A 3 1
B 1 2

推荐答案

尽管PIVOT语法很简单,但我觉得新手在理解它时会遇到问题.因此,通过您需要的查询为Abhinav的答案添加内容.

Though PIVOT syntax is simple, I feel like newbies faces problem in understanding it. So adding to Abhinav''s answer with the query you need.

--Temp table for holding the test value
DECLARE @ClientVisit TABLE ( ClientID INT, EmpName VARCHAR(50), Code VARCHAR(50))
INSERT INTO @ClientVisit VALUES (1, 'A', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'B', 'Ptp')
INSERT INTO @ClientVisit VALUES (1, 'A', 'Ptp')
INSERT INTO @ClientVisit VALUES (1, 'A', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'B', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'A', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'B', 'ptp')

--Your query
SELECT EmpName, [Visited], [ptp]
FROM
(SELECT EmpName, Code
FROM @ClientVisit) As Souce
PIVOT (COUNT(Code) FOR Code IN ([Visited], [ptp])) As p


使用pivot
Using the pivot command[^] could help you.


这篇关于如何使用客户端明智的方式在sqlserver中获取总记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆