Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Joining tables

 
   Database Help (Home) -> Programming RSS
Next:  sp_monitor  
Author Message
sh0t2bts

External


Since: Feb 02, 2005
Posts: 1



(Msg. 1) Posted: Thu Feb 10, 2005 2:40 pm
Post subject: Joining tables
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have two tables one table T1 only have one result per date table 2 T2 can
range from 0 to 8 results per date,

I want to pull back all of the results in T2 and sum the totals, in my
example it will be summing "Stafftime" and "items", in T1 I need to sum the
"Contrctaed_Hours" and the OverTime. when I do sum the contracted Time I am
getting 61200, as there ate 2 rows in T1.


How can I sum my four colums but only get results for one Date in the T1
table??


Cheers

Mark
Select *
From T1

T1
Row_Date Contracted_Hours OverTime Ext_No
20050209 30600 0 4227

Select *
From T2

T2
RowDate Area Ext_No StaffTime Items
20050209 1 4227 24656 50
20050209 2 4227 6164 10

Select T1.RowDate, T1.Ext_No, sum(Contracted_Hours) as Con_Hrs,
Sum(OverTime) as OT, Sum(Items) as Completed_Work
From T1
Inner Join T2
On T1.Ext_No = T2.Ext_No
Where RowDate = '20050209' and Ext = 4227

 >> Stay informed about: Joining tables 
Back to top
Login to vote
PH

External


Since: Feb 02, 2005
Posts: 8



(Msg. 2) Posted: Thu Feb 10, 2005 2:40 pm
Post subject: RE: Joining tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I think you have to use LEFT OUTER JOIN.

"sh0t2bts" wrote:

 >
 > I have two tables one table T1 only have one result per date table 2 T2 can
 > range from 0 to 8 results per date,
 >
 > I want to pull back all of the results in T2 and sum the totals, in my
 > example it will be summing "Stafftime" and "items", in T1 I need to sum the
 > "Contrctaed_Hours" and the OverTime. when I do sum the contracted Time I am
 > getting 61200, as there ate 2 rows in T1.
 >
 >
 > How can I sum my four colums but only get results for one Date in the T1
 > table??
 >
 >
 > Cheers
 >
 > Mark
 > Select *
 > From T1
 >
 > T1
 > Row_Date Contracted_Hours OverTime Ext_No
 > 20050209 30600 0 4227
 >
 > Select *
 > From T2
 >
 > T2
 > RowDate Area Ext_No StaffTime Items
 > 20050209 1 4227 24656 50
 > 20050209 2 4227 6164 10
 >
 > Select T1.RowDate, T1.Ext_No, sum(Contracted_Hours) as Con_Hrs,
 > Sum(OverTime) as OT, Sum(Items) as Completed_Work
 > From T1
 > Inner Join T2
 > On T1.Ext_No = T2.Ext_No
 > Where RowDate = '20050209' and Ext = 4227
 >
 >
 >

 >> Stay informed about: Joining tables 
Back to top
Login to vote
Alejandro Mesa2

External


Since: Aug 12, 2004
Posts: 138



(Msg. 3) Posted: Thu Feb 10, 2005 2:40 pm
Post subject: RE: Joining tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Try,

Select
  T1.RowDate,
  T1.Ext_No,
  sum(distinct T1.Contracted_Hours) as Con_Hrs,
  Sum(distinct T1.OverTime) as OT,
  (select Sum(T2.Items) from T2 where T2.RowDate = T1.RowDate and T1.Ext_No =
T2.Ext_No) as Completed_Work
From
  T1
Where
  T1.RowDate = '20050209' and T1.Ext_No = 4227
go


AMB


