在SQL中删除一列

时间:2012-11-15 13:54:09

标签: sql sql-server tsql sql-server-2012

SELECT VendorName, COUNT(*) as Total_Invoices
FROM   Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE  Vendors.VendorName = 'IBM'
GROUP BY VendorName
ORDER BY Total_Invoices DESC

会出现

VendorName  Total_Invoices
IBM                2

我想这样做只是说

Total_Invoices
     2

未显示IBM VendorName,但仍在计算IBM的总发票

3 个答案:

答案 0 :(得分:4)

您可以安全地删除此案例中的VendorName

SELECT COUNT(*) as Total_Invoices
FROM   Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE  Vendors.VendorName = 'IBM'
GROUP BY VendorName
ORDER BY Total_Invoices DESC
另一方面,

select也称为投影。你可以安全地删除它而不会造成任何伤害。但是在GROUP BY子句中删除它是一个非常不同的事情。

答案 1 :(得分:3)

您只需清除VendorName中的Select即可。 在这种情况下,您无需使用GROUP BYORDER BY

SELECT COUNT(*) as Total_Invoices
FROM   Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE  Vendors.VendorName = 'IBM'

答案 2 :(得分:1)

从SELECT中删除VendorName。