由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 请教SQL server的一个programming的问题,谢谢
相关主题
SQL combine two columns from two different tables no shared (转载)Table Merge (SQL Server)
ask for help with a simple query!!!誰來幫我開來開悄? Interesting SQL query
Oracle,table有上千个columnhow to get the product for a column
help about SQL for ACCESS请教SQL
SQL combine two tables into one table and add a new column急问一个关于T-SQL的问题,谢谢
compare two large tables SQL (转载)SQL Server - how to obtain data type name
SQL find distinct values in large table求教如何针对这种情况进行优化
interview question (SQL)Re: 刚电面一个,fail了 (转载)
相关话题的讨论汇总
话题: orderid话题: table话题: orderdate话题: sql话题: 客户
进入Database版参与讨论
1 (共1页)
m**********2
发帖数: 2252
1
有个table,有3个columns。一个是客户ID,一个orderID,一个orderdate。每一个
order就有一个orderID。比如客户ID 500,会有orderID 201,302,809. 客户ID 600
,会有orderID 211,298,344。
现在我想在这个table加一个column,ordersequenceID,比如客户ID 500,根据
orderID的大小,如果orderID是201,就是1,orderID是302,就是2,orderID是809,就
是3. 同样,比如比如客户ID 600,根据orderID的大小,如果orderID是211,就是1,
orderID是298,就是2,orderID是344,就是3。
这个应该怎样做?
谢谢。
B*****g
发帖数: 34098
2
very very bad design, do not do that

600

【在 m**********2 的大作中提到】
: 有个table,有3个columns。一个是客户ID,一个orderID,一个orderdate。每一个
: order就有一个orderID。比如客户ID 500,会有orderID 201,302,809. 客户ID 600
: ,会有orderID 211,298,344。
: 现在我想在这个table加一个column,ordersequenceID,比如客户ID 500,根据
: orderID的大小,如果orderID是201,就是1,orderID是302,就是2,orderID是809,就
: 是3. 同样,比如比如客户ID 600,根据orderID的大小,如果orderID是211,就是1,
: orderID是298,就是2,orderID是344,就是3。
: 这个应该怎样做?
: 谢谢。

gy
发帖数: 620
3
Strongly stupport BEIJIN.
BTW, 你为啥要那么设计呢?

【在 B*****g 的大作中提到】
: very very bad design, do not do that
:
: 600

m**********2
发帖数: 2252
4
哦。不是我要设计。。
我们marketing就想知道,一个客户从第一个order到第二个order大概隔多少时间,从
第二个order到第三个order大概隔多少时间。。
这个就是临时计算一下。不是在设计里的。
有没有办法算一下?用cursor?

【在 gy 的大作中提到】
: Strongly stupport BEIJIN.
: BTW, 你为啥要那么设计呢?

m**********2
发帖数: 2252
5
Beijing给说说为啥这个design very ver bad?

【在 B*****g 的大作中提到】
: very very bad design, do not do that
:
: 600

B*****g
发帖数: 34098
6
SELECT id, orderdate, ROW_NUMBER OVER (PARTITION BY id ORDER BY orderdate) A
S ordersequenceID FROM table

【在 m**********2 的大作中提到】
: 哦。不是我要设计。。
: 我们marketing就想知道,一个客户从第一个order到第二个order大概隔多少时间,从
: 第二个order到第三个order大概隔多少时间。。
: 这个就是临时计算一下。不是在设计里的。
: 有没有办法算一下?用cursor?

m**********2
发帖数: 2252
7
牛! 谢了!

A

【在 B*****g 的大作中提到】
: SELECT id, orderdate, ROW_NUMBER OVER (PARTITION BY id ORDER BY orderdate) A
: S ordersequenceID FROM table

j*****n
发帖数: 1781
8
hehe, see it again....

A

【在 B*****g 的大作中提到】
: SELECT id, orderdate, ROW_NUMBER OVER (PARTITION BY id ORDER BY orderdate) A
: S ordersequenceID FROM table

B*****g
发帖数: 34098
9
有包子吗?

orderdate)

【在 j*****n 的大作中提到】
: hehe, see it again....
:
: A

c**t
发帖数: 2744
10
这么简单居然要包子

【在 B*****g 的大作中提到】
: 有包子吗?
:
: orderdate)

相关主题
compare two large tables SQL (转载)Table Merge (SQL Server)
SQL find distinct values in large table誰來幫我開來開悄? Interesting SQL query
interview question (SQL)how to get the product for a column
进入Database版参与讨论
B*****g
发帖数: 34098
11
不到1小时按1小时收钱

【在 c**t 的大作中提到】
: 这么简单居然要包子
b*****e
发帖数: 364
12
SELECT a.ID, a.ORDERID, MIN(a.orderdate-b.orderdate) as DateDifference,
COUNT(b.orderID)+1 as OrderSequence
FROM Table a
JOIN Table b
ON a.ID=b.ID
WHERE a.orderdate>b.orderDate
GROUP BY a.ID, a.ORDERID;
Z*****l
发帖数: 14069
13
这个太高深莫测了。
假设table只有一行数据,这个WHERE使得什么也不出来。
假设table有两行或N行date相同的数据,这个WHERE还是使得什么也不出来。

【在 b*****e 的大作中提到】
: SELECT a.ID, a.ORDERID, MIN(a.orderdate-b.orderdate) as DateDifference,
: COUNT(b.orderID)+1 as OrderSequence
: FROM Table a
: JOIN Table b
: ON a.ID=b.ID
: WHERE a.orderdate>b.orderDate
: GROUP BY a.ID, a.ORDERID;

B*****g
发帖数: 34098
14
要厚道

【在 Z*****l 的大作中提到】
: 这个太高深莫测了。
: 假设table只有一行数据,这个WHERE使得什么也不出来。
: 假设table有两行或N行date相同的数据,这个WHERE还是使得什么也不出来。

Z*****l
发帖数: 14069
15
sorry,失言了。

【在 B*****g 的大作中提到】
: 要厚道
1 (共1页)
进入Database版参与讨论
相关主题
Re: 刚电面一个,fail了 (转载)SQL combine two tables into one table and add a new column
请教一个SQL问题compare two large tables SQL (转载)
mysql 问题 (转载)SQL find distinct values in large table
请教一个mysql 排序问题。interview question (SQL)
SQL combine two columns from two different tables no shared (转载)Table Merge (SQL Server)
ask for help with a simple query!!!誰來幫我開來開悄? Interesting SQL query
Oracle,table有上千个columnhow to get the product for a column
help about SQL for ACCESS请教SQL
相关话题的讨论汇总
话题: orderid话题: table话题: orderdate话题: sql话题: 客户