Results 1 to 11 of 11

Thread: [RESOLVED] Need help with date

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Resolved [RESOLVED] Need help with date

    Hello everybody,

    I need some hint about WHERE clause for my query. I have a customer table and a session history table with CustomerID as foreign key and LoginDate. When a customer logs in, a record is entered in session history table with current date and time.

    I need to pick all the customers who have NOT logged in for last 15 days.

    Any idea??

    Thanks.
    Last edited by usamaalam; Jul 30th, 2008 at 06:38 AM. Reason: Not resolved

  2. #2
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: Need help with date

    Something like this?

    Select CustomerID
    From sessionhistorytable
    Where loginDate Between '1900-01-01' and Current Date - 15 Days

    This should return all users that have NOT logged in within the last 15 days.

    Please note, this is DB2, so the syntax may differ slightly depending on what dbms you are using.
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  3. #3

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Need help with date

    I'll test it, but I think this will show records between '1900-01-01' and (Current Date - 15) Days. Please note according to the schema, if a customer has not logged in within last 15 days, there is no record with his ID in SessionHistory table for the dates between current date and (current date - 15 days). He may have a record two months ago or 25 days ago or may be not record at all.

    Thanks.

  4. #4
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: Need help with date

    vb Code:
    1. select CustomerID, LoginDate from Customer, SessionHistory
    2. where SessionHistory.CustomerID = Customer.CustomerID
    3. and SessionHistory.LoginDate < getdate() - 15
    Microsoft Techie

  5. #5

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Need help with date

    Great, thanks a lot.

  6. #6
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: [RESOLVED] Need help with date

    edit: was posted my response after pvbangera.
    Last edited by Blakk_Majik; Jul 29th, 2008 at 07:42 AM.
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  7. #7

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Need help with date

    Sorry for re-opening this thread. My sp gives me some invalid results, it gives me customers who have logged in today as well.

    Code:
    CREATE PROCEDURE pcsn_GetCustomersNotLoggedIn
    
    @NumDays AS INT
    
    AS
    
    SELECT DISTINCT
    	(
    		SELECT 
    			MAX([SessionHistory].[LoginTime])
    		FROM
    			[SessionHistory]
    		WHERE
    			[vuCustomers].[ID] = [SessionHistory].[FKCustomerID]
    	) AS [LastLoginDate],
    	[vuCustomers].[ID],
    	[vuCustomers].[CustomerNumber],
    	[vuCustomers].[Password],
    	[vuCustomers].[MembershipDate],
    	[vuCustomers].[RenewalDate],
    	[vuCustomers].[ExpiryDate],
    	[vuCustomers].[FirstName],
    	[vuCustomers].[LastName],
    	[vuCustomers].[Email],
    	[vuCustomers].[CompanyName],
    	[vuCustomers].[PhoneNumber],
    	[vuCustomers].[CellNumber],
    	[vuCustomers].[Address],
    	[vuCustomers].[City],
    	[vuCustomers].[Zip],
    	[vuCustomers].[FKCountryID],
    	[vuCustomers].[Country],
    	[vuCustomers].[FKPlanID],
    	[vuCustomers].[Plan],
    	[vuCustomers].[IsActive]
    FROM
    	[vuCustomers]
    LEFT JOIN
    	[SessionHistory]
    ON
    	[vuCustomers].[ID] = [SessionHistory].[FKCustomerID]
    WHERE
    	[SessionHistory].[LoginTime]  < GETDATE()  - @NumDays
    ORDER BY
    	[vuCustomers].[CustomerNumber]
    ASC
    GO
    Any ideas??

    Thanks.

  8. #8
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: Need help with date

    Code:
    CREATE PROCEDURE pcsn_GetCustomersNotLoggedIn
    
    @NumDays AS INT
    
    AS
    
    SELECT DISTINCT
    	(
    		SELECT 
    			MAX([SessionHistory].[LoginTime])
    		FROM
    			[SessionHistory]
    		WHERE
    			[vuCustomers].[ID] = [SessionHistory].[FKCustomerID]
    		AND
    		        [SessionHistory].[LoginTime]  < GETDATE()  - @NumDays
    	) AS [LastLoginDate],
    	[vuCustomers].[ID],
    	[vuCustomers].[CustomerNumber],
    	[vuCustomers].[Password],
    	[vuCustomers].[MembershipDate],
    	[vuCustomers].[RenewalDate],
    	[vuCustomers].[ExpiryDate],
    	[vuCustomers].[FirstName],
    	[vuCustomers].[LastName],
    	[vuCustomers].[email],
    	[vuCustomers].[CompanyName],
    	[vuCustomers].[PhoneNumber],
    	[vuCustomers].[CellNumber],
    	[vuCustomers].[Address],
    	[vuCustomers].[City],
    	[vuCustomers].[Zip],
    	[vuCustomers].[FKCountryID],
    	[vuCustomers].[Country],
    	[vuCustomers].[FKPlanID],
    	[vuCustomers].[Plan],
    	[vuCustomers].[IsActive]
    FROM
    	[vuCustomers]
    LEFT JOIN
    	[SessionHistory]
    ON
    	[vuCustomers].[ID] = [SessionHistory].[FKCustomerID]
    WHERE
    	[SessionHistory].[LoginTime]  < GETDATE()  - @NumDays
    ORDER BY
    	[vuCustomers].[CustomerNumber]
    ASC
    GO
    Microsoft Techie

  9. #9

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Need help with date

    I think its showing the customers who have logged in before 15 days. Please see the first image where I am trying to get the customers who have NOT logged in within last 15 days and notice the highlighted date with ID = 86.

    However, notice the second image from actual table which says that customer with ID = 86 has logged in last time on 30th July, 2008. It means this customer shouldn't show up as a result and only those customers should show up who have not logged in after 14th July, i.e. within last 15 days.

    Thanks.
    Attached Images Attached Images   

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Need help with date

    I think you will have to use Not Exists and a subquery.

    Code:
    SELECT DISTINCT
    	(SELECT MAX([SessionHistory].[LoginTime]) FROM [SessionHistory] 
         WHERE [vuCustomers].[ID] = [SessionHistory].[FKCustomerID]) AS [LastLoginDate],
    	[vuCustomers].[ID],
    	[vuCustomers].[CustomerNumber],
    	[vuCustomers].[Password],
    	[vuCustomers].[MembershipDate],
    	[vuCustomers].[RenewalDate],
    	[vuCustomers].[ExpiryDate],
    	[vuCustomers].[FirstName],
    	[vuCustomers].[LastName],
    	[vuCustomers].[email],
    	[vuCustomers].[CompanyName],
    	[vuCustomers].[PhoneNumber],
    	[vuCustomers].[CellNumber],
    	[vuCustomers].[Address],
    	[vuCustomers].[City],
    	[vuCustomers].[Zip],
    	[vuCustomers].[FKCountryID],
    	[vuCustomers].[Country],
    	[vuCustomers].[FKPlanID],
    	[vuCustomers].[Plan],
    	[vuCustomers].[IsActive]
    FROM [vuCustomers]
    WHERE Not Exists 
          (Select FKCustomerID From SessionHistory 
           Where [SessionHistory].[LoginTime] > GETDATE()  - @NumDays And
                 FKCustomerID = [vuCustomers].[ID])
    ORDER BY [vuCustomers].[CustomerNumber] ASC

  11. #11

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Need help with date

    This is perfect, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width