繁体   English   中英

SQL Server 2008查询1行日期转换为列

[英]SQL Server 2008 query 1 row date convert into column

我的查询是

SELECT 
   UB.UnitID as UnitID, UB.Name as Unit, US.UnitID as UnitID, US.Name as Unit,
   UP.UnitID as UnitID, UP.Name as Unit
FROM 
   Product PR 
INNER JOIN
   Unit UB on UB.UnitID = PR.BaseUnitID 
INNER JOIN
   Unit US on US.UnitID = PR.SecondaryUnitID 
INNER JOIN
   Unit UP on P.UnitID = PR.PrimaryUnitID 
where 
   Productid = 1

输出:

BaseUnitID   BaseUnit   SecondaryUnitID    SecondaryUnit   PrimaryUnitID   PrimaryUnit
----------   -------    ----------------   ------------   -------------   ------------
     1       PCS               2           CTN                   3         TRAY

SQL查询转换成这种形式

UnitID    Unit
------    ----
  1       PCS
  2       CTN
  3       TRAY

我不确定这是否是您所需要的,但也许可以为您提供帮助。

SELECT BaseUnitID UnitID, BaseUnit Unit FROM tbl UNION ALL
SELECT SecondaryUnitID, SecondaryUnit FROM tbl UNION ALL
SELECT PrimaryUnitID, PrimaryUnit FROM tbl

鉴于你的问题的编辑版本查询,你可以做你想做使用的是什么orin联接条件:

SELECT U.UnitID as UnitID, U.Name as Unit
FROM Product PR inner join
     Unit U
     on U.UnitID in (PR.BaseUnitID, PR.SecondaryUnitID, PR.PrimaryUnitID)
where Productid = 1;

这是正确的查询

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM