Advertisement

04.07.2008 at 07:31AM PDT, ID: 23301465
[x]
Attachment Details

Assistance with query, trying to seperate out specific variable from SELECT statement

Asked by PROJHOPE in MS SQL Server, SQL Server 2005

Tags: Microsoft, SQL, 2005

The query I have below compares two tables:

component_monetary_balance
to
ldr_acct_bal

I need to compare at a one-to-one relationship but first need to sum up the data from component_monetary_balance to make one row to then compare against the seperate row in ldr_acct_bal

The script works great for 90% of the time, yet there are times where my query is not specific enough

For component_monetary_balance the last digit of the cost_element_id reflects the value of the dbs_transaction_type

If cost_element_id = 1 then dbs_transaction_type = 00, 2 = 02, 3 = 03, etc

My query is not weeding out the dbs_transaction_type and thus the summing of the component_monetary_balance is summing the cost_element_id that ends in '2' and merging them with the ldr_acct_balance values with dbs_transaction_type ending in 00

Trying to add more granular to the script.  Attached is the code and the output, the output from the script is a variance of what doesn't balance between the two tables:




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:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
/* Retrieve all data for component 8933 */
 
SELECT 
a.project_id AS 'Project',
a.component_id AS 'Component',
LEFT (a.cost_element_id,4) AS 'CE',
SUBSTRING (a.cost_element_id,5,1) AS 'CE_End',
a.project_ytd_amt AS 'ProjYTD',
c.dbs_gl_account AS 'Acct',
c.dbs_sub_account AS 'Sub',
c.dbs_net_asset_type AS 'NAT',
c.dbs_transaction_type AS 'TT',
c.dbs_responsibility_center AS 'RC',
c.dbs_prod_line AS 'Line',
c.dbs_proj_code AS 'Proj',
c.dbs_funding_source AS 'Source',
c.dbs_tbd AS 'TBD',
ISNULL (SUM(c.ldr_amt_1 + c.ldr_amt_2 + c.ldr_amt_3 + c.ldr_amt_4 + c.ldr_amt_5 + c.ldr_amt_6 + c.ldr_amt_7 + 
c.ldr_amt_8 + c.ldr_amt_9 + c.ldr_amt_10 + c.ldr_amt_11 + c.ldr_amt_12), 99999.99) AS 'LdrYTD'
INTO #temp
FROM
DBSproj.dbo.component_monetary_balance a
LEFT OUTER JOIN DBSglep.dbo.ldr_acct_bal c
ON
a.project_id = c.dbs_proj_code AND
a.component_id = c.dbs_funding_source AND
LEFT(a.cost_element_id,4) = c.dbs_gl_account
WHERE 
a.curr_type = 'P' AND
a.fiscal_year = '2008' AND
a.amt_class_type = 'ACTUAL' AND
a.component_id = '8933' AND
c.curr_type = 'B1' AND
c.amt_class_type = 'ACTUAL' AND
c.processing_yr = '2008' 
GROUP BY 
a.project_id,a.component_id,a.cost_element_id,a.project_ytd_amt,c.dbs_proj_code,c.dbs_funding_source,
c.dbs_gl_account,c.dbs_sub_account,c.dbs_net_asset_type,c.dbs_transaction_type,
c.dbs_responsibility_center,c.dbs_prod_line,c.dbs_proj_code,c.dbs_funding_source,c.dbs_tbd
ORDER BY
a.project_id
 
 
/* Strip cost element 5th character, sum up amt by Project, Component, CE, place in new temp table */
 
SELECT Project,Component,CE,SUM(ProjYTD)AS 'SUMProjYTD'
INTO #projtemp
FROM #temp
GROUP BY Project, Component,CE
 
/* Place Ledger accounts and amounts in new temp table */
 
SELECT DISTINCT Acct,Sub,NAT,TT,RC,Line,Proj,Source,TBD,LdrYTD
INTO #ldrtemp
FROM #temp
GROUP BY Acct, Sub,NAT,TT,RC,Line,Proj,Source,TBD,LdrYTD
 
/* Compare the two new table table amounts for variances */
 
SELECT a.*,b.*,ISNULL((a.SUMProjYTD - b.LdrYTD),99999.99) AS Variance
FROM #projtemp a INNER JOIN #ldrtemp b
ON a.Project = b.Proj AND
a.Component = b.Source AND
a.CE = b.Acct
ORDER BY a.Project
 
/* Drop temp tables */
 
DROP TABLE #temp
DROP TABLE #projtemp
DROP TABLE #ldrtemp
Attachments:
 
SQL output
 
[+][-]04.08.2008 at 05:32AM PDT, ID: 21304493

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.

 
[+][-]04.08.2008 at 06:45AM PDT, ID: 21305152

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.

 
[+][-]04.08.2008 at 06:53AM PDT, ID: 21305231

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.

 
[+][-]04.08.2008 at 07:58AM PDT, ID: 21305890

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.

 
[+][-]04.08.2008 at 09:01AM PDT, ID: 21306590

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.

 
[+][-]04.08.2008 at 09:06AM PDT, ID: 21306642

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.

 
[+][-]04.08.2008 at 09:44AM PDT, ID: 21307080

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.

 
[+][-]04.08.2008 at 10:54AM PDT, ID: 21307785

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.

 
[+][-]04.08.2008 at 11:50AM PDT, ID: 21308358

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.

 
[+][-]04.08.2008 at 11:59AM PDT, ID: 21308450

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.

 
[+][-]04.08.2008 at 12:15PM PDT, ID: 21308594

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.

 
[+][-]04.08.2008 at 12:23PM PDT, ID: 21308665

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Server 2005
Tags: Microsoft, SQL, 2005
Sign Up Now!
Solution Provided By: mark_wills
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628