If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Need help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-12, 15:16
klenkala klenkala is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
Need help

I have a huge table record counts with 10 columns,

SQL needed for below scenarios::

I)Need to find out the nulls for each column

II) duplicate checks for specfic columns


Thanks
Kumara swamy
Reply With Quote
  #2 (permalink)  
Old 02-15-12, 15:40
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Quote:
I)Need to find out the nulls for each column

II) duplicate checks for specfic columns
Do you want to find the existence of such records(answer is 'Yes' or 'No') or to count such records(answer is one count)?
OR
Do you want to list out such records?
Reply With Quote
  #3 (permalink)  
Old 02-15-12, 23:25
klenkala klenkala is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
database

Thanks for your reply,

I need to know such column values having the nulls & duplicates
Reply With Quote
  #4 (permalink)  
Old 02-16-12, 00:01
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
First of all,
If there are some unique columns or combination of columns,
it would be better to add UNIQUE(or PRIMARY KEY) constraints for those columns or combination of columns,
to prevent insertion of duplicated rows and to eliminate duplication check by users.

A query to check I) and II) might be something like...
Code:
SELECT <columns to be checked duplication>
     , MAX( <col-1> ) AS <col-1>
     , MAX( <col-2> ) AS <col-2>
     ...
     , MAX( <col-n> ) AS <col-n>
     ,    CASE WHEN COUNT(*) > 1 THEN 'Dup ' ELSE '    ' END
       || CASE WHEN COUNT( <col-1> ) < COUNT(*) THEN ' 1' ELSE '  ' END
       || CASE WHEN COUNT( <col-2> ) < COUNT(*) THEN ' 2' ELSE '  ' END
       ...
       || CASE WHEN COUNT( <col-n> ) < COUNT(*) THEN ' n' ELSE '  ' END
       AS flags
 FROM  <your-table>
 GROUP BY
       <columns to be checked duplication>
 HAVING
       COUNT(*) > 0
   OR  COUNT( <col-1> ) < COUNT(*)
   OR  COUNT( <col-2> ) < COUNT(*)
   ...
   OR  COUNT( <col-n> ) < COUNT(*)
;
Note: If duplication and nulls are both exist for a set of rows, values of columns might be inaccurate.
Reply With Quote
  #5 (permalink)  
Old 02-16-12, 03:03
klenkala klenkala is offline
Registered User
 
Join Date: Feb 2012
Posts: 4
need help - DB2

I need to find out the combination of 1,2,3,4,5,6 fields cannot have duplicates, its huge table. What is the best way to find these?
Reply With Quote
Reply

Tags
db2 query

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On