Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 28th, 2004, 02:47 AM
MLSrinivas MLSrinivas is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 MLSrinivas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 41 sec
Reputation Power: 0
Unhappy Join Query Help

Hi,

Please help me in writing a join query.

I have three tables with three columns each. Now I want to retrieve data from all the three

tables as one.

Table 1: EmpId, Date, Points
Table 2: EmpId, Date, Points
Table 3: EmpId, Date, Points

These are related to three different divisions. So, on analysis we have get the no. of points

accumulated on a day. On any day the points can be in all three divisions or in any one or two

divisions. My database SQL Server 2000.

So, how to get this sort of output.

Empid Date Pts(DIv1) Pts(DIv2) Pts(DIv3)
V001 07-24-2004 Null Null 25
V002 07-24-2004 20 Null 25
V003 07-24-2004 Null 30 NUll
V001 07-23-2004 15 Null NUll
V002 07-23-2004 10 25 25
V001 07-22-2004 Null 10 25

I'm badly in need of help. Any sort of help is appreciated.


M.L.Srinivas

Reply With Quote
  #2  
Old July 28th, 2004, 04:09 AM
Kris_Vanherck's Avatar
Kris_Vanherck Kris_Vanherck is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Belgium, Antwerp
Posts: 177 Kris_Vanherck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 sec
Reputation Power: 7
this is what you get when you don't know how to design databases, this guy has the same problem: http://forums.aspfree.com/t33633/s.html
i suggest you also look at those tutorials

what needs to be done for your specific case:

make 1 table with the points in and field to distinguis between divisions, instead of putting them in 3 tables:
PointsTable: empid,date,points, divisionid
DivisionTable: divisionid, name

then you have the right database structure and everything becomes easy:
your result:
SELECT empid, date, sum(points)
FROM pointstable
GROUP BY empid, date

Reply With Quote
  #3  
Old July 28th, 2004, 05:49 AM
MLSrinivas MLSrinivas is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 MLSrinivas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 41 sec
Reputation Power: 0
Hi Kris_Vanherck,

Thanks for your time. Let me make it clear, actually I don't have three different tables. The
structure of my table (some/imp columns)
ActivityDate
Task
Div1Id
Div1pts
DivId
Div2Pts
Div3Id
Div3Pts
One person performs the task and it will be reviewed by one or two persons.
For example, if V001 performs a task, his empid(V001) will be Div1id and points scored by him
will be div1pts. if he reviews some others tasks, his empid will be under div2id or div3id
depending the reviewed time.
So, on any given date i want the sum of points accumulated seperately in all three divisions by
all employees.
Sample data follows:
ActDate Task Div1Id Div1Pts Div2Id Div2pts Div3id Div3Pts

07-24-2004 T1 V001 20 V002 15 NULL NUll
07-24-2004 T2 V003 20 V001 15 V004 10
07-24-2004 T3 V001 20 V004 15 NULL NUll
07-23-2004 T4 V004 20 V001 15 V002 15
07-23-2004 T5 V001 20 V004 15 NULL NUll

And the output should be like:
Empid Date Points(DIv1) POints(DIv2) POints(DIv3)
V001 07-24-2004 Null Null 25
V002 07-24-2004 20 Null 25
V003 07-24-2004 Null 30 NUll
V001 07-23-2004 15 Null NUll
V002 07-23-2004 10 25 25
V001 07-22-2004 Null 10 25

As I could not accomplish this task in one query, I adopted a round about process like , creating
three views (the three tables I mentioned in earlier post) and trying to write a query using full
outer join.
Any sort of help is appreciated.
Thanks
M.L.Srinivas

Reply With Quote
  #4  
Old July 28th, 2004, 08:29 AM
Kris_Vanherck's Avatar
Kris_Vanherck Kris_Vanherck is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Belgium, Antwerp
Posts: 177 Kris_Vanherck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 sec
Reputation Power: 7
if i get this strait it means you want for each date a the list of empid's like this:
date, empid, sum(of all div1pts with date = date and div1id = empid), sum(of all div2pts with date = date and div1id = empid), sum(of all div3pts with date = date and div1id = empid)

don't have much time rigth now so here a quick stab at it:

create view:
SELECT date, div1id as empid, sum(div1pts) as p1,0 as p2, 0 as p3 ... group by date, div1id
UNION
SELECT date, div2id as empid, 0 as p1,sum(div2pts) as p2, 0 as p3 ... group by date, div2id
UNION
SELECT date, div3id as empid, 0 as p1,0 as p2, sum(div3pts) as p3 ... group by date, div3id

then

select date, empid, sum(p1),sum(p2),sum(p3) from view group by data empid

Last edited by Kris_Vanherck : July 28th, 2004 at 08:41 AM.

Reply With Quote
  #5  
Old July 29th, 2004, 06:14 AM
MLSrinivas MLSrinivas is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 MLSrinivas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 41 sec
Reputation Power: 0
Hi Kris_Vanherck,

Brilliant. Thanks alot. I appreciate your logic.

I have got my output.

Thanks

M.L.Srinivas

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Join Query Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek