如何在 T-SQL 中获取以动态日期作为列的报告

发布于 2024-10-11 07:28:24 字数 611 浏览 2 评论 0原文

如何获取以动态日期作为 T-SQL 列的报告。

我疯狂地寻找这样的一段代码。

希望它能帮助其他人。

如果有人知道更好的解决方案,请发布。

DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + convert(nvarchar(12), data, 103) + ']',
'[' + convert(nvarchar(12), data, 103)+ ']')
FROM Item
GROUP BY data
--print @columns


DECLARE @query VARCHAR(8000)

SET @query = '

;WITH G1 As
(
select id_item, convert(nvarchar(12), data, 103) as data, COUNT(*) as numar
from Item
group by id_item, data

)

SELECT *
FROM G1
PIVOT
(
sum(numar)
FOR [data]
IN (' + @columns + ')
) AS G2
'
print @query
EXECUTE(@query)

How to get a report with dynamic dates as columns in T-SQL.

I was looking for a piece of code like this like crazy.

Hope it will help other people.

If anyone knows any better solution please post.

DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + convert(nvarchar(12), data, 103) + ']',
'[' + convert(nvarchar(12), data, 103)+ ']')
FROM Item
GROUP BY data
--print @columns


DECLARE @query VARCHAR(8000)

SET @query = '

;WITH G1 As
(
select id_item, convert(nvarchar(12), data, 103) as data, COUNT(*) as numar
from Item
group by id_item, data

)

SELECT *
FROM G1
PIVOT
(
sum(numar)
FOR [data]
IN (' + @columns + ')
) AS G2
'
print @query
EXECUTE(@query)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉薄对峙 2024-10-18 07:28:24

关于您问题中的示例的一些评论。

使用 QUOTENAME 而不是自己连接方括号。这将正确处理数据中的任何 ] 字符。

您似乎正在混合 nvarcharvarchar。将动态 SQL 变量声明为 NVARCHAR(MAX)(如果面向 SQL Server 2000,则为 NVARCHAR(4000))而不是 VARCHAR(8000)。这意味着您的查询可以正确处理 Unicode 数据,并且在连接动态 SQL 时通常是更安全的做法。

(只是添加一个例子来说明为什么我说这更安全)

create table #t (a int, b char(1));

DECLARE @querystring varchar(max)
DECLARE @nquerystring nvarchar(max)
DECLARE @userinput nvarchar(100) = N'ʼ;Drop Table #t--'

IF @userinput LIKE '%''%'
        BEGIN
        RAISERROR('Possible SQL injection',16,1)
        RETURN
        END


SET @nquerystring = 'select * from #t where b = ''' + @userinput + ''''


exec (@nquerystring)

select OBJECT_ID('tempdb..#t') /*NOT NULL, Still There*/

SET @querystring = @nquerystring /*ʼ nchar(700) gets silently converted to a regular ' character*/

exec (@querystring)

select OBJECT_ID('tempdb..#t') /*NULL, Table Dropped*/

A couple of comments on the example in your question.

Use QUOTENAME rather than concatenating the square brackets yourself. This will deal correctly with any ] characters in the data.

You seem to be mixing nvarchar and varchar. Declare the dynamic SQL variables as NVARCHAR(MAX) (or NVARCHAR(4000) if targeting SQL Server 2000) not VARCHAR(8000). This will mean that your query works correctly with Unicode data and is a generally more secure practice when concatenating dynamic SQL.

(Just to add an example of why I say this is more secure)

create table #t (a int, b char(1));

DECLARE @querystring varchar(max)
DECLARE @nquerystring nvarchar(max)
DECLARE @userinput nvarchar(100) = N'ʼ;Drop Table #t--'

IF @userinput LIKE '%''%'
        BEGIN
        RAISERROR('Possible SQL injection',16,1)
        RETURN
        END


SET @nquerystring = 'select * from #t where b = ''' + @userinput + ''''


exec (@nquerystring)

select OBJECT_ID('tempdb..#t') /*NOT NULL, Still There*/

SET @querystring = @nquerystring /*ʼ nchar(700) gets silently converted to a regular ' character*/

exec (@querystring)

select OBJECT_ID('tempdb..#t') /*NULL, Table Dropped*/
不一样的天空 2024-10-18 07:28:24

这是一个静态版本(来自同事):

with c as (
    select
        p.id_doc,
        p.id_usr,
        DATEDIFF(DAY, p.created_at, getdate()) as date_diff
    from Document p
    where 1 = 1 
), p as (
    select
        pvt.id_usr,
        [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]
    from c
    PIVOT (COUNT(c.id_doc) FOR c.date_diff IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9])) AS pvt
)
select
    u.username,
    u.name,
    [0], [1], [2], [3], [4], [5], [6], [7], [8], [9],
    y.nr,
    y.total_money
from p
inner join Users u on u.id_usr = p.id_usr
cross apply (
    select
        COUNT(1) as nr,
        SUM(premium) as total_money
    from Document z
    where z.id_usr = p.id_usr
) as y
order by nr desc

Here is a static version (from a coleague):

with c as (
    select
        p.id_doc,
        p.id_usr,
        DATEDIFF(DAY, p.created_at, getdate()) as date_diff
    from Document p
    where 1 = 1 
), p as (
    select
        pvt.id_usr,
        [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]
    from c
    PIVOT (COUNT(c.id_doc) FOR c.date_diff IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9])) AS pvt
)
select
    u.username,
    u.name,
    [0], [1], [2], [3], [4], [5], [6], [7], [8], [9],
    y.nr,
    y.total_money
from p
inner join Users u on u.id_usr = p.id_usr
cross apply (
    select
        COUNT(1) as nr,
        SUM(premium) as total_money
    from Document z
    where z.id_usr = p.id_usr
) as y
order by nr desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文