0

Need to also select the total record count given the following query. How would I go about doing this?


SELECT DISTINCT t.Creator AS TABLE_SCHEMA, t.Name AS Table_Name, c.Name AS COLUMN_NAME, 
c.COLTYPE AS DATA_TYPE, CASE WHEN c.KEYSEQ = 1 THEN 'Primary Key' ELSE NULL END AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE 
    FROM SYSIBM.SYSTABLES t 
        INNER JOIN SYSIBM.SYSCOLUMNS c ON c.TBNAME = t.Name
    WHERE t.Creator='MY_SCHEMA_NAME' AND t.Name='MY_TABLE_NAME'

Any help would be appreciated, thanks

5
  • Do you mean in a programming language or just by SQL? Commented Mar 2, 2020 at 6:33
  • How do you want to get the total record count exactly? In an additional column with the same value for all rows? Somehow else? Commented Mar 2, 2020 at 7:16
  • Do you want to get the number of tables your query returned? Or the number of rows in each of those tables? Commented Mar 2, 2020 at 12:43
  • @Mark Barinstein - yes, an additional column with the same value for all rows. Commented Mar 2, 2020 at 13:14
  • @a_horse_woth_no_name number of rows in each of those tables Commented Mar 2, 2020 at 13:15

2 Answers 2

1

For any SELECT statement including your one:

SELECT T.*, COUNT(1) OVER() AS ROW_COUNT
FROM
(
SELECT DISTINCT t.Creator AS TABLE_SCHEMA, t.Name AS Table_Name, c.Name AS COLUMN_NAME, 
c.COLTYPE AS DATA_TYPE, CASE WHEN c.KEYSEQ = 1 THEN 'Primary Key' ELSE NULL END AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE 
    FROM SYSIBM.SYSTABLES t 
        INNER JOIN SYSIBM.SYSCOLUMNS c ON c.TBNAME = t.Name
    WHERE t.Creator='MY_SCHEMA_NAME' AND t.Name='MY_TABLE_NAME'
) T;
Sign up to request clarification or add additional context in comments.

Comments

0

Need to also select the total record count given the following query.

The SELECT DISTINCT seems superfluous. The system tables should not have duplicates. If you are getting duplicates, it is probably because the JOIN conditions are not complete -- you should join on both the table name and creator.

So just use COUNT(*)

SELECT COUNT(*) 
FROM SYSIBM.SYSTABLES t INNER JOIN
     SYSIBM.SYSCOLUMNS c
     ON c.TBNAME = t.Name AND
        c.TBCREATOR = t.TBCREATOR
WHERE t.Creator = 'MY_SCHEMA_NAME' AND
      t.Name = 'MY_TABLE_NAME';

2 Comments

I want to still be able to select the other values I need from the same query though
@weovibewvoibweoivwoiv . . . That is not what your question is asking. This answers the question that you actually asked. You can, however, add count(*) over () to the fixed query (with the right join condition) to add an additional column with the count.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.