Quantcast
Channel: SQL.ru: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 7251

Создание корректной сортивровки по определенному значению

$
0
0
Добрый день.
Написал запрос.
Суть такова,что он выводит список ячеек по определенной сортировке.
  declare @material_id int =69887
  declare @MaterialUnit_id int =225693
  declare @BaseQuantity decimal(25,6)=1200.000000
  declare @SourceObject_id int =17310488
  declare @TechnoZone_id int =178
  declare @MaterialAccessGroup_id int =null
  declare @AllocationParameter_id int =null
  declare @IsPeace int =1

  select @MaterialAccessGroup_id = 
	case when @IsPeace = 1
	 then isnull(PCE_MaterialAccessGroup_id,isnull(MaterialAccessGroup_id,4))
											else isnull(MaterialAccessGroup_id,4) end,
		   @AllocationParameter_id = case @IsPeace /*@ComplectationArea_id*/ when 1 then AllocationParametersZSHO_id else AllocationParametersZKO_id end
	from Materials with (nolock) 
	where tid = @Material_id
  
   declare @Result table (
		tid int identity primary key,
		Location_id int not NULL,
		PutawayBaseQuantity decimal(25,6) NULL,
		IsEmpty int NULL,
		MaterialCount int NULL,
		FreedomBaseQuantity decimal(25,6)/*,
		SearchPriority int null */)
   insert into @Result(Location_id,PutawayBaseQuantity,IsEmpty,MaterialCount,FreedomBaseQuantity /*, SearchPriority */)          
   exec app_GetPutawayObjectList          
     @SourceObject_id = @SourceObject_id,          
     @TechnoZone_id = @TechnoZone_id,          
     @Material_id = @material_id,          
     @MaterialSeries_id = NULL,          
     @BaseQuantity = @BaseQuantity,       
     @MaterialUnit_id = @MaterialUnit_id,          
     @StockType_id = 7,          
     @StockCategory_id = 0,          
     @OwnerDebtor_id = 61330,          
     @DiscountCategory_id = NULL,          
     @OwnerBatch = NULL,          
     @ExpirationDate = '2999-12-31 00:00:00.000',
     @IsPeace = 1   


	 select  lo.LocationName as [Имя яч]
	 
	,b.NameRU as [Пар размещ]
	,@MaterialAccessGroup_id as [Шт гр об ABC  - AB]
	--,@AllocationParameter_id
	,las.NameRU as [группа скорости доступа до ячейки],
	b.tid
	,lo.PickingPriority as [Приорит обх]
	,lo.LocationZ as [Ряд]
	,tz.NameRU
	,tz.tid
	,replace(right(left(lo.LocationName ,7),3),'-','') as [обрезок]

	 ,a.*
	 from @Result a
	left  join locations lo on lo.tid=a.Location_id
  join LocationAllocationParameters b on b.tid=lo.LocationAllocationGroup_id
  join LocationAccessSpeed las on las.tid=lo.LocationAccessSpeed_id
  left join technozones tz on tz.tid=lo.TechnoZone_id
  --1-й приоритет по пар размещ
	 order by b.tid , 
	las.NameRU asc ,
	--  
	---2-й по abc
	--las.tid,

--3-й по ряду(Для M22 ASC,для M21 DESC)

case when tz.tid=207 then replace(right(left(lo.LocationName ,7),3),'-','') 
 
 end asc,
 case when tz.tid=178 then replace(right(left(lo.LocationName ,7),3),'-','') 
 
 end desc,
  case when tz.tid=208 then replace(right(left(lo.LocationName ,7),3),'-','') 
 
 end desc,
   case when tz.tid=209 then replace(right(left(lo.LocationName ,7),3),'-','') 
 end asc



 

Viewing all articles
Browse latest Browse all 7251

Trending Articles