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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Stuck on SELECT and Join

Author  Topic 

stangn99
Starting Member

7 Posts

Posted - 2008-02-04 : 01:00:11
Hi All,

Here is my issue:

I have 2 tables.

- tblone hold exchangeRates, and nationCode

- tbltwo holds stockPrices, quantities, and nationCode

I need to get the AVERAGE value (stockPrice * quantities * exchangeRates) for US Dollars.


This is what I have now:


SELECT AVG (stockPrice * quantities * exchrate) AS averageStock FROM stock JOIN nation
WHERE stock.natcode = nation.natcode


This works, but it includes the exchange rates for ALL nations.
How can I make it so that it gives me the average for ONLY U.S exchange rate?

Basically: stockPrice * quantities * (the U.S exchangeRate from tblOne).

Thanks for any help!

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-02-04 : 01:08:01
SELECT AVG (stockPrice * quantities * exchrate) AS averageStock FROM stock JOIN nation
WHERE stock.natcode = nation.natcode and stock.exchrate='US'
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-02-04 : 01:09:22
Try
SELECT AVG (stockPrice * quantities * exchrate) AS averageStock FROM stock JOIN nation
WHERE stock.natcode = nation.natcode and tblone.nationCode = 'US code'
Go to Top of Page

stangn99
Starting Member

7 Posts

Posted - 2008-02-04 : 01:16:06
quote:
Originally posted by ayamas

SELECT AVG (stockPrice * quantities * exchrate) AS averageStock FROM stock JOIN nation
WHERE stock.natcode = nation.natcode and stock.exchrate='US'




Hmmmm. I get this:

"Error converting data type varchar to numeric"
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-02-04 : 01:18:03
Can you post your DDL with sample data for both the tables? It will help to find cause of the error mentioned.
Go to Top of Page

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-02-04 : 01:20:41
quote:
Originally posted by sunil

Try
SELECT AVG (stockPrice * quantities * exchrate) AS averageStock FROM stock JOIN nation
WHERE stock.natcode = nation.natcode and tblone.nationCode = 'US code'




You neeed to put the exchange rate id for US if you have one.
Go to Top of Page

stangn99
Starting Member

7 Posts

Posted - 2008-02-04 : 01:22:54
quote:
Originally posted by sunil

Can you post your DDL with sample data for both the tables? It will help to find cause of the error mentioned.



Sorry...whats a DDL?

Here is the Content of SQL file that I was given to work with.




if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[nation]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[nation]

GO



if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[stock]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[stock]

GO



create table nation(

natcode char(3) not null,

natname char(20),

exchrate decimal(9,5),

primary key (natcode));



insert into nation values ('UK','United Kingdom',1);

insert into nation values ('USA','United States',0.67);

insert into nation values ('AUS','Australia',0.46);

insert into nation values ('IND','India',0.0228);

GO



create table stock(

stkcode char(3) not null,

stkfirm char(20),

stkprice decimal(6,2),

stkqty decimal(8),

stkdiv decimal(5,2),

stkpe decimal(5),

natcode char(3),

primary key (stkcode),

constraint fk_hasnation foreign key (natcode) references nation(natcode)

ON DELETE NO ACTION);



insert into stock values ('FC','Freedonia Copper',27.5,10529,1.84,16,'UK');

insert into stock values ('PT','Patagonian Tea',55.25,12635,2.5,10,'UK');

insert into stock values ('AR','Abyssinian Ruby',31.82,22010,1.32,13,'UK');

insert into stock values ('SLG','Sri Lankan Gold',50.37,32868,2.68,16,'UK');

insert into stock values ('ILZ','Indian Lead & Zinc',37.75,6390,3,12,'UK');

insert into stock values ('BE','Burmese Elephant',0.07,154713,0.01,3,'UK');

insert into stock values ('BS','Bolivian Sheep',12.75,231678,1.78,11,'UK');

insert into stock values ('NG','Nigerian Geese',35,12323,1.68,10,'UK');

insert into stock values ('CS','Canadian Sugar',52.78,4716,2.5,15,'UK');

insert into stock values ('ROF','Royal Ostrich Farms',33.75,1234923,3,6,'UK');

insert into stock values ('MG','Minnesota Gold',53.87,816122,1,25,'USA');

insert into stock values ('GP','Georgia Peach',2.35,387333,0.2,5,'USA');

insert into stock values ('NE','Narembeen Emu',12.34,45619,1,8,'AUS');

insert into stock values ('QD','Queensland Diamond',6.73,89251,0.5,7,'AUS');

insert into stock values ('IR','Indooroopilly Ruby',15.92,56147,0.5,20,'AUS');

insert into stock values ('BD','Bombay Duck',25.55,167382,1,12,'IND');

GO



I am stuck on answering this:

List the average value for a stock holding in UK pounds. It sounds so easy...but i've been stuck on this question for over 2 hours now and finally decided to seek help
Go to Top of Page

stangn99
Starting Member

7 Posts

Posted - 2008-02-04 : 01:29:04
OK....I'm not sure if this is actually answering the question, but it seems to work with no errors.


SELECT AVG(stkprice * stkqty * exchrate) AS averageStock FROM stock, nation
WHERE stock.natcode = nation.natcode AND nation.natcode = 'UK'


I just added "nation" before "natcode" on the second line.
Go to Top of Page

stangn99
Starting Member

7 Posts

Posted - 2008-02-04 : 01:33:38
Sorry,
I actually removed "JOIN" and replaced it with a ","


If I left "JOIN" I got another error.
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-02-04 : 01:44:38
Use ANSI join syntax:

SELECT AVG (stock.stkprice * stock.stkqty * nation.exchrate) AS averageStock FROM stock inner JOIN nation
on stock.natcode = nation.natcode and nation.natCode = 'USA'
Go to Top of Page

stangn99
Starting Member

7 Posts

Posted - 2008-02-04 : 12:21:26
quote:
Originally posted by sunil

Use ANSI join syntax:

SELECT AVG (stock.stkprice * stock.stkqty * nation.exchrate) AS averageStock FROM stock inner JOIN nation
on stock.natcode = nation.natcode and nation.natCode = 'USA'



OK! That worked GREAT!

Why did you use "ON"? and what is the difference between inner Join and outer Join? How come just "JOIN" would not work?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-04 : 12:39:56
ON simply specifies what field(s) you need to match on from tables involved.Inner join will return only matched records from both tables while outer join returns all records from one of the table (first or second or both based on LEFT or RIGHT or FULL clause) and matched record values from second table and with NULL values for unmatched rows.
JOIN is same as INNER JOIN. so probably you didnt had right fields specified for join condition which caused spurious results.
Go to Top of Page

stangn99
Starting Member

7 Posts

Posted - 2008-02-04 : 18:03:02
quote:
Originally posted by visakh16

ON simply specifies what field(s) you need to match on from tables involved.Inner join will return only matched records from both tables while outer join returns all records from one of the table (first or second or both based on LEFT or RIGHT or FULL clause) and matched record values from second table and with NULL values for unmatched rows.
JOIN is same as INNER JOIN. so probably you didnt had right fields specified for join condition which caused spurious results.



Great!
Thanks for clearing that up. I just tried with only "Join" and it worked...so I guess I was making and error somewhere.

Thanks again
Go to Top of Page
   

- Advertisement -