views:

44

answers:

3

Can functions such as Min() or Max() possibly destroy the integrity of a record? Take the case of the query I recently crafted:

SELECT account, MIN(phone), MIN(chargeid), MIN(locationid) FROM import1 GROUP BY account, phone

is there any chance I am mixing my field data into a new record unintentionally? What if I changed one Min to a Max? Could I destroy record integrity if I chose to?

Thanks, Donovan

+7  A: 

You aren't actually altering data in that query, you are only SELECTing or viewing it.

Unless you do an UPDATE, INSERT, or DELETE or call a stored proc you won't alter the data.

JNK
True but in this instance the selection is being turned into an entirely new data set. When using visual fox you can turn a select statement into an entirely new table, which is why I was concerned.
Donovan
+3  A: 

I'm not sure what you mean by destroying record integrity, but there is nothing wrong with calling MIN or MAX functions. This is a very common sort of query.

You should not assume that all the values in a row in your result set will come from the same row in the original table. The values can come from different rows. For example, if the data in your table is this:

account  phone  chargeid locationid
123      456    10       30
123      456    40       20

Then the result will be this:

account  phone  MIN(chargeid) MIN(locationid)
123      456    10            20

The "10" comes from the first row and the "20" comes from the second row.

Also, your usage of MIN on phone seems unnecessary. You should just select this field as it is part of the group by:

SELECT account, phone, MIN(chargeid), MIN(locationid)
FROM import1
GROUP BY account, phone
Mark Byers
I wanted to make sure I wasn't mixing fields from more than one row with that selection. I am creating a new table based on that statement and I wanted to make sure it was an accurate representation of the data that it was created from
Donovan
Your right on the Min(phone) it didn't used to be part of the grouping and when I included it I missed removing MIN.
Donovan
@Donovan: You could be mixing fields from more than one row. See my updated answer where I give an example of this.
Mark Byers
+1  A: 

I Believe his question is that if the following data were present

Account Phone       ChargeId LocationId
1       111-1111    6         8
2       111-1111    7         7
2       111-1111    8         6

would it return 2, 111-1111, 7, 6 (the 7 from chargeId(2) and the 6 from LocationId(3)).

If that is what you're asking, then yes. It'll ruin your results.

Mike M.
This is why I was trying to use the Primary key that was established using ROW_NUMBER() (from your previous question). Are you able to push the data into a new table with an IDENTITY column specified?
Mike M.
crud. That's what I was afraid of.
Donovan
@Mike M yep. I can if needed. I can mod the table temporarily to include a row number as a key. I won't be able to include it in the query but I can get the table to a point where it has a unique key before running the select statement.
Donovan