Redian新闻
>
ask for help with a simple query!!!
avatar
ask for help with a simple query!!!# Database - 数据库
p*p
1
Could someone help me with the following query? Say there is a table with 2
columns, ProductID and ProductType
ProductID ProductType
1 C
2 B
3 A
4 B
5 A
6 C
7 A
8 B
how to write a query to return 2 products (order by ProductID) from each
producttype? In this case, return:
3 A
5 A
2 B
4 B
1 C
6 C
Use Transact-SQL, one query, no cursor is allowed.
Please help!!!!
avatar
c*t
2
select * from table
where productID in (select distinct productID from table
having count(ProductType) = 2
)
order by productType

【在 p*p 的大作中提到】
: Could someone help me with the following query? Say there is a table with 2
: columns, ProductID and ProductType
: ProductID ProductType
: 1 C
: 2 B
: 3 A
: 4 B
: 5 A
: 6 C
: 7 A

avatar
j*i
3
Looks like this is not what he means, he wants
everything be returned twice, even count(*) may >2,
right?
in oracle you can use rownum to do this bah.

2

【在 c**t 的大作中提到】
: select * from table
: where productID in (select distinct productID from table
: having count(ProductType) = 2
: )
: order by productType

avatar
k*e
4
select p.* from product p
where p.productid in (select top 2 productid from product where
producttype=p.producttype)
order by producttype,productid

with

【在 j**i 的大作中提到】
: Looks like this is not what he means, he wants
: everything be returned twice, even count(*) may >2,
: right?
: in oracle you can use rownum to do this bah.
:
: 2

avatar
j*i
5

This is not good in oracle bah.
which do you use?miscrosoft sql server?
each

【在 k***e 的大作中提到】
: select p.* from product p
: where p.productid in (select top 2 productid from product where
: producttype=p.producttype)
: order by producttype,productid
:
: with

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