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 |
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2004-12-26 : 13:25:16
|
| I have one table that collects a count on differnt areas. Basically it updates with another 1 each time a page is loaded on the website.What I want to do is put together a report that can basically only pull in the items that have a count in the table. For instance I have 700 items but only want to pull in the records that have items in the database, Say 50 of those 700 items. But in the database out of those 50 you could have 7 items for one of those 50 items. I have figured out the count to total the items 7 times on just doing a report for the single item. But want a report that would show all items in the table on the same report. Plus I have other description data in another table for each of the items I want to include in the report.Table 1 is the collection tableTableID, ItemID, ItemCountExample:TableID: 1, ItemID: 1, ItemCount: 1TableID: 2, ItemID: 10, ItemCount: 1TableID: 3, ItemID: 20, ItemCount: 1TableID: 4, ItemID: 405, ItemCount: 1TableID: 5, ItemID: 700, ItemCount: 1Second Desrcitpion TableItemID, Item, ItemLogoItemID: 1, Item: Work Boots, ItemLogo: Boots.gifItemID: 10, Item: Knifes, ItemLogo: Knife.gifItemID: 20, Item: Discs, ItemLogo: Disc.gifItemID: 405, Item: Tools, ItemLogo: Tools.gifItemID: 700, Item: Bolts, ItemLogo: Bolts.gifResulting Report:Work Boots, Boots.gif, ItemCount: 10Knifes, Knife.gif, ItemCount: 12Discs, Discs.gif, ItemCount: 15Tools, Tools.gif, ItemCount: 52Bolts, Bolts.gif, ItemCount: 92I am guessing it would require a double join of some sort in the SQL statement.Any help would be much appreciated.. |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-12-26 : 15:58:50
|
| Are You still having trouble?Post the SQL that You have so far...rockmoose |
 |
|
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2004-12-26 : 20:34:25
|
| SELECT SUM(ItemCount) AS [ReviewCount] from tbl_AD_Stats"This is in ASP code SQL statement that pulls the one item at a item at a time report. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-12-26 : 20:49:38
|
| Use QA (Query Analyzer) to test and work with your SQL before putting it in the ASP page.Are You after something like this?:SELECT t2.Item, t2.ItemLogo, SUM(t1.ItemCount) AS [ItemCount]FROM tbl_AD_Stats t1 JOIN tbl_Description t2 ON t1.ItemID = t2.ItemIDGROUP BY t2.Item, t2.ItemLogorockmoose |
 |
|
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2004-12-26 : 21:27:10
|
| That seems to have worked in Query Analyzer. I will need to group by one more item but will try and figure that out on my own for now. I appreciate your help rockmoose! |
 |
|
|
JimAmigo
Posting Yak Master
119 Posts |
Posted - 2004-12-30 : 12:13:46
|
quote: Originally posted by rockmoose Use QA (Query Analyzer) to test and work with your SQL before putting it in the ASP page.Are You after something like this?:SELECT t2.Item, t2.ItemLogo, SUM(t1.ItemCount) AS [ItemCount]FROM tbl_AD_Stats t1 JOIN tbl_Description t2 ON t1.ItemID = t2.ItemIDGROUP BY t2.Item, t2.ItemLogorockmoose
This worked for the sum of one item, what if I wanted to use another field that I needed to group the SUM ItemCount down by? I have another field called AREAID that tells me where the count is coming from.So if I use my original example with more detail the report would look like this.Work Boots, Boots.gif, AreaID 1, ItemCount: 5Work Boots, Boots.gif, AreaID 2, ItemCount: 3Work Boots, Boots.gif, AreaID 3, ItemCount: 2Knifes, Knife.gif, AreaID 1, ItemCount: 6Knifes, Knife.gif, AreaID 2, ItemCount: 4Knifes, Knife.gif, AreaID 3, ItemCount: 2Discs, Discs.gif, AreaID 1, ItemCount: 5Discs, Discs.gif, AreaID 2, ItemCount: 4Discs, Discs.gif, AreaID 3, ItemCount: 6Tools, Tools.gif, AreaID 1, ItemCount: 20Tools, Tools.gif, AreaID 2, ItemCount: 19Tools, Tools.gif, AreaID 3, ItemCount: 13Bolts, Bolts.gif, AreaID 1, ItemCount: 50Bolts, Bolts.gif, AreaID 1, ItemCount: 30Bolts, Bolts.gif, AreaID 1, ItemCount: 12I am thinking there would be a way to do this with one SQL statement. Thanks for all your help. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-01-02 : 15:50:59
|
| >> This worked for the sum of one item, what if I wanted to use another field that I needed to group the SUM ItemCount down by? I have another field called AREAID that tells me where the count is coming from.Just one item?>> I have another field called AREAID that tells me where the count is coming from.You would have to group by the AREAID field as well.If You are still stuck, please post the complete table DDL, and the SQL You have so far.I cannot tell in which table the AREAID field is located.rockmoose |
 |
|
|
|
|
|
|
|