1

我在存储发票数据的数据库中有一个表。我希望轻松查看有关某项操作何时发生的每日统计信息。当执行该操作时,一条注释会添加到该表中名为“notes”的列中,格式如下:“ORDER FILLED -G J- Apr 20 2013”​​。“G J”是执行我要跟踪的操作的人的首字母。在该注释之前和之后可以有其他注释。不幸的是,此操作仅记录在数据库中的这一位置,我无法控制数据的记录方式。我希望创建一份关于每个用户昨天执行此操作的次数的每日报告。下面的查询完美地工作,但是它将每个用户的结果作为单独的结果返回,我认为它们都在一个网格中。我不是程序员或 SQL 专家,我只是想一起解决一些可行的问题。有没有一种简单的方法来修改下面的查询,以便我可以有一个包含两列的表...第 1 列应该是“用户”,第 2 列应该是“订单填充”我想为每个员工设置一行,如何他们多次执行该动作,最后一排总计。

declare @date VARCHAR(12)


SET @date = (SELECT CONVERT(VARCHAR(12), GETDATE()-1, 100))


select count(invoice_id) as Employee1 from invoice 
where notes like '%ORDER FILLED -G J- '+@date+'%'

select count(invoice_id) as Employee2 from invoice 
where notes like '%ORDER FILLED -S L- '+@date+'%'

select count(invoice_id) as Employee3 from invoice 
where notes like '%ORDER FILLED -E S- '+@date+'%'

select count(invoice_id) as Employee4 from invoice 
where notes like '%ORDER FILLED -R A- '+@date+'%'

select count(invoice_id) as Total from invoice 
where notes like '%ORDER FILLED -% %- '+@date+'%'
4

1 回答 1

0

像这样的东西可能会起作用

select substring(notes, 13, 4) filledby -- should be -G J- if I counted right
, count(*) records
from invoice
where notes like '% ' + @date + '%'
group by substring(notes, 13, 4)

请注意,不同的 RDBMS 对子字符串有不同的语法,并且您没有指定您的语法。

于 2013-04-20T16:21:49.417 回答