SQL查询中的列名称

时间:2015-05-07 03:54:30

标签: sql sql-server tsql

我有这个问题:

select 
(select GETDATE()) as Date,
(select ROUND(sum(QuantityInStore * AveragePrice),2) as "Photo" 
from 
      inventoryinfo, 
      InventoryStoreInfo 
WHERE 
      InventoryCategoryID IN 
      ('3','6','19','22','23','40','32','56','52','41'))

结果很好,但列名不适用于“Photo”列。日期确实显示。照片读取(无列名称)。

6 个答案:

答案 0 :(得分:3)

试试这个:

select 
(select GETDATE()) as Date,
(select 
    ROUND(sum(QuantityInStore * AveragePrice),2)
from 
      inventoryinfo, 
      InventoryStoreInfo 
WHERE 
      InventoryCategoryID IN 
      ('3','6','19','22','23','40','32','56','52','41')
 ) as Photo

答案 1 :(得分:2)

尝试此查询

select  GETDATE() AS [Date]
        ,ROUND(sum(QuantityInStore * AveragePrice),2) as [Photo]
        ,ROUND(sum(QuantityInStore * AveragePrice),2) as COG_Facilities -- Add other outputs here
from 
      inventoryinfo, 
      InventoryStoreInfo 
WHERE 
      InventoryCategoryID IN 
      ('3','6','19','22','23','40','32','56','52','41')

答案 2 :(得分:0)

看看你用照片的双引号(“”)。不需要双引号(“”)只需使用Photo

您的代码可能喜欢

select 
(select GETDATE()) as Date,
(select ROUND(sum(QuantityInStore * AveragePrice),2) as Photo
from inventoryinfo, InventoryStoreInfo
WHERE InventoryCategoryID IN('3','6','19','22','23','40','32','56','52','41'))

答案 3 :(得分:0)

你失去了照片"列名,因为您使用的是子表,而名称只是可见的内部选择范围。

ROUND(sum(QuantityInStore * AveragePrice),2) as "Photo"嵌套在(select ...)内,因此外部选择无法看到"照片"。只有总和子表格的名称为“#34; Photo"”,而不是整个Round子表。

只需将名称添加到您想要命名的整个子表中"照片":

select 
    (select GETDATE()) as Date,
    (select (ROUND(sum(QuantityInStore * AveragePrice), 2) as Photo
            from inventoryinfo, InventoryStoreInfo
            WHERE InventoryCategoryID
            IN ('3','6','19','22','23','40','32','56','52','41')
        ) as photo ) 

你真正的问题是你无缘无故地创建子表。删除额外的选择,跟踪每个标签所指的内容会更容易:

select 
    GETDATE() as Date,
    ROUND(sum(QuantityInStore * AveragePrice), 2) as Photo
            from inventoryinfo, InventoryStoreInfo
            WHERE InventoryCategoryID
            IN ('3','6','19','22','23','40','32','56','52','41')

答案 4 :(得分:0)

尝试这种方式......这是测试代码,适用于sql server 2008

select 
(select GETDATE()) as 'Date',
(select b.tp) as 'Price'
from 
      ProductInfo a, 
      product_detail b

      where b.tp in (2,3,4 )

答案 5 :(得分:0)

看起来你已将别名放在错误的位置,查看你已经开始的大括号以及它将在哪里完成。

select 
(select GETDATE()) as Date,
(select ROUND(sum(QuantityInStore * AveragePrice),2)
from 
      inventoryinfo, InventoryStoreInfo 
WHERE 
      InventoryCategoryID IN 
      ('3','6','19','22','23','40','32','56','52','41')) as Photo