Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 4 query =1 query

Author  Topic 

pascal_jimi
Posting Yak Master

167 Posts

Posted - 2014-06-18 : 00:53:31
Good day
have the following questions:

?????? 1
SELECT so.ClientID, 'All Channels' as CustomerGroup, so.StatementID, so.Brand, so.Product,
Sum(so.Amount) Amount, Sum(so.Value_CP) Value_CP
into #t1
FROM RG_SalesOut_Report so
WHERE so.Block=0 AND so.[All Sources]='SalesOUT'
GROUP BY so.ClientID, so.CustomerGroup, so.StatementID, so.Brand, so.Product
HAVING Sum(so.Value_CP)>0 AND Sum(so.Amount)>0 AND
so.Brand in('Brand1', 'Brand2')

?????? 2

select t1.ClientID, t1.CustomerGroup, t1.StatementID, t1.Brand, t1.Product,
Sum(t1.Amount) AS Amount, Sum(t1.Value_CP) AS Value_CP
into #t2
from #t1 t1
group by t1.ClientID, t1.CustomerGroup, t1.StatementID, t1.Brand, t1.Product

?????? 3

select ROW_NUMBER() over(order by t2.ClientID desc) as ID, *, CONCAT(t2.ClientID, t2.Product) AS Code
into #t3
from #t2 t2
group 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

?????? 4

select 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_SALES
FROM #t3 tab1
RIGHT JOIN #t3 tab2
ON tab1.ID=tab2.ID-1
where 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 time

http://sql-az.tr.gg/

sz1
Aged Yak Warrior

555 Posts

Posted - 2014-06-18 : 05:17:38
You'd be better off creating a view or sp for this.

We are the creators of our own reality!
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-06-18 : 10:10:14
You can use CTEs instead of temp tables. e.g.


WITH T1 as ( Query 1),
T2 as ( Query 2),
T3 as ( Query 3)

SELECT ... Query 4, but reference T1, T2, T3 instead of your temp tables


However, You can't put indexes on CTEs. This may not be a problem if the data is not too big. Otherwise, the final joins may become table scans.

Try it with CTEs. If the performance is acceptable (whatever that means to you!) then fine. If not, go back to temp tables and put indexes on the join columns. Also, why is the WHERE clause in the final join? Why not put it on query 3 you build #t3 in the first place?
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-06-18 : 11:40:16
The short answer is Yes, you can combine all that into one query. Will it be more efficient? I can't say with the information provided. If you want to supply sample data and expected output, someone might be willing to help you re-write your query.

Here are some links on how to post your question and code:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-06-18 : 11:47:20
Also, if you don't group by so.CustomerGroup in #T1 then you shouldn't need to do the insert into #T2 as the #T1 values should be the same as what is produced in #T2. And, if you move the CONCAT and sequence number creation from #T3 to #T1, then you can eliminate step 3.
Go to Top of Page
   

- Advertisement -