Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003

    Unanswered: any way to check if all the values in a column returned are the same value?

    say i have a table, and in it are two columns, column1 and column2 and i do the following query:

    SELECT column1, column2 FROM table WHERE column2 = '12345'

    i want to check if column1 has all the same values, so in the first case, no

    column1      column2
    -------       --------
    4               12345
    9               12345
    5               12345
    column1      column2
    -------       --------
    9               12345
    9               12345
    9               12345
    in the 2nd case, column1 contains all the same values, so yes

    is there anyway i can check this? i would be doing this in a trigger.. say when a new row is inserted, the value of column1 is inserted, but col 2 is null.. so when they try to fill in the value for col2 of that row, the trigger checks to see if the value they put for col 2 is already in the table.. if it isn't, then everything is ok. but if it is already in teh table, then it checks col1 to see if all the values of col1 are the same

    i hope this makes sense


  2. #2
    Join Date
    Aug 2003
    Delft, The Netherlands (EU)
    Within your trigger, you may determine the min() and max() value of Column1 regarding a certain value for column 2:

    SELECT min(Column1), max(column1)
    Into Min1, Max1
    FROM <YourTable>
    WHERE Column2 = <Your current value>
    GROUP BY Column2

    If Min1=Max1, all values are the same.
    Make everything as simple as possible, but not simpler! - A. Einstein
    DB Problems? DB Explorer, BTrieve Re-engineering, DB Conversions & ETL? Conversion Tool

  3. #3
    Join Date
    Oct 2003
    SELECT COUNT(NumValues) FROM (
    SELECT COUNT(*) AS NumValues FROM MyTable
    WHERE Column2 = MyValue
    GROUP BY Column1) AS TempCount;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts