如何获取SQL Server中不同条件的数据?

时间:2014-01-11 07:02:11

标签: sql-server-2008

ALTER PROCEDURE [dbo].[K_FS_GetFEEDMRPDetails] 
AS
BEGIN
SET NOCOUNT ON;

    Select 
        FT.FeedType,
        round(MR.rateperkg, 3) as rateperkg,
        round(MR.rateper50kg, 3)as rateper50kg 
    from 
        K_FS_FeedMrpDetails MR 
    inner join 
        k_FPS_FeedType FT on FT.sno = MR.feedtype 
    where 
        date = convert(varchar(10), getdate(), 101)
 END

此查询显示今天的插入数据。这是正确的,但我也想,如果今天没有输入任何数据,那么我想显示最后输入的数据。我怎样才能做到这一点?请帮帮我......

1 个答案:

答案 0 :(得分:1)

此代码将选择最新插入的行

 SELECT FT.FeedType,
        Round( MR.rateperkg,3)As rateperkg,
        Round( MR.rateper50kg,3)As rateper50kg 
 FROM   K_FS_FeedMrpDetails MR
        inner join k_FPS_FeedType FT
            On FT.sno=MR.feedtype
 WHERE date in (SELECT TOP 1 date FROM K_FS_FeedMrpDetails ORDER BY date Desc )