Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 8th, 2006, 03:29 PM
Authorized User
 
Join Date: Mar 2005
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to miamikk
Default Querying table2 with results from table1

I need help in writing a query.

The query should get top 10 items and their values from current year and the values for the same items from previous year table.

I was able to write the code for 1st part that gets values from 1st table but I don't know how to get the values from 2nd table.

The 2 tables does not have any primary/foreign key relations. Both tables have same structure and same columns.



I am attaching some images below to give more information.

Image of results from my query.
http://www.fiu.edu/%7Eatmakurk/ustrade/7_1.jpg

Image of how the final output should look like.
http://www.fiu.edu/%7Eatmakurk/ustrade/7_2.jpg

The Store Procedure code is:

ALTER Procedure [dbo].[free_customsHS4](
@TblName1 varchar(20),
@TblType varchar(20),
@District varchar(6),
@Month varchar(3)
)


AS
Begin
SET NOCOUNT ON;

Declare @SQuery nvarchar(3000)
set @TblName1 = '[' + @TblName1 + ']'
set @TblType = '[' + @TblType + ']'


SELECT @SQuery = 'select top 10 a.commodity1 as HS4, b.descrip_1 as Description,
sum(a.all_val_mo) as [Amount],

(sum(a.all_val_mo)/(select Sum(a.all_val_mo) FROM ' + @TblName1 + 'a
where a.stat_month <=' + @Month + ' and a.district=' + @District +'))*100 as [% Share]

FROM ' + @TblName1 + ' a left outer join ' + @TblType + ' b on a.commodity1=b.commodity1
where a.stat_month <=' + @Month + ' and a.district=' + @District +'
Group by a.commodity1, b.descrip_1
order by [Amount] desc'

EXEC sp_executesql @SQuery
END


  #2 (permalink)  
Old November 8th, 2006, 03:35 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there.. if they have the same structure they have key fields that are the same. Maybe the relations no exists in the database, but the data is relationated anyway.. both of your table has a hs4 field that look the same. Can you just join the table with that field???

HTH

Gonzalo
  #3 (permalink)  
Old November 8th, 2006, 04:53 PM
Authorized User
 
Join Date: Mar 2005
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to miamikk
Default

When I do a Join, the results are completely different. I am not sure I am doing the join right.


I was playing around with my code and I was able to calculate Amount2 for previous year but I am still stuck at Share2. The code after modification is below: (I pass 2 table names and 2 where conditions)


SELECT @SQuery = 'select top 10 a.commodity1 as HS4, sum(a.all_val_mo) as [Amount1],

(sum(a.all_val_mo)/(select Sum(a.all_val_mo) FROM ' + @TblName1 + 'a where a.stat_month <=' + @Month + ' and a.district=' + @District +'))*100 as [% Share1],

(select sum(b.all_val_mo) from '+ @TblName2 +' b where b.commodity1=a.commodity1 and b.stat_month <=' + @Month + ' and b.district=' + @District +') as [Amount2]

--(sum(b.all_val_mo)/(select Sum(b.all_val_mo) FROM ' + @TblName2 + 'b where b.commodity1=a.commodity1 and

--b.stat_month <=' + @Month + ' and b.district=' + @District +'))*100 as [% Share2]

from '+ @TblName1 + 'a where a.stat_month <=' + @Month + ' and a.district=' + @District +'

Group by a.commodity1 order by [Amount1] desc'

EXEC sp_executesql @SQuery
END



The results look like
http://www.fiu.edu/~atmakurk/ustrade/7-5.jpg

  #4 (permalink)  
Old November 9th, 2006, 09:22 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

But that's not the result you want???



HTH

Gonzalo


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code on table2.php aztek_2006 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 April 13th, 2009 07:31 PM
table2.php in chapter 4 - blank page ?? redarrow BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 2 April 13th, 2009 07:27 PM
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM
"Insert into" close reopen table2 for display gilgalbiblewheel Classic ASP Databases 8 September 25th, 2004 06:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.