473,396 Members | 2,109 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,396 software developers and data experts.

Need help with designing a query

Hi All,

I have a table below and I want to design a query to pull all the
members from the TABLE into a Query Result and into a single column with
points assigned appropriately, but I am having a lot of difficulties
doing this. Any help is greatly appreciated.
TABLE
MEMBER1 MEMBER2 POINTS
Joe Don 2
Macy 1
Jack Nick 2
Joe Rob 2

This is a result I would like to generate from the query
Query Result
MEMBERS Total Points
Joe 2
Don 1
Macy 1
Jack 1
Nick 1
Rob 1

Thank you,
Jim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
1 1233
On 17 Aug 2004 15:53:39 GMT, Jimmy Tran wrote:
Hi All,

I have a table below and I want to design a query to pull all the
members from the TABLE into a Query Result and into a single column with
points assigned appropriately, but I am having a lot of difficulties
doing this. Any help is greatly appreciated.
TABLE
MEMBER1 MEMBER2 POINTS
Joe Don 2
Macy 1
Jack Nick 2
Joe Rob 2

This is a result I would like to generate from the query
Query Result
MEMBERS Total Points
Joe 2
Don 1
Macy 1
Jack 1
Nick 1
Rob 1

Thank you,
Jim


You should always try to post DDL and sample inserts when you ask questions
here; it makes your problem much clearer and makes it easier for those who
wish to help -- we can just paste into query analyzer and begin.

Below is complete DDL, inserts, your solution and its results, as copied
from query analyzer:

create table T1 (
MEMBER1 varchar(20) null,
MEMBER2 varchar(20) null,
POINTS int
)

insert into T1 (MEMBER1, MEMBER2, POINTS)
values ('Joe', 'Don', 2)
insert into T1 (MEMBER1, MEMBER2, POINTS)
values ('Macy', null, 1)
insert into T1 (MEMBER1, MEMBER2, POINTS)
values ('Jack', 'Nick', 2)
insert into T1 (MEMBER1, MEMBER2, POINTS)
values ('Joe', 'Rob', 2)

select MEMBERS, sum(POINTS) as [Total Points]
from (
select MEMBER1 as MEMBERS,
POINTS/case when MEMBER2 is null then 1 else 2 end as POINTS
from T1 where MEMBER1 is not null
union all
select MEMBER2 as MEMBERS,
POINTS/case when MEMBER1 is null then 1 else 2 end as POINTS
from T1 where MEMBER2 is not null
) as T2
group by MEMBERS
order by sum(POINTS) DESC

MEMBERS Total Points
-------------------- ------------
Joe 2
Macy 1
Nick 1
Rob 1
Don 1
Jack 1

I made the assumption from your example that if either member is null, the
non-null member gets all the points; otherwise they are split evenly
between the two members. If in fact it will always be one point per
non-null member per row, then the query is a bit simpler:

select MEMBERS, count(*) as [Total Points]
from (
select MEMBER1 as MEMBERS
from T1 where MEMBER1 is not null
union all
select MEMBER2 as MEMBERS
from T1 where MEMBER2 is not null
) as T2
group by MEMBERS
order by count(*) DESC
Jul 20 '05 #2

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

Similar topics

2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
1
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
2
by: sk | last post by:
I have a table for storing alerts (exceptional situations) occuring on devices that I monitor. Associated with each alert is an alert code, a description, the device responsible for causing the...
2
by: Sky Sigal | last post by:
Hello: I'm currently messing around, and need as much feedback/help as I can get, trying to find the most economical/graceful way to build usercontrols that rely on styling to look any good... ...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.