我必须沿着同一表的几列的多个值选择几列的不同值。我的桌子就是这样
这样显示此表时,我想选择first_name和last_name列的不同值以及主题块的所有值
是否可以显示名字和姓氏的不同值?
Select DISTINCT `first_name` , `last_name` from table1
答案 0 :(得分:0)
我已经尝试过使用partition by子句的给定方案。请检查下面的示例代码片段。可以。
CREATE TABLE #Student
(
first_name VARCHAR(100),
last_name VARCHAR(100),
[subject] VARCHAR(100)
)
INSERT INTO #Student (first_name, last_name, [subject]) VALUES ('a','b','maths')
INSERT INTO #Student (first_name, last_name, [subject]) VALUES ('a','b','science')
INSERT INTO #Student (first_name, last_name, [subject]) VALUES ('e','f','bio')
INSERT INTO #Student (first_name, last_name, [subject]) VALUES ('e','f','eng')
SELECT CASE WHEN reportTbl.RowNo = 1 THEN first_name ELSE '' END as first_name,
CASE WHEN reportTbl.RowNo = 1 THEN last_name ELSE '' END as last_name,
[Subject] FROM
(
SELECT first_name, last_name, [Subject],
ROW_NUMBER() OVER (PARTITION BY first_name, last_name ORDER BY subject) RowNo
FROM #Student
)
reportTbl
添加RowNo列并按子句分隔后的实际表
以下是查询的最终输出