交叉表查询

发布于 2024-08-27 06:47:56 字数 441 浏览 6 评论 0原文

我有一个包含以下列的项目表

ProjectID, ProjectDescription

和一个包含以下列的列表数据表。

ProjectID ListType Date Memo

每个项目在 ListType 表中都有许多列表类型条目。

我想要做的是运行一个查询,该查询将返回类似这样的内容

ProjectID, ProjectDescription, ListType1, ListType1.date, Listtype1.Memo, ListType2, ListType2.date, ListType.Memo

再次强调,每个项目都连接到许多列表类型数据。 我使用的是 Microsoft SQL 2000。因此 Pivot 关键字不起作用

I have a project table with the following columns

ProjectID, ProjectDescription

and a list data table which has the following columns

ProjectID ListType Date Memo

Every project has many list type entries in the ListType Table.

What I want to do is run a query which will return something like this

ProjectID, ProjectDescription, ListType1, ListType1.date, Listtype1.Memo, ListType2, ListType2.date, ListType.Memo

Againg, Every Project is coonected to a number of list type data.
I am using Microsoft SQL 2000. so the Pivot keyword doesnot work

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

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

发布评论

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

评论(1

迷乱花海 2024-09-03 06:47:57

您需要做的是创建动态 SQL 并为每个 ListType 构建附加的 CASE 语句。我必须为工作做类似的事情,他们需要有关事件的某些信息。事件将作为逗号分隔列表传递,然后构建 case 语句。

我有一个存储过程,它从逗号分隔的列表创建一个表

CREATE PROCEDURE [dbo].[ListToTable]
    @vcList     VARCHAR(8000),
    @vcDelimiter    VARCHAR(8000),
    @TableName      SYSNAME,
    @ColumnName SYSNAME
AS
    SET NOCOUNT ON

    DECLARE @iPosStart  INT,
        @iPosEnd    INT,
        @iLenDelim  INT,
        @iExit      INT,
        @vcStr      varchar(8000),
        @vcSql      varchar(8000)

    SET @iPosStart = 1
    SET @iPosEnd = 1
    SET @iLenDelim = LEN(@vcDelimiter)

    SET @vcSql = 'INSERT ' + @TableName + ' (' + @ColumnName + ') VALUES ('''

    SET @iExit = 0

    WHILE @iExit = 0
    BEGIN
        SET @iPosEnd = CHARINDEX(@vcDelimiter, @vcList, @iPosStart)

        IF @iPosEnd <= 0
        BEGIN
            SET @iPosEnd = LEN(@vcList) + 1
            SET @iExit = 1
        END

        SET @vcStr = SUBSTRING(@vcList, @iPosStart, @iPosEnd - @iPosStart)

        EXEC(@vcSql + @vcStr + ''')')

        SET @iPosStart = @iPosEnd + @iLenDelim
    END

    RETURN 0

然后这是动态 SQL

DECLARE @Events VARCHAR(8000) = 'Event1,Event2,... Eventn'

CREATE TABLE #Events
    (
     EventName VARCHAR(MAX)
    )

EXEC [ListToTable] 
    @Events
   ,','
   ,'#Events'
   ,'EventName'

DECLARE @strCASE VARCHAR(MAX)
SET @strCase = ''

DECLARE Events_Cursor CURSOR 
FOR
SELECT EventName FROM [#Events] 

OPEN Events_Cursor
DECLARE @EventName VARCHAR(MAX)

FETCH NEXT FROM Events_Cursor INTO @EventName
WHILE ( @@FETCH_STATUS <> -1 ) 
    BEGIN
        IF ( @@FETCH_STATUS <> -2 ) 
            SET @strCase = @strCase + ',CASE WHEN [EventOpportunityProduct].[Name] = ''' + @EventName + ''' THEN EventOppertunityLineItem.[Trial_Status__c] ELSE NULL END [' + @EventName + ']'
        FETCH NEXT FROM Events_Cursor INTO @EventName
    END
CLOSE Events_Cursor
DEALLOCATE Events_Cursor

然后将 @strCASE 添加到您的 SELECT 语句中。

DECLARE @strSQL VARCHAR(MAX)
SET @strSQL = 'SELECT
    Field1
    ,Field2
    ' + @strCASE + '
   FROM Table'

EXEC (@strSQL)

What you will have to do is create dynamic SQL and build additional CASE statements for every ListType. I had to do something similar for work where they want certain information about events. The events would be passed as a comma separated list and then I build the case statements.

I have a stored proc that creates a table from a comma separated list

CREATE PROCEDURE [dbo].[ListToTable]
    @vcList     VARCHAR(8000),
    @vcDelimiter    VARCHAR(8000),
    @TableName      SYSNAME,
    @ColumnName SYSNAME
AS
    SET NOCOUNT ON

    DECLARE @iPosStart  INT,
        @iPosEnd    INT,
        @iLenDelim  INT,
        @iExit      INT,
        @vcStr      varchar(8000),
        @vcSql      varchar(8000)

    SET @iPosStart = 1
    SET @iPosEnd = 1
    SET @iLenDelim = LEN(@vcDelimiter)

    SET @vcSql = 'INSERT ' + @TableName + ' (' + @ColumnName + ') VALUES ('''

    SET @iExit = 0

    WHILE @iExit = 0
    BEGIN
        SET @iPosEnd = CHARINDEX(@vcDelimiter, @vcList, @iPosStart)

        IF @iPosEnd <= 0
        BEGIN
            SET @iPosEnd = LEN(@vcList) + 1
            SET @iExit = 1
        END

        SET @vcStr = SUBSTRING(@vcList, @iPosStart, @iPosEnd - @iPosStart)

        EXEC(@vcSql + @vcStr + ''')')

        SET @iPosStart = @iPosEnd + @iLenDelim
    END

    RETURN 0

Then here is the dynamic SQL

DECLARE @Events VARCHAR(8000) = 'Event1,Event2,... Eventn'

CREATE TABLE #Events
    (
     EventName VARCHAR(MAX)
    )

EXEC [ListToTable] 
    @Events
   ,','
   ,'#Events'
   ,'EventName'

DECLARE @strCASE VARCHAR(MAX)
SET @strCase = ''

DECLARE Events_Cursor CURSOR 
FOR
SELECT EventName FROM [#Events] 

OPEN Events_Cursor
DECLARE @EventName VARCHAR(MAX)

FETCH NEXT FROM Events_Cursor INTO @EventName
WHILE ( @@FETCH_STATUS <> -1 ) 
    BEGIN
        IF ( @@FETCH_STATUS <> -2 ) 
            SET @strCase = @strCase + ',CASE WHEN [EventOpportunityProduct].[Name] = ''' + @EventName + ''' THEN EventOppertunityLineItem.[Trial_Status__c] ELSE NULL END [' + @EventName + ']'
        FETCH NEXT FROM Events_Cursor INTO @EventName
    END
CLOSE Events_Cursor
DEALLOCATE Events_Cursor

Then add @strCASE into your SELECT statement for ex.

DECLARE @strSQL VARCHAR(MAX)
SET @strSQL = 'SELECT
    Field1
    ,Field2
    ' + @strCASE + '
   FROM Table'

EXEC (@strSQL)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文