Redian新闻
>
誰來幫我開來開悄? Interesting SQL query
avatar
誰來幫我開來開悄? Interesting SQL query# Database - 数据库
a*t
1
I don't understand how this query is able to return a row number for
repeating IDs.
Can big cow explain how?
table:
avatar
B*g
2
这个query能用group by吗?

【在 a*******t 的大作中提到】
: I don't understand how this query is able to return a row number for
: repeating IDs.
: Can big cow explain how?
: table:

avatar
a*t
3
it works
I just don't understand how...

【在 B*****g 的大作中提到】
: 这个query能用group by吗?
avatar
c*d
4
1. which database? Oracle?
2. The sql statement cannot be executed in Oracle
avatar
c*d
5
In Oracle, it should be,
select orderid,customername,
rank() over(partition by orderid order by customername)
from "order" ;
avatar
j*n
6
it looks like T-SQL statement...
However, where is the ProductID from? you did not give all of the code ba?
avatar
B*g
7
should be sql server as table name is [Order]
But when I count ( and ), there are 2 ( and 3 ), can not believe it can
compile.
SELECT
OrderID
, CustomerName
, RowNumber =
(SELECT COUNT(*)
FROM [Order] b
WHERE b.OrderID = a.OrderID
AND b.ProductID < a.ProductID)
FROM [Order] a)
GROUP BY OrderID
ORDER BY OrderID

【在 c*****d 的大作中提到】
: 1. which database? Oracle?
: 2. The sql statement cannot be executed in Oracle

avatar
n*w
8
这种用法简直是竭泽而渔,如果table大的话。
avatar
a*t
9
yeah, M$ SQL server
sorry, replace productID with customer name

【在 j*****n 的大作中提到】
: it looks like T-SQL statement...
: However, where is the ProductID from? you did not give all of the code ba?

avatar
a*t
10
OK, here is a full working example:
CREATE TABLE [dbo].[testing](
[OrderID] [varchar](50) NULL,
[CustName] [varchar](50) NULL
)
and then insert some sample data into the table
123, John
567, Joe
123, Mary
123, Peter
and then here is the query
select
OrderID
, max(case rowno when 0 then CustName end) +
coalesce(max(case rowno when 1 then ', ' + CustName end), '') +
coalesce(max(case rowno when 2 then ', ' + CustName end), '') +
coalesce(max(case rowno when 3 then ', ' +
avatar
a*t
11
yeah, the author mentioned is this not good for big tables without
modification
http://www.thescripts.com/forum/thread580103.html

【在 n*w 的大作中提到】
: 这种用法简直是竭泽而渔,如果table大的话。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。