基于另一个增加SQL列

时间:2013-04-02 13:33:05

标签: sql-server

鉴于我有这样的查询:

select employeeid as id, department as dep from employee where department in (...) 
order by department

我想为每3个部门增加一个额外的列,以便结果看起来像这样:

id dep        magic
1  Accounting 0
30 Accounting 0
66 HR         0
67 HR         0
33 HR         0
2  Sales      0
9  IT         1
12 R&D        1
19 Design     1
45 QA         2
46 QA         2

我尝试过使用各种行号函数,over()和其他伏都教,但我似乎无法正确使用它。希望你能帮我解决这个问题!

编辑:所以这并不像我希望的那样直截了当。上面的例子可能没有捕获我案例中涉及的所有问题。 所以这是我正在运行的真实查询:

select    sdi.sdcid
         ,sdi.keyid1
         ,sdi.paramlistid
         ,sdi.paramlistversionid
         ,sdi.variantid
         ,sdi.dataset
         ,sdi.paramid
         ,sdi.paramtype
         ,sdi.replicateid
         ,sd.sdidataid
        --(ROW_NUMBER() over(order by sd.sdidataid asc) -1) /3 partition
         from sdidataitem sdi
         left join sdidata sd on 
         sdi.keyid1 = sd.keyid1 
         and sdi.keyid2 = sd.keyid2 
         and sdi.keyid3 = sd.keyid3 
         and sdi.paramlistid = sd.paramlistid 
         and sdi.paramlistversionid = sd.paramlistversionid 
         and sdi.variantid = sd.variantid 
         and sdi.dataset = sd.dataset 
         where sd.sdidataid in (select top 30 sdidataid from sdidata) 
         and sdi.u_analysserieid is null
         order by sd.sdidataid asc    

当我使用分区列运行查询时出现以下错误:Cannot find either column "sd" or the user-defined function or aggregate "sd.sdidataid", or the name is ambiguous. 这是创建所涉及的两个表的代码:

