Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a requirement where we get data something like this from 2 tables
Tag and value are 2 columns.

tag      Value
====     =====
Color    RED
Color    GREEN
Color    BLUE
Theme    PARTY
Theme    Diwali
Theme    Christmas
Theme    Holi



Now,

If I pass red to the query, it must remove all the color tags and it should return something like this:


tag      Value
====     =====
Color    RED
Theme    PARTY
Theme    Diwali
Theme    Christmas
Theme    Holi



I was looking for a SQL query to show a single matched row for the column data.

Just in the above example, When I pass Red to the value column(and also color to the Tag column), Nothing should change, except all the color tags rows must be removed except the one i selected. rest of the data remains same.


this data is being shared from 2 tables.

and the query to retrieve data will be something like this.

SQL
SELECT DISTINCT Make.tag, fest.value FROM Make JOIN fest ON make.ID = fest.ID 
WHERE (fest.ID IS NOT NULL) and ( not fest.ID = '') AND 
( Make.tag = 'color' OR Make.tag = 'Theme') ORDER BY Make.tag, fest.ID;


Thank you!
Posted
Updated 30-Oct-15 14:14pm
v4
Comments
Shyam S Singh 30-Oct-15 15:16pm    
Hi Praneeth,

Can you provide the actual structure of both tables, would be helpful us to provide demo script to insert the data into it ?
[no name] 30-Oct-15 15:17pm    
"from 2 tables":
Please show the two tables in more details with the relevant fields.
Thank you.

OP: I made some formatting changes on your Q. I hope it is ok...
praneeth arnepalli 30-Oct-15 18:42pm    
I was looking for a SQL query to show a single matched row for the column data.

Just in the above example, When I pass Red to the value column(and also color to the Tag column), Nothing should change, except all the color tags rows must be removed except the one i selected. rest of the data remains same.

tag Value
==== =====
Color RED
Theme PARTY
Theme Diwali
Theme Christmas
Theme Holi


this data is being shared from 2 tables.

and the query to retrieve data will be something like this.


SELECT DISTINCT Make.tag, fest.value FROM Make JOIN fest ON make.ID = fest.ID
WHERE (fest.ID IS NOT NULL) and ( not fest.ID = '') AND
( Make.tag = 'color' OR Make.tag = 'Theme') ORDER BY Make.tag, fest.ID;


Thank you!
George Jonsson 30-Oct-15 19:24pm    
You need to update your question with the structure, and maybe example data, for both tables.
Then someone can test it and help you to create a suitable query.

Try this:

SQL
SELECT SQ.tag
      ,SQ.value
FROM
(
SELECT DISTINCT
       Make.tag
     , fest.value
     , ROW_NUMBER() OVER (ORDER BY Make.tag, fest.ID) AS RID
FROM Make
JOIN fest
    ON
        make.ID = fest.ID
WHERE
     NULLIF(fest.ID, '') IS NOT NULL
    AND
    ( Make.tag = 'color' OR Make.tag = 'Theme')
)AS SQ
WHERE
     CASE WHEN Tag = 'Color' AND Value = 'RED' THEN 1
          WHEN Tag = 'Theme' THEN 1
          ELSE 0
     END = 1
ORDER BY RID
 
Share this answer
 
Based on such small portion of information, my best guess is:
SQL
SELECT m.Tag, f.Value
FROM Make AS m JOIN fest AS f ON m.ID = f.ID 
WHERE m.Tag = @tag AND f.Value=@val
UNION ALL
SELECT m.Tag, f.Value
FROM Make AS m JOIN fest AS f ON m.ID = f.ID
WHERE m.Tag != @tag 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900