2

再会。

我知道这个问题很受欢迎,但我没有看到对我的帮助......

结构数据库:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[ExPRoG_Statistic_banners](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [user_id] [varchar](50) NULL,
    [banner_idn] [varchar](50) NULL,
    [banner_page] [varchar](150) NULL,
    [action] [varchar](50) NULL,
    [datetime] [datetime] NULL,
    [server_referer] [varchar](150) NULL,
    [ip] [varchar](50) NULL,
    [browser] [varchar](500) NULL,
    [id_firm] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

SQL:

SELECT *
FROM (
    SELECT 
          id
        , [user_id]
        , [day] = DAY(datetime)
    FROM Table
    WHERE banner_idn='50927853' AND datetime BETWEEN '2013-08-08 00:00:000' AND '2013-08-22 00:00:000'
) src
PIVOT 
(
    SUM(cast(id as bigint)) FOR [day] IN (
        [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
        [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
        [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) pvt

我选择,但我有错误:

Arithmetic overflow error converting expression to data type int.

类型输入 id - int。

对于删除错误我想使用 cast() - cast(id as bigint),但现在我得到错误Incorrect syntax near '('. for line cast(id as bigint)

请告诉我哪里出错了?

请告诉我如何消除错误?

4

1 回答 1

1

试试这个——

SELECT *
FROM (
    SELECT 
          id
        , [user_id]
        , [day] = DAY([datetime])
    FROM dbo.[Table]
    WHERE banner_idn = '50927853' 
        AND [datetime] BETWEEN '20130808' AND '20130822'
) src
PIVOT 
(
    COUNT(id) FOR [day] IN (
        [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
        [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
        [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) pvt
于 2013-08-13T05:49:53.043 回答