Solved

sql calculate reminders

Posted on 2016-09-02
11
56 Views
Last Modified: 2016-09-09
I need to create a query about car service reminders. The car can have 3 type of reminders: 2- Registration, 3 - Emission Test, 4 - State Inspection

The car table has the last date the task was performed.
CarActivitiees table has all prams repeat very 2 years , remind 10 days before it is due and so on

carActivities dataStructure
id      int       primary key
activityID      int        0 - oil change, 1-oil Change; 2 - Registration; 3-Emission;4-State Inspection
repeatNumber      int       (every 2 days, or every 2 years )
repeatCycle      int       (every 2 days, or every 2 years )
remindNumber      int       (every 2 days, or every 2 years )
remindCycle      int       (every 2 days, or every 2 years )
carKey      int      the car we are dealing with
0
Comment
Question by:erikTsomik
  • 7
  • 3
11 Comments
 
LVL 19

Author Comment

by:erikTsomik
ID: 41782288
Any suggestions?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41782310
That would be vastly easier to do if you provided an actual table definition (CREATE TABLE ...) for carActivities and car.  Can't even tell the date column(s) to use from what you posted.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 41782313
CREATE TABLE [dbo].[carActivities](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[activityID] [int] NOT NULL,
	[repeatNumber] [int] NOT NULL,
	[repeatCycle] [int] NOT NULL,
	[remindNumber] [int] NOT NULL,
	[remindCycle] [int] NOT NULL,
	[carKey] [int] NOT NULL,
 CONSTRAINT [PK_car_activities] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'1-oil Change; 2 - Registration; 3-Emission;4-State Inspection' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'carActivities', @level2type=N'COLUMN',@level2name=N'activityID'
GO

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41782322
The car table has the last date the task was performed.

What is that table's structure?  Is there an activity id on it as well?  Or are all the dates de-normalized into one row?
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 41782324
here is table car Structure . The fields that I am interesting in are [regdate] ,[emissiondate],      [inspectiondate], and oilChangeMilage which is the last milage when the oil chnage was done. The dates are denormalize into 1 row
CREATE TABLE [dbo].[car](
	[carkey] [int] IDENTITY(1,1) NOT NULL,
	[carnumber] [char](10) NOT NULL,
	[locationkey] [int] NULL,
	[description] [varchar](50) NULL,
	[tagnumber] [varchar](10) NULL,
	[note] [varchar](255) NULL,
	[carType] [varchar](10) NULL,
	[carActiveIn] [bit] NULL,
	[carServiceIn] [bit] NULL,
	[originstate] [varchar](2) NULL,
	[sessiontype] [varchar](10) NULL,
	[modelyear] [int] NULL,
	[make] [varchar](20) NULL,
	[model] [varchar](20) NULL,
	[vin] [varchar](50) NULL,
	[regdate] [datetime] NULL,
	
	[emissiondate] [datetime] NULL,
	
	[inspectiondate] [datetime] NULL,
	
	[carstatus] [varchar](10) NULL,
	[instructorkey] [int] NULL,
	[retired] [char](1) NULL,
	[isstatic] [char](1) NULL,
	[oilChangeMilage] [int] NULL,
 CONSTRAINT [PK_car] PRIMARY KEY NONCLUSTERED 
(
	[carkey] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 19

Author Comment

by:erikTsomik
ID: 41782964
Any suggestions
0
 
LVL 10

Expert Comment

by:Duy Pham
ID: 41785150
repeatNumber      int       (every 2 days, or every 2 years )
repeatCycle      int       (every 2 days, or every 2 years )
remindNumber      int       (every 2 days, or every 2 years )
remindCycle      int       (every 2 days, or every 2 years )

Please be more specific about these fields. What do they mean? Types of Units (minutes/hours/days/etc.)?
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 41785337
repeatnumber (1,2,3,4 and so on )is just a number  repeatCycle is a unit it is measure on  (days,month,weeks,years )
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 41786674
Are there any suggestions?
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 41786714
Still not enough details for a full answer:

repeatCycle is a unit it is measure on  (days,month,weeks,years )

But cycle is an int -- does 1 stand for day, or 100 or whatever?  No way for us to know what cycle code you assigned for what time period.

But something like below should be close and give you an idea on how to join:

SELECT c.*, ca.*
FROM dbo.car c
CROSS JOIN (
    SELECT DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) AS todays_date
) AS assign_alias_names
INNER JOIN dbo.carActivities ca ON
    ((ca.activityID = 2 AND todays_date = DATEADD(DAY, ca.remindNumber, DATEADD(YEAR, +2, c.regdate))) OR
     (ca.activityID = 3 AND todays_date = DATEADD(DAY, ca.remindNumber, DATEADD(YEAR, +2, c.emissiondate))) OR
     (ca.activityID = 4 AND todays_date = DATEADD(DAY, ca.remindNumber, DATEADD(YEAR, +2, c.inspectiondate))))
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 41786795
repeat Cycle is 0- miles, 1 - day,2,weeks,3 - month,4 -- Year
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Help - 12 42
shrink datafile Sql server 4 18
add 1 to a field for 100 rows 11 24
SQL JOIN + SUBQUERY? 3 15
Creating and Managing Databases with phpMyAdmin in cPanel.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now