简体   繁体   English

将列标题转换为行元素

[英]Converting Column Headers to Row elements

I have 2 tables I am combining and that works but I think I designed the second table wrong as I have a column for each item of what really is a multiple choice question. 我有2个正在组合的表,并且可以工作,但是我认为我设计的第二个表是错误的,因为我为确实是多项选择题的每个项目都设有一列。 The query is this: 查询是这样的:

select Count(n.ID) as MemCount, u.Pay1Click, u.PayMailCC, u.PayMailCheck, u.PayPhoneACH, u.PayPhoneCC, u.PayWuFoo
from name as n inner join
UD_Demo_ORG as u on n.ID = u.ID
where n.MEMBER_TYPE like 'ORG_%' and n.CATEGORY not like '%_2' and 
(u.Pay1Click = '1' or u.PayMailCC = '1' or u.PayMailCheck = '1' or u.PayPhoneACH = '1' or u.PayPhoneCC = '1' or u.PayWuFoo = '1')
group by u.Pay1Click, u.PayMailCC, u.PayMailCheck, u.PayPhoneACH, u.PayPhoneCC, u.PayWuFoo

The results come up like this: 结果如下:

Count Pay1Click   PayMailCC   PayMailCheck   PayPhoneACH   PayPhoneCC   PayWuFoo
8      0           0          0              0             0            1
25     0           0          0              0             1            0
8      0           0          0              1             0            0
99     0           0          1              0             0            0
11     0           1          0              0             0            0

So the question is, how can I get this to 2 columns, Count and then the headers of the next 6 headers so the results look like this: 因此,问题是,如何将其分为2列,即Count,然后是接下来的6个标题的标题,因此结果如下所示:

Count PaymentType
8      PayWuFoo        
25     PayPhoneCC          
8      PayPhoneACH         
99     PayMailCheck        
11     PayMailCC           

Thanks. 谢谢。

I think indeed you made a mistake in the structure of the second table. 我认为确实您在第二张表的结构中犯了一个错误。 Instead of creating a row for each multiple choice question, i would suggest transforming all those columns to a 'answer' column, so you would have the actual name of the alternative as the record in that column. 我建议不要将所有这些列都转换为“答案”列,而不是为每个选择题创建一行,因此您将使用替代项的实际名称作为该列中的记录。

But for this, you have to change the structure of your tables, and change the way the table is populated. 但是为此,您必须更改表的结构,并更改表的填充方式。 you should get the name of the alternative checked and put it into your table. 您应该检查替代项的名称并将其放入表中。

More on this, you could care for repetitive data in your table, so writing over and over again the same string could make your table grow larger. 除此之外,您还可以照顾表中的重复数据,因此一遍又一遍地写相同的字符串会使表变大。 if there are other things implied to the answer, other informations in the UD_Demo_ORG table, then you can normalize the table, creating a payment_dimension table or something like this, give your alternatives an ID such as 如果UD_Demo_ORG表中还包含其他暗示的信息,其他信息,则可以将其规范化,创建payment_dimension表或类似的东西,并给您的替代项一个ID,例如

ID   PaymentType   OtherInfo(description, etc)...
1     PayWuFoo         ...
2     PayPhoneCC       ... 
3     PayPhoneACH      ...
4     PayMailCheck     ...
5     PayMailCC        ...

This is called a dimension table, and then in your records, you would have the ID of the payment type, and not the information you don't need. 这称为维度表,然后在您的记录中,您将拥有付款类型的ID,而不是不需要的信息。 So instead of a big result set, maybe you could simplify by much your query and have just 因此,除了庞大的结果集之外,也许您可​​以简化很多查询,并且只需

Count PaymentId
8      1        
25     2          
8      3         
99     4        
11     5 

as a result set. 结果集。 it would make the query faster too, and if you need other information, you can then join the table and get it. 它也可以使查询更快,如果您需要其他信息,则可以联接表并获取它。

BUT if the only field you would have is the name, perhaps you could use the paymentType as the "id" in this case... just consider it. 但是,如果您唯一拥有的字段是名称,那么在这种情况下,您可以将paymentType用作“ id” ...考虑一下。 It is scalable if you separate to a dimension table. 如果您分离到维度表,则它是可伸缩的。

Some references for further reading: 一些参考资料供进一步阅读:

  1. http://beginnersbook.com/2015/05/normalization-in-dbms/ "Normalization in DBMS" http://beginnersbook.com/2015/05/normalization-in-dbms/ “ DBMS中的标准化”

  2. http://searchdatamanagement.techtarget.com/answer/What-are-the-differences-between-fact-tables-and-dimension-tables-in-star-schemas "Differences between fact tables and dimensions tables" http://searchdatamanagement.techtarget.com/answer/What-are-the-differences-between-fact-tables-and-dimension-tables-in-star-schemas “事实表与维度表之间的差异”

Try this one 试试这个

Select Count,

CASE WHEN Pay1Click=1 THEN 'Pay1Click'
           PayMailCC=1 THEN ' PayMailCC'
          PayMailCheck=1 THEN 'PayMailCheck'
          PayPhoneACH=1 THEN 'PayPhoneACH'
          PayPhoneCC=1 THEN 'PayPhoneCC'
          PayWuFoo=1 THEN 'PayWuFoo'
     END as PaymentType

FROM ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM