5

Trying to get a check sum of results of a SELECT statement, tried this

SELECT sum(crc32(column_one))
FROM database.table;

Which worked, but this did not work:

SELECT CONCAT(sum(crc32(column_one)),sum(crc32(column_two)))
FROM database.table;

Open to suggestions, main idea is to get a valid checksum for the SUM of the results of rows and columns from a SELECT statement.

blunders
  • 3,619
  • 10
  • 43
  • 65

2 Answers2

13

The problem is that CONCAT and SUM are not compatible in this format.

CONCAT is designed to run once per row in your result set on the arguments as defined by that row.

SUM is an aggregate function, designed to run on a full result set.

CRC32 is of the same class of functions as CONCAT.

So, you've got functions nested in a way that just don't play nicely together.

You could try:

SELECT CONCAT(
    (SELECT sum(crc32(column_one)) FROM database.table),
    (SELECT sum(crc32(column_two)) FROM database.table)
);

or

SELECT sum(crc32(column_one)), sum(crc32(column_two))
FROM database.table;

and concatenate them with your client language.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • Please always keep in mind for your large tables of "Expected collisions" https://stackoverflow.com/a/14210379/1896134 Which potentially could give you a `false positive` – JayRizzo Jun 08 '17 at 21:21
0
SELECT SUM(CRC32(CONCAT(column_one, column_two)))
FROM database.table;

or

SELECT SUM(CRC32(column_one) + CRC32(column_two))
FROM database.table;
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
Curtis Yallop
  • 6,696
  • 3
  • 46
  • 36