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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2003-01-23 : 08:50:05
|
| Phil writes "How would I combine this statement into 1 count?select count(*)from logsfall2002..da_meal_transactions_logunion allselect count(*)from da_meal_transactions_logNotice I'm selecting from 2 different databases. Also, there will be more difficulty in this statement I'm going to have group by's and order by's. I really want to have this query added between 2 databases:select stationHall,mealfunctionName,'MealCount' = count(*)from da_meal_transactions_logwhere returnstatus in (1,4)group by stationHall,mealfunctionNameorder by stationhall,mealfunctionNameI want to add the 'MealCount' from the 2 databases and have them be only 1 count using a union all. Is this possible?Thanks,Phil" |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2003-01-23 : 09:00:31
|
| something along the lines of select sum(ab) from (select count(*) as ab from db1.table1unionselect count(*) as ab from db2.table2) ashould work....there are other examples floating around here....searching for "linked servers" and "union" may be useful....BOL should also have some info.... |
 |
|
|
tacket
Starting Member
47 Posts |
Posted - 2003-01-23 : 12:02:29
|
| THANKS A BUNCH! That totally worked. Here was the final product in case other people are curious.select stationHall, mealfunctionName, 'Mealcount' = sum(mealcount)from ( select stationHall, mealfunctionName, count(*) as mealcount from da_meal_transactions_logwhere returnstatus in (1,4)group by stationHall,mealfunctionNameunion select stationHall, mealfunctionName, count(*) as mealcount from logsFALL2002..da_meal_transactions_log where returnstatus in (1,4)group by stationHall,mealfunctionName) agroup by stationHall,mealfunctionNameorder by stationhall,mealfunctionNameThanks,Phil |
 |
|
|
|
|
|
|
|