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是不是是不对的?