I've been working on a report and I cant use @variable. So I'm trying to solve my problem with just "simple" SQL.
The problem I have is that I have one table (DEPTH
) containing all locations on a warehouse. But this table doesn't tell if the location is ocupied or not. To find out I need to use "where in" or "where not in" in table (PRODUCTLOCATION
)
I would like to have this as one output instead of running 2 queries.
With UNION
I get what I want but the format is wrong.
So LoationCount result should be where exist in 1 column and where not exist in another column
I tried so many things. As you can see I want this grouped by EquipmentType and so on
Here is my script as it is right now.
select EquipmentType, EquipmentName, LocationName, LocationCount, Used_Or_UnUsed, LocationID, LocationType
from
(select et.equiptext EquipmentType,
e.EQUIPTEXT EquipmentName,
s.SIZETEXT LocationName, count(*) LocationCount, 'Empty Locations' as Used_Or_UnUsed, s.sizeid LocationID ,
case when d.LOCATIONTYPE=1 then 'Pick Location' when d.LOCATIONTYPE=2 then 'Buffer Location' else 'Unknown' end as LocationType
from depth d with (nolock)
join SIZEPARAM s with (nolock) on d.SIZEID=s.SIZEID
join POSITIONS p with (nolock) on d.POSITIONID=p.POSITIONID
join shelf sh with (nolock) on p.SHELFID=sh.SHELFID
join EQUIPMENT e with (nolock) on sh.EQUIPID=e.EQUIPID
join EQUIPMENTTYPE et with (nolock) on e.EQUIPTYPEID=et.EQUIPTYPEID
where d.DEPTHID not in (select depthid from productlocation)
and d.SIZEID in (select SIZEID from SIZEPARAM where SizeCategoryID in (10,20,30,40,50,60,70,210))
group by et.EQUIPTEXT,e.EQUIPTEXT, d.SIZEid, s.SIZEID, s.SIZETEXT, d.LOCATIONTYPE
) t
group by t.EquipmentType, t.EquipmentName, t.LocationName, t.LocationCount, t.Used_Or_UnUsed, t.LocationID, t.LocationType
union all
select EquipmentType, EquipmentName, LocationName, LocationCount, Used_Or_UnUsed, LocationID, LocationType
from
(select et.equiptext EquipmentType,
e.EQUIPTEXT EquipmentName,
s.SIZETEXT LocationName, count(*) LocationCount, 'Used Locations' as Used_Or_UnUsed, s.sizeid LocationID ,
case when d.LOCATIONTYPE=1 then 'Pick Location' when d.LOCATIONTYPE=2 then 'Buffer Location' else 'Unknown' end as LocationType
from depth d with (nolock)
join SIZEPARAM s with (nolock) on d.SIZEID=s.SIZEID
join POSITIONS p with (nolock) on d.POSITIONID=p.POSITIONID
join shelf sh with (nolock) on p.SHELFID=sh.SHELFID
join EQUIPMENT e with (nolock) on sh.EQUIPID=e.EQUIPID
join EQUIPMENTTYPE et with (nolock) on e.EQUIPTYPEID=et.EQUIPTYPEID
where d.DEPTHID in (select depthid from productlocation)
and d.SIZEID in (select SIZEID from SIZEPARAM where SizeCategoryID in (10,20,30,40,50,60,70,210))
group by et.EQUIPTEXT,e.EQUIPTEXT, d.SIZEid, s.SIZEID, s.SIZETEXT, d.LOCATIONTYPE
) t
group by t.EquipmentType, t.EquipmentName, t.LocationName, t.LocationCount, t.Used_Or_UnUsed, t.LocationID, t.LocationType
order by t.EquipmentName asc
Please forgive my crappy formatting of this question.