sunday888 2008-11-21 08:49
浏览 207
已采纳

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

  • 写回答

3条回答 默认 最新

  • т 2008-11-21 14:50
    关注

    我想可能是由于两个表关联的时候条件不足上面的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条件没起作用

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型