Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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,我正在使用mssql,在使用子查询时遇到问题。真正的查询相当复杂,但其结构与此相同: select customerName, customerId, ( select count(*) from Purchases where Purchases.customerId=customerData.customerId ) as numberTransactions from customerData 我想做的是按照事务数对表进行排序,但是当我使用

我正在使用mssql,在使用子查询时遇到问题。真正的查询相当复杂,但其结构与此相同:

select 
  customerName, 
  customerId,
  (
    select count(*) 
    from Purchases 
    where Purchases.customerId=customerData.customerId
  ) as numberTransactions
from customerData
我想做的是按照事务数对表进行排序,但是当我使用

order by numberTransactions

它告诉我没有这样的领域。有可能这样做吗?我是否应该使用某种特殊关键字,如
this
,或
self

使用字段号,在这种情况下:

order by 3

做一个内部连接。它更容易阅读


或者,如果这不好,您可以使用临时表(#T1等)构造查询。

您需要复制您的逻辑。SQL Server对于已命名但不是FROM语句中数据集一部分的列不是很智能

所以使用

select 
  customerName, 
  customerId,
  (
    select count(*) 
    from Purchases p
    where p.customerId = c.customerId
  ) as numberTransactions
from customerData c
order by (select count(*) from purchases p where p.customerID = c.customerid)

此外,使用别名,可以使代码更易于阅读和维护。;)

使用
groupby
JOIN
也可以实现同样的效果,这样您就不用再使用子查询了。这也可能更快。

我认为您可以在SQL2005中完成,但不能在SQL2000中完成

有时您必须与SQL的语法(预期的子句范围)搏斗

此外,使用JOIN的解决方案也是正确的。查看查询计划,SQL Server应该为这两种解决方案提供相同的计划。

有更好的方法来获得结果,但仅从您的示例查询中,这将适用于SQL2000或更高版本。

如果您将您的别名用一个记号'numberTransactions'括起来,然后通过'numberTransactions'调用订单

select
  customerName, 
  customerId,
  (
    select count(*) 
    from Purchases 
    where Purchases.customerId=customerData.customerId
  ) as 'numberTransactions'
from customerData
ORDER BY 'numberTransactions'

在本例中,innerjoin更容易实现。但是我正在处理的实际查询已经使用了一个与另一个表的内部联接,因此它实际上必须是一个子查询。如果您的查询将来发生更改,这是一种非常糟糕的方法。我同意JPunyon的观点,对于你的选择,几乎总是明确的,而不是含蓄的。它确实降低了可读性,并且在您/其他人稍后阅读代码时可能会造成混乱。这并不理想,但似乎没有其他方法可以做到这一点,而无需复制逻辑、复制代码或使用join。我相信Nathan B提出的解决方案非常有效。不需要复制逻辑、复制代码或使用连接:)我同意。这不是最好的解决方案,只是写得最快。
select 
  customerName, 
  customerId,
  (
    select count(*) 
    from Purchases p
    where p.customerId = c.customerId
  ) as numberTransactions
from customerData c
order by (select count(*) from purchases p where p.customerID = c.customerid)
SELECT *
FROM
(
select
  customerName,
  customerId,
  (
    select count(*)
    from Purchases
    where Purchases.customerId=customerData.customerId
  ) as numberTransactions
from customerData
) as sub
order by sub.numberTransactions
select
  customerName, 
  customerId,
  (
    select count(*) 
    from Purchases 
    where Purchases.customerId=customerData.customerId
  ) as 'numberTransactions'
from customerData
ORDER BY 'numberTransactions'