在一行中创建两个服务

时间:2017-11-22 14:15:32

标签: sql sql-server

我正在参加宴会厅预订项目。其中,

我有一张桌子tblServices

serID   serName     serStatus
-----   ---------   ---------
1       Service1    Available
2       Service2    Available
3       Service3    Available
4       Service4    Available
5       Service5    Available

我试图在一行中将它们作为两个服务集来访问,如:

serName     serStatus   serName2    serStatus2
--------    ---------   --------    ---------
Service1    Available   Service2    Available
Service3    Available   Service4    Available
Service5    Available

尝试是:

SELECT serName,serStatus,serName2,serStatus2 FROM tblServices WHERE ???

如何解决这个问题?

非常感谢帮助!

编辑:

我在一个表中存储了基本服务和额外服务,因此额外的服务名称与:extra连接在一起。因此,我无法使用以下答案获得最后一行。

serID   serName           serStatus
-----   --------------    ---------
1       Service1          Available
2       Service2          Available
3       Service3          Available
4       Service4          Available
5       Service5:extra    Available
6       Service6:extra    Available
7       Service7:extra    Available
8       Service8:extra    Available

现在,下面是一个查询,它将仅从上表中选择额外的服务:

SELECT serID, LEFT(serName, CHARINDEX(':', t.serName + ':') - 1) AS serName, serStatus
FROM dbo.tblService AS t
WHERE t.serName LIKE '%:%'

表是:

serID   serName     serStatus
-----   --------    ---------
5       Service5    Available
6       Service6    Available
7       Service7    Available
8       Service8    Available

如何使用此查询以及以下答案的查询?

3 个答案:

答案 0 :(得分:3)

<强> SQL DEMO

SELECT s1.serName ,  s1.serStatus,
       s2.serName as serName2,  s2.serStatus  serStatus2
FROM service s1
LEFT JOIN service s2
  ON s1.serID = s2.serID-1
WHERE s1.serID % 2 = 1  

答案 1 :(得分:1)

你的身份证有差距吗?如果是的话@Juan Carlos Oropeza解决方案将无效。试试这个:

Expression<Func<Item, int>> exp = x => x.Id;
var me = exp.Body as MemberExpression;
var propInfo = me.Member as PropertyInfo;
var value = propInfo.GetValue(myItem, null);

答案 2 :(得分:1)

如果MS SQL 2012及以上,使用LAG / LEAD可以轻松获得此返回,如果serID有间隙,请改用row_number。

CREATE TABLE tblServices
(SerID int NOT NULL,
serName varchar(20),
serStatus varchar(20)
)

INSERT tblServices (serID, serName, serStatus)
values(1,'Service1','Available')
INSERT tblServices (serID, serName, serStatus)
values(2,'Service2','Available')
INSERT tblServices (serID, serName, serStatus)
values(3,'Service3','Available')
INSERT tblServices (serID, serName, serStatus)
values(4,'Service4','Available')
INSERT tblServices (serID, serName, serStatus)
values(5,'Service5:extra','Available')

SELECT * FROM
(
  select serID, serName, serStatus, LEAD(serName) OVER (ORDER BY serID ASC) AS SerName2, LEAD(serStatus) OVER (ORDER BY serID ASC) AS SerStatus2
FROM tblServices
) AS Temp
where serID %2 <>0

enter image description here

更新了新更改的查询,您可以删除:使用子字符串

更容易
SELECT * FROM
(
select SerID, serName,serStatus, LEAD(serName) OVER (ORDER BY serID ASC)     AS SerName2, LEAD(serStatus) OVER (ORDER BY serID ASC) AS SerStatus2
FROM tblServices
) AS Temp
where serID %2 = 1
and serName LIKE '%:%'

enter image description here

服务数据,我在MS SQL 2012上再次运行脚本没有任何问题 enter image description here