[英]Oracle SQL Distinct Clause not presenting distinct values
I have a script when I'm trying to select locations in an inventory where quantity of said location is <= 5. The query is complete, now I'm trying to do some fine tuning, and what I'm running into now is when I use the distinct clause I am still receiving duplicate records in the same column. 当我尝试选择清单中位置数量<= 5的位置时,我有一个脚本。查询已完成,现在我试图进行一些微调,现在我正在运行的是当我使用distingle子句时,我仍在同一列中收到重复的记录。 I do know the column next to the first are unique, but I thought distinguishing distinct and one column would roll over to next related to said column.
我确实知道第一列旁边的列是唯一的,但是我认为区分不同的列和一个列将滚动到与该列相关的下一个列。
Here is my code: 这是我的代码:
select DISTINCT bin.scannable_id as bin,
bi.bin_id as case1,
pallet.scannable_id as pallet,
-- bi.isbn as fcsku,
nvl(fs.asin,bi.isbn) as asin,
sum(bi.quantity) as quantity,
pallet.creation_date as received_date
from containers bin
join containers pallet on pallet.containing_container_id = bin.container_id
join containers case on case.containing_container_id = pallet.container_id
join bin_items bi on bi.container_id = case.container_id
left join fcskus fs on fs.fcsku = bi.isbn
where bin.scannable_id like 'R-1-T%'
and bi.quantity <= '5'
group by bin.scannable_id, pallet.scannable_id, bi.bin_id, bi.owner,bi.isbn,nvl(fs.asin,bi.isbn), pallet.creation_date
order by sum(bi.quantity);
My output, which is obviously showing duplicate records in the scannable_id column: 我的输出,显然是在scannable_id列中显示了重复的记录:
Correct Formatting Thanks to conrad. 正确的格式感谢conrad。
select DISTINCT bin.scannable_id as bin,
pallet.scannable_id as pallet,
nvl(fs.asin,bi.isbn) as asin,
sum(bi.quantity) as quantity
from containers bin
join containers pallet on pallet.containing_container_id = bin.container_id
join containers case on case.containing_container_id = pallet.container_id
join bin_items bi on bi.container_id = case.container_id
left join fcskus fs on fs.fcsku = bi.isbn
where bin.scannable_id like 'R-1-T%'
having sum(bi.quantity) <= '5'
group by bin.scannable_id, pallet.scannable_id, nvl(fs.asin,bi.isbn), bi.quantity
order by sum(bi.quantity);
As said on the comments you dont need a DISTINCT
if you have the group by
statement. 如评论所述,如果您有
group by
语句,则不需要DISTINCT
。 And format your date field because depending on your oracle client configuration it will not show you the entire date format (eg date time). 格式化日期字段,因为根据您的oracle客户端配置,它不会显示完整的日期格式(例如日期时间)。 So try with this:
所以尝试这个:
select bin.scannable_id as bin,
bi.bin_id as case1,
pallet.scannable_id as pallet,
nvl(fs.asin,bi.isbn) as asin,
to_char(pallet.creation_date, 'yyyy-mm-dd') as received_date
sum(bi.quantity) as quantity,
from containers bin
join containers pallet on pallet.containing_container_id = bin.container_id
join containers case on case.containing_container_id = pallet.container_id
join bin_items bi on bi.container_id = case.container_id
left join fcskus fs on fs.fcsku = bi.isbn
where bin.scannable_id like 'R-1-T%'
and bi.quantity <= '5'
group by bin.scannable_id,
pallet.scannable_id,
bi.bin_id,
bi.owner,
bi.isbn,
nvl(fs.asin,bi.isbn),
to_char(pallet.creation_date, 'yyyy-mm-dd')
order by sum(bi.quantity);
bi.bin_id
is different for each row, so you do only have distinct results in your resultset. bi.bin_id
对于每一行都是不同的,因此您的结果集中只有不同的结果。
distinct
is applied to the final visible resultset (once the to_char
etc. functions are processed) distinct
是应用于最终的可见结果集(一旦处理了to_char
等函数) distinct
is redundant if you already use a group by
expression group by
表达式group by
则distinct
是多余的 Solution: skipp the bi.bin_id
column from your select expression. 解决方案:从您选择的表达式中
bi.bin_id
列。
Your logic is also confusing. 您的逻辑也令人困惑。 You want to know the SUM of all the bi.* elements.
您想知道所有bi。*元素的总和。 To do so you cannot group by
bi.bin_id
nor any field from the bi
table. 为此,您无法按
bi.bin_id
或bi
表中的任何字段进行bi.bin_id
。 This is the reason why your quantity
result is always 1. 这就是为什么您的
quantity
结果始终为1的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.