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 |
|
jameswoodmancy
Starting Member
13 Posts |
Posted - 2007-11-21 : 05:16:15
|
| I have a table that contains pricestblPricescustomerId customername yearnumber monthnumber price1 smith 2004 1 4551 Smith 2004 2 321 1 Smith 2004 3 3455etc...for each monththen1 smith 2005 1 45451 smith 2005 2 49for each year up until 2007.also i have about 20 customers2 jones 2004 1 343452 jones 2004 1 354345etc...i have a customers table with about 25 customers incustomerid customername1 smith2 jones3 blogsetc...basically i want to create a join that will join the two tables on customer id but giving the following results.1 smith 2004 1 4551 Smith 2004 2 321 1 Smith 2004 3 3455etc..2 jones 2004 1 343452 jones 2004 2 354345and then blogs who isnt in the tblPrices i want to show null values for each year and each month number so3 blogs 2004 1 null3 blogs 2004 1 nullif i do a left outer join i get one value3 blogs null null nulland then onto the next customer4 woods 2004 1 333 anyone know how to do this join?thanks for your help |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-11-21 : 06:23:26
|
| select c.CustomerID, c.CustomerName, d.yearnumber, d.monthnumber, t.price(select distinct CustomerID, CustomerName from tblPrices) ccross join(select distinct yearnumber, monthnumber from tblPrices) dleft join tblPrices ton c.CustomerID = t.CustomerIDand d.yearnumber = t.yearnumberand d.monthnumber = t.monthnumber You should have a customer and date table so that you don't need to get the values from tblPrices.tblPrices should just have the CustomerID, date and price.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
jameswoodmancy
Starting Member
13 Posts |
Posted - 2007-11-21 : 07:07:40
|
| hi, thanks! they seems to be something with the syntax though and i cant figure out whatMsg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'select'.Msg 102, Level 15, State 1, Line 2Incorrect syntax near ')'.Msg 102, Level 15, State 1, Line 4Incorrect syntax near 'd'. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-21 : 07:23:06
|
[code]select c.CustomerID, c.CustomerName, d.yearnumber, d.monthnumber, t.pricefrom ( select distinct CustomerID, CustomerName from tblPrices) ccross join( select distinct yearnumber, monthnumber from tblPrices) dleft join tblPrices ton c.CustomerID = t.CustomerIDand d.yearnumber = t.yearnumberand d.monthnumber = t.monthnumber[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|