问题: 我通过在不同的表上应用多个连接来从sql server填充asp.net报告。它可以工作,但是在一个问题的地方,我会通过单元格no的帮助选择PoliceStation名称。问题是1个小区没有可以是许多警察局,所以它挑选每个警察的名字并在报告中显示它。我只想要一个警察局名称,我需要从PoliceStations表
查询:
SELECT [ID]
,LEFT(REPLACE(convert(varchar, CmsSMSDb.dbo.SendMessages.SendingDateTime, 113), ' ','/'), 11) + ' ' +
RIGHT(REPLACE(convert(varchar, CmsSMSDb.dbo.SendMessages.SendingDateTime, 113), ' ','/'), 12)
as SendingDateTime
,[ToMobileNo]
,[Message]
,Designations.Name as [ToDesignation]
FROM [CmsSMSDb].[dbo].[SendMessages]
inner join
CPOCMS.dbo.Designations
ON CPOCMS.dbo.Designations.MobileNo = CmsSMSDb.dbo.SendMessages.ToMobileNo
where Convert(date,CmsSMSDb.dbo.SendMessages.SendingDateTime)>= @DateFrom
AND
Convert(date,CmsSMSDb.dbo.SendMessages.SendingDateTime)<= @DateTo
Union All
SELECT [ID]
,LEFT(REPLACE(convert(varchar, CmsSMSDb.dbo.SendMessages.SendingDateTime, 113), ' ','/'), 11) + ' ' +
RIGHT(REPLACE(convert(varchar, CmsSMSDb.dbo.SendMessages.SendingDateTime, 113), ' ','/'), 12)
as SendingDateTime
,[ToMobileNo]
,[Message]
,'SDpo'+' '+CPOCMS.dbo.PoliceStations.PsName as [ToDesignation]
From [CmsSMSDb].[dbo].[SendMessages]
inner join CPOCMS.dbo.PoliceStations
ON CPOCMS.dbo.PoliceStations.sDpo_ContactNo = SendMessages.ToMobileNo
where Convert(date,CmsSMSDb.dbo.SendMessages.SendingDateTime)>= @DateFrom
AND
Convert(date,CmsSMSDb.dbo.SendMessages.SendingDateTime)<= @DateTo
order by SendMessages.ID
数据库结构:
答案 0 :(得分:0)
只需使用这样的TOP子句。
SELECT TOP 1 ColumnA,
...,
...
...
..