这是我下面的代码:
SELECT
DV.ERSDataValues_ERSCommodity_ID AS Commodity_ID,
SU.ERSSector_Desc AS Sector,
GU.ERSGroup_Desc AS Group_Description,
CSC.ERSCommoditySubCommodity_Desc AS CommodityName,
STU.ERSStatisticType_Attribute AS Statistic_Type,
SLU.ERSSource_Desc AS Source,
CDS.ERSCommodity_PhysicalAttribute_Desc AS Physical_Attribute,
UPU.ERSUtilPractice_Desc AS Util_Practice,
PU.ERSProdPractice_Desc AS Production_Practice,
TDT.ERSTimeDimensionType_Desc AS Time_Frequency,
TU.ERSTimeDimension_Date AS [Date],
GTU.ERSGeographyType_Desc AS Geography_Type,
GDU.ERSGeographyDimension_Desc AS Geography_Dimension,
ULU.ERSUnit_Desc AS Unit_Description,
DV.ERSDataValues_AttributeValue AS [Value]
FROM
CoSD.ERSCommodityDataSeries AS CDS
INNER JOIN
CoSD.ERSDataValues AS DV ON CDS.ERSCommodity_ID = DV.ERSDataValues_ERSCommodity_ID
INNER JOIN
CoSD.ERSSector_LU AS SU ON CDS.ERSCommodity_ERSSector_ID = SU.ERSSector_ID
INNER JOIN
CoSD.ERSGroup_LU AS GU ON CDS.ERSCommodity_ERSGroup_ID = GU.ERSGroup_ID
INNER JOIN
CoSD.ERSProdPractice_LU AS PU ON CDS.ERSCommodity_ERSProdPractice_ID = PU.ERSProdPractice_ID
INNER JOIN
CoSD.ERSUtilPractice_LU AS UPU ON CDS.ERSCommodity_ERSUtilPractice_ID = UPU.ERSUtilPractice_ID
AND CDS.ERSCommodity_ERSUtilPractice_ID = UPU.ERSUtilPractice_ID
INNER JOIN
CoSD.ERSUnit_LU AS ULU ON DV.ERSDataValues_ERSUnit_ID = ULU.ERSUnit_ID
INNER JOIN
CoSD.ERSSource_LU AS SLU ON CDS.ERSCommodity_ERSSource_ID = SLU.ERSSource_ID
INNER JOIN
CoSD.ERSStatisticType_LU AS STU ON CDS.ERSCommodity_ERSStatisticType_ID = STU.ERSStatisticType_ID
INNER JOIN
CoSD.ERSTimeDimension_LU AS TU ON DV.ERSDataValues_ERSTimeDimension_ID = TU.ERSTimeDimension_ID
INNER JOIN
CoSD.ERSGeographyDimension_LU AS GDU ON DV.ERSDataValues_ERSGeography_ID = GDU.ERSGeographyDimension_ID
INNER JOIN
CoSD.ERSTimeDimensionType_LU AS TDT ON TU.ERSTimeDimension_TimeDimensionType_ID = TDT.ERSTimeDimensionType_ID
INNER JOIN
CoSD.ERSGeographyType_LU AS GTU ON GDU.ERSGeographyDimension_ERSGeographyType_ID = GTU.ERSGeographyType_ID
INNER JOIN
CoSD.ERSCommoditySubCommodity_LU AS CSC ON CSC.ERSCommoditySubCommodity_ID=CDS.ERSCommoditySubCommodity_ID
UNION ALL
SELECT cds.ERSCommodity_ID AS Commodity_ID
,slu.ERSSector_Desc AS Sector
,glu.ERSGroup_Desc AS Group_Description
,csc.ERSCommoditySubCommodity_Desc AS CommodityName
,stu.ERSStatisticType_Attribute_Desc
,CVO.ERSConstructedVariable_InputSources
,cds.ERSCommodity_PhysicalAttribute_Desc
,ulu.ERSUtilPractice_Desc
,plu.ERSProdPractice_Desc
,CVO.ERSConstructedVariable_OutputTimeDimensionValue
,CVO.ERSConstructedVariable_TimeDimensionDate
,gtlu.ERSGeographyType_Desc
,gdlu.ERSGeographyDimension_Country
,unlu.ERSUnit_Desc
,CVO.ERSConstructedVariable_OutputValue
FROM CoSD.ERSConstructedVariablesOutcomes AS CVO
INNER JOIN CoSD.ERSCommodityDataSeries AS cds ON CVO.ERSConstructedVariable_NewDataSeriesID = cds.ERSCommodity_ID
INNER JOIN CoSD.ERSSector_LU AS slu ON cds.ERSCommodity_ERSSector_ID = slu.ERSSector_ID
INNER JOIN CoSD.ERSGroup_LU AS glu ON cds.ERSCommodity_ERSGroup_ID = glu.ERSGroup_ID
INNER JOIN CoSD.ERSCommoditySubCommodity_LU AS csc ON cds.ERSCommoditySubCommodity_ID = csc.ERSCommoditySubCommodity_ID
INNER JOIN CoSD.ERSStatisticType_LU AS stu ON cds.ERSCommodity_ERSStatisticType_ID = stu.ERSStatisticType_ID
INNER JOIN CoSD.ERSUtilPractice_LU AS ulu ON cds.ERSCommodity_ERSUtilPractice_ID = ulu.ERSUtilPractice_ID
INNER JOIN CoSD.ERSProdPractice_LU AS plu ON cds.ERSCommodity_ERSProdPractice_ID = plu.ERSProdPractice_ID
INNER JOIN CoSD.ERSGeographyDimension_LU AS gdlu ON CVO.ERSConstructedVariable_OutputGeographyDimensionID = gdlu.ERSGeographyDimension_ID
INNER JOIN CoSD.ERSUnit_LU AS unlu ON CVO.ERSConstructedVariable_OutputUnitID = unlu.ERSUnit_ID
INNER JOIN CoSD.ERSGeographyType_LU AS gtlu ON gdlu.ERSGeographyDimension_ERSGeographyType_ID = gtlu.ERSGeographyType_ID
WHERE (CVO.ERSConstructedVariable_DataRowPrivacyID = 1)
我想添加行号功能,以便为每行获取唯一的ID, 我在此链接中尝试过这种方式 Row_Number() with union query 但这没有用,对如何执行此操作有任何想法吗?
答案 0 :(得分:0)
我建议您不要使用[source]或[date]作为列别名,并且在where子句中,您不需要用括号将每个谓词括起来。
我无法验证的是,所有列数据类型都是兼容的,但查询应如下所示:
SELECT
commodity_id
, sector
, group_description
, commodityname
, statistic_type
, [source]
, physical_attribute
, util_practice
, production_practice
, time_frequency
, [date]
, geography_type
, geography_dimension
, unit_description
, value
, row_number() over(order by commodity_id) as rn
FROM (
SELECT
dv.ERSDataValues_ERSCommodity_ID AS commodity_id
, su.ERSSector_Desc AS sector
, gu.ERSGroup_Desc AS group_description
, csc.ERSCommoditySubCommodity_Desc AS commodityname
, stu.ERSStatisticType_Attribute AS statistic_type
, slu.ERSSource_Desc AS source
, cds.ERSCommodity_PhysicalAttribute_Desc AS physical_attribute
, upu.ERSUtilPractice_Desc AS util_practice
, pu.ERSProdPractice_Desc AS production_practice
, tdt.ERSTimeDimensionType_Desc AS time_frequency
, tu.ERSTimeDimension_Date AS date
, gtu.ERSGeographyType_Desc AS geography_type
, gdu.ERSGeographyDimension_Desc AS geography_dimension
, ulu.ERSUnit_Desc AS unit_description
, dv.ERSDataValues_AttributeValue AS value
FROM CoSD.ERSCommodityDataSeries AS cds
INNER JOIN CoSD.ERSDataValues AS dv ON cds.ERSCommodity_ID = dv.ERSDataValues_ERSCommodity_ID
INNER JOIN CoSD.ERSSector_LU AS su ON cds.ERSCommodity_ERSSector_ID = su.ERSSector_ID
INNER JOIN CoSD.ERSGroup_LU AS gu ON cds.ERSCommodity_ERSGroup_ID = gu.ERSGroup_ID
INNER JOIN CoSD.ERSProdPractice_LU AS pu ON cds.ERSCommodity_ERSProdPractice_ID = pu.ERSProdPractice_ID
INNER JOIN CoSD.ERSUtilPractice_LU AS upu ON cds.ERSCommodity_ERSUtilPractice_ID = upu.ERSUtilPractice_ID
AND cds.ERSCommodity_ERSUtilPractice_ID = upu.ERSUtilPractice_ID
INNER JOIN CoSD.ERSUnit_LU AS ulu ON dv.ERSDataValues_ERSUnit_ID = ulu.ERSUnit_ID
INNER JOIN CoSD.ERSSource_LU AS slu ON cds.ERSCommodity_ERSSource_ID = slu.ERSSource_ID
INNER JOIN CoSD.ERSStatisticType_LU AS stu ON cds.ERSCommodity_ERSStatisticType_ID = stu.ERSStatisticType_ID
INNER JOIN CoSD.ERSTimeDimension_LU AS tu ON dv.ERSDataValues_ERSTimeDimension_ID = tu.ERSTimeDimension_ID
INNER JOIN CoSD.ERSGeographyDimension_LU AS gdu ON dv.ERSDataValues_ERSGeography_ID = gdu.ERSGeographyDimension_ID
INNER JOIN CoSD.ERSTimeDimensionType_LU AS tdt ON tu.ERSTimeDimension_TimeDimensionType_ID = tdt.ERSTimeDimensionType_ID
INNER JOIN CoSD.ERSGeographyType_LU AS gtu ON gdu.ERSGeographyDimension_ERSGeographyType_ID = gtu.ERSGeographyType_ID
INNER JOIN CoSD.ERSCommoditySubCommodity_LU AS csc ON csc.ERSCommoditySubCommodity_ID = cds.ERSCommoditySubCommodity_ID
UNION ALL
SELECT
cds.ERSCommodity_ID AS commodity_id
, slu.ERSSector_Desc AS sector
, glu.ERSGroup_Desc AS group_description
, csc.ERSCommoditySubCommodity_Desc AS commodityname
, stu.ERSStatisticType_Attribute_Desc
, cvo.ERSConstructedVariable_InputSources
, cds.ERSCommodity_PhysicalAttribute_Desc
, ulu.ERSUtilPractice_Desc
, plu.ERSProdPractice_Desc
, cvo.ERSConstructedVariable_OutputTimeDimensionValue
, cvo.ERSConstructedVariable_TimeDimensionDate
, gtlu.ERSGeographyType_Desc
, gdlu.ERSGeographyDimension_Country
, unlu.ERSUnit_Desc
, cvo.ERSConstructedVariable_OutputValue
FROM CoSD.ERSConstructedVariablesOutcomes AS cvo
INNER JOIN CoSD.ERSCommodityDataSeries AS cds ON cvo.ERSConstructedVariable_NewDataSeriesID = cds.ERSCommodity_ID
INNER JOIN CoSD.ERSSector_LU AS slu ON cds.ERSCommodity_ERSSector_ID = slu.ERSSector_ID
INNER JOIN CoSD.ERSGroup_LU AS glu ON cds.ERSCommodity_ERSGroup_ID = glu.ERSGroup_ID
INNER JOIN CoSD.ERSCommoditySubCommodity_LU AS csc ON cds.ERSCommoditySubCommodity_ID = csc.ERSCommoditySubCommodity_ID
INNER JOIN CoSD.ERSStatisticType_LU AS stu ON cds.ERSCommodity_ERSStatisticType_ID = stu.ERSStatisticType_ID
INNER JOIN CoSD.ERSUtilPractice_LU AS ulu ON cds.ERSCommodity_ERSUtilPractice_ID = ulu.ERSUtilPractice_ID
INNER JOIN CoSD.ERSProdPractice_LU AS plu ON cds.ERSCommodity_ERSProdPractice_ID = plu.ERSProdPractice_ID
INNER JOIN CoSD.ERSGeographyDimension_LU AS gdlu ON cvo.ERSConstructedVariable_OutputGeographyDimensionID = gdlu.ERSGeographyDimension_ID
INNER JOIN CoSD.ERSUnit_LU AS unlu ON cvo.ERSConstructedVariable_OutputUnitID = unlu.ERSUnit_ID
INNER JOIN CoSD.ERSGeographyType_LU AS gtlu ON gdlu.ERSGeographyDimension_ERSGeographyType_ID = gtlu.ERSGeographyType_ID
WHERE cvo.ERSConstructedVariable_DataRowPrivacyID = 1
) AS derived