Question

Would somebody please take a look at this? I am trying to sum the total weight of all of our skus, but I need them to group either by foreign or domestic. SQL 2008 server.

Select 
  IMSYS01.on_hand_qty * IMSYS01.piece_weight,
  IMSYS01.sku_no,
  case when(imsys01.warehouse = '109') then 'foreign' else 'domestic' end as 'vLocation'
From
  IMSYS01 IMSYS01
Where
  (IMSYS01.sku_no < '99999' And
  IMSYS01.velo_code <> 'X')
Group By
 IMSYS01.on_hand_qty * IMSYS01.piece_weight, 'vLocation', IMSYS01.sku_no
Was it helpful?

Solution

Oh, the dreaded use of single quotes for an identifier. Don't use single quotes for an identifier. This is what you want:

Select IMSYS01.on_hand_qty * IMSYS01.piece_weight,
       IMSYS01.sku_no,
       (case when(imsys01.warehouse = '109') then 'foreign' else 'domestic' end) as vLocation
From IMSYS01 IMSYS01
Where (IMSYS01.sku_no < '99999' And IMSYS01.velo_code <> 'X')
Group By IMSYS01.on_hand_qty * IMSYS01.piece_weight, 
         (case when(imsys01.warehouse = '109') then 'foreign' else 'domestic' end),
         IMSYS01.sku_no;

This is a great example of why you should never use single quotes for identifiers. It causes confusion. SQL Server supports other methods, but you don't even need to escape vlocation. It is a fine identifier just by itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top