我需要根据WHERE子句的结果为SELECT子句添加一个连接字段。这是我目前的查询:
Declare
@Low numeric(13,0) = 10000,
@High numeric(13,0) = 100000000000,
@Name varchar(100) = '%',
@Stname varchar(100) = '%georgia',
@Sumlev varchar(3) = 1,
@County varchar(30) = 123456,
@Place varchar (5) = 8,
@Gid2 varchar(5),
@Gid2_1 varchar (7);
if object_id('tempdb..#GeoID2') is not null drop table #GeoID2
SELECT
SUMLEV, State, County, Place, Name, Stname, ESTIMATESBASE2010, CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY) AS Gid2
INTO
#GeoID2
FROM
[CensusData].[dbo].[SUB-EST2014_ALL]
WHERE
(NOT (SUMLEV = @Sumlev)) AND (NOT (County = @County)) AND (NOT (PLACE = @Place)) AND
(ESTIMATESBASE2010 > @Low) AND (ESTIMATESBASE2010 < @High) AND (Name LIKE @Name) and (Stname LIKE @Stname)
SELECT
[#GeoID2].SUMLEV, [#GeoID2].State, [#GeoID2].County, [#GeoID2].Place, [#GeoID2].Stname, [#GeoID2].Name, [#GeoID2].ESTIMATESBASE2010, [ACS_14_5YR_S1901_with_ann].[HC01_EST_VC13] as Avg_Salary, [#GeoID2].Gid2, [#GeoID2].Gid21
FROM
[CensusData].[dbo].[#GeoID2] INNER JOIN
[CensusData].[dbo].[ACS_14_5YR_S1901_with_ann] ON [CensusData].[dbo].[#GeoID2].Gid2 = [CensusData].[dbo].[ACS_14_5YR_S1901_with_ann].[GEO id2]
ORDER BY
[#GeoID2].NAME
我需要在我的第一个SELECT子句中添加一个CASE语句,为Gid2赋值。当前值分配为:
CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY) AS Gid2
我需要做的是在分配此值之前检查[County]的值。根据该结果,我将使用两个公式之一。这是我尝试过的(不起作用):
Case
When (County = 0)
Then 'CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 5 - LEN(PLACE)) + PLACE) AS Gid2'
Else 'CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY) AS Gid2'
End
我不确定使用CASE是正确的方法,但我无法找出任何其他方法来解决这个问题。有人可以帮忙吗?
答案 0 :(得分:0)
好的我明白了。我正在使用&#39;当我应该使用时,在我的Concat功能面前(。这是一个有效的查询:
Declare
@Low numeric(13,0) = 10000,
@High numeric(13,0) = 100000000000,
@Name varchar(100) = '%',
@Stname varchar(100) = '%georgia',
@Sumlev varchar(3) = 1,
@County varchar(30) = 123456,
@Place varchar (5) = 8,
@Gid2 varchar(5),
@Gid2_1 varchar (7);
if object_id('tempdb..#GeoID2') is not null drop table #GeoID2
SELECT
SUMLEV, State, County, Place, Name, Stname, ESTIMATESBASE2010,
Case
When (County = 0)
Then (CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 5 - LEN(PLACE)) + PLACE))
Else (CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY))
End AS Gid2
INTO
#GeoID2
FROM
[CensusData].[dbo].[SUB-EST2014_ALL]
WHERE
(NOT (SUMLEV = @Sumlev)) AND (NOT (County = @County)) AND (NOT (PLACE = @Place)) AND
(ESTIMATESBASE2010 > @Low) AND (ESTIMATESBASE2010 < @High) AND (Name LIKE @Name) and (Stname LIKE @Stname)
SELECT
[#GeoID2].SUMLEV, [#GeoID2].State, [#GeoID2].County, [#GeoID2].Place, [#GeoID2].Stname, [#GeoID2].Name, [#GeoID2].ESTIMATESBASE2010, [ACS_14_5YR_S1901_with_ann].[HC01_EST_VC13] as Avg_Salary, [#GeoID2].Gid2
FROM
[CensusData].[dbo].[#GeoID2] INNER JOIN
[CensusData].[dbo].[ACS_14_5YR_S1901_with_ann] ON [CensusData].[dbo].[#GeoID2].Gid2 = [CensusData].[dbo].[ACS_14_5YR_S1901_with_ann].[GEO id2]
ORDER BY
[#GeoID2].NAME