简体   繁体   English

在mysql中添加公式

[英]add formula in mysql

I want create a new table from the existing table. 我想从现有表创建一个新表。

This is existing table 这是现有表

A       B               C     
title   view_count  date   

aaa      24      5/18/13    
bbb      10      5/18/13    
ccc      10      5/18/13    
aaa      10      5/19/13    
aaa      20      5/20/13    
bbb      10      5/20/13    
ccc      20      5/20/13    
ccc      20      5/21/13    
aaa       5      5/22/13    
bbb       3      5/22/13    
ccc       1      5/22/13    

I try to make new table as follows: 我尝试制作新表,如下所示:

title  date1(5/18/13)    date2(5/19/13)    date3(5/20/13)

aaa      24 (view_count)      10                20

How can I make this in MySQL? 如何在MySQL中做到这一点?

======================= I add my question. =======================我添加我的问题。 I've tried these queries you help, and then I got results what I want. 我尝试过这些对您有帮助的查询,然后得到想要的结果。 Can I make them as a new table? 我可以将它们作为新桌子吗? I want to make a new table as these results. 我想制作一张新桌子作为这些结果。

EDIT...OP changed his question while I was answering it, EDIT ... OP在我回答问题时改变了他的问题,

SUMIFS() Can Help! SUMIFS()可以帮助您!

I use this a lot as I lookup and return data all day. 我整天查询并返回数据时经常使用它。 It allows you to return a value from a range where a one or more certain criteria from a range is true. 它允许您从某个范围中的一个或多个特定条件为真的范围中返回一个值。 Works great with data structured like yours. 适用于像您这样的结构化数据。

If your headers in row 1 are as shown with the data in row 2 and Your Today date is in F1, then the formula you can type in column D2 and drag down would be... 如果第1行的标题与第2行的数据一起显示,并且“ Today日期在F1中,那么您可以在D2列中键入并向下拖动的公式将是...

=(SUMIFS(B:B,A:A,A2,C:C,$F$1-1)-SUMIFS(B:B,A:A,A2,C:C,$F$1-2))/(SUMIFS(B:B,A:A,A2,C:C,$F$1-1))

You can change the value of cell F1 with the excel function Today() and or just put Today into your formula. 您可以使用excel函数Today()更改单元格F1的值,或者仅将Today放入公式中。

I hope this helps. 我希望这有帮助。

-Schebealls -Schebealls

在此处输入图片说明

You can do this like so: 您可以这样做:

select title,
       sum(case `date` when '2013/05/18' then view_count else 0 end) `date1(5/18/13)`,
       sum(case `date` when '2013/05/19' then view_count else 0 end) `date2(5/19/13)`,
       sum(case `date` when '2013/05/20' then view_count else 0 end) `date3(5/20/13)`
from myTable
group by title

- to return values for all titles; -返回所有标题的值; if you only want to see results for title aaa, then replace group by title with where title='aaa' . 如果您只想查看标题aaa的结果,则将group by title替换group by title where title='aaa'

Try this Query 试试这个Query

select 
    distinct
    t.title,
    t1.view_count as `date1(5/18/13)`,
    t2.view_count as `date2(5/19/13)`,
    t3.view_count as `date2(5/20/13)`
from table_name t
left join table_name t1 on t.title=t1.title and t1.date='2013/5/18'
left join table_name t2 on t.title=t2.title and t2.date='2013/5/19'
left join table_name t3 on t.title=t3.title and t3.date='2013/5/20'
where t.title='aaa'

SQL Fiddle SQL小提琴

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

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