CREATE TABLE [lv2056d].[sdidata]  ( 
[sdcid]                     nvarchar(40) NOT NULL,
[keyid1]                    nvarchar(40) NOT NULL,
[keyid2]                    nvarchar(40) NOT NULL,
[keyid3]                    nvarchar(40) NOT NULL,
[paramlistid]               nvarchar(40) NOT NULL,
[paramlistversionid]        nvarchar(40) NOT NULL,
[variantid]                 nvarchar(40) NOT NULL,
[dataset]                   numeric(18,0) NOT NULL,
[limitruleid]               nvarchar(40) NULL,
[limitruleversionid]        nvarchar(40) NULL,
[modifiableflag]            nvarchar(1) NULL,
[approvalsequenceflag]      nvarchar(1) NULL,
[approvalpassrule]          nvarchar(20) NULL,
[condition]                 nvarchar(80) NULL,
[availabilityflag]          nvarchar(1) NULL,
[workflowid]                nvarchar(40) NULL,
[workflowversionid]         nvarchar(40) NULL,
[workflowinstance]          numeric(18,0) NULL,
[usersequence]              numeric(18,0) NULL,
[notes]                     nvarchar(2000) NULL,
[auditsequence]             numeric(18,0) NULL,
[auditdeferflag]            nvarchar(1) NULL,
[tracelogid]                nvarchar(40) NULL,
[createdt]                  datetime NULL,
[createby]                  nvarchar(40) NULL,
[createtool]                nvarchar(20) NULL,
[moddt]                     datetime NULL,
[modby]                     nvarchar(40) NULL,
[s_datasetstatus]           nvarchar(20) NULL,
[modtool]                   nvarchar(20) NULL,
[s_notebookreference]       nvarchar(20) NULL,
[s_assignedanalyst]         nvarchar(40) NULL,
[s_instrumentid]            nvarchar(40) NULL,
[s_icoverriddenflag]        nvarchar(1) NULL,
[s_retestedflag]            nvarchar(1) NULL,
[s_remeasuredflag]          nvarchar(1) NULL,
[s_remeasureinstance]       numeric(18,0) NULL,
[s_qcbatchid]               nvarchar(40) NULL,
[s_qcbatchitemid]           nvarchar(20) NULL,
[trackitemid]               nvarchar(40) NULL,
[scheduleplanid]            nvarchar(40) NULL,
[scheduleplanitemid]        nvarchar(40) NULL,
[uniquenessflag]            nvarchar(1) NULL,
[approvalflag]              nvarchar(1) NULL,
[sdidataid]                 nvarchar(40) NULL,
[sourceworkitemid]          nvarchar(40) NULL,
[sourceworkiteminstance]    numeric(18,0) NULL,
[s_cancellableflag]         nvarchar(1) NULL,
[documentid]                nvarchar(40) NULL,
[documentversionid]         nvarchar(40) NULL,
[blockflag]                 nvarchar(1) NULL,
[activeflag]                nvarchar(1) NULL,
[s_assigneddepartment]      nvarchar(40) NULL,
[s_instrumentusedflag]      nvarchar(1) NULL,
CONSTRAINT [xpksdidata] PRIMARY KEY CLUSTERED([sdcid],[keyid1],[keyid2],[keyid3],[paramlistid],[paramlistversionid],[variantid],[dataset]))
GO
CREATE TABLE [lv2056d].[sdidataitem]  ( 
[sdcid]                 nvarchar(40) NOT NULL,
[keyid1]                nvarchar(40) NOT NULL,
[keyid2]                nvarchar(40) NOT NULL,
[keyid3]                nvarchar(40) NOT NULL,
[paramlistid]           nvarchar(40) NOT NULL,
[paramlistversionid]    nvarchar(40) NOT NULL,
[variantid]             nvarchar(40) NOT NULL,
[dataset]               numeric(18,0) NOT NULL,
[paramid]               nvarchar(40) NOT NULL,
[paramtype]             nvarchar(80) NOT NULL,
[replicateid]           numeric(18,0) NOT NULL,
[aliasid]               nvarchar(80) NULL,
[mandatoryflag]         nvarchar(1) NULL,
[datatypes]             nvarchar(20) NULL,
[enteredvalue]          numeric(28,10) NULL,
[enteredtext]           nvarchar(255) NULL,
[enteredunits]          nvarchar(40) NULL,
[operatorrule]          nvarchar(255) NULL,
[transformvalue]        numeric(28,10) NULL,
[transformdt]           datetime NULL,
[transformtext]         nvarchar(255) NULL,
[transformrule]         nvarchar(4000) NULL,
[displayvalue]          nvarchar(255) NULL,
[displayunits]          nvarchar(40) NULL,
[displayformat]         nvarchar(255) NULL,
[rangeoperator]         nvarchar(20) NULL,
[enteredqualifier]      nvarchar(20) NULL,
[entrysdcid]            nvarchar(40) NULL,
[entryreftypeid]        nvarchar(40) NULL,
[calcrule]              nvarchar(4000) NULL,
[measurementactionid]   nvarchar(40) NULL,
[releasedflag]          nvarchar(1) NULL,
[valuestatus]           nvarchar(20) NULL,
[condition]             nvarchar(80) NULL,
[transformdeferflag]    nvarchar(1) NULL,
[textcolor]             numeric(18,0) NULL,
[usersequence]          numeric(18,0) NULL,
[notes]                 nvarchar(2000) NULL,
[auditsequence]         numeric(18,0) NULL,
[auditdeferflag]        nvarchar(1) NULL,
[tracelogid]            nvarchar(40) NULL,
[createdt]              datetime NULL,
[createby]              nvarchar(40) NULL,
[createtool]            nvarchar(20) NULL,
[moddt]                 datetime NULL,
[modby]                 nvarchar(40) NULL,
[modtool]               nvarchar(20) NULL,
[s_acoverriddenflag]    nvarchar(1) NULL,
[s_analystid]           nvarchar(40) NULL,
[s_qcevalstatus]        nvarchar(20) NULL,
[displayvalueformat]    nvarchar(255) NULL,
[calcexcludeflag]       nvarchar(1) NULL,
[sdidataitemid]         nvarchar(40) NULL,
[instrumentid]          nvarchar(40) NULL,
[instrumentfieldid]     nvarchar(20) NULL,
[activeflag]            nvarchar(1) NULL,
[externalreference]     nvarchar(255) NULL,
[resulttimeoffset]      numeric(28,10) NULL,
[u_analysserieid]       nvarchar(20) NULL,
[u_showindataentry]     nvarchar(20) NULL,
[w_instrumentid]        nvarchar(20) NULL,
[u_defvalset]           nvarchar(20) NULL,
CONSTRAINT [xpksdidataitem] PRIMARY KEY CLUSTERED([sdcid],[keyid1],[keyid2],[keyid3],[paramlistid],[paramlistversionid],[variantid],[dataset],[paramid],[paramtype],[replicateid]))
GO

2 个答案:

答案 0 :(得分:1)

你可以使用(ROW_NUMBER()OVER ...)/ 3

select employeeid as id, department as dep , 
(row_number() over (order by department asc) -1) /3 partition
from employee 
where department in (...) 
order by department

关于您的查询。 1)你似乎在第一场后想念逗号 2)如果你真的加入表格,最好使用join,只是为了清晰

select sd.sdidataid, (ROW_NUMBER() over(order by sd.sdidataid asc) -1) /3 partition 
from sdidataitem sdi inner join sdidata sd on 
    sdi.keyid1 = sd.keyid1 (... more column restrictions) 
and sdi.dataset = sd.dataset 
where sd.sdidataid in (select top 30 sdidataid from sdidata) 
and sdi.u_analysserieid is null 
order by sd.sdidataid asc

答案 1 :(得分:0)

Declare @tab table (id int,dep varchar(50))
insert into @tab Values(1,'Accounting'),(2,'Accounting'),(3,'Accounting'),(4,'AAA'),(5,'BBB'),(6,'ccc'),(7,'ddd'),(8,'eee'),(9,'fff');


Select t.*,a.Magic
from @tab t
join
(
Select dep,(ROW_NUMBER() over (order by dep) -1)/3 as Magic
from @tab
group by dep
) a on a.dep=t.dep
order by t.dep