Solved

Do computation in select statement using a current row column

Posted on 2009-04-01
8
259 Views
Last Modified: 2012-05-06
I am writing SQL and need to write a query which returns, not only columns, but a computation on a column using the value of another column in the row.

so far I have:
query1 -> "select a, b, c, d from table1 where f = 1"

I want to do add another column to query1 (say ..e) which is the result of a select on another table where I use column 'a' from query1.

How can I accomplish this?
0
Comment
Question by:ipaman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24040392
SELECT a, b, c, d, e FROM Table1 T1 INNER JOIN Table2 T2 ON T1.A = T2.ID
What is the name of the column which acts as your relationship in table2.
Infact why dont you paste your table structures here and we can go through writting you a join
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24040393
you can embed the 2nd query within the select and reference the columns from the outer query like this...


select a, b, c, d, (select table2.e+ table1.a from table2 where table2.id = table1.c)
from table1 where f = 1
0
 

Author Comment

by:ipaman
ID: 24040758
Below is my current query:
select id,agent_number 'Agent #',coverage_area 'Coverage Area', [Type] = CASE agent_type
WHEN 'S' THEN 'Skip'
WHEN 'R' THEN 'Recovery'
WHEN 'M' THEN 'Remarketing'
END
,city 'City',
[state] 'State',terms 'Terms', recovery_fee 'Recovery',closed_fee 'Closed',imp_client_fee 'Impound',
recovery_vol_fee 'Voluntary', imp_days_nocharge_fee 'Free Days', rating 'Rating'
from Agent
where agent_status = 1
and agent_number is not null
order by state

This is the query I need to do to create the 'New column' called '#Cases' in the query above:
(select count(distinct case_number) from [case] c, history h,
where c.id=h.case_id and
c.agent_id='current value of column 'a' above'
and  h.status=4) '# Cases'

so the relationship is agent_id in the case table to the id in the agent table...but I am only doing a count.
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 74

Expert Comment

by:sdstuber
ID: 24040809
try this...  (assuming "a" is agent.id)
select id,agent_number 'Agent #',coverage_area 'Coverage Area', [Type] = CASE agent_type
WHEN 'S' THEN 'Skip'
WHEN 'R' THEN 'Recovery'
WHEN 'M' THEN 'Remarketing'
END
,city 'City',
[state] 'State',terms 'Terms', recovery_fee 'Recovery',closed_fee 'Closed',imp_client_fee 'Impound',
recovery_vol_fee 'Voluntary', imp_days_nocharge_fee 'Free Days', rating 'Rating',
(select count(distinct case_number) from [case] c, history h, 
where c.id=h.case_id and 
c.agent_id=Agent.id
and  h.status=4) '# Cases'
from Agent 
where agent_status = 1 
and agent_number is not null 
order by state

Open in new window

0
 

Author Comment

by:ipaman
ID: 24041486
the "select count(distinct case_number..." fails because it doesn't know what 'Agent' is.
If I add the agent table to the select and do a join it willl give me the intersection of the tables.
I only want to do the calculation on the other table where the current value of 'id' is used in the subquery.

not sure if i am making sense on this...
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24041528
maybe your database doesn't support subqueries of that form.
You didn't specify what db you were using, I assumed Oracle, but based on your syntax, it must be something else.   If the subquery syntax isn't supported in your db then you'll need to do a join  as suggested above
0
 

Author Comment

by:ipaman
ID: 24041863
sorry. this is sql server.
0
 

Accepted Solution

by:
ipaman earned 0 total points
ID: 24043176
I believe I found the best solution to this based upon a previous post I had:

with assigned_cases as
(select agent_id, count(*) Assigned from [case] where ([status] & 4) > 0  group by agent_id),

tass_cases as
(select agent_id, count(distinct case_number) TAss from [case] c,history h
 where c.id=h.case_id and h.status=4
 group by agent_id),

trec_cases as
(select c.agent_id, count(distinct case_number) TRec from [case] c, history h
 where c.id=h.case_id and h.status=32
 group by c.agent_id)

select id,agent_number 'Agent #',[name],coverage_area 'Coverage Area', [Type] = CASE agent_type
WHEN 'S' THEN 'Skip'
WHEN 'R' THEN 'Recovery'
WHEN 'M' THEN 'Remarketing'
END
,city 'City',
[state] 'State',terms 'Terms', recovery_fee 'Recovery',closed_fee 'Closed',imp_client_fee 'Impound',
recovery_vol_fee 'Voluntary', imp_days_nocharge_fee 'Free Days', rating 'Rating',
assigned_cases.Assigned 'Number Assigned',
'Cap.' = CASE WHEN ISNUMERIC(number_trucks) = 1
        THEN convert(int, number_trucks) * 15
      ELSE ''
      END,
tass_cases.TAss '# Cases',
'Rec.%' = CASE WHEN ((trec_cases.TRec/(tass_cases.TAss * .8)) * 100)> 100
          THEN 100
          ELSE (trec_cases.TRec/(tass_cases.TAss * .8)) * 100
          END
from Agent a
left outer join assigned_cases on a.id=assigned_cases.agent_id
left outer join tass_cases on a.id=tass_cases.agent_id
left outer join trec_cases on a.id=trec_cases.agent_id
where a.agent_status = 1
and a.agent_number is not null
order by state
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question