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()
при каждом обращении рассчитывать предыдущее состояние склада, вычитать из него 3-4 тысячи заказов и прибавлять 3-4 сотни поставок – не решение
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
SELECT ID,NameGroup FROM Groupt
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
public DataTable ToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof(T).Name);
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
}
foreach (T item in items)
{
var values = new object[props.Length];
for (int i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
} return tb;
}
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
}