0

如何在 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 子句的所有连接数据

在此处输入图像描述

作者表

   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]

书桌

     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]

书名表

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]

作者数据

在此处输入图像描述

图书数据

在此处输入图像描述

书名数据

在此处输入图像描述

4

2 回答 2

1

SQL小提琴

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

结果

| VIRCOL | EMPID | EMPNAME |
|--------|-------|---------|
|      1 |  1001 | sample1 |
|      2 |  1003 | sample2 |
|      3 |  1005 | sample3 |
于 2013-09-07T22:38:19.510 回答
0

在子查询中移动您的 Where BookTitle like '%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 
于 2013-09-07T22:30:52.813 回答