473,774 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tricky group by date problem

Hi,

I have a tricky SQL query problem that I'm having probs with.

I have a table which resembles something like this

Date | Price1 | Price2 | Price3
01 Jan 2006 | 100 | 100 | 100
02 Jan 2006 | 100 | 100 | 100
03 Jan 2006 | 100 | 100 | 100
04 Jan 2006 | 115 | 100 | 100
05 Jan 2006 | 115 | 100 | 100
06 Jan 2006 | 115 | 115 | 115
07 Jan 2006 | 115 | 100 | 100
08 Jan 2006 | 100 | 100 | 100
09 Jan 2006 | 100 | 100 | 100

and I want to write a query/view that will return this
>From | To | Price1 | Price2 | Price3
01 Jan 2006 | 03 Jan 2006 | 100 | 100 | 100
04 Jan 2006 | 05 Jan 2006 | 115 | 100 | 100
06 Jan 2006 | 06 Jan 2006 | 115 | 115 | 115
07 Jan 2006 | 07 Jan 2006 | 115 | 100 | 100
08 Jan 2006 | 09 Jan 2006 | 100 | 100 | 100

Any ideas?

I know how to write a routine that would do the same in VB but I am
looking to do a lot of the same calculation/query so I need it to be
fast (which VB wouldnt be)

TIA

Eddie

Nov 2 '06
15 5965
Hello, Ed

Sorry for not looking close enough at the sample data and expected
result. Here is my first attempt at the real problem:

SELECT (
SELECT MIN(Date) FROM TheTable d
WHERE d.Date<=a.Date
AND a.Price1=d.Pric e1 AND a.Price2=d.Pric e2 AND a.Price3=d.Pric e3
AND NOT EXISTS (
SELECT * FROM TheTable e
WHERE e.Date BETWEEN d.Date AND a.Date
AND (e.Price1<>d.Pr ice1 OR e.Price2<>d.Pri ce2 OR e.Price3<>d.Pri ce3)
)
) as StartDate, Date as EndDate,
Price1, Price2, Price3
FROM TheTable a
WHERE NOT EXISTS (
SELECT * FROM TheTable b
WHERE a.Date<b.Date
AND a.Price1=b.Pric e1 AND a.Price2=b.Pric e2 AND a.Price3=b.Pric e3
AND NOT EXISTS (
SELECT * FROM TheTable c
WHERE c.Date BETWEEN a.Date AND b.Date
AND (c.Price1<>b.Pr ice1 OR c.Price2<>b.Pri ce2 OR c.Price3<>b.Pri ce3)
)
)

The above query was inspired by reading (a few years ago) the following
article:
http://msdn.microsoft.com/library/en...eintervals.asp

Razvan

Nov 2 '06 #11
Louis,
>Date seems to be an attribute of an Order (correct me if I'm wrong) and
Price seems to be an attribute of a product, so price would need to be in
a products table and Date would need to be in an orders table.
Since Eddie did not post any information regarding the underlying business
model, how can we conclude what the actual entities and relationships are?
Perhaps it could be a table that represents pricing variations of some
product or service on a given date at three different locations or on three
distinct times or simply three samplings for statistical purposes.

We cannot know if a table is sufficiently normalized or not, unless we fully
comprehend the underlying business model and rules.
>The fact that you have a lot of repeating groups, e.g. 100|100|100 shows
that the data isn't properly normalised.
How do we know if there is a repeating group however? Would you consider a
table like the following not to be properly normalized, given that you have
key column and a few datetime columns?
EmployeeHistory ( emp_id, b_date, a_date, h_date, t_date )

-- with the abbreviated names b_date, a_date, h_date and t_date to stand for
birth_date, applied_date, hire_date and terminated_date respectively.

A repeating group is a rampantly abused term that, for historical reasons,
means a group of values in a column. It does not suggest a repeating set of
similarly named columns in a table, though quite a few online sources
mistakenly state so. In the original post, there is nothing that suggests
the existence of a "repeating group". You have a simple table with four
columns -- essentially four distinctly named, typed attributes with scalar
values, where each non-key attributes are functionally dependant on the key
attribute.

Nothing in his post so far has suggested there are any normalization issues
either. And the problem he posed does not suggest the lack of normalization
either.

Very few online references are useful for learning fundamentals. One of
them, esp for 1NF is:
http://www.dbdebunk.com/page/page/629796.htm

The right way to learn normalization is to use good books. As for a
recommendation consider:
http://www.amazon.com/gp/product/0321197844/
http://www.amazon.com/dp/0596100124/

--
Anith
Nov 2 '06 #12
I saw in one of the solutions
select dt + 1 from #anytable --like ( here dt is a
datetime field )
which one is more efficient or robust between
dateadd(day, 1, dt) -- or only using
dt + 1

