[英]how to add asterics to row when one item in the row is duplicated and only show one row
The title here is a bit confusing I totally realize that but here is what im trying to accomplish. 我完全意识到这里的标题有点令人困惑,但这是我正在努力实现的目标。 I have a table with one column that repeats. 我有一张重复的表。
the table looks like this. 桌子看起来像这样。
create table testCheck (check_num varchar2(20), amount number(17,2), invh_Code varchar2(20))
and it has data like so 它有这样的数据
INSERT INTO "TESTCHECK" (CHECK_NUM, AMOUNT, INVH_CODE) VALUES ('001', '50', '1123')
INSERT INTO "TESTCHECK" (CHECK_NUM, AMOUNT, INVH_CODE) VALUES ('001', '50', '1123')
INSERT INTO "TESTCHECK" (CHECK_NUM, AMOUNT, INVH_CODE) VALUES ('002', '100', '1234'
I would like to write a query that would give me the distinct check_num's but put an asterisk on the items that are actually duplicated. 我想编写一个查询,该查询将为我提供独特的check_num,但在实际重复的项目上加上星号。
so in other words expected result in this case would look like this. 因此换句话说,这种情况下的预期结果将如下所示。
"CHECK_NUM" "AMOUNT" "INVH_CODE"
"001*" 50 "1123"
"002" 100 "1234"
notice that with check 001 i dont really care what columns get picked for amount and invh_code when is a duplicate. 请注意,使用支票001时,我并不在乎重复的金额和invh_code会选择哪些列。 it just has to be part of one of the original dup records. 它只必须是原始dup记录之一的一部分。
I've been able to get as far as getting the unique values but what i cant do is figure out for the life of me how to put the asterisk in there. 我已经能够获得独特的价值,但是我无法解决的就是在我的一生中如何将星号放入其中。
here is my query. 这是我的查询。
with Checkquery as (
SELECT count(*) over (partition by CHECK_NUM order by CHECK_NUM ROWS UNBOUNDED PRECEDING ) thiscount, CHECK_NUM, AMOUNT, INVH_CODE
FROM TESTCHECK
group by
check_num,
AMOUNT,
INVH_CODE)
select check_num, amount,invh_Code from Checkquery where thiscount ='1';
can someone please point me in the right direction. 有人能指出我正确的方向吗? I simply want to identify the dups put an asterisk on the record and then only select one of the records. 我只想确定在唱片上放一个星号的公仔,然后只选择其中一个唱片。
Oracle 11g R2 Schema Setup : Oracle 11g R2架构设置 :
create table testCheck (check_num varchar2(20), amount number(17,2), invh_Code varchar2(20));
INSERT INTO "TESTCHECK" (CHECK_NUM, AMOUNT, INVH_CODE) VALUES ('001', '50', '1123');
INSERT INTO "TESTCHECK" (CHECK_NUM, AMOUNT, INVH_CODE) VALUES ('001', '50', '1123') ;
INSERT INTO "TESTCHECK" (CHECK_NUM, AMOUNT, INVH_CODE) VALUES ('002', '100', '1234');
Query 1 : 查询1 :
SELECT check_num||decode(count(*), 1, null, '*') check_num, min(amount), min(invh_code)
from testcheck
group by check_num
order by check_num
| CHECK_NUM | MIN(AMOUNT) | MIN(INVH_CODE) |
|-----------|-------------|----------------|
| 001* | 50 | 1123 |
| 002 | 100 | 1234 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.