问一个SQL的问题,请求解答....

select substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
sum(qty_ord) ord, --销量
count(distinct seller_id) custNum, --上柜客户数
sum(qty_ord * pri3 - qty_ord * unit_cost) profit
from (select * from tb_customer_info where status = '有效') cust,
(select branch_id,
manager_id,
qty_ord,
seller_id,
pri3,
unit_cost,
item_id
from tb_cig_order
where 1 = 1
and (item_id = '6901028180504' or
item_id in
('6901028207881', '6901028149242', '6901028143080'))
where 1 = 1
and cigOrder.manager_id = '103701010009'
and cigOrder.manager_id = cust.manager_id
group by cigOrder.item_id, substr(cust.customertype, 1, 1)

上面是我的sql语句,出现的问题是计算销量啥的都正确,但是我标注的那个上柜客户数确有问题,问题是它只按照item_id分组的
结果进行了统计,而没有按照item_id和substr(cust.customertype, 1, 1)一起分组后的结果统计。这是为什么?
查询的结果是
B 6901028143080 17388 31 539028
C 6901028143080 6118 31 189658
Q 6901028143080 17388 31 539028
Y 6901028143080 2898 31 89838
Z 6901028143080 20930 31 648830
B 6901028149242 220428 197 4959630
C 6901028149242 77558 197 1745055
Q 6901028149242 220428 197 4959630
Y 6901028149242 36738 197 826605
Z 6901028149242 265330 197 5969925
B 6901028180504 52380 143 1414260
C 6901028180504 18430 143 497610
Q 6901028180504 52380 143 1414260
Y 6901028180504 8730 143 235710
Z 6901028180504 63050 143 1702350
B 6901028207881 145962 183 3940974
C 6901028207881 51357 183 1386639
Q 6901028207881 145962 183 3940974
Y 6901028207881 24327 183 656829
Z 6901028207881 175695 183 4743765

第四列的结果是不正确的。。。值应该都不同的。。。
请帮助。。。
[b]问题补充:[/b]
select substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
sum(qty_ord) ord, --销量
count(distinct seller_id) custNum, --上柜客户数
sum(qty_ord * pri3 - qty_ord * unit_cost) profit
from (select * from tb_customer_info where status = '有效') cust,
(select branch_id,
manager_id,
qty_ord,
seller_id,
pri3,
unit_cost,
item_id
from tb_cig_order
where 1 = 1
and (item_id = '6901028180504' or
item_id in
('6901028207881', '6901028149242', '6901028143080'))
where 1 = 1
and cigOrder.manager_id = '103701010009'
and cigOrder.manager_id = cust.manager_id
group by cigOrder.item_id, substr(cust.customertype, 1, 1)

上面是我的sql语句,出现的问题是计算销量啥的都正确,但是我标注的那个上柜客户数确有问题,问题是它只按照item_id分组的
结果进行了统计,而没有按照item_id和substr(cust.customertype, 1, 1)一起分组后的结果统计。这是为什么?
查询的结果是
B 6901028143080 17388 31 539028
C 6901028143080 6118 31 189658
Q 6901028143080 17388 31 539028
Y 6901028143080 2898 31 89838
Z 6901028143080 20930 31 648830
B 6901028149242 220428 197 4959630
C 6901028149242 77558 197 1745055
Q 6901028149242 220428 197 4959630
Y 6901028149242 36738 197 826605
Z 6901028149242 265330 197 5969925
B 6901028180504 52380 143 1414260
C 6901028180504 18430 143 497610
Q 6901028180504 52380 143 1414260
Y 6901028180504 8730 143 235710
Z 6901028180504 63050 143 1702350
B 6901028207881 145962 183 3940974
C 6901028207881 51357 183 1386639
Q 6901028207881 145962 183 3940974
Y 6901028207881 24327 183 656829
Z 6901028207881 175695 183 4743765

第四列的结果是不正确的。。。值应该都不同的。。。
请帮助。。。

非常感谢armoking的回答,但是仍然没有解决问题
我原来的SQL是直接从pl/sql developer中复制出来的。应该没有问题。
我再把sql简化一下:
select substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
count(distinct cigOrder.seller_id) custNum, --上柜客户数

from tb_cig_order cigOrder
inner join tb_customer_info cust
on cigOrder.manager_id = cust.manager_id
and cust.status = '有效'
where
cigOrder.manager_id = '103701010009'
and cigOrder.item_id in ('6901028180504', '6901028207881')
group by cigOrder.item_id, substr(cust.customertype, 1, 1)

我的问题是:现在count(distinct seller_id)的结果是错误的。它只按照
group by item_id计算了值。。而忽略了group by substr(cust.customertype, 1, 1)

[b]问题补充:[/b]
select substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
sum(qty_ord) ord, --销量
count(distinct seller_id) custNum, --上柜客户数
sum(qty_ord * pri3 - qty_ord * unit_cost) profit
from (select * from tb_customer_info where status = '有效') cust,
(select branch_id,
manager_id,
qty_ord,
seller_id,
pri3,
unit_cost,
item_id
from tb_cig_order
where 1 = 1
and (item_id = '6901028180504' or
item_id in
('6901028207881', '6901028149242', '6901028143080'))
where 1 = 1
and cigOrder.manager_id = '103701010009'
and cigOrder.manager_id = cust.manager_id
group by cigOrder.item_id, substr(cust.customertype, 1, 1)

上面是我的sql语句,出现的问题是计算销量啥的都正确,但是我标注的那个上柜客户数确有问题,问题是它只按照item_id分组的
结果进行了统计,而没有按照item_id和substr(cust.customertype, 1, 1)一起分组后的结果统计。这是为什么?
查询的结果是
B 6901028143080 17388 31 539028
C 6901028143080 6118 31 189658
Q 6901028143080 17388 31 539028
Y 6901028143080 2898 31 89838
Z 6901028143080 20930 31 648830
B 6901028149242 220428 197 4959630
C 6901028149242 77558 197 1745055
Q 6901028149242 220428 197 4959630
Y 6901028149242 36738 197 826605
Z 6901028149242 265330 197 5969925
B 6901028180504 52380 143 1414260
C 6901028180504 18430 143 497610
Q 6901028180504 52380 143 1414260
Y 6901028180504 8730 143 235710
Z 6901028180504 63050 143 1702350
B 6901028207881 145962 183 3940974
C 6901028207881 51357 183 1386639
Q 6901028207881 145962 183 3940974
Y 6901028207881 24327 183 656829
Z 6901028207881 175695 183 4743765

第四列的结果是不正确的。。。值应该都不同的。。。
请帮助。。。
问题补充:
select substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
sum(qty_ord) ord, --销量
count(distinct seller_id) custNum, --上柜客户数
sum(qty_ord * pri3 - qty_ord * unit_cost) profit
from (select * from tb_customer_info where status = '有效') cust,
(select branch_id,
manager_id,
qty_ord,
seller_id,
pri3,
unit_cost,
item_id
from tb_cig_order
where 1 = 1
and (item_id = '6901028180504' or
item_id in
('6901028207881', '6901028149242', '6901028143080'))
where 1 = 1
and cigOrder.manager_id = '103701010009'
and cigOrder.manager_id = cust.manager_id
group by cigOrder.item_id, substr(cust.customertype, 1, 1)

上面是我的sql语句,出现的问题是计算销量啥的都正确,但是我标注的那个上柜客户数确有问题,问题是它只按照item_id分组的
结果进行了统计,而没有按照item_id和substr(cust.customertype, 1, 1)一起分组后的结果统计。这是为什么?
查询的结果是
B 6901028143080 17388 31 539028
C 6901028143080 6118 31 189658
Q 6901028143080 17388 31 539028
Y 6901028143080 2898 31 89838
Z 6901028143080 20930 31 648830
B 6901028149242 220428 197 4959630
C 6901028149242 77558 197 1745055
Q 6901028149242 220428 197 4959630
Y 6901028149242 36738 197 826605
Z 6901028149242 265330 197 5969925
B 6901028180504 52380 143 1414260
C 6901028180504 18430 143 497610
Q 6901028180504 52380 143 1414260
Y 6901028180504 8730 143 235710
Z 6901028180504 63050 143 1702350
B 6901028207881 145962 183 3940974
C 6901028207881 51357 183 1386639
Q 6901028207881 145962 183 3940974
Y 6901028207881 24327 183 656829
Z 6901028207881 175695 183 4743765

第四列的结果是不正确的。。。值应该都不同的。。。
请帮助。。。

非常感谢armoking的回答,但是仍然没有解决问题
我原来的SQL是直接从pl/sql developer中复制出来的。应该没有问题。
我再把sql简化一下:
select substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
count(distinct cigOrder.seller_id) custNum, --上柜客户数

from tb_cig_order cigOrder
inner join tb_customer_info cust
on cigOrder.manager_id = cust.manager_id
and cust.status = '有效'
where
cigOrder.manager_id = '103701010009'
and cigOrder.item_id in ('6901028180504', '6901028207881')
group by cigOrder.item_id, substr(cust.customertype, 1, 1)

我的问题是:现在count(distinct seller_id)的结果是错误的。它只按照

group by item_id计算了值。。而忽略了group by substr(cust.customertype, 1, 1)

谢谢。
我试过这样查,这样查的结果是正确的。
itemId count(seller_id)
2222 23
2333 32
8989 233

但是我现在除了根据item_id分组外还想继续分组,itemId代表卷烟ID,而
substr(cust.customertype, 1, 1) 可能是零售户的规模等属性,继续分组,查看不同规模的上柜客户数。

这样用distinct是不是是不对的?

我想可能是由于两个表关联的时候条件不足上面的join造成了迪卡尔积
当substr(cust.customertype, 1, 1) as ty 与 cust.manager_id是n:1关系
并且cigOrder.manager_id与cigOrder.seller_id是1:n关系时
只使用manager_id进行关联就会造成迪卡尔积

比如cust表中有以下3条记录
ty manager_id
A 1
B 1
C 1

而cigOrder表中有以下两条记录
manager_id seller_id
1 x
1 y

只用manager_id进行关联
就会得到以下迪卡尔积形式的视图
ty manager_id seller_id
A 1 x
A 1 y
B 1 x
B 1 y
C 1 x
C 1 y

因为对于每个ty的值,seller_id都有值x,y
所以用ty作为group by key条件使用时
每个count(distinct seller_id)都等于2

所以,看上去group by ty这个group by条件没起作用

基于以上分析,建议你再仔细分析一下这两张表之间的关联关系
或者,你的业务需求是不对的

这个sql是不需要写成两个子查询inner join 的形式的,以下的sql应当是等效的
[code="sql"]
select
substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
sum(cigOrder.qty_ord) ord, --销量
count(distinct cigOrder.seller_id) custNum, --上柜客户数
sum(cigOrder.qty_ord * cigOrder.pri3
- cigOrder.qty_ord * cigOrder.unit_cost) profit
from tb_cig_order as cigOrder
inner join tb_customer_info as cust on
cigOrder.manager_id = cust.manager_id
and cust.status = '有效'
where cigOrder.manager_id = '103701010009'
and cigOrder.item_id in (
'6901028180504','6901028207881', '6901028149242', '6901028143080'
)
group by cigOrder.item_id, substr(cust.customertype, 1, 1)
[/code]

至于结果为什么不对
这很让人困惑

不过,你原来sql文,似乎是不完整的,因为第二个子查询少一个右括号,并且“cigOrder”这个别名也没有声明,这个sql执行时应该是会出错的
你原本的sql是不是这样的
[code="sql"]
select
substr(cust.customertype, 1, 1) ty,
cigOrder.item_id itemId,
sum(qty_ord) ord, --销量
count(distinct seller_id) custNum, --上柜客户数
sum(qty_ord * pri3 - qty_ord * unit_cost) profit
from (
select *
from tb_customer_info
where status = '有效'
) cust,
(
select
branch_id,
manager_id,
qty_ord,
seller_id,
pri3,
unit_cost,
item_id
from tb_cig_order
where 1 = 1
and (item_id = '6901028180504' or
item_id in
('6901028207881', '6901028149242', '6901028143080')
)
) cigOrder
where 1 = 1
and cigOrder.manager_id = '103701010009'
and cigOrder.manager_id = cust.manager_id
group by cigOrder.item_id, substr(cust.customertype, 1, 1)
[/code]

我看不到tb_cig_order和tb_customer_info这两张表的原始数据,
不过,你不妨先执行下面这个语句,看看只按照 group by item_id的计算结果
[code="sql"]
select
cigOrder.item_id itemId,
count(distinct cigOrder.seller_id) custNum --上柜客户数
from tb_cig_order cigOrder
where cigOrder.manager_id = '103701010009'
and cigOrder.item_id in (
'6901028180504', '6901028207881'
)
group by cigOrder.item_id
[/code]