棘手的日期问题 [英] Tricky group by date problem

查看:89
本文介绍了棘手的日期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个棘手的SQL查询问题,我正在使用它。


我有一张桌子类似于这样的东西


日期|价格1 |价格2 |价格3

2006年1月1日| 100 | 100 | 100

2006年1月2日| 100 | 100 | 100

2006年1月3日| 100 | 100 | 100

2006年1月4日| 115 | 100 | 100

05 Jan 2006 | 115 | 100 | 100

2006年1月6日| 115 | 115 | 115

2006年1月7日| 115 | 100 | 100

2006年1月8日| 100 | 100 | 100

2006年1月9日| 100 | 100 | 100


我想写一个将返回此查询/视图的

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


>来自|到|价格1 |价格2 |价格3
>From | To | Price1 | Price2 | Price3



2006年1月1日| 2006年1月3日| 100 | 100 | 100

2006年1月4日| 2006年1月5日| 115 | 100 | 100

2006年1月6日| 2006年1月6日| 115 | 115 | 115

2006年1月7日| 2006年1月7日| 115 | 100 | 100

2006年1月8日| 2006年1月9日| 100 | 100 | 100


任何想法?


我知道如何编写一个在VB中也会这样做的例行程序但我是

想要做很多相同的计算/查询,所以我需要它来快速获得
(VB不会这样)


TIA


Eddie

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

推荐答案

您遇到麻烦的原因是您的数据未正常化。

此外,您的数据基于随时间的变化而不是分组,因此要在T-SQL中完成此操作,您可能必须使用CURSORS

和VB一样复杂(如果不是更复杂)

,所以我的建议是规范化数据库。如果这不是

可能那么坚持在VB中这样做

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


这是一个非常简单的查询(更容易比在VB中做的那样):


SELECT MIN(日期)为From,MAX(Date)为To,

Price1,Price2,Price3

来自TheTable

GROUP BY Price1,Price2,Price3


Razvan

ed ************* @ gmail.com 写道:
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:




我有一个棘手的SQL查询问题,我正在使用它。


我有一张类似于这样的桌子


日期|价格1 |价格2 |价格3

2006年1月1日| 100 | 100 | 100

2006年1月2日| 100 | 100 | 100

2006年1月3日| 100 | 100 | 100

2006年1月4日| 115 | 100 | 100

05 Jan 2006 | 115 | 100 | 100

2006年1月6日| 115 | 115 | 115

2006年1月7日| 115 | 100 | 100

2006年1月8日| 100 | 100 | 100

2006年1月9日| 100 | 100 | 100


我想编写一个将返回此查询/视图的页面
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

来自|到|价格1 |价格2 |价格3
From | To | Price1 | Price2 | Price3



2006年1月1日| 2006年1月3日| 100 | 100 | 100

2006年1月4日| 2006年1月5日| 115 | 100 | 100

2006年1月6日| 2006年1月6日| 115 | 115 | 115

2006年1月7日| 2006年1月7日| 115 | 100 | 100

2006年1月8日| 2006年1月9日| 100 | 100 | 100


任何想法?


我知道如何编写一个在VB中也会这样做的例行程序但我是

想要做很多相同的计算/查询,所以我需要它来快速获得
(VB不会这样)


TIA


Eddie

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


嗨Razvan,


谢谢,但这不会工作。


其中一个回复将是


01 Jan 2006 | 2006年1月9日| 100 | 100 | 100


Ed


Razvan Socol写道:
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'a相当简单的查询(比在VB中更容易):


SELECT MIN(日期)为From,MAX(Date)为To,

Price1,价格2,价格3

来自TheTable

GROUP BY价格1,价格2,价格3


Razvan

< a href =mailto:ed ************* @ gmail.com> ed ************* @ gmail.com 写道:
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:




我有一个棘手的SQL查询问题,我正在使用它。


我有一张类似这样的桌子


日期|价格1 |价格2 |价格3

2006年1月1日| 100 | 100 | 100

2006年1月2日| 100 | 100 | 100

2006年1月3日| 100 | 100 | 100

2006年1月4日| 115 | 100 | 100

05 Jan 2006 | 115 | 100 | 100

2006年1月6日| 115 | 115 | 115

2006年1月7日| 115 | 100 | 100

2006年1月8日| 100 | 100 | 100

2006年1月9日| 100 | 100 | 100


我想写一个将返回此查询/视图的
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

>来自|到|价格1 |价格2 |价格3
>From | To | Price1 | Price2 | Price3



2006年1月1日| 2006年1月3日| 100 | 100 | 100

2006年1月4日| 2006年1月5日| 115 | 100 | 100

2006年1月6日| 2006年1月6日| 115 | 115 | 115

2006年1月7日| 2006年1月7日| 115 | 100 | 100

2006年1月8日| 2006年1月9日| 100 | 100 | 100


任何想法?


我知道如何编写一个在VB中也会这样做的例行程序但我是

想要做很多相同的计算/查询,所以我需要它来快速获得
(VB不会这样)


TIA


Eddie

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


这篇关于棘手的日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