鉴于下表:
Length | Width | Color | ID
===========================
18 | 18 | blue | 1
---------------------------
12 | 12 | red | 1
---------------------------
Run Code Online (Sandbox Code Playgroud)
我想生成一个列/行:
SIZES
=================
18 x 18, 12 x 12,
Run Code Online (Sandbox Code Playgroud)
我可以在SQL中执行以下操作:
DECLARE @SIZES VARCHAR(8000)
SELECT @SIZES = COALESCE(@SIZES, '') + Convert(varchar(80), [Length]) + ' x ' +
Convert(varchar(80), [Width]) + ', '
FROM table
where ID = 1
GROUP BY [Length], [Width]
ORDER BY [Length], [Width]
SELECT SIZES = @SIZES
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何在LINQ中做到这一点.
我得到的最接近的是:
from t in table …Run Code Online (Sandbox Code Playgroud) 我有一个方法会收到一个string,但在我可以使用它之前,我必须将其转换为int.有时它可以null,我必须改变它的价值"0".今天我有:
public void doSomeWork(string value)
{
int SomeValue = int.Parse(value ?? "0"); //it can throw an exception(i know)
}
Run Code Online (Sandbox Code Playgroud)
我做到了,但我的老板让我重构它:
public void doSomeWork(string value)
{
if(string.IsNullOrEmpty(value))
value = "0";
int SomeValue = int.Parse(value);
}
Run Code Online (Sandbox Code Playgroud)
在您看来,什么是最好的选择?
如何修复存储过程的这一部分?
select将返回1,0或null.如果select返回1或0,我希望@override设置为该值.如果它返回null,那么我希望@override设置为1.
我的语法出了点问题; 我被告知"选择'附近的语法不正确"和"附近的错误sytax")'".
这是sql:
set @override = (coalesce(select override from groupPreferences g inner join
preferences p on g.preferenceId = p.preferenceId where groupId = 13
and description = 'myDescription'), 1))
Run Code Online (Sandbox Code Playgroud) 我有以下一点代码,它到目前为止工作:
SELECT
[id],
COALESCE ([Company], [LastName] + ', ' + [FirstName]) as Customer
FROM [some_database].[dbo].[some_table]
ORDER BY Customer
Run Code Online (Sandbox Code Playgroud)
但是,我想使用DISTINCT关键字来消除重复的"客户"条目.这可能吗?我尝试了几种不同的方法,但无济于事.
我想做的是提供一个值,如果我的数据透视表中不存在另一个值。
数据透视表
SELECT *
FROM MyTable
PIVOT ( MAX(Number) for Total in ([Bob], [Jim], [Carol], [Simon])) as MaxValue
Run Code Online (Sandbox Code Playgroud)
结果
Item | Bob | Jim | Carol | Simon
Item1 3 4 7
Item2 2 9 1
Item3 5
Run Code Online (Sandbox Code Playgroud)
我要在上面的表格上尝试改进的是,如果没有编号,则显示该人是否已分配了项目。
预期结果
Item | Bob | Jim | Carol | Simon
Item1 3 4 X 7
Item2 2 9 1 X
Item3 X X X 5
Run Code Online (Sandbox Code Playgroud)
我有一列(上面已注释掉),如果该人被分配了该项目,则该列具有该人的名字,但是我在想COALESCE,如果用户被分配了该项目,也许可以用它来放置“ X”,否则就什么也没有。尽管我不知道如何执行此操作。也许这是错误的方法。让我知道是否提供一些信息。谢谢!
如何使用coalesce 或case statement在JPA 2中使用CriteriaBuilder
对于许多已启动的记录,它们将为空,因此对于这些记录,employeeName将为null.我想显示System Generated如果employeeName对于那些在数据库表中initiateBy employee为null的项目为null.
我在实体中有以下关系
项目
@Entity
@Table(name = "PROJECT")
public class Project {
@Id
@Column(name = "PROJECTID")
private Long projectId;
....
....
@ManyToOne
@JoinColumn(name = "EMPLOYEENUMBER", referencedColumnName = "EMPLOYEENUMBER")
private Employee empNumber;
@ManyToOne
@JoinColumn(name = "INITIATEDBY", referencedColumnName = "EMPLOYEENUMBER")
private Employee initiatedBy;
Run Code Online (Sandbox Code Playgroud)
雇员
@Entity
@Table(name = "EMPLOYEES")
public class Employee {
@Id
@Column(name = "EMPLOYEENUMBER")
private String employeeNo;
@Column(name = "EMPLOYEENAME")
private String employeeName;
.....
.....
@OneToMany(mappedBy = "empNumber") …Run Code Online (Sandbox Code Playgroud) 我想根据当前行的值确定JOIN列.
例如,我的作业表有4个日期列:offer_date,accepted_date,start_date,reported_date.
我想根据日期检查汇率.我知道reported_date永远不会为null,但这是我的最后一招,所以我有一个优先顺序,可以加入对于exchange_rate表.我不太确定如何使用CASE声明,如果这是正确的方法.
INNER JOIN exchange_rate c1 ON c1.date = {{conditionally pick a column here}}
-- use j.offer_date if not null
-- use j.accepted_date if above is null
-- use j.start_date if above two are null
-- use j.reported_date if above three are null
Run Code Online (Sandbox Code Playgroud) 我想将这个原始 sql 语句转换为 ActiveRecord 语句:
def self.get_new_entry_code(table_id)
sql = "SELECT coalesce(max(code_id) + 1, 1) FROM configentries WHERE configtable_id = " + table_id.to_s
connection.execute(sql)
end
Run Code Online (Sandbox Code Playgroud)
我正在与coalesce(). 我尝试了以下方法:
def self.get_new_entry_code(table_id)
result = Configentry.select("coalesce(max(code_id) + 1, 1").where("configtable_id = " + table_id.to_s).first
result.id
end
Run Code Online (Sandbox Code Playgroud)
但这不起作用,而且也太“原始”。我怎样才能做到这一点?
我现在注意到的是,只有当我输入puts result. 怎么会这样?
错误:
E, [2017-05-10T12:30:04.604953 #93397] ERROR -- : PG::SyntaxError: ERROR: syntax error at or near "FROM"
LINE 1: ...e(max(code_id) + 1, 1) WHERE configtable_id = 106 FROM "conf...
^
: SELECT coalesce(max(code_id) + …Run Code Online (Sandbox Code Playgroud) 我有这个表:
create table series(
serie varchar(10),
season varchar(10),
chapter varchar(10),
primary key ( serie, season, chapter)
);
insert into series values ('serie_1', 'season_1', 'Chap_1'),
('serie_1', 'season_1', 'Chap_2'),
('serie_1', 'season_2', 'Chap_1'),
('serie_2', 'season_1', 'Chap_1'),
('serie_2', 'season_2', 'Chap_1'),
('serie_2', 'season_2', 'Chap_2'),
('serie_3', 'season_1', 'Chap_1'),
('serie_3', 'season_2', 'Chap_1');
create table actua(
idActor varchar(10),
serie varchar(10),
season varchar(10),
chapter varchar(10),
salary numeric(6),
foreign key ( serie, season, chapter) references series,
primary key ( idActor, serie, season, chapter)
);
insert into actua values ('A1', 'serie_1', …Run Code Online (Sandbox Code Playgroud) 我们如何在不指定列名的情况下使用dplyr ( tidyverse ) 为所有列获取第一个非缺失值 -合并- 行方式?
示例数据:
df <- data.frame(x = c(NA, "s3", NA, NA,"s4"),
y = c("s1", NA, "s6", "s7", "s4"),
z = c("s1", NA, NA, "s7", NA))
Run Code Online (Sandbox Code Playgroud)
我们可以使用do.call,但这看起来不太整洁:
df$xyz <- do.call(coalesce, df)
# x y z xyz
# 1 <NA> s1 s1 s1
# 2 s3 <NA> <NA> s3
# 3 <NA> s6 <NA> s6
# 4 <NA> s7 s7 s7
# 5 s4 s4 <NA> s4
Run Code Online (Sandbox Code Playgroud)
这可行,但我不想指定列:
df %>%
mutate(xyz = …Run Code Online (Sandbox Code Playgroud) coalesce ×10
sql ×5
postgresql ×2
activerecord ×1
c# ×1
case ×1
criteria-api ×1
distinct ×1
dplyr ×1
having ×1
hibernate ×1
join ×1
jpa ×1
jpa-2.0 ×1
linq ×1
null ×1
pivot ×1
r ×1
refactoring ×1
rowwise ×1
select ×1
sql-server ×1
variables ×1