Query or SP question

  • I am trying to determine if a job transfers and sells again what the previous job was...I currently have it so:

    FnameLnameCust_IDLotIDSaleDateCancelDateIsTransfer
    BettyBotox1213/23/04 4:40 PM3/23/04 4:42 PM0
    BettyBotox1213/30/04 8:36 AM3/30/04 8:44 AM0
    BettyBotox1263/30/04 9:14 AM3/31/04 12:12 PM1
    BettyBotox1243/31/04 2:46 PM3/31/04 3:36 PM1
    BettyBotox1293/31/04 3:38 PMNULL0
    billsmith13213/22/04 9:32 AMNULL0
    BoyGeorge33333/23/04 4:11 PMNULL0
    brucewayne2221123/15/04 8:57 AM3/15/04 9:04 AM0
    brucewayne2221123/15/04 9:57 AM3/16/04 2:51 PM0
    CarolDouis3223233/31/04 8:53 AMNULL0
    JohnSchnieder3354353/30/04 8:42 AMNULL0
    KrisLee3123433/19/04 9:55 AMNULL0
    KristiFioca2133233/30/04 8:42 AMNULL0
    LynSmith33332323/31/04 8:54 AMNULL0

    I need to have it such:

    FnameLnameCust_IDLotIDSaleDateCancelDateIsTransferTransferredFromLotID
    BettyBotox1213/23/04 4:40 PM3/23/04 4:42 PM0Null
    BettyBotox1213/30/04 8:36 AM3/30/04 8:44 AM0Null
    BettyBotox1263/30/04 9:14 AM3/31/04 12:12 PM1Null
    BettyBotox1243/31/04 2:46 PM3/31/04 3:36 PM16
    BettyBotox1293/31/04 3:38 PMNULL04
    billsmith13213/22/04 9:32 AMNULL0Null
    BoyGeorge33333/23/04 4:11 PMNULL0Null
    brucewayne2221123/15/04 8:57 AM3/15/04 9:04 AM0Null
    brucewayne2221123/15/04 9:57 AM3/16/04 2:51 PM0Null
    CarolDouis3223233/31/04 8:53 AMNULL0Null
    JohnSchnieder3354353/30/04 8:42 AMNULL0Null
    KrisLee3123433/19/04 9:55 AMNULL0Null
    KristiFioca2133233/30/04 8:42 AMNULL0Null
    LynSmith33332323/31/04 8:54 AMNULL0Null

    So basically if a sale cancels and it is a transfer, on the next sale I need to know from which lot

    a customer transfered from.

    Any help is appreciated!!!

     

  • If using SQL 200 then simple create a USer Defined Function to lookup the previous value like so.

    CREATE FUNCTION dbo.PreviousLot (@Cust_ID int, @Cur_Lot int)

    RETURNS int

    AS

    BEGIN

     DECLARE @chkLot int,

      @chkDate datetime

     SELECT

      TOP 1

      @chkLot = S1.LotID,

      @chkDate = S1.CancelDate

     FROM

      dbo.tblSales S1

     INNER JOIN

      dbo.TblSales S2 

     ON

      S1.Cust_ID = S2.Cust_ID AND

      IsNull(S1.CancelDate,'19000101') < S2.SaleDate AND

      S1.LotID != S2.LotID AND

      S2.LotID = @Cur_Lot

     WHERE

      S1.Cust_ID = @Cust_ID

     ORDER BY

      S1.SaleDate DESC

     DECLARE @output int

     SET @output = (CASE WHEN @chkDate IS NULL THEN @chkLot ELSE NULL END)

     RETURN (@output)

    END

    Then in you select do this

    SELECT (CASE WHEN IsTransfer = 1 THEN dbo.PreviousLot(Cust_ID, LotID) ELSE NULL END) AS TransferredFromLotID ....

    See if this helps.

  • Antares, I think you're on the right track but it came up wrong...this is how it came up...

    FnameLnameCust_IDLotIDSaleDateCancelDateIsTransferTransferredFromLotID
    BettyBotox1213/23/04 4:40 PM3/23/04 4:42 PM0Null
    BettyBotox1213/30/04 8:36 AM3/30/04 8:44 AM0Null
    BettyBotox1263/30/04 9:14 AM3/31/04 12:12 PM19
    BettyBotox1243/31/04 2:46 PM3/31/04 3:36 PM19
    BettyBotox1293/31/04 3:38 PMNULL0Null
    billsmith13213/22/04 9:32 AMNULL0Null
    BoyGeorge33333/23/04 4:11 PMNULL0Null
    brucewayne2221123/15/04 8:57 AM3/15/04 9:04 AM0Null
    brucewayne2221123/15/04 9:57 AM3/16/04 2:51 PM0Null
    CarolDouis3223233/31/04 8:53 AMNULL0Null
    JohnSchnieder3354353/30/04 8:42 AMNULL0Null
    KrisLee3123433/19/04 9:55 AMNULL0Null
    KristiFioca2133233/30/04 8:42 AMNULL0Null
    LynSmith33332323/31/04 8:54 AMNULL0Null

    Thanks for trying, let me know if you find anything more on it!

  • In the previous code change this

    IsNull(S1.CancelDate,'19000101') < S2.SaleDate

    To This

    S1.CancelDate < S2.SaleDate

     

    And try again.

  • Thanks for the prompt reply! Came up as all nulls that time

     

    FnameLnameCust_IDLotIDSaleDateCancelDateIsTransferTransferredFromLotID
    BettyBotox1213/23/04 4:40 PM3/23/04 4:42 PM0Null
    BettyBotox1213/30/04 8:36 AM3/30/04 8:44 AM0Null
    BettyBotox1263/30/04 9:14 AM3/31/04 12:12 PM1Null
    BettyBotox1243/31/04 2:46 PM3/31/04 3:36 PM1Null
    BettyBotox1293/31/04 3:38 PMNULL0Null
    billsmith13213/22/04 9:32 AMNULL0Null
    BoyGeorge33333/23/04 4:11 PMNULL0Null
    brucewayne2221123/15/04 8:57 AM3/15/04 9:04 AM0Null
    brucewayne2221123/15/04 9:57 AM3/16/04 2:51 PM0Null
    CarolDouis3223233/31/04 8:53 AMNULL0Null
    JohnSchnieder3354353/30/04 8:42 AMNULL0Null
    KrisLee3123433/19/04 9:55 AMNULL0Null
    KristiFioca2133233/30/04 8:42 AMNULL0Null
    LynSmith33332323/31/04 8:54 AMNULL0Null
  • Antares, I think I got it!

    Create  FUNCTION dbo.PreviousLot (@Cust_ID int, @Cur_Lot int, @SaleDate datetime)

    RETURNS int

    AS

    BEGIN

     DECLARE

      @chkCust int,

      @chkLot int,

      @chkCancelDate datetime

    select @chkCust=a.cust_id,@chkLot=a.lotid,@chkCancelDate=max(b.canceldate)

      from tblSales b inner join tblsales a

      on b.canceldate<@SaleDate

      and b.cust_id=@Cust_ID

      and b.istransfer=1

      group by a.cust_id,a.lotid

    DECLARE @output int

     SET @output = (select lotid

      from tblSales

      where CancelDate=@chkCancelDate

      and cust_id=@Cust_ID)

     RETURN (@output)

    END

     

    select *,dbo.PreviousLot(cust_id,lotid,saledate)

    from tblSales

    Thanks for pointing me in the right direction, I don't think I ever would have thought to do it in a function!

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply