SELECT ticket_number, some_id
FROM (
SELECT ticket_number, some_id,
rank() OVER (PARTITION BY ticket_number ORDER BY some_id DESC) RnkDESK,
rank() OVER (PARTITION BY ticket_number ORDER BY some_id ASC) RnkASC
FROM table ) x
WHERE X.RnkDESK = 1 OR X.RnkASC = 1
SELECT
FROM TABLE
inner join TABLE2 ON ...
inner join TABLE3 ON ...
SELECT * FROM
[OTD_db].[dbo].[DocumentsA] As [CurStatID]
INNER JOIN [OTD_db].[dbo].[Status] AS [Vv1] ON [Vv1].[StatusID] = [CurStatID].[CurrentStatusID] and [Vv1].[Name] Like 'Шаблон'
ALTER PROCEDURE [dbo].[LockUnLockOrder]
@HeadGUID uniqueidentifier, @UserGUID uniqueidentifier, @Lock bit
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
if @HeadGUID is null or @UserGUID is null or @Lock is null
begin
raiserror('Процедура LockUnLockOrder: Неинициализированный параметр',16,1)
return -1
end
if V_ORDER.dbo.CheckEditStatus(@HeadGUID,@UserGUID) = 1
begin
if @Lock = 1
UPDATE [V_ORDER].[dbo].[OrderHeader]
SET isEdit = 1
,BeginEdit = getdate()
,UserEdit = @UserGUID
WHERE GUID = @HeadGUID
else
if @Lock = 0
UPDATE [V_ORDER].[dbo].[OrderHeader]
SET isEdit = 0
,BeginEdit = null
,UserEdit = null
WHERE GUID = @HeadGUID
return 1
end
else raiserror('Невозможно изменить статус заказа',16,1)
end
-- =============================================
-- Author: k1lex
-- Create date: 20160818
-- Description: функция возвращает разрешение редактирования заказа. 1 - можно редактировать. 0 - нельзя
-- =============================================
ALTER FUNCTION [dbo].[CheckEditStatus]
(
-- Add the parameters for the function here
@HeadGUID uniqueidentifier, @UserGUID uniqueidentifier
)
RETURNS bit
AS
BEGIN
DECLARE @bit BIT
IF (
SELECT COUNT(*)
FROM [V_ORDER].[dbo].[OrderHeader] O WITH (NOLOCK)
WHERE O.GUID = @HeadGUID
AND (ISNULL(O.IsReadOnly, 0) = 1 OR (O.userEdit != @UserGUID AND O.IsEdit = 1 AND DATEDIFF(HOUR, BeginEdit, GETDATE()) < 1)
and [Status]=1
)
) = 0
SET @bit = 1
ELSE
SET @bit = 0
RETURN @bit
END
select
t1.id , t1.project_id , t1.sum_1 , t1.sum_2
from table t1
inner join
( select project_id , COUNT(sum_1) ac CT from table group by project_id ) t2 ON t1.project_id=t2.project_id and t2.CT>1
inner join
( select project_id , COUNT(sum_2) ac CT from table group by project_id ) t3 ON t1.project_id=t3.project_id and t3.CT=1
SELECT [Поле группировки], sum(что сложить) FROM `table`
WHERE <условие>
group by [Поле группировки]
SELECT [Поле группировки], sum(что сложить) FROM `table`
group by [Поле группировки]
having [условие]
IF OBJECT_ID('tempdb..#testtable') IS NOT NULL DROP TABLE #testtable
create table #testtable (number nvarchar(255), spac money, dateR date)
insert into #testtable
(number, spac, dateR)
values
(1,10,'20140505'),
(1,11,'20141212'),
(2,50,'20160505'),
(2,40,'20160508')
IF OBJECT_ID('tempdb..#testtableRNK') IS NOT NULL DROP TABLE #testtableRNK
select * ,
rank() over (partition by Number order by dateR desc) as RNK
into #testtableRNK
from #testtable
select * from #testtableRNK r1
inner join #testtableRNK r2 ON r1.number= r2.number and r2.RNK=(r1.RNK+1) and r1.RNK=1 and r2.spac< r1.spac
SELECT tmp_type.type1,tmp_type.type2,sum(money) FROM `accounts`
inner join tmp_type ON tmp_type.type1 =accounts.type or tmp_type.type2 =accounts.type
group by tmp_type.type1,tmp_type.type2
WHERE date >= (CURDATE()-1) AND date < CURDATE()
SELECT
id_new,
id_user
FROM (
SELECT
id_new,
id_user,
RANK() OVER (PARTITION BY id_user ORDER by id_new DESK ) AS RNK
FROM tbl_bookmarks
GROUP BY id_user,id_new
) X WHERE RNK=1
SELECT MAX(id_new),id_user
FROM tbl_bookmarks
GROUP BY id_user
RANK() over(partition by table2.id order by table1.id) as RNK
select * from
(
SELECT *, RANK() over(partition by table2.id order by table1.id) as RNK
FROM `table1` INNER JOIN `table2` WHERE `table1`.`date`=CURDATE()
) X where X.rnk=1
create table #table ( id int, parrentid int)
insert into #table (id, parrentid)
values
(1 , 1),
(2 , 2),
(3 , 2),
(4 , 3),
(5 , 3),
(6 , 4),
(7 , 5),
(8 , 1),
(9 , 1 )
select * from #TABLE T
left join #TABLE T2 ON T.ID=T2.parrentid
where T2.id is null
SELECT O.ID
,O.Customer
,x.Summ
,y.summ
FROM ORDER O
LEFT JOIN (
SELECT order_id
,SUM(Price * Value) AS summ
FROM Order_other
GROUP BY order_id
) x ON O.Id = x.Order_id
LEFT JOIN (
SELECT order_id
,SUM(Weight) AS summ
FROM Order_position
GROUP BY order_id
) y ON O.Id = y.Order_id
select
top 100
tags,
COUNTposts
from
(
select tags, COUNT( posts ) as COUNTposts
from Posts_Tags
where (указываешь диапазон времени)
group by tags
) x
order by COUNTposts
CREATE TABLE #products (
id int,
name nvarchar(255),
price money
)
insert into #products
(
id,
name,
price
)
values
(0,'пиво',100),(1,'водка',200),(2,'пиво',150),(3,'пиво',100),(4,'спирт',1000)
SELECT
id,
name,
price
FROM (
SELECT id
,NAME
,price
,RANK() OVER (PARTITION BY NAME ORDER BY ID DESC) RNK
FROM #products ) X
where X.RNK=1