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]

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]

著者データ

ここに画像の説明を入力

ブックデータ

ここに画像の説明を入力

書名データ

ここに画像の説明を入力

4

2 に答える 2

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 
于 2013-09-07T22:30:52.813 に答える