Sql 多表查询

发布于 2024-09-18 05:03:58 字数 717 浏览 5 评论 0原文

我使用 SQL 2000 和数据库有以下表格

LandParcel (Table Name)
BlockID  ParcelNo NameofOnwe
11001    1056     Chandana
11001    1078     Sisil
11001    1158     Kumara
11078    105      SK
11078    245      Shantha
Actions (Table)
Blockid  ParcelNo  ActionTaken
11001    1056      Received
11001    1078      Received
11001    1158      Received
11078    105       Received
11078    245       Received
11001    1056      Send To LR
11001    1078      Send to LR
11078    105       Send To LT

我想要以下查询

Blockid  ActionTaken   ParcelNos
11001    Received      1056, 1078, 1158
11078    Received      105, 245
11001    Send To LR    1056, 1078
11078    Send To LR    105

请帮助我 旃陀那

I Used SQL 2000 and database have following tables

LandParcel (Table Name)
BlockID  ParcelNo NameofOnwe
11001    1056     Chandana
11001    1078     Sisil
11001    1158     Kumara
11078    105      SK
11078    245      Shantha
Actions (Table)
Blockid  ParcelNo  ActionTaken
11001    1056      Received
11001    1078      Received
11001    1158      Received
11078    105       Received
11078    245       Received
11001    1056      Send To LR
11001    1078      Send to LR
11078    105       Send To LT

I want the following query

Blockid  ActionTaken   ParcelNos
11001    Received      1056, 1078, 1158
11078    Received      105, 245
11001    Send To LR    1056, 1078
11078    Send To LR    105

Pl help me
Chandana

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

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

发布评论

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

