473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Little puzzle on data selection

I have the following data (very simplified version)

TransactionId Agent_Code
------------- ----------
191462 95328C
205427 000024C
205427 75547C

Agent Code 75547C is a corporate agent. The others are not. I have a
list of corporate codes so I can query against it, BUT what I want to
do is...

Return a unique TransactionId and max of the AgentCode, but if the
Agent is a corporate agent, I need to return max of the corporate agent
codes. We can have multiple agents against the transaction and
sometimes have a mix of corporate and none corporate agents. What we
need to do is see the corporate adviser if there is one. I only want 1
record per TransactionId.

We derive more data (sales hierarchy) from this, so are not interested
in anything other than the maximum, but need to know if it was
corporate which therefore gives me a different hierarchy later.

Ideally I want to do this in a view and not use an SP. I can then use
this in my main view. If I have to resort to an SP, then so be it, but
I would appreciate any helpful comments (or even better, the answer)
Thanks

Ryan

Jul 20 '05 #1
4 973

"Ryan" <ry********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have the following data (very simplified version)

TransactionId Agent_Code
------------- ----------
191462 95328C
205427 000024C
205427 75547C

Agent Code 75547C is a corporate agent. The others are not. I have a
list of corporate codes so I can query against it, BUT what I want to
do is...

Return a unique TransactionId and max of the AgentCode, but if the
Agent is a corporate agent, I need to return max of the corporate agent
codes. We can have multiple agents against the transaction and
sometimes have a mix of corporate and none corporate agents. What we
need to do is see the corporate adviser if there is one. I only want 1
record per TransactionId.


I would think something like (Obviously untested):

--No Corp Agent
Select TransactionID, Max(Agent_Code) from Transtable a where NOT
EXIST(select * from TransTable b inner join CorpAgents c on b.Agent_Code =
c.Agent_Code where b.TransactionID = a.TransactionID)

UNION ALL
--Corp Agent
Select TransactionID, Max(Agent_Code) from TransTable a inner join
CorpAgents c on a.Agent_Code = c.Agent_Code

Good Luck

Jim
Jul 20 '05 #2
[posted and mailed, please reply in news]

Ryan (ry********@hotmail.com) writes:
Return a unique TransactionId and max of the AgentCode, but if the
Agent is a corporate agent, I need to return max of the corporate agent
codes. We can have multiple agents against the transaction and
sometimes have a mix of corporate and none corporate agents. What we
need to do is see the corporate adviser if there is one. I only want 1
record per TransactionId.


SELECT TransactionID, coalesce(maxcorp, maxanyone)
FROM (SELECT t.TransactionID, maxanyone = MAX(t.AgentCode),
maxanyone = MAX(a.AgentCode)
FROM transactions t
LEFT JOIN agents a ON t.AgentCode = a.AgentCode
GROUP BY t.TransactionID)

And as you surely know, had you included CREATE TABLE, INSERT and expected
output, the solution would have been tested. Now it's not.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Thanks ! Will give this a try.

Erland Sommarskog <es****@sommarskog.se> wrote in message news:<Xn**********************@127.0.0.1>...
[posted and mailed, please reply in news]

Ryan (ry********@hotmail.com) writes:
Return a unique TransactionId and max of the AgentCode, but if the
Agent is a corporate agent, I need to return max of the corporate agent
codes. We can have multiple agents against the transaction and
sometimes have a mix of corporate and none corporate agents. What we
need to do is see the corporate adviser if there is one. I only want 1
record per TransactionId.


SELECT TransactionID, coalesce(maxcorp, maxanyone)
FROM (SELECT t.TransactionID, maxanyone = MAX(t.AgentCode),
maxanyone = MAX(a.AgentCode)
FROM transactions t
LEFT JOIN agents a ON t.AgentCode = a.AgentCode
GROUP BY t.TransactionID)

And as you surely know, had you included CREATE TABLE, INSERT and expected
output, the solution would have been tested. Now it's not.

Jul 23 '05 #4
>> Return a unique transaction_id and max of the agent_code, but if
the agent is a corporate agent, I need to return max of the corporate
agent codes. <<

Just for fun, try this version:

CREATE VIEW CorpTrans (transaction_id, agent_code)
AS
SELECT transaction_id,
COALESCE(
MAX(CASE WHEN T1.agent_code IN (SELECT agent_code FROM
CorpAgents)
THEN T1.agent_code ELSE NULL END) -- corp_agent,
MAX(CASE WHEN T1.agent_code NOT IN (SELECT agent_code FROM
CorpAgents)
THEN T1.agent_code ELSE NULL END) -- non_corp_agent
) AS agent_code
FROM Transactions AS T1
GROUP BY Transaction_id;

You could also drop the COALESCE (), if it would be more useful to see
both kinds of agents. id the number of corporate agents is small enugh
to fit into main storage, this might actually be a good way to do it!
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: rossum | last post by:
Either I have found a puzzle, or I am just being stupid. Inside a namespace I have a class with a static const double member. The member is not an int so I cannot initialise it within the class,...
5
by: Jonny | last post by:
Hello, I have created a button on my form which when pressed does the following : 1) Run a pre-defined Macro, which is picking up a query and running my data to excel. However, I need the...
0
by: David Krmpotic | last post by:
Regards, I need a little explanation in converting word VB macros to charp code! first, I recorded simple macro which replaces every occurence of one word in whole document (is it?) , here:...
8
by: --CELKO-- | last post by:
Mother Celko's Monday SQL Puzzle #3 I am gathering material for a second edition of SQL PUZZLES & ANSWERS. The easiest way to do this is to post a puzzle and harvest answers. The solvers get...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.