Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
uday424
Starting Member
6 Posts |
Posted - 2009-10-21 : 14:18:27
|
| Below are the results of two queries, which are queried for the same table. Please help me in combining these two tables based on common columns which are DATE, DAY AND VOLUME. Date Day Start_time RID Volume2008-05-02 Friday 11:20:43.0000000 2 8902008-05-02 Friday 11:20:43.0000000 17 10942008-05-02 Friday 11:20:43.0000000 23 10302008-05-02 Friday 11:20:43.0000000 34 8252008-05-02 Friday 11:20:43.0000000 39 13202008-05-02 Friday 11:20:43.0000000 48 18622008-05-02 Friday 11:20:43.0000000 49 5290Date Day End_time RID Volume2008-05-02 Friday 11:35:53.0000000 2 7282008-05-02 Friday 11:35:53.0000000 17 8252008-05-02 Friday 11:35:53.0000000 23 9202008-05-02 Friday 11:35:53.0000000 34 7082008-05-02 Friday 11:35:53.0000000 39 11012008-05-02 Friday 11:35:53.0000000 48 13662008-05-02 Friday 11:35:53.0000000 49 4457I need it like this:Date Day Start_time RID Volume End_time Volume2008-05-02 Friday 11:20:43.0000000 2 890 11:35:53.0000000 7282008-05-02 Friday 11:20:43.0000000 17 1094 11:35:53.0000000 8252008-05-02 Friday 11:20:43.0000000 23 1030 11:35:53.0000000 9202008-05-02 Friday 11:20:43.0000000 34 825 11:35:53.0000000 7082008-05-02 Friday 11:20:43.0000000 39 1320 11:35:53.0000000 11012008-05-02 Friday 11:20:43.0000000 48 1862 11:35:53.0000000 13662008-05-02 Friday 11:20:43.0000000 49 5290 11:35:53.0000000 4457 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-10-21 : 14:23:46
|
quote: Originally posted by uday424 Below are the results of two queries, which are queried for the same table.
OK, be a good idea if you show us the queriesquote: Please help me in combining these two tables based on common columns which are DATE, DAY AND VOLUME.
Sure...sounds like you want GROUP BY...but we still need to see the queries, and what you want to do with the columns that you want "combined".and you can use [ code] [ /code] tags (with out the space, to keep data formatted correctly.Date Day Start_time RID Volume2008-05-02 Friday 11:20:43.0000000 2 8902008-05-02 Friday 11:20:43.0000000 17 10942008-05-02 Friday 11:20:43.0000000 23 10302008-05-02 Friday 11:20:43.0000000 34 8252008-05-02 Friday 11:20:43.0000000 39 13202008-05-02 Friday 11:20:43.0000000 48 18622008-05-02 Friday 11:20:43.0000000 49 5290Date Day End_time RID Volume2008-05-02 Friday 11:35:53.0000000 2 7282008-05-02 Friday 11:35:53.0000000 17 8252008-05-02 Friday 11:35:53.0000000 23 9202008-05-02 Friday 11:35:53.0000000 34 7082008-05-02 Friday 11:35:53.0000000 39 11012008-05-02 Friday 11:35:53.0000000 48 13662008-05-02 Friday 11:35:53.0000000 49 4457 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-10-21 : 14:35:23
|
You didn't post your queries.....come on nowAnd let's discuss two partsYou haveDate Day End_time RID Volume2008-05-02 Friday 11:20:43.0000000 2 890Date Day End_time RID Volume2008-05-02 Friday 11:35:53.0000000 2 728 And wantDate Day End_time RID Volume End Time 2 RID 22008-05-02 Friday 11:20:43.0000000 2 890 11:35:53.0000000 728 Combine would be the wrong word.....What if you had 10 RID's and End Times?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
uday424
Starting Member
6 Posts |
Posted - 2009-10-21 : 14:41:32
|
These are the two tables and their related queries:Date Day Start_time RID Volume2008-05-02 Friday 11:20:43.0000000 2 8902008-05-02 Friday 11:20:43.0000000 17 10942008-05-02 Friday 11:20:43.0000000 23 10302008-05-02 Friday 11:20:43.0000000 34 8252008-05-02 Friday 11:20:43.0000000 39 13202008-05-02 Friday 11:20:43.0000000 48 18622008-05-02 Friday 11:20:43.0000000 49 5290 Date Day End_time RID Volume2008-05-02 Friday 11:35:53.0000000 2 7282008-05-02 Friday 11:35:53.0000000 17 8252008-05-02 Friday 11:35:53.0000000 23 9202008-05-02 Friday 11:35:53.0000000 34 7082008-05-02 Friday 11:35:53.0000000 39 11012008-05-02 Friday 11:35:53.0000000 48 13662008-05-02 Friday 11:35:53.0000000 49 4457 select date, DATENAME(dw, Date) as Day, time as Start_time, rid, sum(volume) as Sum_of_volume from t_2009 where time = '11:20:43' group by rid, time, date order by date, rid, time select date, DATENAME(dw, Date) as Day, time as End_time, rid, sum(volume) as Sum_of_volume from t_2009 where time = '11:35:53' group by rid, time, date order by date, rid, timeI want the table to be like this. I already posted the two tables and their related queries. Date Day Start_time RID Volume End_time Volume2008-05-02 Friday 11:20:43.0000000 2 890 11:35:53.0000000 7282008-05-02 Friday 11:20:43.0000000 17 1094 11:35:53.0000000 8252008-05-02 Friday 11:20:43.0000000 23 1030 11:35:53.0000000 9202008-05-02 Friday 11:20:43.0000000 34 825 11:35:53.0000000 7082008-05-02 Friday 11:20:43.0000000 39 1320 11:35:53.0000000 11012008-05-02 Friday 11:20:43.0000000 48 1862 11:35:53.0000000 13662008-05-02 Friday 11:20:43.0000000 49 5290 11:35:53.0000000 4457 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
uday424
Starting Member
6 Posts |
Posted - 2009-10-21 : 14:50:21
|
| it is nvarchar(10), but i guess it has got nothing to do with this issue right!!! |
 |
|
|
uday424
Starting Member
6 Posts |
Posted - 2009-10-21 : 15:41:09
|
| Please help me Brett... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-10-21 : 15:41:42
|
kTry thisSELECT [Date] , DATENAME(dw, [Date]) AS [Day] , rid_Start , [Time] AS Start_Time , SUM(volume) AS SUM_of_volume_Start , rid_End , [Time] AS End_Time , SUM(volume) AS SUM_of_volume_EndFROM ( SELECT [Date] , rid AS Rid_Start , [Time] AS Start_Time , DATENAME(dw, [Date]) AS [Day] , SUM(volume) AS SUM_of_volume_Start FROM t_2009 WHERE [Time] = '11:20:43'GROUP BY [Date], rid , [Time]) AS sINNER JOIN ( SELECT [Date] , rid AS Rid_End , [Time] AS End_Time , DATENAME(dw, [Date]) AS [Day] , SUM(volume) AS SUM_of_volume_End FROM t_2009 WHERE [Time] = '11:35:53'GROUP BY [Date], rid , [Time]) AS eon s.[Date] = e.[Date] Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
uday424
Starting Member
6 Posts |
Posted - 2009-10-21 : 15:58:01
|
Thanks for the quick reply , Brett, but it not leading me to the required result.I executed the query which u posted with slight modifications. Below is the query and its result. SELECT [Date], DATENAME(dw, [Date]) AS [Day], Rid_Start, Start_Time, SUM_of_volume_Start, Rid_End, End_Time, SUM_of_volume_EndFROM ( SELECT distinct[Date] as Date1 , rid AS Rid_Start , [Time] AS Start_Time , DATENAME(dw, [Date]) AS [Day] , SUM(volume) AS SUM_of_volume_Start FROM t_2009_WHERE [Time] = '11:20:43'GROUP BY [Date], rid , [Time]) AS sINNER JOIN ( SELECT distinct[Date] , rid AS Rid_End , [Time] AS End_Time , DATENAME(dw, [Date]) AS [Day] , SUM(volume) AS SUM_of_volume_End FROM t_2009 WHERE [Time] = '11:35:53'GROUP BY [Date], rid , [Time]) AS eon s.Date1 = e.Date2008-05-02 Friday 2 11:20:43.0000000 890 2 11:35:53.0000000 7282008-05-02 Friday 2 11:20:43.0000000 890 17 11:35:53.0000000 8252008-05-02 Friday 2 11:20:43.0000000 890 23 11:35:53.0000000 9202008-05-02 Friday 2 11:20:43.0000000 890 34 11:35:53.0000000 7082008-05-02 Friday 2 11:20:43.0000000 890 39 11:35:53.0000000 11012008-05-02 Friday 2 11:20:43.0000000 890 48 11:35:53.0000000 13662008-05-02 Friday 2 11:20:43.0000000 890 444 11:35:53.0000000 1052008-05-02 Friday 2 11:20:43.0000000 890 427 11:35:53.0000000 1712008-05-02 Friday 2 11:20:43.0000000 890 431 11:35:53.0000000 1502008-05-02 Friday 2 11:20:43.0000000 890 432 11:35:53.0000000 1522008-05-02 Friday 2 11:20:43.0000000 890 435 11:35:53.0000000 02008-05-02 Friday 2 11:20:43.0000000 890 436 11:35:53.0000000 1602008-05-02 Friday 2 11:20:43.0000000 890 439 11:35:53.0000000 92 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-10-21 : 17:55:22
|
Aren't the basically the same?And all you have to do is move around columns?What's wrong with it?Also, the queries really don't make much senseDate Day Start_time RID Volume End_time Volume2008-05-02 Friday 11:20:43.0000000 2 890 11:35:53.0000000 728Date Day RID_Start Start_time Volume_Start2008-05-02 Friday 2 11:20:43.0000000 890 RID_End End_time Volume_End 2 11:35:53.0000000 728 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
|
|
|
|
|