I'm using SQL Server and I have a table with columns:
Id, State1, State2, State3, City1, City2, City3, Street1, Street2, Street3.
Where State1, City1, Street1 are a "triple".
I need to select all data from the table where
(State1 = xyz, City1 = xyzv, Street1 = abcd) or (State2 = xyz....)
and show it like that: State1 as State/State2 as State -> the same name for each triple columns.
I need to say that each triple in row is unique or null. So there can be 1,2,3 unique addresses in row.
How can I do it? Now I have something like that, but there is an
Msg 116, Level 16, State 1, Line 14 Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. error.
select ds.Data as Data,
case
when ds.Gmina1 = @StateName and ds.Miejscowosc1 = @CityName and ds.Street1 = @StreetName and ds.LocalNumber1 = @LocalNumber then (select ds.Gmina1 as Gmina, ds.Miejscowosc1 as Miasto, ds.Street1 as Ulica, ds.LocalNumber1 from Dostawy)
when ds.Gmina2 = @StateName and ds.Miejscowosc2 = @CityName and ds.Street2 = @StreetName and ds.LocalNumber2 = @LocalNumber then (select ds.Gmina2 as Gmina, ds.Miejscowosc2 as Miasto, ds.Street2 as Ulica, ds.LocalNumber2 from Dostawy)
when ds.Gmina2 = @StateName and ds.Miejscowosc3 = @CityName and ds.Street3 = @StreetName and ds.LocalNumber3 = @LocalNumber then (select ds.Gmina3 as Gmina, ds.Miejscowosc3 as Miasto, ds.Street3 as Ulica, ds.LocalNumber3 from Dostawy)
else null
end
from dbo.Dostawy as ds
I need to select all data from the table **where**...- perhaps it's better to put those conditions intoWHEREclause?