我试图从一列获取max + 1值,并从另一列获取所有值。但是,我的查询没有给出任何结果。
例如,
SectionItemID SectionItem
1 blue
2 red
查询应该返回
SectionItemID SectionItem
3 blue
red
继承我拥有的东西
SELECT SectionItem,MAX(SectionItemID) + 1 AS SectionItemID FROM Core.SectionItem_Lkup
答案 0 :(得分:4)
SELECT SectionItem,
(select MAX(SectionItemID)+1 FROM Core.SectionItem_Lkup) AS SectionItemID
FROM Core.SectionItem_Lkup
答案 1 :(得分:0)
每当您GROUP BY
时,您应汇总所涉及的其他列。
的最佳方式是来聚合其他列。对于您的szenario,您可以使用group_concat
SELECT MAX(SectionItemID)+1, Group_concat(SectionItem) FROM tbl
注意:该查询不包含任何Group By
,因为您不想在SectionItemId
或SectionItem
上进行分组。省略Group By
并使用聚合函数将在整个表中使用它们。
输出:
MAX(SECTIONITEMID)+1 GROUP_CONCAT(SECTIONITEM)
3 blue,red
答案 2 :(得分:0)
select case when t2.SectionItem = 'blue'
then cast(max(t1.SectionItemID) + 1 as varchar(1))
else '' end
as SectionItemID
, t2.SectionItem
from Core.SectionItem_Lkup t1
full outer join Core.SectionItem_Lkup t2 on 1 = 1
group by t2.SectionItem
order by
case when t2.SectionItem = 'blue'
then cast(max(t1.SectionItemID) + 1 as varchar(1))
else '' end
desc