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 |
|
Kevin2006
Starting Member
7 Posts |
Posted - 2006-07-11 : 10:02:23
|
| Problem: Tank monitoring systems are installed at customer sites to read the inventory level in inches of the product in their tanks. This occures nightly and the reading level is recorded in the reading table. Customers are invoiced based on their usage either weekly, bi-weekly, or monthly. For the bi-weekly report I need to retrieve the inventory level for two sundays ago (ex. 6/25/2006) the current weeks sunday (ex. 07-09-2006), deliveries that occured between 06-26-2006 and 07-02-2006 as week 1 deliveries, deliveries that occured between 07-02-2006 and 07-09-2006 as week 2 deliveries, the sum of beginning inventory deliveries week 1 and deliveries week 2 - ending inventory as usage for each site that has invoicing equal to B (bi-weekly). Tables:SiteSiteID (PK, Identity,int)CompanyName (varchar)Address (varchar)refilllevel (int)lbsperinch (float) *the monitor in the tank reads inches, billing and deliveries are in lbsinvoicing (varchar) * can be bi-weekly, weekly, monthlyReadingReadingID (PK, Identity, int)SiteiID (FK, int)Reading (float) *inches of product in tankReadingDT (datetime)DeliveriesDeliveryID (PK, Identity, Int)SiteID (FK, Int)DeliveryLBS (int)DeliveryDT (datetime)If anyone can point me to some example queries/ stored procedures that do something similar i would be greatly apreciative. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-07-11 : 11:00:59
|
| Which bit are you having problems with.Post what you have tried so far and people will suggest where you should go.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Kevin2006
Starting Member
7 Posts |
Posted - 2006-07-11 : 12:15:25
|
Ok nobody laugh, i am learning. :) Here is my stored procedure. I am not sure how to calculate the usage column and i am not sure how to break deliveries up into two columns, one showing the 1st week deliveries and one showing the 2nd. Also on the rare occasion there are two readings in one day how do i make sure i get the latest one in the day. Thanks,Kevinset ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author: Kevin McPhail-- Create date: 07-11-2006-- Description: Retrieve BI-Weekly Inventory Report-- =============================================ALTER PROCEDURE [dbo].[GetBiWeeklyUsage] -- Add the parameters for the stored procedure here AS DECLARE @dtGetDate DateTime; DECLARE @dtStartDate DateTime; DECLARE @dtEndDate DateTime; SELECT @dtGetDate = cast(floor(cast(getdate() as float)) as datetime) SELECT @dtEndDate =DATEADD(dd, 1 - DATEPART(dw, @dtGetDate), @dtGetDate) SELECT @dtStartDate = @dtEndDate-14BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT Site.CompanyName, Site.LocationCity, Site.LocationState, Site.LbsPerInch, (SELECT Sum(Deliveries.Deliver_LBS) FROM Deliveries WHERE Deliveries.DeliveryDt >= @DtStartDate and Deliveries.DeliveryDt < @dtEndDate and Site.SiteID = Deliveries.SiteID) AS DeliveryLBS, (SELECT Reading.Reading * Site.LbsPerInch FROM Reading WHERE reading.readingdt >= @dtStartDate and reading.readingdt < (@dtStartDate +1) and Site.SiteID = Reading.SiteID) AS BeginningInvlbs, (SELECT Reading.Reading * Site.LbsPerInch FROM Reading WHERE reading.readingdt >= @dtEndDate and reading.readingdt < (@dtEndDate +1) and Site.SiteID = Reading.SiteID) AS EndingInvLbs FROM Site WHERE Site.Invoicing = 'B'END |
 |
|
|
Kevin2006
Starting Member
7 Posts |
Posted - 2006-07-12 : 09:45:53
|
| I have a few questions regarding the stored procedure above. First is using subselects to get the columns from the other two tables the right approach or should i be exploring inner/outer joins. Also if i want to get a max of the date selected for beginninginv and ending inv what in the query would i wrap the max around. I.E. i want to get the last reading on sunday for the end date and the last reading on the sunday two weeks prior. Occasionally there are two readings in the same day (very rare) which of course creates an exception in the SP. Finally i need to calculate a usage column for the result set that is the sum of beginninginv+deliveries-endinginv. the calculations for these three columns are already in the result set but i am not sure how i can reuse them for usage. I.E. beginninginv + deliveries - endinginv as usage. I realize after spending a good deal of time reading through these forums that a more appropriate place for my original post and these followups would be the new to sql forum (as I am definately a beginner) so if one of the moderators wants to move it that would be fine.Thanls for any assistance,Kevin |
 |
|
|
Kevin2006
Starting Member
7 Posts |
Posted - 2006-07-13 : 11:09:17
|
| Anyone have a chance to take a look at this? |
 |
|
|
|
|
|
|
|