将AccessDB查询转换为T-SQL查询

时间:2018-02-13 03:09:36

标签: sql-server ms-access

我对TransformPivot有疑问。我需要将以下Access DB查询转换为T-SQL:

TRANSFORM First([UETTDRSB39A Criteria].[RPL Link]) AS [FirstOfRPL Link]
SELECT [UETTDRSB39A Criteria].[RPL Link], [UETTDRSB39A Criteria].Detail1
FROM [UETTDRSB39A Criteria], [Candidate Evidence records]
WHERE ((([UETTDRSB39A Criteria].Pathway) In ('Common','Electrical')))
GROUP BY [UETTDRSB39A Criteria].[RPL Link], [UETTDRSB39A Criteria].Detail1
PIVOT [UETTDRSB39A Criteria].Prerequisite;

我尝试转换查询,但是,我遇到了一些错误。

select pt.* 
from (

SELECT [UETTDRSB39A Criteria].[RPLLink], [UETTDRSB39A Criteria].Detail1, Prerequisite

FROM [UETTDRSB39A Criteria], [CandidateEvidenceRecords]

WHERE ((([UETTDRSB39A Criteria].Pathway) In ('Common','Electrical')))

GROUP BY [UETTDRSB39A Criteria].[RPLLink], [UETTDRSB39A Criteria].Detail1
) src

pivot (FIRST(RPLLink) for src.Prerequisite in ([UEENEEE101],[UEENEEE102],[UEENEEE104],[UEENEEE105],[UEENEEE107],[UEENEEE137],
[UEENEEG006],[UEENEEG033],[UEENEEG063],[UEENEEG101],[UEENEEG102],[UEENEEG106],[UEENEEG108], [UEENEEG109],
[UEENEEK142],[UETTDREL16A],[UETTDRIS67A],[UETTDRSB39A])) pt;

错误显示'首先不是聚合函数'。但是,当我输入:

select pt.* 
from (
SELECT [UETTDRSB39A Criteria].[RPLLink], [UETTDRSB39A Criteria].Detail1, Prerequisite

FROM [UETTDRSB39A Criteria], [CandidateEvidenceRecords]

WHERE ((([UETTDRSB39A Criteria].Pathway) In ('Common','Electrical')))

GROUP BY [UETTDRSB39A Criteria].[RPLLink], [UETTDRSB39A Criteria].Detail1
) src

pivot (top 1 (RPLLink) for src.Prerequisite in ([UEENEEE101],[UEENEEE102],[UEENEEE104],[UEENEEE105],[UEENEEE107],[UEENEEE137],
[UEENEEG006],[UEENEEG033],[UEENEEG063],[UEENEEG101],[UEENEEG102],[UEENEEG106],[UEENEEG108], [UEENEEG109], [UEENEEK142],[UETTDREL16A],[UETTDRIS67A],[UETTDRSB39A])) pt;

表架构:

CREATE TABLE [dbo].[UETTDRSB39A Criteria](
[UoC] [nvarchar](50) NULL,
[AncestorLevel] [int] NULL,
[Pathway] [nvarchar](100) NULL,
[Prerequisite] [nvarchar](50) NULL,
[Title] [nvarchar](500) NULL,
[Descriptor] [nvarchar](4000) NULL,
[UoCType] [nvarchar](50) NULL,
[Summary] [nvarchar](4000) NULL,
[Ref] [nvarchar](50) NULL,
[Detail] [nvarchar](4000) NULL,
[Type1] [nvarchar](800) NULL,
[RPLType] [nvarchar](500) NULL,
[RPLPreferred] [nvarchar](2000) NULL,
[RPLLink] [nvarchar](50) NULL,
[Detail1] [nvarchar](4000) NULL
)


CREATE TABLE [dbo].[CandidateEvidenceRecords](
[ID] [int] NULL,
[EvidenceID] [nvarchar](50) NULL,
[Candidate] [nvarchar](150) NULL,
[EvidenceType] [nvarchar](1000) NULL,
[UoC] [nvarchar](50) NULL,
[Evidence] [nvarchar](2000) NULL,
[DateOfIssue] [date] NULL,
[DateOfExpiry] [date] NULL,
[Issuer] [nvarchar](1500) NULL,
[CertificateNumber] [nvarchar](500) NULL,
[Confirmed] [bit] NULL,
[ConfirmedBy] [nvarchar](100) NULL,
[ConfirmedDate] [date] NULL
)

Access Query Output

SQL Query Output 1

SQL Query Output 2

它仍然不起作用。

期待您的回复。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果只有一个值,则可以使用Max()代替First()

如果有多个值,并且您希望按某个键排序的第一个值,请使用子查询过滤掉其他值,然后使用Max()