我的语法代码

时间:2018-03-19 18:03:21

标签: sql data-analysis

我有这个任务: 一个。连接:生成一个报告,将供应商名称和位置列为“名称(位置)” 湾上面的列表包含两个字段中的尾随空格;修剪空格并显示结果。 C。为数据库中新派生的虚拟列指定名称 d。提醒一下,检索供应商'DLL01'提供的产品数量 即检索每个供应商提供的产品数量,并将结果显示在两列中 'vend_id'和'num_products' 我写了一些代码,但它没有运行。

a. select [vend_name] +'('+[vend_address] + [vend_city]+')' as 'Name (Location)'
from [dbo].[Vendors]

b. select 
Ltrim(Rtrim([vend_name] +'('+[vend_address] + [vend_city]+ ')' )as 'Name (Location)'
from [dbo].[Vendors]

c.select 
Ltrim(Rtrim([vend_name] +'('+[vend_address] + [vend_city]+')')),'   ','  '+ char(7)),
char(7),'') as 'vend information'
from [dbo].[Vendors]

d.select [vend_name] +'('+[vend_address] + [vend_city]+')' as 'Name (Location)'
from [dbo].[Vendors]

e. select [vend_id]='DLL01', count([vend_id]) as num products
from [dbo].[Vendors]
where [vend_id]='DLL01'

我认为这只是语法问题。

你能帮助我吗?

感谢!!!

2 个答案:

答案 0 :(得分:0)

对于b你需要关闭你的Ltrim括号 - 你只关闭了rtrim一个:

   select Ltrim(Rtrim([vend_name] +'('+[vend_address] + [vend_city]+ ')' ))as 'Name (Location)'
    from [dbo].[Vendors]

答案 1 :(得分:0)

一个。看起来很好。在(

之前添加了空格
select [vend_name] +' ('+[vend_address] + [vend_city]+')' as 'Name (Location)'
from [dbo].[Vendors]

湾添加了遗失的)

select Ltrim(Rtrim([vend_name] +' ('+[vend_address] + [vend_city]+ ')')) as 'Name (Location)'
from [dbo].[Vendors]

℃。避免使用包含(空格)的列名。

select Ltrim(Rtrim([vend_name] +' ('+[vend_address]+[vend_city]+')')) as 'vendor_information'
from [dbo].[Vendors]

d。添加了WHERE子句。

select [vend_name] +' ('+[vend_address] + [vend_city]+')' as 'Name (Location)'
from [dbo].[Vendors]
WHERE [vend_id]='DLL01'

即添加了GROUP BY以获取计数并将计数更改为*

select [vend_id], count(*) as num_products
from [dbo].[Vendors]
GROUP BY [vend_id]

我不确定期望是什么,但你应该在所有列上使用LTRIM(RTRIM(x)),因为空格可以在任何列中。