评论(2

你又不是我 2024-09-25 05:03:58

我不太喜欢光标,但我找不到其他解决方案。我稍后可能会尝试在没有光标的情况下执行此操作。

 /* Sample data.*/


create TABLE #LandParcel (BlockID INT,  ParcelNo INT, NameofOnwe VARCHAR(50))

insert INTo #LandParcel select 11001   , 1056 ,   'Chandana'
insert INTo #LandParcel select 11001  ,  1078   , 'Sisil'
insert INTo #LandParcel select 11001   , 1158  ,  'Kumara'
insert INTo #LandParcel select 11078   , 105  ,   'SK'
insert INTo #LandParcel select 11078  ,  245   ,   'Shantha'


CREATE TABLE #Actions (Blockid INT, ParcelNo  INT, ActionTaken VARCHAR(50))

insert INTo #Actions select 11001 ,   1056    ,'Received'
insert INTo #Actions select 11001  ,  1078   , 'Received'
insert INTo #Actions select 11001   , 1158  ,  'Received'
insert INTo #Actions select 11078   , 105    , 'Received'
insert INTo #Actions select 11078  ,  245    , 'Received'
insert INTo #Actions select 11001  ,  1056    ,  'Send To LR'
insert INTo #Actions select 11001   , 1078    ,  'Send to LR'
insert INTo #Actions select 11078  ,  105     ,  'Send To LT'

/* End sample data */

/* Update query*/

CREATE TABLE #temp (BlockId INT, ActionTaken VARCHAR(50), ParcelNumbers VARCHAR(100))

INSERT  INTO #temp
SELECT  l.blockid, a.ActionTaken, NULL
FROM    #LandParcel l INNER JOIN #Actions a on l.blockid = a.blockid
GROUP BY l.blockid, a.actiontaken

DECLARE @blockId INT
DECLARE @actionTaken VARCHAR(50)
DECLARE @parcel VARCHAR(100)

SET @parcel = ''

DECLARE @cursorParcel CURSOR
    SET     @cursorParcel = CURSOR FAST_FORWARD
    FOR
    SELECT  blockid, ActionTaken FROM #temp

    OPEN    @cursorParcel

    FETCH   NEXT    FROM    @cursorParcel
    INTO    @blockId,
            @actionTaken
    WHILE @@FETCH_STATUS = 0
    BEGIN

        SELECT @parcel = CASE @parcel 
                WHEN '' THEN convert(VARCHAR(10), a.ParcelNo )
                ELSE @parcel + ', ' + convert(VARCHAR(10), a.ParcelNo ) 
                END
        from #temp t INNER JOIN #Actions a on t.blockid = a.blockid and t.actiontaken = a.actiontaken
        where t.BlockId = @blockId
        AND     t.ActionTaken = @actionTaken

        UPDATE  #temp SET ParcelNumbers = @parcel
        WHERE   BlockId = @blockId
        AND     ActionTaken = @actionTaken

        SET @blockId        = NULL
        SET @actionTaken    = NULL
        SET @parcel         = ''

        FETCH   NEXT    FROM    @cursorParcel
        INTO    @blockId,
                @actionTaken

    END


CLOSE       @cursorParcel
DEALLOCATE  @cursorParcel

SELECT * FROM #temp

DROP TABLE #temp
DROP TABLE #LandParcel
DROP TABLE #Actions

I don't like cursors much but I cold not find other solution. I might try later doing this without cursors.

 /* Sample data.*/


create TABLE #LandParcel (BlockID INT,  ParcelNo INT, NameofOnwe VARCHAR(50))

insert INTo #LandParcel select 11001   , 1056 ,   'Chandana'
insert INTo #LandParcel select 11001  ,  1078   , 'Sisil'
insert INTo #LandParcel select 11001   , 1158  ,  'Kumara'
insert INTo #LandParcel select 11078   , 105  ,   'SK'
insert INTo #LandParcel select 11078  ,  245   ,   'Shantha'


CREATE TABLE #Actions (Blockid INT, ParcelNo  INT, ActionTaken VARCHAR(50))

insert INTo #Actions select 11001 ,   1056    ,'Received'
insert INTo #Actions select 11001  ,  1078   , 'Received'
insert INTo #Actions select 11001   , 1158  ,  'Received'
insert INTo #Actions select 11078   , 105    , 'Received'
insert INTo #Actions select 11078  ,  245    , 'Received'
insert INTo #Actions select 11001  ,  1056    ,  'Send To LR'
insert INTo #Actions select 11001   , 1078    ,  'Send to LR'
insert INTo #Actions select 11078  ,  105     ,  'Send To LT'

/* End sample data */

/* Update query*/

CREATE TABLE #temp (BlockId INT, ActionTaken VARCHAR(50), ParcelNumbers VARCHAR(100))

INSERT  INTO #temp
SELECT  l.blockid, a.ActionTaken, NULL
FROM    #LandParcel l INNER JOIN #Actions a on l.blockid = a.blockid
GROUP BY l.blockid, a.actiontaken

DECLARE @blockId INT
DECLARE @actionTaken VARCHAR(50)
DECLARE @parcel VARCHAR(100)

SET @parcel = ''

DECLARE @cursorParcel CURSOR
    SET     @cursorParcel = CURSOR FAST_FORWARD
    FOR
    SELECT  blockid, ActionTaken FROM #temp

    OPEN    @cursorParcel

    FETCH   NEXT    FROM    @cursorParcel
    INTO    @blockId,
            @actionTaken
    WHILE @@FETCH_STATUS = 0
    BEGIN

        SELECT @parcel = CASE @parcel 
                WHEN '' THEN convert(VARCHAR(10), a.ParcelNo )
                ELSE @parcel + ', ' + convert(VARCHAR(10), a.ParcelNo ) 
                END
        from #temp t INNER JOIN #Actions a on t.blockid = a.blockid and t.actiontaken = a.actiontaken
        where t.BlockId = @blockId
        AND     t.ActionTaken = @actionTaken

        UPDATE  #temp SET ParcelNumbers = @parcel
        WHERE   BlockId = @blockId
        AND     ActionTaken = @actionTaken

        SET @blockId        = NULL
        SET @actionTaken    = NULL
        SET @parcel         = ''

        FETCH   NEXT    FROM    @cursorParcel
        INTO    @blockId,
                @actionTaken

    END


CLOSE       @cursorParcel
DEALLOCATE  @cursorParcel

SELECT * FROM #temp

DROP TABLE #temp
DROP TABLE #LandParcel
DROP TABLE #Actions
预谋 2024-09-25 05:03:58

您可以使用 SQL Server 2000 中的 FOR XML RAW 来模拟 SQL Server 2005 的 FOR XML PATH 行为:

SELECT
  a.BlockID,
  a.ActionTaken,
  REPLACE(
  REPLACE(
  REPLACE(
  (SELECT ParcelNo
   FROM Actions
   WHERE BlockID = a.BlockID
   AND ActionTaken = a.ActionTaken
   ORDER BY ParcelNo
   FOR XML RAW), '"/><row ParcelNo="', ', '),
   '<row ParcelNo="', ''),
   '"/>', '')  AS ParcelNos
FROM Actions a 
INNER JOIN LandParcel l 
  ON a.BlockID = l.BlockID
  AND a.ParcelNo = l.ParcelNo
GROUP BY 
  a.BlockID,
  a.ActionTaken
ORDER BY 
  a.ActionTaken

You can use FOR XML RAW from SQL Server 2000 to simulate the FOR XML PATH behavior of SQL Server 2005:

SELECT
  a.BlockID,
  a.ActionTaken,
  REPLACE(
  REPLACE(
  REPLACE(
  (SELECT ParcelNo
   FROM Actions
   WHERE BlockID = a.BlockID
   AND ActionTaken = a.ActionTaken
   ORDER BY ParcelNo
   FOR XML RAW), '"/><row ParcelNo="', ', '),
   '<row ParcelNo="', ''),
   '"/>', '')  AS ParcelNos
FROM Actions a 
INNER JOIN LandParcel l 
  ON a.BlockID = l.BlockID
  AND a.ParcelNo = l.ParcelNo
GROUP BY 
  a.BlockID,
  a.ActionTaken
ORDER BY 
  a.ActionTaken
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文