Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-如果为空,则使用默认值选择_Sql_Sql Server - Fatal编程技术网

SQL-如果为空,则使用默认值选择

SQL-如果为空,则使用默认值选择,sql,sql-server,Sql,Sql Server,我有这样一个问题: SELECT id, name, price, floorplan FROM Inventory ORDER BY price 这将从myInventory表中返回按价格排序的id、名称、价格、楼层平面图。通过这个查询,我返回了3行,最后一行有一个floorplan值,另外两行为null。是否可以使用非空floorplan替换空floorplan列?我不想对这些进行分组,因为我需要返回3行 SELECT id, name, price, (SELECT

我有这样一个问题:

SELECT id, name, price, floorplan 
FROM Inventory 
ORDER BY price
这将从my
Inventory
表中返回按价格排序的id、名称、价格、楼层平面图。通过这个查询,我返回了3行,最后一行有一个floorplan值,另外两行为null。是否可以使用非空floorplan替换空floorplan列?我不想对这些进行分组,因为我需要返回3行

SELECT id, name, price, 
       (SELECT TOP 1 floorplan FROM Inventory WHERE floorplan IS NOT NULL) as [floorplan]
  FROM Inventory

这应该适用于您的3行,但如果有更多记录,我会将其放在前1行。您需要指定要查看的楼层平面,如果多于1个楼层平面不为空

您可以使用窗口函数聚合,如
max()over()

rextester演示:

使用此测试设置:

create table inventory (id int, name varchar(32), price decimal(9,2), floorplan int)
insert into inventory values 
 (1,'one',1.01,null)
,(2,'two',2.02,null)
,(3,'three',3.03,1024)
返回:

+----+-------+-------+-----------+
| id | name  | price | floorplan |
+----+-------+-------+-----------+
|  1 | one   | 1.01  |      1024 |
|  2 | two   | 2.02  |      1024 |
|  3 | three | 3.03  |      1024 |
+----+-------+-------+-----------+

考虑这个例子。我认为子查询应该是相关的。如果没有,烘干机将得到平面布置图,但数据中没有平面布置图。谢谢

    CREATE TABLE Inventory 
    (id int 
    ,name varchar(30)
    ,price money
    ,floorplan char(1)
    )

    insert inventory 
    SELECT 1, 'Washer', 300, NULL
    insert inventory 
    SELECT 1, 'Washer', 330, NULL
    insert inventory 
    SELECT 1, 'Washer', 340, 'Y'
    insert inventory 
    SELECT 2, 'Dryer', 275,  NULL

    SELECT id, name, price, 
        (SELECT TOP 1 floorplan FROM Inventory AS Y WHERE floorplan IS NOT NULL
        AND Y.id = I.id) as [floorplan]
    FROM Inventory AS I

找到楼层平面的最大值。使用isnull函数,在floorplan为null时,将其替换为其最大值

  SELECT id, 
         name, 
         price, 
         isnull(floorplan, (select max(floorplan) from Inventory) )
    FROM Inventory 
ORDER BY price

哇,你给了我很多好处。不过有一个建议,对于sql问题的预期结果和当前结果,再加上发布你尝试过的内容会有所帮助。这不需要访问表twiceI,我认为这与其他答案相同,用户只提到了三行,所以我相信在sqlfiddle和Dryer中不需要correlationPaste的第一个查询就有了平面图“Y”。这是正确的行为吗?在没有orderby的情况下使用TOP意味着您不关心返回哪些行。每个查询@user979331的结果可能会有所不同。有些事情需要非常谨慎,这是错误的。没有订单并不意味着你每次都会得到不同的结果。如果没有添加数据,每次都会得到相同的结果。默认情况下,它是按集群键排序的,在这种情况下,如果没有添加任何数据,则集群键应该是最重要的,这会破坏事务数据库(如SQL Server)的用途。这是一个大胆的假设。不管怎样,你。您选择了最糟糕的方法。我阅读了您的答案,但这并没有改变我的评论。此处不需要isnull,如果有1行以上的值不是NULL,则此逻辑将失败。然后,它将带回额外的行。更明智的做法是使用max()并删除条件检查。@我只是想理解你的评论。。您的意思是它将有一个错误“子查询返回多行”?但是子查询中有一个max函数,所以我们每次都不会得到超过一行。我不清楚。您将不会收到错误。我只是暗示您的逻辑是将所有空值替换为该表的最大值。OP没有指定是否会有更多非空值的行,但是如果有,那么我喜欢您的方法,因为它保持了前几行的完整性。Hoever,根据示例,您只需要@SqlZim所建议的内容。
  SELECT id, 
         name, 
         price, 
         isnull(floorplan, (select max(floorplan) from Inventory) )
    FROM Inventory 
ORDER BY price