将两列组合成一列并添加文本

时间:2012-11-15 14:17:46

标签: sql tsql sql-server-2012

我的目标是(对于所有要计算的列)

The Number of Invoices Per Date
1 There were a total of 1 invoice(s) on Apr 02, 2012
2 There were a total of 5 invoice(s) on Apr 01, 2012

InvoiceDate表格需要格式化,但我似乎无法完成CAST IT当天的多个发票日期必须以某种方式计算:S

SELECT convert(varchar, getdate(), 107) AS InvoiceDate FROM Invoices

以上就我所知,但它一遍又一遍地重复相同的日期

2 个答案:

答案 0 :(得分:2)

听起来你想要这个:

select 
  'There were a total of '
  + cast(count(*) as varchar(10)) 
  + ' invoice(s) on '
  + convert(varchar(12), yourDateCol, 107)
from invoices
group by convert(varchar(12), yourDateCol, 107)

答案 1 :(得分:1)

您只需将日期分组:

SELECT COUNT(*) AS Quantity, convert(varchar,YourDateFieldName, 107)
FROM Invoices
GROUP BY convert(varchar,YourDateFieldName, 107)
ORDER BT YourDateFieldName

请注意,在您的示例中,GETDATE()(又名CURRENT_TIMESTAMP)将始终返回当前时间戳。