set column value based on distinct values in another column

笑着哭i 提交于 2019-12-11 14:06:28

问题


I am trying to do something very similar to this question: mysql - UPDATEing row based on other rows

I have a table, called modset, of the following form:

member    year    y1    y2    y3    y1y2    y2y3    y1y3    y1y2y3 
a         1       0     0     0     0       0       0       0
a         2       0     0     0     0       0       0       0
a         3       0     0     0     0       0       0       0
b         1       0     0     0     0       0       0       0
b         2       0     0     0     0       0       0       0
c         1       0     0     0     0       0       0       0
c         3       0     0     0     0       0       0       0
d         2       0     0     0     0       0       0       0

Columns 3:9 are binary flags to indicate which combination of years the member has records in. So I wish the result of an SQL update to look as follows:

member    year    y1    y2    y3    y1y2    y2y3    y1y3    y1y2y3 
a         1       0     0     0     0       0       0       1
a         2       0     0     0     0       0       0       1
a         3       0     0     0     0       0       0       1
b         1       0     0     0     1       0       0       0
b         2       0     0     0     1       0       0       0
c         1       0     0     0     0       0       1       0
c         3       0     0     0     0       0       1       0
d         2       0     1     0     0       0       0       0

The code in the question linked above does something very close but only when it is a count of the distinct years in which the member has records. I need to base the columns on the specific values of the years in which the member has records.

Thanks in advance!

SOLUTION

SELECT member,
    case when min(distinct(year)) = 1 and max(distinct(year)) = 1 then 1 else 0 end y1,
    case when min(distinct(year)) = 1 and max(distinct(year)) = 2 then 1 else 0 end y1y2,
    case when min(distinct(year)) = 1 and max(distinct(year)) = 3 and count(distinct(year)) = 2 then 1 else 0 end y1y3,
    case when min(distinct(year)) = 1 and max(distinct(year)) = 3 and count(distinct(year)) = 3 then 1 else 0 end y1y2y3,
    case when min(distinct(year)) = 2 and max(distinct(year)) = 2 then 1 else 0 end y2,
    case when min(distinct(year)) = 2 and max(distinct(year)) = 3 then 1 else 0 end y2y3,
    case when min(distinct(year)) = 3 then 1 else 0 end y3
INTO temp5
FROM modset
GROUP BY member;

UPDATE modset M
SET y1 = T.y1, y2 = T.y1, y3 = T.y3, y1y2 = T.y1y2, y1y3 = T.y1y3, y2y3 = T.y2y3, y1y2y3 = T.y1y2y3
FROM temp5 T
WHERE T.member = M.member;

回答1:


What is the query you are using to return the indicators of the years the member has records in?

It sounds like you would want take your query results and use it in your update:

http://dev.mysql.com/doc/refman/5.0/en/update.html

It may look something like this:

UPDATE targetTable t, sourceTable s 
SET t.y1 = s.y1, t.y2 = s.y2 -- (and so on...)
WHERE t.member = s.member AND t.year = m.year;


来源:https://stackoverflow.com/questions/14082515/set-column-value-based-on-distinct-values-in-another-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!