Advertisement

06.24.2008 at 08:41AM PDT, ID: 23511420
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.6

SQL to search through a list of ID's/numbers on a temporary table

Asked by ihatelag in SQL Query Syntax, MS SQL Server, SQL Server 2005

Tags: ,

I have two tables, table Customer and Table CustomerAccount

There are customers in our database who are agents.

An Agent can be a super Agent with many agents underneath him. Also the tricky bit is trying to find those Agents (under a Super Agent) who have agents underneath them too!

- so we have IDSuperAgent number (not all agents are super agents and have a null value for this field) and IDAgent number (All agents must have IDAgent number).  All accounts have a IDAccount number regardless of level.

IDSuperAgent          IDAgente               IDAccount            Balance    IDTypeOfAccount
 29500                     29500                    29500                  5000                    2
29500                      50                          50                        500                      2
29500                      49                          49                        1500                    2      
49                            48                          48                        2000                    2      
48                            47                          47                        500                      2

There is no flag to separate each level in the table, so we can only find the agents from the normal customers by using the IDTypeOfAccount field (which will be 2  =  agents) .

I have attached the SQL which is trying to read from a temporary table where I am trying to add the balance of the Agents under the SuperAgent and those agents which have agents underneath themselves.

Please can you help in identifying perhaps a better way to query the information or help fix my attempted query to get a list of id's i put in a temporary table in order to capture the correct id's

I think a derived table might solve it but i am not very used to creating this type of query.

You help is most appreciated.

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:
drop table #tmpCustomer
drop table #tmpData
 
-- get Agents who belong to super agent 29500
SELECT    tbl_Customer.IDAgent, tbl_CustomerAccount.Balance
into #tmpCustomer
FROM         tbl_Customer WITH (nolock) INNER JOIN
                      tbl_CustomerAccount WITH (nolock) ON tbl_Customer.IDUtente = tbl_CustomerAccount.IDUtente
                     
WHERE       tbl_Customer.IDSuperAgent = '29500' 
and tbl_Customer.IDTypeOfAccount = 2
group by tbl_Customer.IDAgent, tbl_CustomerAccount.Balance
order by tbl_Customer.IDAgent
 
 
-- get all agents under the above agents (who sit under super agent 29500) captured in the query above
SELECT  tbl_Customer.IDAgent, tbl_Customer.Name, tbl_Customer.Surname, tbl_CustomerAccount.Balance
into #tmpData
FROM         tbl_Customer WITH (nolock) INNER JOIN
                      tbl_CustomerAccount WITH (nolock) ON tbl_Customer.IDUtente = tbl_CustomerAccount.IDUtente
                      INNER JOIN #tmpCustomer tmp  on tmp.IDAgent = u.IDSuperAgent
                     
WHERE       (tbl_Customer.IDAgent in u.IDSuperAgent)  --<---- where I need help not sure how to query against the list in tmpcustomer
and tbl_Customer.IDTypeOfAccount = 2 and tbl_Customer.IDSuperAgent is not null
group by  tbl_Customer.IDAgent, tbl_Customer.Name, tbl_Customer.Surname, tbl_CustomerAccount.Balance
 
 
-- get all agents data and under agents combined
 
Select distinct tmp.IDAgent, tmp.Name, tmp.Surname, sum(tmp.Balance + cu.Balance)
from tbl_Customer as u with (nolock) inner join
tbl_CustomerAccount as cu with (nolock) ON tbl_Customer.IDAgent = tbl_CustomerAccount.IDUtente
                     
WHERE       tbl_Customer.IDSuperAgent = '29500' 
and tbl_Customer.IDTypeOfAccount = 2 
group by tmp.IDAgent, tmp.Name, tmp.Surname
 
Loading Advertisement...
 
[+][-]06.24.2008 at 08:49AM PDT, ID: 21857050

View this solution now by starting your 14-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: SQL Query Syntax, MS SQL Server, SQL Server 2005
Tags: SQL, SQL Server 2005
Sign Up Now!
Solution Provided By: drydenhogg
Participating Experts: 1
Solution Grade: C
 
 
[+][-]06.24.2008 at 08:59AM PDT, ID: 21857194

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 14-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628