question

SQLGuy avatar image
SQLGuy asked

SQL Help Querying String in Non-Unique Record

I have a table: CREATE TABLE #SingleSELECTFromMultiRecords( [ID] INT IDENTITY(1,1) NOT NULL, [REASON][VARCHAR](500), [To][VARCHAR](500), [From] [VARCHAR](50), [COMMENT][VARCHAR] (200)) with some data.... INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('TestText1', 'Test1', 'Test2', 'ALL') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('TestText2', 'ABC1', 'ABC2', 'ALL') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('TestText3', 'ABC3', 'ABC4', 'ALL') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('Not covered', 'PRE4', 'POST4', 'Per Dium') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('Not covered', 'PRE1', 'PRE2', 'Per Dium') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('Not covered', 'PRE1', 'POST1', 'Per Dium1') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('Not covered', 'PRE2', 'POST2', 'Per Dium2') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('Not covered', 'PRE3', 'POST3', 'Per Dium3') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('Not covered', 'PRE4', 'POST4', 'Per Dium4') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('Not covered', 'PRE1', 'PRE2', 'ALL') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('TestText4', 'Test4', 'Test5', 'ALL') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('TestText6', 'ABC7', 'ABC8', 'ALL') INSERT INTO #SingleSELECTFromMultiRecords (REASON, [To], [From], Comment) VALUES ('TestText9', 'ABC10', 'ABC11', 'ALL') What I am looking for is a query that: 1. If it's ALL for the REASON where REASON is TestText# then SELECT either To or From for that record. 2. If Comment does not = ALL, then match the field 'Comment' (Containing Per Dium) to a value. Basically if REASON contains Test text - select either to or from, if REASON = Not Covered (non-unique) then match the comment field to SELECT To or FROM from the record.
sql query
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
can you give example output for this - I'm struggling to understand the requirement properly
0 Likes 0 ·
SQLGuy avatar image SQLGuy commented ·
OutPut: SELECT To FROM #SingleSELECTFromMultiRecords WHERE Reason = 'Test Text 2' -- will = ABC1 SELECT To FROM #SingleSELECTFromMultiRecords WHERE Reason = 'Not covered' -- will return multiple records What I need is a dynamic way to say: If Comment = 'ALL' then Select me based on REASON If Comment <> "ALL' then match me to a string and return only the record where the string matches.
0 Likes 0 ·

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.