0

I am trying to split out few values from a database.

Sample data

There are two columns, Test and Test_Parameter. I want to extract those rows which have different "test_parameter" values for same "Test" value. For example, in my screenshot, I want "Grade:" to be selected as it has different values of Test_Parameter. I know it has a very simple solution but I am not able to figure it out.

0

4 Answers 4

1

You can use aggregation and having:

select test
from t
group by test
having min(test_parameter) <> max(test_parameter);
Sign up to request clarification or add additional context in comments.

4 Comments

Hey Gordon, thanks a lot for the reply! I was just wondering about the max and min function used by you. Can you please explain that to me?
Max and Min are table functions that can be referenced here in the Snowflake documentation: docs.snowflake.net/manuals/sql-reference/functions/min.html
@AshishKumar . . . These are aggregation functions and a very basic part of SQL.
Yeah I have used them before, but in my case it was a string, so I was asking the context for which you have used.
0

I might be confused by the ask, but wouldn't this work?

SELECT *
FROM t
WHERE test <> test_parameter

3 Comments

Yeah I just tried this statement myself while I was posting this question over here. This is the simplest solution available I guess. But I was not sure about this as it is too simple.
Sometimes, it's just that easy. :-) One thing to be careful about is the case-sensitivity. If you want it to be case insensitive, then you should wrap an UPPER() or LOWER() around both test and test_parameter.
I think this answer may have understood the question differently while producing the same output. To test that, just add in another test with just one test_parameter such that test<>test_parameter, then run all the answers. The question is -Are you trying to output test where test value doesn't equal corresponding test_parameter value? --VS-- Are you trying to output tests whose test_parameters are not homogeneous? If former, go with this answer. If latter, go with Gordon's answer.
0

Try any of below to remove colon and compare :

    SELECT DISTINCT test FROM table1 WHERE test NOT LIKE test_parameter||'%'

    SELECT DISTINCT test FROM table1 WHERE SUBSTR(test,1,INSTR(test,':')-1) <> test_parameter

Comments

0

I like Gordon's answer better, but a maybe simple you understand albeit I suspect slower method would be:

SELECT test 
FROM (
    SELECT test,
        COUNT(DISTINCT(test_parameter) AS c
    FROM table1
    GROUP BY 1
)
WHERE c > 1

the above is if you really don't want the counts in the results, otherwise the having clause makes its simpler:

SELECT test,
    COUNT(DISTINCT(test_parameter) AS c
FROM table1
GROUP BY 1
HAVING c > 1

Comments

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.