Good day have the following questions:?????? 1SELECT so.ClientID, 'All Channels' as CustomerGroup, so.StatementID, so.Brand, so.Product,Sum(so.Amount) Amount, Sum(so.Value_CP) Value_CPinto #t1FROM RG_SalesOut_Report soWHERE so.Block=0 AND so.[All Sources]='SalesOUT'GROUP BY so.ClientID, so.CustomerGroup, so.StatementID, so.Brand, so.ProductHAVING Sum(so.Value_CP)>0 AND Sum(so.Amount)>0 ANDso.Brand in('Brand1', 'Brand2')?????? 2select t1.ClientID, t1.CustomerGroup, t1.StatementID, t1.Brand, t1.Product,Sum(t1.Amount) AS Amount, Sum(t1.Value_CP) AS Value_CPinto #t2from #t1 t1group by t1.ClientID, t1.CustomerGroup, t1.StatementID, t1.Brand, t1.Product?????? 3select ROW_NUMBER() over(order by t2.ClientID desc) as ID, *, CONCAT(t2.ClientID, t2.Product) AS Codeinto #t3from #t2 t2group by t2.ClientID, t2.CustomerGroup, t2.StatementID, t2.Brand, t2.Product, t2.Amount, t2.Value_CP, CONCAT(t2.ClientID, t2.Product)ORDER BY t2.ClientID DESC, t2.Product, t2.StatementID desc?????? 4select tab1.ID, tab2.ID as ID_2, tab1.ClientID, tab2.ClientID as cl_ID2, tab1.CustomerGroup, tab1.StatementID, tab1.Brand,tab1.Product, tab1.Amount, tab1.Value_CP, IIF(tab1.code=tab2.code, DATEDIFF(MONTH,tab2.StatementID, tab1.StatementID), 0) AS M_SALESFROM #t3 tab1RIGHT JOIN #t3 tab2ON tab1.ID=tab2.ID-1where tab1.StatementID>='2013-01-01'order by tab1.ID asc
works well, but run 4 times to get the desired result is it possible to write one complex query and run one time? thanks for your precious timehttp://sql-az.tr.gg/