简体   繁体   中英

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.

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. it just has to be part of one of the original dup records.

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.

SQL Fiddle

Oracle 11g R2 Schema Setup :

 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 :

SELECT check_num||decode(count(*), 1, null, '*') check_num, min(amount), min(invh_code)
  from testcheck
 group by check_num
 order by check_num

Results :

| CHECK_NUM | MIN(AMOUNT) | MIN(INVH_CODE) |
|-----------|-------------|----------------|
|      001* |          50 |           1123 |
|       002 |         100 |           1234 |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM