显示序列号根据表中的数据数量显示月份

时间:2018-05-05 08:06:04

标签: sql sql-server

我想以 MON / Serial_No 格式显示优惠券号码。 我有保存在表格中的凭证日期,我想从凭证日期显示月份,并使用序列号显示 优惠券无格式 Voucher no

例如: 如果JAN月有5条记录,则显示JAN / 1,JAN / 2,JAN / 3,JAN / 4,JAN / 5 如果FEB月有3条记录,则显示FEB / 1,FEB / 2,FEB / 3

这是我的代码/查询

SELECT
UPPER(LEFT(DATENAME(MONTH, Cust_Payment.Date_Of_Payment), 3)) as 
'Voucher_No',
Cust_Payment.Date_Of_Payment as 'Voucher_Date'
from Customer_Payments Cust_Payment

查询输出: output

请帮忙。 提前谢谢。

2 个答案:

答案 0 :(得分:3)

试试这个

SELECT
     (
      UPPER(LEFT(DATENAME(MONTH, Cust_Payment.Date_Of_Payment), 3)) +
      '/' +
      CONVERT(varchar(10),row_number() over (partition by MONTH(Cust_Payment.Date_Of_Payment) order by Cust_Payment.Date_Of_Payment))
     )as 'Voucher_No',
     Cust_Payment.Date_Of_Payment as 'Voucher_Date'
from Customer_Payments Cust_Payment

答案 1 :(得分:1)

试试这个

(UPPER(LEFT(DATENAME(MONTH, Cust_Payment.Date_Of_Payment), 3)) +'/' +
 CONVERT(varchar(10),row_number() over (partition by MONTH(Cust_Payment.Date_Of_Payment) order by Cust_Payment.Date_Of_Payment)))as 'Voucher_No2',