473,767 Members | 1,579 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 #1
15 5962
The reason you are having trouble is that your data is not normalized.
Also your data is based on change over time and not on groupings, so to
accomplish this in T-SQL you'll probably have to use CURSORS which is
going to be just as complicated (if not more complicated) than doing it
in VB, so my suggestion is to normalise the database. If this is not
possible then rather stick to doing it in VB

Nov 2 '06 #2
It's a pretty simple query (much easier than doing it in VB):

SELECT MIN(Date) as From, MAX(Date) as To,
Price1, Price2, Price3
FROM TheTable
GROUP BY Price1, Price2, Price3

Razvan

ed************* @gmail.com wrote:
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 #3
Hi Razvan,

Thanks, but this wont work.

One of the responses will be

01 Jan 2006 | 09 Jan 2006 | 100 | 100 | 100

Ed

Razvan Socol wrote:
It's a pretty simple query (much easier than doing it in VB):

SELECT MIN(Date) as From, MAX(Date) as To,
Price1, Price2, Price3
FROM TheTable
GROUP BY Price1, Price2, Price3

Razvan

ed************* @gmail.com wrote:
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 #4

lo***********@h otmail.com wrote:
The reason you are having trouble is that your data is not normalized.
I'm intrigued. How would you normalise the data further - the date is a
unqiue field and their would be no benefit in storing the prices in a
separate table

Nov 2 '06 #5
I'm intrigued. How would you normalise the data further - the date is a
unqiue field and their would be no benefit in storing the prices in a
separate table
Well, remember that all non-key fields needs to be dependant on the
primary key and only on the primary key. 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. The fact that you have a
lot of repeating groups, e.g. 100|100|100 shows that the data isn't
properly normalised. This is not something I can teach you in one
e-mail, so please have a look at the following link:

http://www.utexas.edu/its/windows/da...ing/index.html

Nov 2 '06 #6
Try this

create table #mytable(dt datetime,Price1 int, Price2 int, Price3 int)
insert into #mytable(dt,Pri ce1,Price2,Pric e3)
select '01 Jan 2006',100,100,1 00 union all
select '02 Jan 2006',100,100,1 00 union all
select '03 Jan 2006',100,100,1 00 union all
select '04 Jan 2006',115,100,1 00 union all
select '05 Jan 2006',115,100,1 00 union all
select '06 Jan 2006',115,115,1 15 union all
select '07 Jan 2006',115,100,1 00 union all
select '08 Jan 2006',100,100,1 00 union all
select '09 Jan 2006',100,100,1 00
select min(dt) as [From],
max(dt) as [To],
Price1,
Price2,
Price3
from (
select a.dt,
a.Price1,
a.Price2,
a.Price3,
a.dt-(select count(*)
from #mytable b
where b.Price1=a.Pric e1
and b.Price2=a.Pric e2
and b.Price3=a.Pric e3
and b.dt <= a.dt) as Num
from #mytable a
) X
group by Price1,Price2,P rice3,Num
order by 1

Nov 2 '06 #7
One method:

CREATE TABLE dbo.TestTable
(
MyDate datetime
CONSTRAINT PK_TestTable PRIMARY KEY,
price1 int NOT NULL,
price2 int NOT NULL,
price3 int NOT NULL
)

INSERT INTO dbo.TestTable
SELECT '20060101', 100, 100, 100
UNION ALL SELECT '20060102', 100, 100, 100
UNION ALL SELECT '20060103', 100, 100, 100
UNION ALL SELECT '20060104', 115, 100, 100
UNION ALL SELECT '20060105', 115, 100, 100
UNION ALL SELECT '20060106', 115, 115, 115
UNION ALL SELECT '20060107', 115, 100, 100
UNION ALL SELECT '20060108', 100, 100, 100
UNION ALL SELECT '20060109', 100, 100, 100
GO

SELECT
FromDates.MyDat e AS FromDate,
COALESCE(ToDate s.MyDate, FromDates.MyDat e) AS ToDate,
FromDates.Price 1,
FromDates.Price 2,
FromDates.Price 3
FROM dbo.TestTable AS FromDates
LEFT JOIN dbo.TestTable AS ToDates ON
FromDates.MyDat e = (
SELECT MIN(MyDate)
FROM dbo.TestTable AS b
WHERE
b.MyDate FromDates.MyDat e AND
b.Price1 = FromDates.Price 1 AND
b.Price2 = FromDates.Price 2 AND
b.Price3 = FromDates.Price 3
)
WHERE
NOT EXISTS(
SELECT *
FROM dbo.TestTable AS b
WHERE
b.MyDate = FromDates.MyDat e - 1 AND
b.Price1 = FromDates.Price 1 AND
b.Price2 = FromDates.Price 2 AND
b.Price3 = FromDates.Price 3
)
ORDER BY
FromDates.MyDat e
--
Hope this helps.

Dan Guzman
SQL Server MVP

<ed************ *@gmail.comwrot e in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
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 #8
Mark,

Neat query - to quote Wayne's World, "I am not worthy!"

Thanks for taking the time.

Ed :)

Nov 2 '06 #9
Mark,

Neat query - to quote Wayne's World, "I am not worthy!"

Thanks for taking the time.

Ed :)

Nov 2 '06 #10

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

Similar topics

9
3731
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
3408
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
1347
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
1745
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
1419
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
9575
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
10170
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
9841
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...
0
8840
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
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
5280
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...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3931
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2808
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.