[英]How to make dynamic pivot for this table when I pass only one parameter @did int?
I have two tables, @DETAILS
and @CONTACTDETAILS
. 我有两个表,
@DETAILS
和@CONTACTDETAILS
。 In those two tables, @DETAILS
is a static table in which contactid
will be the primary key, and it will be the foreign key for the @CONTACTDETAILS
在这两个表中,
@DETAILS
是一个静态表,其中contactid
将是主键,它将是@CONTACTDETAILS
的外键
Here are the tables with data: 以下是包含数据的表格:
DECLARE @DETAILS TABLE (CONTACTID INT IDENTITY(1,1), VALUE VARCHAR(200))
INSERT INTO @DETAILS
SELECT 'PHONENUMBER' UNION ALL
SELECT 'MOBILENUMBER' UNION ALL
SELECT 'ADDRESS1' UNION ALL
SELECT 'ADDRESS2' UNION ALL
SELECT 'EMPLOYEENAME' UNION ALL
SELECT 'EMPLOYEEID'
DECLARE @CONTACTDETAILS TABLE (DETAILID INT IDENTITY(1,1),
DID INT,
CONTACTID INT,
DETAILSVALUE VARCHAR(200)
)
INSERT INTO @CONTACTDETAILS
SELECT 1,5,'SIVA' UNION ALL
SELECT 1,6,'SIA001' UNION ALL
SELECT 1,2,'9344139991' UNION ALL
SELECT 2,1,'245678' UNION ALL
SELECT 2,2,'1686597' UNION ALL
SELECT 2,5,'GANESH' UNION ALL
SELECT 1,3,'ST-ROAD'
SELECT * FROM @DETAILS
The output from this is: 这个输出是:
CONTACTID VALUE
----------------------
1 PHONENUMBER
2 MOBILENUMBER
3 ADDRESS1
4 ADDRESS2
5 EMPLOYEENAME
6 EMPLOYEEID
SELECT * FROM @CONTACTDETAILS
The output for this second query is: 第二个查询的输出是:
DETAILID DID CONTACTID DETAILSVALUE
-----------------------------------
1 1 6 SIVA
2 1 5 SIA001
3 1 2 9344139991
4 2 1 245678
5 2 2 1686597
6 2 5 GANESH
7 1 3 ST-ROAD
In the @CONTACTDETAILS
column I have a value of the DID
column which I used to show one particular employee detail in my stored procedure I am using 在
@CONTACTDETAILS
列中,我有一个DID
列的值,我用它来显示我正在使用的存储过程中的一个特定员工详细信息
Parameter @DID int
- I just tried this query 参数
@DID int
- 我刚试过这个查询
declare @DID int=1
SELECT
(SELECT VALUE FROM @DETAILS WHERE CONTACTID=A.CONTACTID)AS CONTACTID,
A.DETAILSVALUE
FROM
@CONTACTDETAILS A
WHERE
A.DID=@DID
which will show the result like this 这将显示结果
CONTACTID DETAILSVALUE
EMPLOYEEID SIVA
EMPLOYEENAME SIA001
MOBILENUMBER 9344139991
ADDRESS1 ST-ROAD
and now i am trying output like this 现在我正在尝试这样的输出
EMPLOYEEID EMPLOYEENAME MOBILENUMBER ADDRESS1
SIA001 SIVA 9344139991 ST-ROAD
can any one plz help me 任何人都可以帮助我
The basic set up if you know the value ahead of time will be: 如果您提前知道价值的基本设置将是:
select *
from
(
select c.did,
d.value,
c.detailsvalue
from DETAILS d
inner join CONTACTDETAILS c
on d.CONTACTID = c.CONTACTID
) src
pivot
(
max(detailsvalue)
for value in (EMPLOYEEID, EMPLOYEENAME, ADDRESS1,
MOBILENUMBER, PHONENUMBER)
) piv;
See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo
The above gets you all of the values but if you want to implement dynamic SQL to filter the result based on the @did
, then you will use: 上面的内容为您提供了所有值,但如果您想实现动态SQL来根据
@did
过滤结果,那么您将使用:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@did int
set @did = 1
select @cols = STUFF((SELECT ',' + QUOTENAME(d.value)
from DETAILS d
inner join CONTACTDETAILS c
on d.CONTACTID = c.CONTACTID
where c.did = @did
group by d.value, c.DETAILID
order by c.DETAILID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ' + @cols + '
from
(
select
d.value,
c.detailsvalue
from DETAILS d
inner join CONTACTDETAILS c
on d.CONTACTID = c.CONTACTID
where c.did = '+cast(@did as varchar(10))+'
) x
pivot
(
max(detailsvalue)
for value in (' + @cols + ')
) p '
execute(@query);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.