添加基于日期[借入日期]给出否或是的列

时间:2019-11-19 07:52:14

标签: sql sql-server

  1. 基本上我想要的是将出现在[借用日期]旁边的列

  2. 此列将称为“状态”,它将有两个值

  3. 这些值取决于以下条件:如果今天的日期比[借用的日期]大3天,则“状态”列=过期,否则它应显示“未过期”

这是我的代码。

SELECT dbo.borrowing.[book_id], 
       dbo.bookregistration.[book_title], 
       dbo.bookregistration.[book_category], 
       dbo.bookregistration.[book_type], 
       dbo.bookregistration.edition, 
       dbo.borrowing.[borrowed_date], 
       dbo.borrowing.[adm_no] 
FROM   dbo.bookregistration 
INNER JOIN dbo.borrowing ON dbo.bookregistration.[book_id] = dbo.borrowing.[book_id] 

5 个答案:

答案 0 :(得分:1)

这是您的查询。 首先,我们将使用my_datframe.to_csv('file_name.csv') 使其更短,第二次使用alias

这是您的完整查询。

select case

答案 1 :(得分:0)

您可以使用DATE_ADD和CASE语句满足以下要求-

String

答案 2 :(得分:0)

SELECT id
    ,date
    ,first_value(id) over (partition by id order by date) = date as first
FROM table_name;

答案 3 :(得分:0)

尝试一下:

 SELECT
      B.[Book_ID]
      , BR.[Book_Title]
      , BR.[Book_Category]
      , BR.[Book_Type]
      , BR.Edition
      , CASE WHEN DATE_ADD([B.Borrowed_Date], INTERVAL 3 DAY) > GetDate() THEN 'Not Overdue' ELSE 'Overdue' END Status
      , BR.[Adm_NO]   
 FROM dbo.BookRegistration BR
 INNER JOIN dbo.Borrowing B ON BR.[Book_ID] = B.[Book_ID]

答案 4 :(得分:0)

我们在这里:

SELECT dbo.Borrowing.[Book_ID], dbo.BookRegistration.[Book_Title]
    , dbo.BookRegistration.[Book_Category], dbo.BookRegistration.[Book_Type]
    , dbo.BookRegistration.Edition, dbo.Borrowing.[Borrowed_Date] AS [Borrowed_Date]
    , case when DATEADD(DD,3,[Borrowed_Date]) > GETDATE() THEN 'Not Overdue' ELSE 'Overdue' END AS Status
    , dbo.Borrowing.[Adm_NO]
FROM dbo.BookRegistration
INNER JOIN dbo.Borrowing ON dbo.BookRegistration.[Book_ID] = dbo.Borrowing.[Book_ID]