简体   繁体   English

Select语句中的条件问题

[英]Issue with condition in Select Statement

I am getting the problem with following query. 我遇到以下查询的问题。

I have four tables: 我有四张桌子:

OBGT - T0
OACT- T1
OBGS - T2
OASC - T3

And my query is: 我的疑问是:

SELECT *FROM (
SELECT DISTINCT
    concat([Segment_0], '-' , [Segment_1], '-',[Segment_2])  As [AcctCode],
    T1.[AcctName],
    T2.[Name],
    concat(T3.[Code],'-',T3.[Name]),T0.[DebLTotal] AS [ANNUAL BUDGET],
    (SELECT concat(T3.Code , '-', T3.[Name]) where T3.SegmentId = '1') AS [Project],
    (SELECT concat(T3.Code , '-', T3.[Name]) where T3.SegmentId = '2') AS [Distt]
FROM OBGT T0
    INNER JOIN OACT T1 ON T0.[AcctCode] = T1.[AcctCode]
    INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId] 
    INNER JOIN OASC T3 ON (T3.SegmentId = '1' AND T3.Code = [Segment_1])
                       OR (T3.SegmentId = '2' AND T3.Code = [Segment_2])

) AS d

WHERE Distt = '001' So in this query: WHERE Distt ='001'所以在这个查询中:

  • OASC - T3 table have the field SegmentId which have two Id 1 and 2 OASC-T3表具有字段SegmentId,其具有两个Id 1和2
  • OACT - T0 table have two fields Segment_1 (values are matching with SegmentId = 1) and Segment_2 (values arematching with SegmentId = 2). OACT - T0表有两个字段Segment_1(值与SegmentId = 1匹配)和Segment_2(值与SegmentId = 2匹配)。

You can check in the query. 您可以签入查询。

But the problem is coming if I put a condition. 但是,如果我提出一个条件,问题就会出现。

where Project = 100 is working but if the condition is where Project = 100 and Distt = 001 then I get an error: where Project = 100正在工作,但如果条件是where Project = 100 and Distt = 001那么我得到一个错误:

Msg 207, Level 16, State 1, Line 11 Msg 207,Level 16,State 1,Line 11
Invalid column name 'Distt'. 列名称“Distt”无效。

Put you original query as a derived table: 将原始查询作为派生表:

select * from
(
SELECT DISTINCT
    concat([Segment_0], '-' , [Segment_1], '-',[Segment_2])  As 'AcctCode',
    T1.[AcctName] as dummyname1,
    T2.[Name]  as dummyname2,
    concat(T3.[Code],'-',T3.[Name]) as dummyname3,
    T0.[DebLTotal] AS 'ANNUAL BUDGET',
    (SELECT concat(T3.Code , '-', T3.[Name]) where T3.SegmentId = '1') AS 'Project',
    (SELECT concat(T3.Code , '-', T3.[Name]) where T3.SegmentId = '2') AS 'Distt'
FROM OBGT T0
INNER JOIN OACT T1
    ON T0.[AcctCode] = T1.[AcctCode]
INNER JOIN OBGS T2
    ON T0.[Instance] = T2.[AbsId] 
INNER JOIN OASC T3
    ON (T3.SegmentId = '1' AND T3.Code = [Segment_1])
    OR (T3.SegmentId = '2' AND T3.Code = [Segment_2])
) dt
where Project = 100 and Distt =001

NOTE : You need to add column names/aliases for each column before you can use it! 注意 :您需要为每列添加列名称/别名,然后才能使用它!

In the SELECT clause you have created an expression, you have then created a column alias ie {expression} as [Alias] . SELECT子句中创建了一个表达式,然后创建了一个列别名,即{expression}[Alias]

Two things, to fix your problem you should use a derived table (see below), secondly don't use single quotes for column aliases - use square brackets. 有两件事,为了解决你的问题,你应该使用派生表(见下文),其次不要使用单引号作为列别名 - 使用方括号。

SELECT *
FROM (
    SELECT DISTINCT
        concat([Segment_0], '-' , [Segment_1], '-',[Segment_2])  As [AcctCode],
        T1.[AcctName],
        T2.[Name],
        concat(T3.[Code],'-',T3.[Name]),T0.[DebLTotal] AS 'ANNUAL BUDGET',
        (SELECT concat(T3.Code , '-', T3.[Name]) where T3.SegmentId = '1') AS [Project],
        (SELECT concat(T3.Code , '-', T3.[Name]) where T3.SegmentId = '2') AS [Distt]
    FROM OBGT T0
        INNER JOIN OACT T1 ON T0.[AcctCode] = T1.[AcctCode]
        INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId] 
        INNER JOIN OASC T3 ON (T3.SegmentId = '1' AND T3.Code = [Segment_1])
                           OR (T3.SegmentId = '2' AND T3.Code = [Segment_2])

    ) AS d
WHERE Distt = 001

Be aware you might get another issue with your query if those sub-queries on the SELECT clause return more than 1 row! 请注意,如果SELECT子句上的子查询返回超过1行,您的查询可能会遇到另一个问题! Your query will give another error. 您的查询将给出另一个错误。

Project works because it's probably a column in one of the tables. 项目有效,因为它可能是其中一个表中的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM