编辑: 搞定了。我创建了第二个视图来保持枢轴,我认为这让我感到困惑。然后将我对AmusementMeter表的连接编辑为:
left join AmusementMeter as am_in on m.MachineId = am_in.MachineId and am_in.[Type] = '+'
left join AmusementMeter as am_out on m.MachineId = am_out.MachineId and am_out.[Type] = '-'
现在一切都回来了。
感谢您的帮助。
我在使用Sql Server 2012时遇到了这个查询的麻烦。我正在从多个表中拉出来,而我现在只遇到一个问题。对于每台机器,AmesementMeter表中有两个AmusementMeter记录。我需要将AmusementMeter表中的两条记录作为一条记录,因此返回的输出中每个MachineId没有两行。
我有什么:
SELECT DISTINCT
m.MachineId as 'Machine Id' ,
m.Name as 'Machine Name',
mc.Name as 'Machine Class',
m.InstallDate as 'Setup Date',
m.Serial as 'Machine Serial Number',
m.[Description] as 'Machine Description',
l.ID as 'Location Id',
l.Name as 'Location Name',
m.DateMovedToLocation as 'Date moved to location',
m.Manufacturer,
m.PurchaseDate as 'Purchase Date',
m.PurchasePrice as 'Purchase Price',
m.PurchaseFrom as 'Purchase From',
m.CabinetSerial as 'Cabinet Serial Number',
m.Color as 'Cabinet Color',
m.BillValidatorDescription as 'Bill Validator Description',
CASE WHEN am.[Type] = '+' THEN am.Reading END AS 'IN Meter Reading',
CASE WHEN am.[Type] = '+' THEN am.Value END AS 'IN Meter Value',
CASE WHEN am.[Type] = '+' THEN am.Meter END AS 'IN Meter Digits',
CASE WHEN am.[Type] = '-' THEN am.Reading END AS 'IN Meter Reading',
CASE WHEN am.[Type] = '-' THEN am.Value END AS 'IN Meter Value',
CASE WHEN am.[Type] = '-' THEN am.Meter END AS 'IN Meter Digits',
CASE WHEN Pvt.[1] IS NULL THEN '' ELSE Pvt.[1] END AS 'REDEMPTION',
CASE WHEN Pvt.[2] IS NULL THEN '' ELSE Pvt.[2] END AS 'MAX BET',
CASE WHEN Pvt.[3] IS NULL THEN '' ELSE Pvt.[3] END AS 'JACKPOT CAP',
CASE WHEN Pvt.[4] IS NULL THEN '' ELSE Pvt.[4] END AS '50 AND 100 STAT',
CASE WHEN Pvt.[5] IS NULL THEN '' ELSE Pvt.[5] END AS 'MUTHA GOOSE KEY',
CASE WHEN Pvt.[6] IS NULL THEN '' ELSE Pvt.[6] END AS 'LICENSE EXPIRATION YR',
CASE WHEN Pvt.[7] IS NULL THEN '' ELSE Pvt.[7] END AS 'PERMIT NUMBER',
CASE WHEN Pvt.[8] IS NULL THEN '' ELSE Pvt.[8] END AS 'GAGGLE VER 126',
CASE WHEN Pvt.[9] IS NULL THEN '' ELSE Pvt.[9] END AS 'METER VAULT RIVETS ',
CASE WHEN Pvt.[10] IS NULL THEN '' ELSE Pvt.[10] END AS 'ASSET ID TAG'
FROM
(SELECT
mudfr.MachineId, mudfr.UDFieldId, mudfr.Value
FROM
MachineUserDefinedFieldRelation AS mudfr INNER JOIN
MachineUserDefinedField AS mudf ON mudfr.UDFieldId = mudf.FieldId) AS pv PIVOT
(Max(Value) FOR UDFieldId IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS Pvt JOIN
Machine AS m on m.MachineId = Pvt.MachineId JOIN
MachineClass as mc on m.CategoryID = mc.ID JOIN
Location as l on m.LocationID = l.ID INNER JOIN
AmusementMeter as am on am.MachineId = m.MachineId
输出(我剪掉了很多,留在问题列中): 不会让我发布截图,抱歉。我得到这样的东西,我有两次MachineId出现。其他值来自AmuesmentMeter表。我希望它只显示一次,并且我们将值设置为空值。
550001 NULL NULL NULL 928204 0.25 6
550001 547563 0.25 6 NULL NULL NULL
我想要的是:(预期输出)
550001 547563 0.25 6 928204 0.25 6
感谢您的帮助。
答案 0 :(得分:1)
您必须在GROUP BY
上添加m.MachineId
条款,然后为您想要的每个列选择MAX(columnname)
"合并"。
答案 1 :(得分:0)
编辑:搞定了。我创建了第二个视图来保持枢轴,我认为这让我感到困惑。然后将我对AmusementMeter表的连接编辑为:
left join AmusementMeter as am_in on m.MachineId = am_in.MachineId and am_in.[Type] = '+'
left join AmusementMeter as am_out on m.MachineId = am_out.MachineId and am_out.[Type] = '-'
现在一切都回来了。
感谢您的帮助。