•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 456,536 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,073 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 1471 | Replies: 2
![]() |
| |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Hello, I am trying to write a single query to run a report. I have it working for a single farm, but I need it to work for a list of farms. I am having trouble linking the farm number for the inner query to the farm number for the outer query. Anyone have any ideas on how to make this work. I have racked my brain and cannot make it work.
Basically I need to get the average mortality of the last 6 flocks, and the APC from the current flock out of a table for this report.
Single farm query that works:
But whenever I replace the hard coded value of '2073' with the farmno from the outer select I get no records because it cannot get the correct values for the TOP specifications in the inner queries. I really need to do this with a single query if possible to plug into the report, otherwise I will have to scrap the report and code all of this. If there is is no other option, then I don't mind writing the code; but using the reporting enigine to do this would be nice.
Any ideas would be greatly appreciated. Thanks in advance for any help.
Scott
Basically I need to get the average mortality of the last 6 flocks, and the APC from the current flock out of a table for this report.
Single farm query that works:
select * from (select Top 1 t1.farmno, avg(t1.Mort0Pct) * 100 as SixFlockAvgMort0, min((AvgAdjustedPrimeCost2 - AdjustedPrimeCost) * 100) as APCRating from (select top 6 zst.FarmNo, (cast(zst.MortalityWeek0 as float) / cast(zst.HeadPlaced as float)) as Mort0Pct, zst.settledate FROM zrstSettle zst where zst.MinGuaranteeFlockExcludeFlag = 0 AND zst.ApprovedFlag = 1 and zst.farmno = '2073' order by zst.settledate desc) t1 Inner join zrstSettle on t1.FarmNo = zrstSettle.FarmNo where zrstSettle.farmno = '2073' AND zrstSettle.MinGuaranteeFlockExcludeFlag = 0 AND zrstSettle.ApprovedFlag = 1 group by t1.farmno, zrstSettle.settledate order by SixFlockAvgMort0 desc) t2 where farmno = '2073' order by t2.SixFlockAvgMort0 desc
But whenever I replace the hard coded value of '2073' with the farmno from the outer select I get no records because it cannot get the correct values for the TOP specifications in the inner queries. I really need to do this with a single query if possible to plug into the report, otherwise I will have to scrap the report and code all of this. If there is is no other option, then I don't mind writing the code; but using the reporting enigine to do this would be nice.
Any ideas would be greatly appreciated. Thanks in advance for any help.
Scott
if you used SP, it'd be better, as you could use variables and so on, and also for performance
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
if you used SP, it'd be better, as you could use variables and so on, and also for performance
Thanks RamyMahrous. Unfortunately the reporting engine only works with a SQL statement. I went ahead and just coded the report. It was fairly easy to manipulate in code. I appreciate the reply.
Scott
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- C++ Performance Tips (C++)
- Python Tuples and Mysql Query (MySQL)
- Python tuples and Sql query (Python)
- QBasic Nested for loop (Legacy and Other Languages)
- MySQL nested query / joined query conversion help (MySQL)
- complex arithmatic (C++)
- MySQL vs. PostgreSQL (MySQL)
- ASP slow-down server script (ASP)
Other Threads in the MS SQL Forum
- Previous Thread: auto generate id numbers
- Next Thread: Restricting access to db


Hybrid Mode