如何在sqlserver
中的select语句中获取此虚拟列示例EmpTable
EmpID EmpName
1001 sample1
1002 test1
1003 sample2
1004 test2
1005 sample3
SQL语句Select virCol, EmpID, EmpName from EmpTable Where EmpName Like '%sam%'
示例输出
virCol EmpID EmpName
1 1001 sample1
2 1003 sample2
3 1005 sample3
我尝试了Select virCol = row_number() over (order by EmpID), EmpID, EmpName from EmpTable Where EmpName Like '%sam%'
但我得到了这个输出
virCol EmpID EmpName
1 1001 sample1
3 1003 sample2
5 1005 sample3
用于计算虚拟列的SQL是什么?因为我会进一步使用这样的东西:
Select virCol, EmpID, EmpName from EmpTable Where EmpName Like '%sam%' AND virCol between 1 and 2
有这个输出:
virCol EmpID EmpName
1 1001 sample1
2 1003 sample2
修改
我的实际查询和输出
select * from(
select
Row,
BookTitleID,
BookTitle,
CallNumber,
FullName,
CopiesOnShelves
from
(
select
Book.BookTitleID,
BookTitles.BookTitle,
BookTitles.CallNumber,
FullName = LastName + ', ' + FirstName + ' ' + MiddleName,
CopiesOnShelves = count(case Status when 'OnShelf' then 1 else null end),
Row = row_number() over (order by BookTitle)
From
Book
left outer join
BookTitles
on BookTitles.BookTitleID = Book.BookTitleID
left outer join
Authors
on Authors.AuthorID = BookTitles.AuthorID
Group By Book.BookTitleID, BookTitles.BookTitle, BookTitles.CallNumber,
LastName, FirstName, MiddleName
) sub
) sub2
Where BookTitle like '%some%'
Order By Row
所有已加入的数据与where子句
AuthorsTable
CREATE TABLE [dbo].[Authors](
[AuthorID] [int] IDENTITY(1,1) NOT NULL,
[LastName] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
CONSTRAINT [PK_Authors] PRIMARY KEY CLUSTERED
(
[AuthorID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
BookTable
CREATE TABLE [dbo].[book](
[AccessionNumber] [int] IDENTITY(1,1) NOT NULL,
[BookTitleID] [int] NULL,
[Status] [varchar](50) NULL,
[Barcode] AS ([AccessionNumber]+(100000)),
[DateAcquired] [date] NULL,
CONSTRAINT [PK_book_1] PRIMARY KEY CLUSTERED
(
[AccessionNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
BookTitles表
CREATE TABLE [dbo].[BookTitles](
[BookTitleID] [int] IDENTITY(1,1) NOT NULL,
[BookTitle] [nvarchar](max) NULL,
[CallNumber] [nvarchar](50) NULL,
[AuthorID] [int] NULL,
[YearOfPublication] [smallint] NULL,
[Edition] [nvarchar](50) NULL,
[Publisher] [nvarchar](50) NULL,
[ISBN] [nvarchar](50) NULL,
[Subject1Number] [int] NULL,
[Subject2Number] [int] NULL,
[Copies] [int] NULL,
CONSTRAINT [PK_BookTitles] PRIMARY KEY CLUSTERED
(
[BookTitleID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
作者数据
图书数据
预订标题数据
答案 0 :(得分:1)
MS SQL Server 2008架构设置:
CREATE TABLE EmpTable
([EmpID] int, [EmpName] varchar(7))
;
INSERT INTO EmpTable
([EmpID], [EmpName])
VALUES
(1001, 'sample1'),
(1002, 'test1'),
(1003, 'sample2'),
(1004, 'test2'),
(1005, 'sample3')
;
查询1 :
Select virCol = row_number() over (order by EmpID),
EmpID, EmpName
from (Select EmpID,
EmpName
from EmpTable
Where EmpName Like '%sam%') EmpTable
ORDER BY 1
<强> Results 强>:
| VIRCOL | EMPID | EMPNAME |
|--------|-------|---------|
| 1 | 1001 | sample1 |
| 2 | 1003 | sample2 |
| 3 | 1005 | sample3 |
答案 1 :(得分:0)
在子查询中移动Where BookTitle,例如'%some%',因此在编号之前会过滤查询:
select * from(
select
Row,
BookTitleID,
BookTitle,
CallNumber,
FullName,
CopiesOnShelves
from
(
select
Book.BookTitleID,
BookTitles.BookTitle,
BookTitles.CallNumber,
FullName = LastName + ', ' + FirstName + ' ' + MiddleName,
CopiesOnShelves = count(case Status when 'OnShelf' then 1 else null end),
Row = row_number() over (order by BookTitle)
From
Book
left outer join
BookTitles
on BookTitles.BookTitleID = Book.BookTitleID
left outer join
Authors
on Authors.AuthorID = BookTitles.AuthorID
Where BookTitle like '%some%'
Group By Book.BookTitleID, BookTitles.BookTitle, BookTitles.CallNumber,
LastName, FirstName, MiddleName
) sub
) sub2
Order By Row