如何结合第一和​​第二选择语句

时间:2018-08-22 22:45:53

标签: sql sql-server select group-by

第一个选择语句填充为空,第二个选择语句删除重复项。我该如何从第二条SQL语句中运行第二条SQL语句,或者有没有办法将它们组合在一起?

这是SQL提琴:Fiddle SQL

首选

/*
 Fill in null values with last record that is not null
*/
SELECT Id, FirstName, LastName, 

ISNULL(Address, 
(
SELECT TOP (1) Address    
 FROM test   
 WHERE (FirstName = MainTable.FirstName) AND (Address IS NOT NULL) AND (Address <> '')

ORDER BY Id DESC)

) AS Address

FROM test AS MainTable

第二个选择

 /*
Return a single record
*/
SELECT FirstName, LastName, MAX(Address) 
FROM test
Group By FirstName, LastName

预期结果:

| 4, john, doe, 9Street, SC | 
| 5, marry, doe, 78Street, DC | 

4 个答案:

答案 0 :(得分:1)

据我了解,您想要最新的(按ID)地址和不为空的城市,对吗?如果是这样:

pipelines.py

答案 1 :(得分:1)

如果您使用的是SQL Server 2012版或更高版本,则可以使用last_value()获取有序行的分区(此处为firstnamelastname)中的最后一个值(此处为通过id)。

很遗憾,它不允许忽略NULL。但是,如果我们将感兴趣的列IS NULL排在不感兴趣的行之前,则可以解决此问题。我们可以这样进行:首先对CASE排序,如果列IS NULL返回较低的值,否则返回较高的值。然后,我们ORDER BY的第二列是id

每个firstnamelastname仅获得一行,然后执行简单的DISTINCT,因为addresscity在任何行中都相同共享firstnamelastname

SELECT DISTINCT
       t1.firstname,
       t1.lastname,
       last_value(t1.address) OVER (PARTITION BY t1.firstname,
                                                 t1.lastname
                                    ORDER BY CASE
                                            WHEN t1.address IS NULL THEN
                                              -1
                                            ELSE
                                              1
                                            END,
                                            t1.id
                                    RANGE BETWEEN UNBOUNDED PRECEDING
                                          AND UNBOUNDED FOLLOWING) address,
       last_value(t1.city) OVER (PARTITION BY t1.firstname,
                                              t1.lastname
                                 ORDER BY CASE
                                            WHEN t1.city IS NULL THEN
                                              -1
                                            ELSE
                                              1
                                          END,
                                          t1.id
                                 RANGE BETWEEN UNBOUNDED PRECEDING
                                               AND UNBOUNDED FOLLOWING) city
       FROM test t1;

答案 2 :(得分:0)

如果您使用的是<form class="form-login" action="index.html"> <div class="login-wrap"> <input type="text" class="form-control" id="id" placeholder="User ID" autofocus> <br /> <input type="password" id="pass" class="form-control" placeholder="Password"> <br /> <button class="btn btn-theme btn-block" type="submit"><i class="fa fa-lock"></i> SIGN IN</button> </div> </form>或更高版本,则SQL Server 2012答案似乎是最好的答案。如果您使用的是较低版本,则可以查看以下答案。

sticky bit

答案 3 :(得分:0)

我认为标题是错误的,应该像“如何获取不同列的最新值”一样。

还希望您的表应包含EmpIDUSERID,以便可以在它们上创建索引。

或者如果可以的话,可以对表格进行进一步规范化。

Create table Table1(Empid int pk,FirstName,LastName)

Create table Table2(id int identity(1,1),EMPID FK,Adress,city)

查询这两个表将非常有效。

SELECT DISTINCT t.Firstname
    ,t.Lastname
    ,t1.Address
    ,t2.City
FROM #test t
CROSS APPLY (
    SELECT TOP 1 t1.Address
    FROM #test t1
    WHERE t1.Address IS NOT NULL
        AND t.Firstname = t1.Firstname
        AND t.Lastname = t1.Lastname
    ORDER BY t1.id DESC
    ) t1
CROSS APPLY (
    SELECT TOP 1 t2.City
    FROM #test t2
    WHERE t2.City IS NOT NULL
        AND t.Firstname = t2.Firstname
        AND t.Lastname = t2.Lastname
    ORDER BY t2.id DESC
    ) t2