Advertisement

07.05.2008 at 09:49PM PDT, ID: 23541286 | Points: 500
[x]
Attachment Details

Peformance Calculating 3 month & 6 month NET LOSS

Asked by RunnerNwizard in MS SQL Server, Databases Miscellaneous, Data Manipulation

Part of my problem is that I am working with a lot of data ("test_ab" -> 7.5 million rows x ~75 col) and performance of some of my queries are time consuming.  

The code snippet below contains, a working example of what the output for net_loss, should look like, however I am having great performance issues running this on my large scaled table "test_ab" .  

Is there a way to simplify , my code to make it more efficient?  I have not been able to get the 3month ("net_loss_3m_avg") and 6month ("net_loss_6m_avg") to work properly.  

Below I have a working example of how the one month net loss should function, with the test case scenarios (for when net_loss is for a particular date such as NULLs or 0 ) .

PERFORMANCE IS A BIG ISSUE, I need to be able to run this query to update a very large table.

Also I am having trouble with just transferring this querys "net_loss_1m_avg" results into my orginal table, could you also provide the UPDATE query statement so I can see what Im doing wrong when trying to update "test_ab" (main table's)  column for "net_loss_1m_avg" using the data from this select query.

Thank You.



Thank You.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
-------------- Scaled down example of my ab_test table (7.5million rows x ~75 cols)------------------------
 
BEGIN tran
CREATE TABLE #test (
        id INT IDENTITY(1,1) NOT NULL,
        deal INT, 
        [type] INT,
        [name] INT,
        YYYYMM CHAR(6),
        net_loss MONEY
)
 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 1,0, 1, '200604', 562978.63 ) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 1,0, 1, '200605', 563226.13) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 1,0, 1, '200606', 576005.17) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 1,0, 1, '200607', 576005.17) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 1,0, 1, '200608', 576821.17);
 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '199909', NULL) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '199910', 0) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '199911', 0) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '199912', 0) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '200001', 2937.18);
 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '200002', 2937.18) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '200003', 105757.77) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '200004', 105757.77) 
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '200005', 105757.77);
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '200006', 125743.32);
INSERT INTO #test (     deal,[type],[name],YYYYMM, net_loss) VALUES ( 2,1, 9, '200007', 148218.86);
 
 
 
WITH test AS (
SELECT  t2.deal, t2.[type], t2.[name], t2.YYYYMM, CAST( LEFT (t2.YYYYMM,4) + '/' + RIGHT(t2.YYYYMM,2) + '/01' AS DATETIME) AS date,
 
CASE 
WHEN SUM(ISNULL(t2.[net_loss],999999999) - 0 ) = 999999999 THEN NULL
WHEN SUM(ISNULL(t1.[net_loss],999999999) - 0 ) = 999999999 THEN NULL
WHEN SUM(ISNULL(t1.[net_loss],0) - ISNULL(t2.[net_loss],0)) = 0 Then NULL
else abs(SUM(ISNULL(t1.[net_loss],0) - ISNULL(t2.[net_loss],0))) END
AS [net_loss_1m]
 
FROM    #test t1 RIGHT JOIN #test t2 
                ON      t1.deal = t2.deal AND t1.[name] = t2.[name] AND t1.[type] = t2.[type]
                        AND DATEDIFF(mm,CAST( LEFT (t1.YYYYMM,4) + '/' + RIGHT(t1.YYYYMM,2) + '/01' AS DATETIME),
                                        CAST( LEFT (t2.YYYYMM,4) + '/' + RIGHT(t2.YYYYMM,2) + '/01' AS DATETIME)) = 1
GROUP BY t2.deal, t2.[type], t2.[name], t2.YYYYMM ) 
 
SELECT deal,[type],[name],YYYYMM, net_loss_1m, net_loss_3m_avg, net_loss_6m_avg
       --------------> , (SELECT sum(net_loss_1m) FROM test t2 WHERE DATEDIFF(mm,t2.date,t1.date) BETWEEN 0 AND 3)/3 AS net_loss_3m_avg
       --------------> , (SELECT sum(net_loss_1m) FROM test t2 WHERE DATEDIFF(mm,t1.date,t2.date) BETWEEN 0 AND 6)/6 AS net_loss_6m_avg
FROM test t1
 
ROLLBACK tran
[+][-]07.06.2008 at 02:58AM PDT, ID: 21939777

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 01:07AM PDT, ID: 21996378

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.18.2008 at 09:13AM PDT, ID: 22037349

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.10.2008 at 11:42AM PDT, ID: 22441520

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080924-EE-VQP-38 / EE_QW_EXPERT_20070906