SQL 2000查询问题

发布于 2024-10-03 02:58:58 字数 618 浏览 2 评论 0原文

考虑下表

create table sample(id, name, numeric, qno, ans1,ans2,ans3)

sample data

1, 'vivek', 1, 'a', 'b', ''
2, 'vivek', 1, 'c', 'd', ''
3, 'vivek', 2, 'e', 'f', 'g'
4, 'vivek', 3, 'h', 'i', 'j'
5, 'vijay', 1, 'k', '', 'l'
6, 'vijay', 2, 'm', '', 'n'
7, 'vijay', 2, 'o', '', 'p'
8, 'vikram', 3, 'q', 'r', 's'


output expected

column Names: name, info1, info2, info3

    values
'vivek','ab','ef','hij'
'vivek','cd','',''
'vijay','kl', 'mn',''
'vijay','','op',''
'vikram','','','qrs'

将行转换为列。

换句话说。一个答案将占一行。一个问题可以有多个答案。

输出应将答案编号作为列。

sql 2000中有什么快速简便的技术吗?

consider the following table

create table sample(id, name, numeric, qno, ans1,ans2,ans3)

sample data

1, 'vivek', 1, 'a', 'b', ''
2, 'vivek', 1, 'c', 'd', ''
3, 'vivek', 2, 'e', 'f', 'g'
4, 'vivek', 3, 'h', 'i', 'j'
5, 'vijay', 1, 'k', '', 'l'
6, 'vijay', 2, 'm', '', 'n'
7, 'vijay', 2, 'o', '', 'p'
8, 'vikram', 3, 'q', 'r', 's'


output expected

column Names: name, info1, info2, info3

    values
'vivek','ab','ef','hij'
'vivek','cd','',''
'vijay','kl', 'mn',''
'vijay','','op',''
'vikram','','','qrs'

converting rows into columns.

in other words. one answer will have one row. there can be multiple answers to one question.

the output should have answer numbers as columns.

is there any quick and easy technique in sql 2000?

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

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

发布评论

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

评论(2

耳钉梦 2024-10-10 02:58:58

首先,如果我理解你的问题,那么我认为你的第一个结果应该是
'vivek','ab','efg','hij' 而不是 'vivek','ab','ef','hij'

如果是这样,那么我认为以下可能会做什么你想要的。

IF OBJECT_ID('TempDB..#TempSample') IS NOT NULL
    DROP TABLE #TempSample

DECLARE @PrevName  nvarchar(80),
@PrevRow_number     INT,
@PrevQno int 


select
    isNull(id, 0)   as id , 
    isNull(sname, '')   as sName , 
    IsNull(qno,0)       as qno ,
    ans1, 
    ans2, 
    ans3  , 
    CAST(0 AS INT) AS row_number ,
    Cast('' as Nvarchar(80)) as newAns1, 
    Cast('' as Nvarchar(80)) as newAns2, 
    Cast('' as Nvarchar(80)) as newAns3
    INTO #TempSample
    from 
    (select id, sname, qno, ans1, ans2, ans3 from sample ) t3


ALTER TABLE #TempSample ADD PRIMARY KEY CLUSTERED (sName, qno , id)

UPDATE #TempSample 
    SET @PrevRow_number = row_number = CASE WHEN   sName = @PrevName and qno = @PrevQno THEN @PrevRow_number +1 ELSE 1 END,
        @PrevName  = sname ,
        @PrevQno   = qno,
        newAns1                        = case when sName = @PrevName and qno=1 Then newAns1 + ans1+ans2+ans3 else newAns1 end ,
        newAns2                        = case when sName = @PrevName and qno=2 Then newAns2 + ans1+ans2+ans3 else newAns2 end ,
        newAns3                        = case when sName = @PrevName and qno=3 Then newAns3 + ans1+ans2+ans3 else newAns3 end 
FROM #TempSample  WITH(TABLOCKX)
OPTION (MAXDOP 1)


Select sName,  Max(newAns1) as Ans1 , Max(newAns2) as Ans2, Max(newAns3) as Ans3 
From #TempSample 
Group by sName, Row_Number
Order By sName  

最近在 Sql 2000 中遇到了类似的问题,因此必须归功于这篇文章 在 SqlServerCentral 上

Firstly, if I understand your problem, then I think your first result should be
'vivek','ab','efg','hij' rather than 'vivek','ab','ef','hij'

If so, then I think the following may do what you want.

IF OBJECT_ID('TempDB..#TempSample') IS NOT NULL
    DROP TABLE #TempSample

DECLARE @PrevName  nvarchar(80),
@PrevRow_number     INT,
@PrevQno int 


select
    isNull(id, 0)   as id , 
    isNull(sname, '')   as sName , 
    IsNull(qno,0)       as qno ,
    ans1, 
    ans2, 
    ans3  , 
    CAST(0 AS INT) AS row_number ,
    Cast('' as Nvarchar(80)) as newAns1, 
    Cast('' as Nvarchar(80)) as newAns2, 
    Cast('' as Nvarchar(80)) as newAns3
    INTO #TempSample
    from 
    (select id, sname, qno, ans1, ans2, ans3 from sample ) t3


ALTER TABLE #TempSample ADD PRIMARY KEY CLUSTERED (sName, qno , id)

UPDATE #TempSample 
    SET @PrevRow_number = row_number = CASE WHEN   sName = @PrevName and qno = @PrevQno THEN @PrevRow_number +1 ELSE 1 END,
        @PrevName  = sname ,
        @PrevQno   = qno,
        newAns1                        = case when sName = @PrevName and qno=1 Then newAns1 + ans1+ans2+ans3 else newAns1 end ,
        newAns2                        = case when sName = @PrevName and qno=2 Then newAns2 + ans1+ans2+ans3 else newAns2 end ,
        newAns3                        = case when sName = @PrevName and qno=3 Then newAns3 + ans1+ans2+ans3 else newAns3 end 
FROM #TempSample  WITH(TABLOCKX)
OPTION (MAXDOP 1)


Select sName,  Max(newAns1) as Ans1 , Max(newAns2) as Ans2, Max(newAns3) as Ans3 
From #TempSample 
Group by sName, Row_Number
Order By sName  

Had a similar problem with Sql 2000 recently, so credit must go to this article on SqlServerCentral

安人多梦 2024-10-10 02:58:58

不,在 sql 2000 中没有简单的方法来创建数据透视表。

No, there's no simple way to create a pivot in sql 2000.

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