473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

CREATING TABLE USING SQL BASED ON SEQUENTIAL DATES

I would like to reopen this discussion. I need to create this table
using SQL that will return this dataset.

BRAID RATEDEFID MIN(BRADATE) MAX(BRADATE RATECODE PRODNAME
614 14 12/1/2002 12/4/2004 Rack Beach
Tower Terrace
618 14 12/8/2002 12/11/2004 Rack Beach
Tower Terrace
622 14 12/15/2002 12/19/2004 Rack Beach
Tower Terrace
I have provided the test data below and the SP I have been working with.
The SP works fine as long as there are 4 seqential dates. I get
unpredictable results when the count <> 4. I am also concerned on what
happens at the end of the month. For example, 1/31/2004, 2/1/2004.
Does this affect the SP? I appreciate your continued support.

Michael

CREATE TABLE [dbo].[BASERATEAVAIL_Tmp] (
[BRAID] [decimal](18, 0) NOT NULL ,
[RATEDEFID] [decimal](18, 0) NOT NULL ,
[BRADATE] [datetime] NOT NULL ,
[RATECODE] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL
,
[PRODNAME] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL
) ON [PRIMARY]

GO
INSERT INTO dbo.BASERATEAVAIL_Tmp
(BRAID, RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (614, 14, '12/1/2002', 'Rack', 'Beach Tower Terrace');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (615, 14, '12/2/2002', 'Rack', 'Beach Tower Terrace');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (616, 14, '12/3/2002', 'Rack', 'Beach Tower Terrace');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (617, 14, '12/4/2002', 'Rack', 'Beach Tower Terrace');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (618, 14, '12/8/2002', 'Rack', 'Beach Tower Terrace');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (619, 14, '12/9/2002 ', ' Rack ', ' Beach Tower Terrace ');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (620, 14, '12/10/2002', ' Rack ', ' Beach Tower Terrace ');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (621, 14, '12/11/2002', ' Rack ', ' Beach Tower Terrace ');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (622, 14, '12/15/2002', ' Rack ', ' Beach Tower Terrace ');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (623, 14, '12/16/2002', ' Rack ', ' Beach Tower Terrace ');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (624, 14, '12/17/2002', ' Rack ', ' Beach Tower Terrace ');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (625, 14, '12/18/2002', ' Rack ', ' Beach Tower Terrace ');
INSERT
INTO dbo.BASERATEAVAIL_Tmp(BRAID,
RATEDEFID, BRADATE, RATECODE, PRODNAME)
VALUES (626, 14, '12/19/2002 ', ' Rack ', ' Beach Tower Terrace ');
CREATE PROCEDURE usp @n INT
AS
SET ANSI_WARNINGS OFF
SELECT MIN( braid) AS "braid",
ratedefid, ratecode, prodname,
MIN( CASE n WHEN 1 THEN bradate END ) AS "min_date",
MAX( CASE n WHEN 0 THEN bradate END ) AS "max_date"
FROM ( SELECT t1.braid, t1.ratedefid, t1.ratecode, t1.prodname,
t1.bradate,
COUNT( * ) % @n AS "n",
( COUNT( * ) - 1 ) / @n AS "r"
FROM BASERATEAVAIL_Tmp t1
INNER JOIN BASERATEAVAIL_Tmp t2
ON t2.bradate <= t1.bradate
GROUP BY t1.braid, t1.ratedefid, t1.ratecode, t1.prodname,
t1.bradate
) D
GROUP BY r, ratedefid, ratecode, prodname
HAVING MAX( CASE n WHEN 0 THEN bradate END ) IS NOT NULL ;

Usage:

EXEC usp @n = 4

--
Anith
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
1 2047
Michael Hardy (mi***********@kerzner.com) writes:
I would like to reopen this discussion. I need to create this table
using SQL that will return this dataset.

BRAID RATEDEFID MIN(BRADATE) MAX(BRADATE RATECODE PRODNAME
614 14 12/1/2002 12/4/2004 Rack Beach
Tower Terrace
618 14 12/8/2002 12/11/2004 Rack Beach
Tower Terrace
622 14 12/15/2002 12/19/2004 Rack Beach
Tower Terrace
I have provided the test data below and the SP I have been working with.
The SP works fine as long as there are 4 seqential dates. I get
unpredictable results when the count <> 4. I am also concerned on what
happens at the end of the month. For example, 1/31/2004, 2/1/2004.
Does this affect the SP? I appreciate your continued support.


I only vaguely recall your original thread, so I don't recall all
requirments. It is not clear from your example how different ratecodes,
prodnames and ratedefid would affect the result. Neither do I understand
the purpose of that parameter to the stored procedure. Here is some code
that finds the date intervals, that you may be able to work from.

SELECT x.BRAID, startdate = x.BRADATE, stopdate = y.BRADATE
INTO #temp
FROM (SELECT a.BRAID, a.BRADATE
FROM BASERATEAVAIL_Tmp a
WHERE NOT EXISTS
(SELECT *
FROM BASERATEAVAIL_Tmp b
WHERE b.BRADATE = dateadd(DAY, -1, a.BRADATE))) x
CROSS JOIN
(SELECT a.BRADATE
FROM BASERATEAVAIL_Tmp a
WHERE NOT EXISTS
(SELECT *
FROM BASERATEAVAIL_Tmp b
WHERE b.BRADATE = dateadd(DAY, +1, a.BRADATE))) y
WHERE x.BRADATE < y.BRADATE

DELETE #temp
FROM #temp a
WHERE EXISTS (SELECT *
FROM #temp b
WHERE b.startdate = a.startdate
AND b.stopdate < a.stopdate)

SELECT * FROM #temp

DROP TABLE #temp

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hennie de Nooijer | last post by:
Because of an error in google or underlying site i can reply on my own issue. Therefore i copied the former entered message in this message....
5
by: Mal | last post by:
Hello. I have a database that tracks reservations at a campground. I want to be able to make a calendar type report that shows how many people are here in given period. Stored for each...
1
by: Alicia | last post by:
I basically wanted something that would do a count of the dates, then group them by their week name.. BEFORE: Resource Date Total number of times that date Appeared (Count) 4/3/03 ...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
3
by: shahram.shirazi | last post by:
Hi guys, I was wondering if someone could help me a bit here. Im trying to desing an electronic register system for a school. In terms of the table design, I obviously need a Student Details...
3
by: rreitsma | last post by:
I want to create a form that will allow the user to select from a list of available reports and based on a filter limit the records displayed in the report. I have figured out how to access the...
7
by: rcamarda | last post by:
I wish to build a table based on values from another table. I need to populate a table between two dates from another table. Using the START_DT and END_DT, create records between those dates. I...
2
by: tasmontique | last post by:
I am working on an access 2002 flight schedule database. I am new to access but have some basic understanding of sql and vb6 code. I have learned a lot from this website. Thanks much Hopefully...
2
by: Mike P | last post by:
I need to create a grid which along the y axis has a user name and along the x axis has a date. Somehow I need to populate the x axis with 6 months worth of dates from the current date, and then...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.