Nov 3 '06 #13
Hi Boetsid,

I always use dt + 1 because then you're not having to invoke the
dateadd function (which I'm guessing adds overhead).

As dates are stored as numbers anyway within SQL, I dont think you can
go wrong with dt+1, or infact dt-1 or dt+.5 :-)

Ed

boetsid wrote:
I saw in one of the solutions
select dt + 1 from #anytable --like ( here dt is a
datetime field )
which one is more efficient or robust between
dateadd(day, 1, dt) -- or only using
dt + 1
Nov 3 '06 #14
See if this helps:
http://groups.google.com/group/micro...7f0f210e5ec52/

--
Anith
Nov 3 '06 #15
On 2 Nov 2006 23:31:55 -0800, boetsid wrote:
>I saw in one of the solutions
select dt + 1 from #anytable --like ( here dt is a
datetime field )
which one is more efficient or robust between
dateadd(day, 1, dt) -- or only using
dt + 1
Hi boetsid,

Both are equally robust. The behaviour of + with a datetime argument is
documented in Books Online, so it won't change withour prior warning.

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/4ba8baac-5f07-432c-87c5-d23e7011da55.ht m,
example B.

If you want to know which is more efficient, then set up a loop to
repeat this calculation a few million times. Run it twice, using the
different techniques, and compare results. I don't expect that you'll
find any measurable difference, though.

--
Hugo Kornelis, SQL Server MVP
Nov 3 '06 #16

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

Similar topics

9
3732
by: Steve Jorgensen | last post by:
Hi all, I'm working on the schema for a database that must represent data about stock & bond funds over time. My connundrum is that, for any of several dimension fields, including the fund name itself, the dimension may be represented in different ways over time, and may split or combine from one period to the next. When querying from the database for an arbitrary time period, I need the data to be rolled up to the smallest extent...
25
3409
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
1
1395
by: Pea | last post by:
I'm working with a system usage database. I want to filter out repetitive logins. The query I have retrieves data like this: USER_DATE USER_TIME1 USER_USERID USER_ACCOUNT 10/01/2004 19:56 y708ga27 The Capital Group 10/01/2004 19:58 y708ga27 The Capital Group 10/01/2004 19:59 y708ga27 The Capital Group 10/01/2004 20:19 y708ga27 The Capital Group In a case like this - with consecutive logins for the same user, if
2
1362
by: smoscar | last post by:
I am " fixing " and ASP page that was created some time ago, and i'm running into a problem searching the database connected to it. micsearch = "SELECT FacilityName, Location, County, State, Description, View, Company, FacilityType, Date, Filename FROM Assets WHERE Location = '"&misearch&"' OR KEYWORDS like '"&misearch&"' " The problem lies in the end of the statement " Keywords like '"&misearch&"'
3
1348
by: rittersporn | last post by:
I want to model relations between "Groups" and also annotate the relations! My solution so far: Group - GroupName : Text | PRIMARY_KEY - Titel : Text - Info : Memo
2
1822
by: pruebauno | last post by:
I am currently working on a tricky problem at work. I googled around a bit, but "time intervals" did not come up with anything useful. Although I have some rough idea of how I could solve it, I still would value some input. I have information of (It has only couple dozen entries.) ServiceNum, DollarCost and a input database table in the form (several GBytes): ClientNumber (CN), BeginDate(BD), EndDate(ED),
22
1747
by: graham.parsons | last post by:
Guys, Hopefully someone can help. We have a monitoring program that has threads which start and stop monitoring at various times. There are two tables: THREADLIFECYCLE unique_id
1
1420
by: oneannoyingguy | last post by:
I am having some trouble reaching my intended results in combining information from a few tables. The easiest way to explain is with a dummy model t1________________________ date - portfolio - group - contn ====================== d1 A a 5 d1 A b 6 d1 A c 7 d2 A b 9 d2 A c 10
0
1404
by: ombralonga | last post by:
Hi, I'm getting the following validation error with XMLspy when validating an "xsd" file: Schema Error: the group 'DateUnionGroup' is undefined However, in the xsd document the group 'DateUnionGroup' IS defined.
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10267
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.