我正在尝试提取合并energy <- structure(list(nodes = structure(list(name = structure(c(15L,
11L, 8L, 17L, 7L, 10L, 2L, 1L, 12L, 4L, 14L, 3L, 9L, 16L, 13L,
6L, 5L), .Label = c("Biomass", "Coal", "Commericial", "Electricity",
"Energy Services", "Exports", "Geothermal", "Hydro", "Industrial",
"Natural Gas", "Nuclear", "Petroleum", "Rejected Energy", "Residential",
"Solar", "Transportation", "Wind"), class = "factor")), .Names = "name", class = "data.frame", row.names = c(NA,
-17L)), links = structure(list(source = c(0L, 0L, 1L, 2L, 2L,
3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 11L, 11L,
12L, 12L, 13L, 13L), target = c(9L, 10L, 9L, 9L, 12L, 9L, 9L,
10L, 11L, 9L, 10L, 11L, 12L, 13L, 9L, 12L, 9L, 10L, 11L, 12L,
13L, 9L, 10L, 11L, 12L, 13L, 10L, 11L, 12L, 13L, 14L, 15L, 14L,
16L, 14L, 16L, 14L, 16L, 14L, 16L), value = c(0.25, 0.28, 8.34,
2.38, 0.01, 1.81, 0.16, 0.04, 0.02, 9.99, 4.75, 3.3, 9.36, 0.92,
14.3, 1.41, 0.52, 0.45, 0.13, 2.28, 1.35, 0.28, 0.98, 0.56, 8.2,
25.4, 4.78, 4.63, 3.27, 0.03, 25.4, 0.08, 3.95, 7.33, 3.05, 5.66,
4.91, 19.6, 21.9, 5.81), energy_type = structure(c(12L, 12L,
9L, 7L, 7L, 13L, 6L, 6L, 6L, 8L, 8L, 8L, 8L, 8L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L, 3L, 3L, 3L, 3L, 11L,
5L, 11L, 4L, 11L, 4L, 11L, 4L, 11L, 4L), .Label = c("Biomass",
"Coal", "Electricity", "Energy Services", "Exports", "Geothermal",
"Hydro", "Natural", "Nuclear", "Petroleum", "Rejected Energy",
"Solar", "Wind"), class = "factor")), .Names = c("source", "target",
"value", "energy_type"), class = "data.frame", row.names = c(NA,
-40L))), .Names = c("nodes", "links"))
library(networkD3)
sankeyNetwork(Links = energy$links, Nodes = energy$nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "Quads", LinkGroup = 'energy_type', colourScale = JS(
'd3.scaleOrdinal()
.domain(["Solar","Nuclear","Hydro","Wind","Geothermal","Natural Gas","Coal","Biomass","Petroleum","Electricity","Residential","Commericial","Industrial","Transportation","Rejected Energy","Exports","Energy Services"])
.range(["#FFFF00","#FF0000","#0000FF","#800080","#A52A2A","#00FFFF","#000000","#00FF00","#008000","#FFA500","#FAAFBE","#FAAFBE","#FAAFBE","#FAAFBE","#C0C0C0","#FFA500","#808080"])'
),
fontSize = 12, nodeWidth = 75, iterations = 100)
# putting in a data.frame might help see problems
color_scale <- data.frame(
range = c("#FFFF00","#FF0000","#0000FF","#800080","#A52A2A","#00FFFF","#000000","#00FF00","#008000","#FFA500","#FAAFBE","#FAAFBE","#FAAFBE","#FAAFBE","#C0C0C0","#FFA500","#808080"),
domain = c("Solar","Nuclear","Hydro","Wind","Geothermal","Natural Gas","Coal","Biomass","Petroleum","Electricity","Residential","Commericial","Industrial","Transportation","Rejected Energy","Exports","Energy Services"),
nodes = energy$nodes,
stringsAsFactors = FALSE
)
# once corrected color_scale can be used like this
sankeyNetwork(
Links = energy$links, Nodes = energy$nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "Quads", LinkGroup = 'energy_type', colourScale = JS(
sprintf(
'd3.scaleOrdinal()
.domain(%s)
.range(%s)
',
jsonlite::toJSON(color_scale$domain),
jsonlite::toJSON(color_scale$range)
)
),
fontSize = 12, nodeWidth = 75, iterations = 100
)
# if we change Natural Gas to Natural then
# the cyan appears as desired
color_scale[6,2] <- "Natural"
,GROUP BY
和ORDER BY
的记录,但我没有得到结果/期望的结果。这是我的表HAVING
:
test
我想要的结果是这样的:
+--------------------------------------+-----+---------------------+------+
| id | no | date_entered | name |
+--------------------------------------+-----+----------------------------+
| c44d1977-0193-a2f1-00f9-57e4c53ba416 | 104 | 2016-09-23 06:02:16 | Z-44 |
| ca1879a2-df08-4fe3-b144-57e4d2ab0c62 | 104 | 2016-09-23 06:57:35 | Z-44 |
| 5dd46d35-358a-314e-30fa-57e4d2ca92ad | 104 | 2016-09-23 06:59:17 | Z-44 |
| d474cc6a-e7a1-15d1-d209-57e4c5aa607d | 105 | 2016-09-23 05:03:08 | Z-45 |
| db77b687-763d-b63a-be77-57e4d2e246fb | 105 | 2016-09-23 05:51:41 | Z-45 |
| 70f0f514-c0fd-ddfd-55a6-57e4d21b5e3c | 106 | 2016-09-23 05:29:39 | Z-46 |
+--------------------------------------+-----+---------------------+------+
这里我只想获取那些属于同一+--------------------------------------+-----+----------+
| id | no | COUNT(*) |
+--------------------------------------+-----+----------+
| 5dd46d35-358a-314e-30fa-57e4d2ca92ad | 104 | 3 |
| db77b687-763d-b63a-be77-57e4d2e246fb | 105 | 3 |
+--------------------------------------+-----+----------+
并且是最旧的记录。此查询也应该在有多个记录属于同一no
的情况下运行。所以这是我正在使用的查询
no
但我得到了这个结果
SELECT id, no, COUNT(*)
FROM test
GROUP BY no
HAVING COUNT(*) > 1
ORDER BY date_entered DESC;
现在,我已经四处寻求帮助了,我得到了这个How to combine GROUP BY, ORDER BY and HAVING,并且在+--------------------------------------+-----+----------+
| id | no | COUNT(*) |
+--------------------------------------+-----+----------+
| c44d1977-0193-a2f1-00f9-57e4c53ba416 | 104 | 3 |
| d474cc6a-e7a1-15d1-d209-57e4c5aa607d | 105 | 3 |
+--------------------------------------+-----+----------+
的已接受答案中提到了,所以我也尝试了这个查询:
However, you need to pick the fields you ACTUALLY WANT then select only those and group by them
但执行此查询后,我没有。请在这里指导我。
答案 0 :(得分:2)
一种方法是使用substring_index()
/ group_concat()
技巧:
SELECT SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY date_entered DESC), ',', 1) as id,
no, COUNT(*)
FROM test
GROUP BY no
HAVING COUNT(*) > 1 ;
这种方法有一些限制。 GROUP_CONCAT()
的默认内部大小为1,024字节。这很容易重新配置,但它假定您没有给定数量的太多ID。
有其他方法,使用变量或更复杂的SQL语句。但是,这是最适合您当前查询的。