如何在sql的第一行获取一些记录

时间:2016-01-19 11:13:12

标签: sql sql-server select

我有这样的q查询:

 Select  WarehouseCode  from [tbl_VW_Epicor_Warehse]

我的输出看起来像这样

WarehouseCode
Main
Mfg
SP
W01
W02
W03
W04
W05

但有时我想把W04作为第一张唱片,有时我想把W01作为第一张唱片。

如何编写查询以获取第一行中的某些记录? 任何帮助表示赞赏

4 个答案:

答案 0 :(得分:1)

使用参数选择可以根据需要传递给查询的顶行,并按照计算该值是否与参数匹配的列进行排序。类似于以下ORDER BY子句:

DECLARE @Warehouses TABLE (Id INT NOT NULL, Label VARCHAR(3))

INSERT @Warehouses VALUES
 (1,'W01')
,(2,'W02')
,(3,'W03')

DECLARE @TopRow VARCHAR(3) = 'W02'

SELECT *
FROM @Warehouses
ORDER BY CASE Label WHEN @TopRow THEN 1 ELSE 0 END DESC

答案 1 :(得分:0)

由@ankit bajpai评论

您正在寻找CASE使用ORDER BY语句实现的自定义排序

每当你想要WAo4在顶部时,你可以使用

 ORDER BY Case When col = 'W04'  THEN 1 ELSE 2 END
  

以下示例:

Select col from 
(
select 'Main' col  union ALL
select  'Mfg' union ALL
select  'SP' union ALL
select  'W01' union ALL
select  'W02' union ALL
select  'W03' union ALL
select  'W04' union ALL
select  'W05' 
) randomtable 

ORDER BY Case When col = 'W04'  THEN 1 ELSE 2 END

编辑:作为答案后标记

支持@Maha Khairy,因为它被标记为答案,而唯一的答案是DIFFRENT 所有人都在推动OP使用" ORDER by case case"

  

让我们使用UNION ALL APPROCH

create table #testtable (somedata  varchar(10))

insert into #testtable

Select col from 
(
 select  'W05'  col union ALL
select 'Main'   union ALL
select  'Mfg' union ALL
select  'SP' union ALL
select  'W01' union ALL
select  'W02' union ALL
select  'W03' union ALL
select  'W04'

) randomtable 


Select * From #testtable where somedata = 'W04'
Union ALL
Select * From #testtable where somedata <> 'W04'

结果集将数据呈现给网格,如请求OP

idia首先获得等于&#39; W04&#39;的所有行。是然后 不等于&#39; W04&#39;然后结合结果。所以行&#39; W04&#39; 将始终位于顶部,因为它首先在查询中使用,足够公平。

,但这并不是唯一可以使用(自定义排序/排序)的地方 而且是一个重要的表现 是 &#34;案例与#34;永远不会利用KEY但是Union ALL将会更好地探索它更多的测试表 并检查差异

CREATE TABLE  #Orders
(  
    OrderID    integer  NOT NULL IDENTITY(1,1),
    CustID     integer  NOT NULL,
    StoreID    integer  NOT NULL,
    Amount     float    NOT NULL,

    makesrowfat nchar(4000)
);
GO
-- Sample data
WITH  
    Cte0 AS (SELECT 1 AS C UNION ALL SELECT 1), --2 rows  
    Cte1 AS (SELECT 1 AS C FROM Cte0 AS A, Cte0 AS B),--4 rows  
    Cte2 AS (SELECT 1 AS C FROM Cte1 AS A ,Cte1 AS B),--16 rows 
    Cte3 AS (SELECT 1 AS C FROM Cte2 AS A ,Cte2 AS B),--256 rows 
    Cte4 AS (SELECT 1 AS C FROM Cte3 AS A ,Cte3 AS B),--65536 rows 
    Cte5 AS (SELECT 1 AS C FROM Cte4 AS A ,Cte2 AS B),--1048576 rows 
    FinalCte AS (SELECT  ROW_NUMBER() OVER (ORDER BY C) AS Number FROM   Cte5)


INSERT  #Orders 
    (CustID, StoreID, Amount)
SELECT 
    CustID  = Number / 10,
    StoreID = Number % 4,
    Amount  = 1000 * RAND(Number)
FROM FinalCte
WHERE 
    Number <= 1000000;
GO

现在让我们为custid&#34; 93190&#34;

做同样的事情
Create NONclustered Index IX_CustID_Orders ON #Orders (CustID)
INCLUDE (OrderID ,StoreID, Amount ,makesrowfat    )
  

WARM CHACHE RESULTS

SET STATISTICS TIME ON

 DECLARE @OrderID     integer 
 DECLARE @CustID      integer 
 DECLARE @StoreID     integer 
 DECLARE @Amount      float   
 DECLARE @makesrowfat nchar(4000)


Select  @OrderID    =OrderID ,   
        @CustID     =CustID    , 
        @StoreID    =StoreID    ,
        @Amount     =Amount     ,
        @makesrowfat=makesrowfat

        FROM
(
Select * From  #Orders where custid =93190
Union ALL
Select * From  #Orders where custid <>93190
)TLB

**

  

- 经过时间= 2571毫秒。

**

 DECLARE @OrderID     integer 
 DECLARE @CustID      integer 
 DECLARE @StoreID     integer 
 DECLARE @Amount      float   
 DECLARE @makesrowfat nchar(4000)

 Select  @OrderID    =OrderID ,   
        @CustID     =CustID    , 
        @StoreID    =StoreID    ,
        @Amount     =Amount     ,
        @makesrowfat=makesrowfat

        From  #Orders

 ORDER BY Case When custid = 93190  THEN 1 ELSE 2 END
  

经过时间= 70616毫秒

**

  

UNION ALL性能2571毫秒。 ORDER BY CASE表现   70616 ms

**

UNION ALL是一个明显的赢家ORDER BY在性能上并不在附近

但是我们忘记了SQL是声明性语言,   我们无法直接控制sql如何获取数据,有一个软件代码(随着版本的变化而变化)   之间   用户和SQL Server数据库引擎,它是SQL SERVER OPTIMIZER,它被编码以获取数据集   由USER指定,它有责任以最少的资源获取数据。所以有机会   在指定ORDER BY

之前,您不会按顺序获得结果

其他一些参考文献:

<强> @Damien_The_Unbeliever   Why would anyone offer an ordering guarantee except when an ORDER BY clause is included? - there's an obvious opportunity for parallelism (if sufficient resources are available) to compute each result set in parallel and serve each result row (from the parallel queries) to the client in whatever order each individual result row becomes available. –

Conor Cunningham:   If you need order in your query results, put in an ORDER BY. It's that simple. Anything else is like riding in a car without a seatbelt.

所有评论和编辑都是欢迎

答案 2 :(得分:0)

您可以尝试选择包含您想要首先显示的代码的行,方法是指定where条件以单独选择该行,然后您可以将所有其他选择与所有其他没有此名称的行合并

如下

SELECT WarehouseCode FROM Table WHERE WarehouseCode ='W04'
UNION ALL 
SELECT WarehouseCode FROM Table WHERE WarehouseCode <>'W04'

答案 3 :(得分:0)

您可能不需要将此列表存储在表格中吗?你想要这样的东西吗?

SELECT * FROM (VALUES ('WarehouseCode'),
                        ('Main'),
                        ('Mfg'),
                        ('SP'),
                        ('W01'),
                        ('W02'),
                        ('W03'),
                        ('W04'),
                        ('W05')) as v(s)

您可以在此处手动更改订单。