重写不带嵌套select的SQL语句

重写不带嵌套select的SQL语句,sql,select,Sql,Select,必须有某种方法重新编写以下语句。这很难看,但很管用。一定有更好的办法。我想知道,所以我不会继续使用嵌套选择编写语句 产出将是: H45_134, 190 H45_180, 143 本质上,我想得到的是不同的设备以及该设备在订单上的使用次数。在上面的输出中,“H45_134”是设备制造商编号,190是设备在订单上使用的总次数。190是订单上的行数量之和,但仅在设备制造商编号匹配的情况下 SELECT distinct he1.[Mfg_Number] as HingeDeviceOneL

必须有某种方法重新编写以下语句。这很难看,但很管用。一定有更好的办法。我想知道,所以我不会继续使用嵌套选择编写语句

产出将是:

H45_134, 190
H45_180, 143
本质上,我想得到的是不同的设备以及该设备在订单上的使用次数。在上面的输出中,“H45_134”是设备制造商编号,190是设备在订单上使用的总次数。190是订单上的行数量之和,但仅在设备制造商编号匹配的情况下

    SELECT distinct he1.[Mfg_Number] as HingeDeviceOneLocation, 
    (select sum(lineqty)
        FROM [MQPDatabase].[dbo].[Hinge_Edge] he2 inner join lineinfo li on li.ctr=he2.lineinfoctr 
            inner join order_header oh on oh.ctr=li.order_headerctr
            where location_1 >0 
                and location_2 =0 
                and location_3 = 0 
                and location_4=0 
                and location_5=0 
                and oh.jobnum='T35204D' 
                and he1.mfg_number=he2.mfg_number) as DeviceQty
  FROM [MQPDatabase].[dbo].[Hinge_Edge] he1 inner join lineinfo li on li.ctr=he1.lineinfoctr 
    inner join order_header oh on oh.ctr=li.order_headerctr
    where location_1 >0 
        and location_2 =0 
        and location_3 = 0 
        and location_4=0 
        and location_5=0 
        and oh.jobnum='T35204D'
试试这个

SELECT he1.[Mfg_Number] as HingeDeviceOneLocation, sum(lineqty) as DeviceQty
  FROM [MQPDatabase].[dbo].[Hinge_Edge] he1 
    inner join lineinfo li on li.ctr=he1.lineinfoctr 
    inner join order_header oh on oh.ctr=li.order_headerctr
    where location_1 >0 
        and location_2 =0 
        and location_3 = 0 
        and location_4=0 
        and location_5=0 
        and oh.jobnum='T35204D'
  GROUP BY he1.[Mfg_Number]

您可能需要使用GROUPBY和having子句。没有时间帮你弄清楚,但那是你应该调查的