Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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


Reply With Quote
  #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
Reply With Quote
  #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

Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.