本文发表在 rolia.net 枫下论坛In my distinct select, record 6 doesn't show. Hmmmmmmmmm.... That means anybody doesn't show up in the party will be...
CREATE TABLE #pvt (VendorID int, Emp1 int, Emp2 int,
Emp3 int, Emp4 int, Emp5 int)
GO
INSERT INTO #pvt VALUES (1,4,3,5,4,4)
INSERT INTO #pvt VALUES (2,4,1,5,5,5)
INSERT INTO #pvt VALUES (3,null,null,null,null,4)
INSERT INTO #pvt VALUES (4,4,2,null,null,4)
INSERT INTO #pvt VALUES (5,5,1,5,5,null)
INSERT INTO #pvt VALUES (6,null,null,null,null,null)
INSERT INTO #pvt VALUES (7,null,0,1,2,4)
GO
select * from #pvt
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM #pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt
GO
select distinct VendorID
from
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM #pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt
select vendorid
from #pvt
group by vendorid
having sum(Emp1)=8
drop table #pvt更多精彩文章及讨论,请光临枫下论坛 rolia.net
CREATE TABLE #pvt (VendorID int, Emp1 int, Emp2 int,
Emp3 int, Emp4 int, Emp5 int)
GO
INSERT INTO #pvt VALUES (1,4,3,5,4,4)
INSERT INTO #pvt VALUES (2,4,1,5,5,5)
INSERT INTO #pvt VALUES (3,null,null,null,null,4)
INSERT INTO #pvt VALUES (4,4,2,null,null,4)
INSERT INTO #pvt VALUES (5,5,1,5,5,null)
INSERT INTO #pvt VALUES (6,null,null,null,null,null)
INSERT INTO #pvt VALUES (7,null,0,1,2,4)
GO
select * from #pvt
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM #pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt
GO
select distinct VendorID
from
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM #pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt
select vendorid
from #pvt
group by vendorid
having sum(Emp1)=8
drop table #pvt更多精彩文章及讨论,请光临枫下论坛 rolia.net