嵌套选择查询

时间:2020-10-01 15:14:12

标签: sql sql-server

我使用sql不到一年了,遇到了一些看起来很简单的东西,但语法不正确。

我正在尝试联接其他表(表名称=“设备”),并使用该表中的另一个字段(高级别)来限制以下查询的结果:

select deviceid, inventorydate, case when DeltaVersion ='' then '000'

              else DeltaVersion end as DeltaVersion,

--            CurrentATMVersion
              cast(CurrentATMVersion as varchar(50)) as 'CurrentATMVersion',

--            InstallATMVersion
              cast(InstallATMVersion as varchar(50)) as 'InstallATMVersion',

--            InstallDyDVersion
              cast(InstallDyDVersion as varchar(50)) as 'InstallDyDVersion',

              ATMInstallDate,

--            Java Version
              cast(JavaVersion as varchar(50)) as 'JavaVersion',

--            Chrome Version
              cast(ChromeVersion as varchar(50)) as 'ChromeVersion'
            
            from (SELECT a.[deviceid] ,

              a.[inventorydate],

              cast(a.[inventorydata].query('   for $b in /proviewinventoryfile/Section[@type="pvinvent"]/Component[@type="Software"]/Component[@name="ATM40"]//*

                                                            where $b/Name = "DeltaVersion"

                                                            return (data($b/Value))') as varchar(50)) as "DeltaVersion",

              cast(a.[inventorydata].query('  for $b in /proviewinventoryfile/Section[@type="pvinvent"]/Component[@type="Software"]/Component[@name="ATM40"]//*

                                                                     where $b/Name = "CurrentATMVersion"

                                                                     return (data($b/Value))') as varchar(50)) as "CurrentATMVersion",

              cast(a.[inventorydata].query('  for $b in /proviewinventoryfile/Section[@type="pvinvent"]/Component[@type="Software"]/Component[@name="ATM40"]//*

                                                                     where $b/Name = "InstallATMVersion"

                                                                     return (data($b/Value))') as varchar(50)) as "InstallATMVersion",

              cast(a.[inventorydata].query('  for $b in /proviewinventoryfile/Section[@type="pvinvent"]/Component[@type="Software"]/Component[@name="ATM40"]//*

                                                                     where $b/Name = "InstallDyDVersion"

                                                                     return (data($b/Value))') as varchar(50)) as "InstallDyDVersion",

              cast(a.[inventorydata].value('(/proviewinventoryfile/Section[@type="pvinvent"]/Component[@type="Software"]/Component[@name="ATM40"]/ValueItem[3]/Value )[1]','nvarchar(max)' ) as varchar(100))  as "ATMInstallDate",

             
              cast(a.[inventorydata].query('  for $b in /proviewinventoryfile/Section[@type="pvinvent"]/Component[@type="Software"]/Component[@name="JavaSoft"]//*

                                                                     where $b/Name = "Java Version"

                                                                     return (data($b/Value))') as varchar(50)) as "JavaVersion",

              cast(a.[inventorydata].query('  for $b in /proviewinventoryfile/Section[@type="pvinvent"]/Component[@type="Software"]/Component[@name="Google Settings"]//*

                                                                     where $b/Name = "Chrome Version"

                                                                     return (data($b/Value))') as varchar(50)) as "ChromeVersion"

  FROM [proview].[dbo].[inventory] as a 

  INNER JOIN

  (    SELECT [deviceid], max ([inventorydate] ) as id

         FROM [proview].[dbo].[inventory]

         group by deviceid
                 
  ) as b

on b.id = a.inventorydate

and b.deviceid = a.deviceid)  c;

我不知道将内部连接语句放在哪里- 两个表中的公共字段均为deviceid 希望将结果限制在上述查询中,其中device.hierlevel类似于“ 0.12%”

提前

寻求帮助

1 个答案:

答案 0 :(得分:0)

您称为b的子查询是您决定选择哪些库存行的地方,因此我将设备查找放在此处:

FROM proview.dbo.inventory as a 
INNER JOIN
(   
  SELECT i.deviceid, max(i.inventorydate) AS max_inventorydate
  FROM proview.dbo.inventory i
  WHERE i.deviceid IN (SELECT d.deviceid FROM device d WHERE d.hierlevel LIKE '0.12%')
  GROUP BY i.deviceid
) AS b ON b.deviceid = a.deviceid AND b.max_inventorydate = a.inventorydate