"sh0t2bts" wrote:

 >
 > I have two tables one table T1 only have one result per date table 2 T2 can
 > range from 0 to 8 results per date,
 >
 > I want to pull back all of the results in T2 and sum the totals, in my
 > example it will be summing "Stafftime" and "items", in T1 I need to sum the
 > "Contrctaed_Hours" and the OverTime. when I do sum the contracted Time I am
 > getting 61200, as there ate 2 rows in T1.
 >
 >
 > How can I sum my four colums but only get results for one Date in the T1
 > table??
 >
 >
 > Cheers
 >
 > Mark
 > Select *
 > From T1
 >
 > T1
 > Row_Date Contracted_Hours OverTime Ext_No
 > 20050209 30600 0 4227
 >
 > Select *
 > From T2
 >
 > T2
 > RowDate Area Ext_No StaffTime Items
 > 20050209 1 4227 24656 50
 > 20050209 2 4227 6164 10
 >
 > Select T1.RowDate, T1.Ext_No, sum(Contracted_Hours) as Con_Hrs,
 > Sum(OverTime) as OT, Sum(Items) as Completed_Work
 > From T1
 > Inner Join T2
 > On T1.Ext_No = T2.Ext_No
 > Where RowDate = '20050209' and Ext = 4227
 >
 >
 >
 >> Stay informed about: Joining tables 
Back to top
Login to vote
Kelby

External


Since: Feb 10, 2005
Posts: 2



(Msg. 4) Posted: Thu Feb 10, 2005 3:37 pm
Post subject: RE: Joining tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

SELECT
T1.RowDate,
T1.Ext_No,
T1.Contracted_Hours,
T1.OverTime,
T2.StaffTime,
T2.Items
FROM
T1

INNER JOIN
(
SELECT
T2.RowDate,
T2.Ext_No,
SUM(StaffTime),
SUM(Items)
FROM
T2
GROUP BY
T2.RowDate,
T2.Ext_No
) T2group
ON T2group.Ext_No = T1.Ext_No
AND T2group.RowDate = T1.Row_Date

"sh0t2bts" wrote:

 >
 > I have two tables one table T1 only have one result per date table 2 T2 can
 > range from 0 to 8 results per date,
 >
 > I want to pull back all of the results in T2 and sum the totals, in my
 > example it will be summing "Stafftime" and "items", in T1 I need to sum the
 > "Contrctaed_Hours" and the OverTime. when I do sum the contracted Time I am
 > getting 61200, as there ate 2 rows in T1.
 >
 >
 > How can I sum my four colums but only get results for one Date in the T1
 > table??
 >
 >
 > Cheers
 >
 > Mark
 > Select *
 > From T1
 >
 > T1
 > Row_Date Contracted_Hours OverTime Ext_No
 > 20050209 30600 0 4227
 >
 > Select *
 > From T2
 >
 > T2
 > RowDate Area Ext_No StaffTime Items
 > 20050209 1 4227 24656 50
 > 20050209 2 4227 6164 10
 >
 > Select T1.RowDate, T1.Ext_No, sum(Contracted_Hours) as Con_Hrs,
 > Sum(OverTime) as OT, Sum(Items) as Completed_Work
 > From T1
 > Inner Join T2
 > On T1.Ext_No = T2.Ext_No
 > Where RowDate = '20050209' and Ext = 4227
 >
 >
 >
 >> Stay informed about: Joining tables 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Joining Two tables. - Hi, I have this two tables: --First table which contains some forum definitions. CREATE TABLE [dbo].[Forums]( [ForumId] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Forums_ForumId] DEFAULT (newid()), [ForumName] [nvarchar](400) COLLATE..

Joining 2 tables, pulling MAX field value from second - Let's say I have two tables: TABLE1 (T1) pkID Field1 Field2 Field3 TABLE2 (T2) pkID fkID Field1 Field2 EffDate Now, what I want to SELECT is T1.Field1, T1.Field2, T1.Field3, MAX(T2.EffDate). In other words, I want to pull back the 3....

Joining on "between x and y" - Hi, I have these tables: Sales (SaleId, ProductId, QTY) Bandwidth (ProductId, RangeFrom, RangeTo, Price) A RangeTo field is included in the Bandwidth table cause an "open end" is possible; 1-10, 11-20, 21> Now i would like to get a table...

joining 2 queries - How would i join these 2 queries: QUERY 1: SELECT DATEPART(yyyy, PERIODS.PERIOD_DATE) AS dteYear, DATEPART(mm, PERIODS.PERIOD_DATE) AS dteMonth, SUM(FCST.FCST_QTY) AS FCST FROM FCST INNER JOIN PERIODS ON..

JOINing the same table - This is probably something simple I'm missing, but here it is anyway. I have a table called "Employee". Primary key is "pk_EmployeeID". There is a foreign key field called "fk_SupervisorID" which relates to pk_EmployeeID. ...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]