sql 查询null 变为0

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 查询null 变为0相关的知识,希望对你有一定的参考价值。

select if a is null then 0 else a from aa
....

ORACLE下:

select decode(a,null,0,a)
from aa

SQLSERVER下:

select case when a = null then 0 else a end
from aa

上面这句就是判断语句,当A为NULL的时候,将NULL替换成0,不为NULL的时候,还是A。
*(a = null 或者a is null)

不明白再问我,谢谢!
参考技术A select isnull(a,0) from aa

r 使用此代码将SQL查询列表变为可视化数据库的使用方式。


## Install packages if you don't already have them
install.packages(c("stringr", "tm", "igraph"), dependencies = TRUE)

## Load the packages
library(stringr)
library(tm)
library(igraph)

## Read in the data
queries <- read.csv("~/Downloads/queries.csv")
tables <- read.csv("~/Downloads/tables.csv")

# See what tables look like
head(queries,1)
head(tables)

## Create an empty data frame, with rows equal to number of queries being analyzed
x <- data.frame(rep(0,length(queries$query)))

## Populate data frame
# In the data frame x, each row is a query and each column is the name of a table from your database; each cell is an indicator for whether a particular table name appears in a particular query
for (i in 1:length(tables$tables)) {
  x[,i] <- str_extract(queries$query, as.character(tables$tables[i]))
}

## Name columns and replace NAs
colnames(x) <- tables$tables
x[is.na(x)] <- 0

##Concatenate each row into a single string
x_args <- c(x, sep = " ")
x$list <- do.call(paste, x_args)

## Clean out zeroes and whitespace
x$list <- str_trim(gsub(0, "", x$list))
x$list <- gsub("[ ]+", " ", x$list)

## Check out data frame
x$list[520:524]

## Convert x$list into a corpus of documents
corpus <- Corpus(VectorSource(x$list))


## Create term-document matrix
tdm <- TermDocumentMatrix(corpus)
inspect(tdm)
termDocMatrix <- as.matrix(tdm)

## Create term-term matrix
termMatrix <- termDocMatrix %*% t(termDocMatrix)

## Create graph-adjacency matrix
g <- graph.adjacency(termMatrix, weighted=T, mode = "undirected")

## Remove loops
g <- simplify(g)

## Set labels and degrees of vertices
V(g)$label <- V(g)$name
V(g)$degree <- degree(g)

## Plot a Graph
plot(g, layout=layout_in_circle(g))

## Plot a prettier graph
V(g)$label.cex <- 3 * (0.06125 * V(g)$degree / max(V(g)$degree) + .2)
V(g)$label.color <- rgb(0, 0, .2, .49 * V(g)$degree / max(V(g)$degree) + .5)
V(g)$frame.color <- rgb(0, 0, .2, .39 * V(g)$degree / max(V(g)$degree) + .6)
egam <- (log(E(g)$weight)+.4) / max(log(E(g)$weight)+.4)
#E(g)$color <- rgb(0, .3, .6, egam)
E(g)$color <- rgb((colorRamp(c("blue", "yellow", "red"))(E(g)$weight/max(E(g)$weight)))/255, alpha=egam)
E(g)$width <- egam

plot(g, layout=layout_in_circle(g), vertex.color = rgb((colorRamp(c("blue", "yellow", "red"))(degree(g)/max(degree(g))))/255), vertex.size = ((V(g)$degree)*2/3)+2, edge.width = 6 * E(g)$weight / max(E(g)$weight))

以上是关于sql 查询null 变为0的主要内容,如果未能解决你的问题,请参考以下文章

sql中如何将已经赋值的字段变为原有的null

sql 子查询中部分数据有空值,怎么返回0,NULL+数字=null出来不可以

Mysql中查询一个表,把结果中的NULL替换成0,请写出sql语句

sql查询双精度-null> 0不返回任何记录

UDF 与子查询性能问题

查询 sql 语句,为null转换为0怎么做