简体   繁体   English

如何在SQL查询的列中添加值?

[英]How to add values to columns on an SQL query?

I have a question for homework that I do not quite understand. 我有一个不太清楚的作业问题。 I am being asked to make a query that will result additional values on the same row, Meaning adding more columns as far as I understand. 我被要求进行查询,这将在同一行上导致其他值,据我所知,这意味着添加更多列。 I have no idea how to do it, Unfortunately answers to existing questions here did not seem to answer my question, Maybe I am missing something (I am an SQL noob). 我不知道该怎么做,不幸的是,这里对现有问题的回答似乎并不能回答我的问题,也许我错过了一些东西(我是SQL noob)。

Here is the question: 这是问题:

Write a query that returns the following data: a row per date and abtest, where each row has the impressions and clicks for all the variants and for the control. 编写一个查询,该查询返回以下数据:每个日期一行,且每行最差,其中每一行都有所有变体和控件的印象和点击。 Variants are identified as vrows where variant_id is not zero, Control is identified as rows where the variant_id is 0. 变体被标识为vrow,其中variant_id不为零,Control被标识为行,其中variant_id为0。

Eg for the below data, the query will return: 例如,对于以下数据,查询将返回:

2018-01-01,2,32323,212,95262,354 2018-01-01,2,32323,212,95262,354

2018-01-01,5,76675,5454,675675,5454 2018-01-01,5,76675,5454,675675,5454

2018-01-02,2,0,0,7834,99 2018-01-02,2,0,0,7834,99

2018-01-02,5,0,0,9664,144 2018-01-02,5,0,0,9664,144

Table: https://i.ibb.co/kVZTyt5/ob.jpg 表格: https : //i.ibb.co/kVZTyt5/ob.jpg

So far I tried this: 到目前为止,我已经尝试过了:

SELECT date, abtest_id, impressions, clicks FROM table1 WHERE variant_id IS NOT NULL
GROUP BY date, abtest_id;

And this is the result: 结果如下:

2018-01-01 2 5454 11 2018-01-01 2 5454 11

2018-01-01 5 76675 5454 2018-01-01 5 76675 5454

2018-01-02 2 7834 99 2018-01-02 2 7834 99

2018-01-02 5 776 12 2018-01-02 5 776 12

I don't know how to add more clicks and impressions to the results on the same row, And looking at the example from the question, I don't understand how and why some of these values are there. 我不知道如何在同一行的结果中增加更多的点击次数和展示次数,再看问题中的示例,我不知道其中的某些值如何以及为什么存在。 Any help would be deeply appreciated. 任何帮助将不胜感激。

Thanks! 谢谢!

In all DBMS You can use INSERT OR UPDATE. 在所有DBMS中,您都可以使用INSERT或UPDATE。

INSERT for add new values Update for change old values 插入以添加新值更新以更改旧值

A Simple Insert syntax is: Insert into tableName (column1, column2,...) Values (value1, value2,...) 一个简单的Insert语法是:插入tableName(column1,column2,...)值(value1,value2,...)

A Simple Update syntax is: 一个简单的更新语法是:

Update TableName
Set Column1=value1, Column2=value2

You can use where end of above simple codes 您可以在上述简单代码的末尾使用

Insert syntax by other table is: Insert into tableName (column1, column2,...) select t.column1,t.column2... From tablename2 通过其他表插入语法是:插入到tableName(column1,column2,...)中,选择t.column1,t.column2 ...从tablename2

A Simple Update syntax is: 一个简单的更新语法是:

Update TableName
Set Column1=value1, Column2=value2
Where colmn1= myValuse

To me you don't need to add columns or rows to your table. 对我来说,您不需要在表中添加列或行。 You need to select data, and make a few sums on some fields. 您需要选择数据,并在某些字段上求和。

I would do something along these lines in MySQL (only full group by mode turned off): 我会在MySQL的这些方面做一些事情(仅按模式关闭整个分组):

SELECT t.*,
SUM(CASE 
    WHEN variant_id != 0 THEN impressions 
    ELSE 0 
    END) v_impressions,
SUM(CASE 
    WHEN variant_id != 0 THEN clicks 
    ELSE 0 
    END) v_clicks
FROM `tests` as t
GROUP BY date, abtest_id;

Although there may be a simpler syntax. 尽管可能存在更简单的语法。

This selects the data from the rows where variant_id = 0, it sums impressions and clicks where variant_id != 0 and groups by date and abtest_id. 这将从variant_id = 0的行中选择数据,对variable_id!= 0的印象和点击求和,并按日期和abtest_id进行分组。

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

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