简体   繁体   English

sql: - 在查询中替换和取消忽略函数

[英]sql: - replace and unpivot functions in a query

Fiddle Example here: http://sqlfiddle.com/#!3/419ec/3 这里的小提琴示例: http ://sqlfiddle.com/#!3/419ec/3

SQL server 2008. SQL Server 2008。

I was wondering whether someone can please explain to me what is going on in the select query below with the replace function and unpivot function. 我想知道是否有人可以通过替换功能和unpivot函数向我解释下面的选择查询中发生了什么。 I'm a newbie to sql and I don't understand the logic of that type of query (de-normalizing a table). 我是sql的新手,我不理解这种类型的查询的逻辑(对表进行反规范化)。

CREATE TABLE TableB
    ([date] datetime, [Id] int, [name] varchar(3), [blah1] varchar(4), [hour1] int, [hour2] int, [hour3] int, [hour4] int)
;

INSERT INTO TableB
    ([date], [Id], [name], [blah1], [hour1], [hour2], [hour3], [hour4])
VALUES
    ('2013-04-01 00:00:00', 1, 'Jim', 'test', 129, 343, 54, 89),
    ('2013-04-01 00:00:00', 2, 'Bob', 'rewe', 45, 6, 45, 2),
    ('2013-04-02 00:00:00', 3, 'Joe', 'fdf', 7, 8, 4, 3)

Select Query: 选择查询:

select date, 
  id, 
  name,
  replace(MightMouse, 'hour', '') hour,
  observationvalue
from tableB
unpivot
(
  observationvalue
  for MightMouse in (hour1, hour2, hour3, hour4)
) unpiv

I thought the usage of the replace function was as follows: 我认为替换功能的用法如下:

REPLACE ( string_expression , string_pattern , string_replacement )

string_expression

http://msdn.microsoft.com/en-us/library/ms186862.aspx http://msdn.microsoft.com/en-us/library/ms186862.aspx

As per the definition of the replace function, string_expression is the string in which a sub-string is being searched (the sub-string can be the full string). 根据replace函数的定义,string_expression是搜索子字符串的字符串(子字符串可以是完整字符串)。 For example, 例如,

replace('mynameisjohn', 'john', '')

This would search for the sub-string john in string_expression mynameisjohn and replace it with empty string, resulting in a string that equals mynameis . 这将在string_expression mynameisjohn搜索子字符串john并将其替换为空字符串,从而生成一个等于mynameis的字符串。

But in the above example, I don't understand what MightyMouse is. 但在上面的例子中,我不明白MightyMouse是什么。 There is no MightyMouse in the original table. 原始表中没有MightyMouse I also don't know how the unpivot part fits into the query as in the flow of execution. 我也不知道unpivot部分如何适合查询,如在执行流程中。

If this was python for example there is a flow to the logic of the code that is intuitive. 如果这是python,例如,代码逻辑的流程是直观的。 With SQL, it seems you can build ugly queries and from sql's perspective things work just fine. 使用SQL,似乎你可以构建丑陋的查询,从sql的角度来看,一切正常。 But from the user's perspective it can be difficult to decompose what is going on in different parts of the query code. 但是从用户的角度来看,很难分解查询代码的不同部分中发生的事情。

Unpivot uses nested left outer join for each column specified in your case Unpivot对您指定的每个列使用嵌套的左外连接

 (hour1, hour2, hour3, hour4)

these column names are also part of the result which is referred as MightMouse in your case 这些列名也是结果的一部分,在您的案例中称为MightMouse

select replace('hour1','hour','')
select replace('hour2','hour','')
select replace('hour3','hour','')

in other words the same query can be written like this 换句话说,可以像这样编写相同的查询

select a.date,a.id, 
  a.name,replace('hour1', 'hour', '') as hour ,a.hour1 as observationvalue
from TableB a
left outer join TableB b
on a.hour1=b.hour1
where a.hour1 is not null
union

select a.date,a.id, 
  a.name,replace('hour2', 'hour', '') as hour ,a.hour2 as observationvalue
from TableB a
left outer join TableB b
on a.hour2=b.hour2
where a.hour2 is not null
union
select a.date,a.id, 
  a.name,replace('hou3', 'hour', '') as hour ,a.hour3 as observationvalue
from TableB a
left outer join TableB b
on a.hour3=b.hour3
where a.hour3 is not null
union
select a.date,a.id, 
  a.name,replace('hour4', 'hour', '') as hour ,a.hour4 as observationvalue
from TableB a
left outer join TableB b
on a.hour4=b.hour4
where a.hour4 is not null

UNPIVOT is a table operator, it operates on the table preceeding it: UNPIVOT是一个表运算符,它在它之前的表上运行:

tableName UNPIVOT (<unpivot-expression>)

Your UNPIVOT adds two extra columns (not one as has been implied), first the observationvalue column, which contains the values from the columns (hour1, hour2, hour3, hour4) rotated from horizontal to vertical. 你的UNPIVOT增加了两个额外的列(不是暗示的一个),首先是observationvalue列,它包含从水平旋转到垂直的列(hour1, hour2, hour3, hour4)的值。

And secondly it adds the MightMouse column which is the Names of the (hour1, hour2, hour3, hour4) columns that the corresponding current observationvalue was pulled from. 其次,它添加了MightMouse列,它是从相应的当前observationvalue中提取的(hour1, hour2, hour3, hour4)列的名称

So this row: 所以这一行:

hour1, hour2, hour3, hour4
129,   343,   54,    89

becomes these rows: 成为这些行:

observationvalue MightMouse
129              hour1
343              hour2
 54              hour3
 89              hour4

(With the other corresponding column values also, of course) (当然还有其他相应的列值)

Hopefully, this also makes clear what the REPLACE is doing and why it works. 希望这也清楚地说明了REPLACE正在做什么以及它为何起作用。


As for the effective (*) order of execution: the FROM clause is always executed first in a SQL Query, then the WHERE clause. 至于有效 (*)执行顺序: FROM子句总是在SQL查询中执行,然后在WHERE子句中执行。 The SELECT clause (the column list and column expressions) is almost last, and an ORDER BY clause would normally be last (there are some MS specific exceptions). SELECT子句(列列表和列表达式)几乎是最后一个, ORDER BY子句通常是最后一个(有一些MS特定的例外)。

(* - this is only the "effective"/"logical" order of execution. The SQL engine is allowed to do things in any actual order that it wants, so long as it has the same logical effect as this order) (* - 这只是执行的“有效”/“逻辑”顺序。允许SQL引擎按照它想要的任何实际顺序执行操作,只要它具有与此顺序相同的逻辑效果)

"MightMouse" is the name of the new column. “MightMouse”是新列的名称。 Essentially, you are taking all of those hour columns and smushing them into one column. 从本质上讲,您将把所有这些小时列并将它们分成一列。 The "for MightMouse in ..." is calling the column MightMouse. “for MightMouse in ...”正在调用MightMouse专栏。 So then, you are replacing the literal string "hour" from that new column with an empty string. 那么,您将使用空字符串替换该新列中的文字字符串“hour”。

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

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