Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to update table based on the contents of other table

Posted on 2011-09-21
14
Medium Priority
?
235 Views
Last Modified: 2012-05-12
I want to update the color of TableB based on the the colors of TableA FOR EACH DAY.  Here are the different cases: Case1 If any given day we have all the 3 colors (Green, Yellow, Red) then we say that day is Red.   Case2: if we have any given day (Green and Yellow only) then we say that day is Yellow.   Case3: If we have any given day (Only Green) then we say that day is Green.  Here is an example that i have:

TableA
Date                                      Prdouct                          color
2011-09-19                              JJ                                Green
2011-09-19                             XL                               Green
2011-09-19                             MM                                Red
2011-09-18                              JJ                                Green
2011-09-18                             XL                               Green
2011-09-18                            MM                                Yellow
2011-09-17                              JJ                                Green
2011-09-17                             XL                               Green
2011-09-17                            MM                                Green

so i want to update the below table based on TablA

TableB
Date                    DayColor
2011-09-19            Red
2011-09-18            Yellow
2011-09-17              Green

thanks
0
Comment
Question by:karinos57
  • 4
  • 3
  • 3
  • +2
14 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 36577171
UPDATE TableA SET Color = TableB.DayColor
FROM TableA
INNER JOIN TableB ON TableA.Date = TableB.Date
0
 
LVL 18

Expert Comment

by:lludden
ID: 36577181
Opps, I didn't read the question correctly.  ignore that response.
0
 
LVL 18

Expert Comment

by:lludden
ID: 36577196
Does any red make the day red (2011-09-19 has 2 green and 1 red)?

Does any yellow make it yellow if there is no red?

The only way it can be green is if all are green?


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:karinos57
ID: 36577403
That is correct that is exactly what I need.  Thanks
0
 
LVL 14

Expert Comment

by:mds-cos
ID: 36577497
Sorry, got to jump so I can't write the query for you right now.  But one fairly simple way should be to use CASE in your update query.  Syntax here:  http://www.devx.com/tips/Tip/15633

Your ELSE for the CASE will of course be 'green'
0
 
LVL 14

Expert Comment

by:mds-cos
ID: 36577528
Oh...ps....you will need to incorporate a subquery to "gather" all results together for a day.  Sorry I don't have time to really work it out right now, but first blush I'm thinking use a case in the subquery that will assign each color a numerical value.  You can then ORDER the subquery in color priority (e.g. red, yellow, green) to get highest priority on top.  Then pull the TOP 1 result from your subquery to get the color for the day.  Now you just need to use a CASE for the update to convert returned number back to text color.
0
 

Author Comment

by:karinos57
ID: 36577637
i have tried to use Case but got some issues that is why i posted here to get EE help.  Is there anyone who can help me here?  thanks
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 750 total points
ID: 36577776
Try it this way:
UPDATE  tb
SET     DayColor = CASE tb.ColorIndex
                     WHEN 3 THEN 'Red'
                     WHEN 2 THEN 'Green'
                     WHEN 1 THEN 'Yellow'
                   END
FROM    TableA ta
        INNER JOIN (SELECT  [Date],
                            MAX(CASE Color
                                  WHEN 'Red' THEN 3
                                  WHEN 'Green' THEN 2
                                  WHEN 'Yellow' THEN 1
                                  ELSE NULL
                                END) ColorIndex
                    FROM    TableA
                    GROUP BY [Date]
                   ) tb ON ta.[Date] = tb.[Date]
WHERE   tb.ColorIndex IS NOT NULL

Open in new window

0
 

Author Comment

by:karinos57
ID: 36578091
acperkins:
thanks.  i am getting this error "Invalid Column name DayColor"
0
 
LVL 9

Assisted Solution

by:sachinpatil10d
sachinpatil10d earned 750 total points
ID: 36578718
Try this

Select [date],
       case Left(Main.Colour,Len(Main.Colour)-1) when 'Green' then 'Green'
       when 'Green,Yellow' then 'Yellow'
       when 'Green,Red,Yellow' then 'Red' end
       As color
From(Select distinct date,
           (Select color + ',' AS [text()]
            From tableA tA
            Where tA.date = tB.date
            group by color,[date]
            ORDER BY tA.[date],color
            For XML PATH ('')) [Colour]
     From tablea tB) [Main]
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36578730
update query

update tableB set dayColor = t.color from (Select [date],
       case Left(Main.Colour,Len(Main.Colour)-1) when 'Green' then 'Green'
       when 'Green,Yellow' then 'Yellow'
       when 'Green,Red,Yellow' then 'Red' end
       As color
From(Select distinct date,
           (Select color + ',' AS [text()]
            From tableA tA
            Where tA.date = tB.date
            group by color,[date]
            ORDER BY tA.[date],color
            For XML PATH ('')) [Colour]
     From tablea tB) [Main]
     ) t
left outer join tableB on tableB.[date] = t.[date]
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36580146
>>i am getting this error "Invalid Column name DayColor" <<
Unfortunately, we cannot go by anything more than what you post.  This is what you gave us:

I want to update the color of TableB based on the the colors of TableA FOR EACH DAY.
...
TableA
Date                                      Prdouct                          color
...
TableB
Date                   DayColor


If you are not prepared to post the real table schema we have to use what you gave us and it is up to you to translate.  We cannot give you a complete solution if you only give us half the problem.
0
 

Author Closing Comment

by:karinos57
ID: 36896881
thx
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36896929
What would it have taken to get an "A" grade?  Was there something I could have done better?  Did you want me to remote into your site and type the query for you?
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question