How to do Conditional Column Joins in SQL? - sql

I have a tables below
Main Table: tblMain
Phase Country City
AAA India Bangalore
AAA USA Chicago
ZZZ USA
ZZZ UK
SubTable: tblSub
Phase Country City Value
AAA USA Chicago 3
AAA USA NY 6
AAA UK London 5
AAA India Bangalore 6
AAA India Delhi 9
ZZZ USA Chicago 7
ZZZ UK London 8
Expected Result
Phase Country City Value
AAA India Bangalore 6
AAA USA Chicago 3
ZZZ USA 7
ZZZ UK 8
I want to join this with my Main table which has Phase, Country and City, however the condition is
For Phase "ZZZ" i want to join only by Country where as for Phase "AAA" i want to join ty Country & City. Is is possible to do in SQL Query without a Stored procedure or temp tables
I am looking to achieve this in plain query. Thanks in advance!!!

SELECT *
FROM This_Table TT
LEFT JOIN Main_Table MT ON TT.Countrry = MT.Country
AND TT.Phase = 'ZZZ'
LEFT JOIN Main_Table MT2 ON TT.Countrry = MT2.Country
AND TT.City = MT2.City
AND TT.Phase = 'AAA'

This should do it:
WHERE
(a.phase = 'ZZZ' AND a.country = b.country)
OR
(a.phase = 'AAA' AND a.country = b.country AND a.city = b.city)

Related

SQL Select inner join with multiple value

How do I get all the values in one select statement?
SELECT CLIENT.name,
CLIENT.province_id, CANADA.name as province_name,
CLIENT.city_id, CANADA.name as city_name
FROM ((CLIENT
INNER JOIN CANADA as ON CLIENT.province_id = CANADA.id)
INNER JOIN CANADA as ON CLIENT.city_id = CANADA.id)
WHERE CLIENT
Province_name & city_name refer to the same column and identify using ID.
CANADA:
CANADA_id
name
id
parent_id
1
Canada
33
0
2 (Province)
Alberta
1100
33
3 (Province)
British Columbia
1200
33
4 (city)
Banff
1101
1100
5 (city)
Calgary
1102
1100
6 (city)
Victory
1201
1200
7 (city)
Vancouver
1202
1200
I would like to return:
name
province_id
province_name
city_id
city_name
John
1100
Alberta
1102
Calgery
SELECT CLIENT.name,
CLIENT.province_id, CANADA_province.name as province_name,
CLIENT.city_id, CANADA_city.name as city_name
FROM CLIENT
INNER JOIN CANADA as CANADA_province
ON CLIENT.province_id = CANADA_province.id
INNER JOIN CANADA as CANADA_city
ON CLIENT.city_id = CANADA_city.id
WHERE CLIENT.name IS NOT NULL;

SQL query combine rows based on common id

I have a table with the below structure:
MID
FromCountry
FromState
FromCity
FromAddress
FromNumber
FromApartment
ToCountry
ToCity
ToAddress
ToNumber
ToApartment
123
USA
Texas
Houston
Well Street
1
Japan
Tokyo
6
ET3
123
Germany
Bremen
Bremen
Nice Street
4
Poland
Warsaw
9
ET67
456
France
Corsica
Corsica
Amz Street
3
Italy
Milan
8
AEC784
456
UK
UK
London
G Street
2
Portugal
Lisbon
1
LP400
The desired outcome is:
MID
FromCountry
FromState
FromCity
FromAddress
FromNumber
FromApartment
ToCountry
ToCity
ToAddress
ToNumber
ToApartment
FromCountry1
FromState1
FromCity1
FromAddress1
FromNumber1
FromApartment1
ToCountry1
ToCity1
ToAddress1
ToNumber1
ToApartment1
123
USA
Texas
Houston
Well Street
1
Japan
Tokyo
6
ET3
Germany
Bremen
Bremen
Nice Street
4
Poland
Warsaw
9
ET67
456
France
Corsica
Corsica
Amz Street
3
Italy
Milan
8
AEC784
UK
UK
London
G Street
2
Portugal
Lisbon
1
LP400
What I am trying to achieve is to bring multiple rows in 1 table, which have the same MID, under 1 row, regardless if there are columns with empty values.
I think that i over complicated the solution to this as I was trying something like this (and of course the outcome is not the desired one):
select [MID],
STUFF(
(select concat('', [FromCountry])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromCountry
,stuff (
(select concat('', [FromState])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromState
,stuff (
(select concat('', [FromCity])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromCity
,stuff (
(select concat('', [FromAddress])
FROM test i
where i.[MID] = o.[MID]
for xml path ('')),1,1,'') as FromAddress
FROM test o
group by [MID]
...
Is there any way to achieve this?
On the assumption there are no more than 2 rows per MID then you can implement a simple row_number() solution.
You need to join one row for each MID to the other, so assign a unique value to each using row_number - there's nothing I can immediately see that indicates which row should be the "second" row - this is assigning row numbers based on the FromCountry - amend as necessary.
I'm not reproducing all the columns here but you get the idea, rinse and repeat for each column.
with m as (
select *, Row_Number() over(partition by Mid order by FromCountry) seq
from t
)
select m.Mid,
m.fromcountry, m.fromstate,
m2.fromcountry FromCountry1, m2.fromstate FromState1
from m
join m m2 on m.mid = m2.mid and m2.seq = 2
where m.seq = 1;
See example fiddle

SQL multiple columns value into one column

Here is my table design
Location Inventory
ID Name HostName LID1 LID2 LID3
1 AAA Peter 1 2 3
2 BBB Betty 2
3 CCC Charlie 1 2
As my expected result is like this below.
HostName Name
Peter AAA
BBB
CCC
Betty BBB
Charlie AAA
BBB
But I run the sql statement not like this.
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID1 = location.ID
UNION
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID2 = location.ID
UNION
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID3 = location.ID
HostName Name
Peter AAA
Peter BBB
Peter CCC
Betty BBB
Charlie AAA
Charlie BBB
Anyone can help me to solve the problem?
Thanks.

SQL Server display left join result set as additional columns instead of rows

I am working with SQL Server 2012, I have a two tables, Prospects and ProspectContact, with this sample data:
ProspectID ProspectName ProspectAdd
-----------------------------------------------
1 ABC Company India
2 XYZ Company UAE
3 PQR Company KSA
4 JKL Company INDIA
ProspectContacts table:
ProspectID CName CAdd CDesignation Email
---------------------------------------------------------------------------
1 Mr Sagar India Manager sagar#gmail.com
1 Mr Saurabh India Manager Saurabh#gmail.com
2 Mr Swami UAE Director swami#gmail.com
2 Mr .ABC UAE Engg abc#gmail.com
3 Mr PQR KSA Manager pqr#gmail.com
4 Mr XYZ INDIA Manager xyz#gmail.com
If I apply left join on the above tables with below query
SELECT
P.[PROSPECTNAME], P.[ADDRESS], PC.CONTACTPERSON, PC.EMAILID
FROM
[PROSPECTS] P
LEFT JOIN
[PROSPECTCONTACTS] PC ON P.PROSPECTID = PC.PROSPECTID
WHERE
P.USERID = #UserID
ORDER BY
CDATE DESC
I get this result set:
ProspectName Adress Name Email
---------------------------------------------------
ABC COMPANY INDIA Mr. Sagar sagar#gmail.com
ABC COMPANY INDIA Mr. Saurabh saurabh#gmail.com
whereas my expected result set would be this:
ProspectName Adress Name1 Email1 Name2 Email2
-----------------------------------------------------------------------------
ABC COMPANY INDIA Mr. Sagar sagar#gmail.com Mr.Saurabh saurabh#gmail.com
XYZ COMPNAY USA Mr.Swami swami#gmail.com Mr. ABC abc#gmail.com
PQR COMPANY KSA Mr.Pqr pqr#gmail.com NULL NULL
JKL COMPNAY INDIA Mr.XYZ xyz#gmail.com NULL NULL
Try something like this:
SELECT P.[PROSPECTNAME], P.[ADDRESS], PC.CONTACTPERSON, PC.EMAILID
FROM [PROSPECTCONTACTS] PC
LEFT JOIN [PROSPECTS] P
ON P.PROSPECTID=PC.PROSPECTID
WHERE P.USERID=#UserID ORDER BY CDATE Desc

Check for an entry in SQL Server

-----------------------
country | city | ids
-----------------------
India Mumbai 1
India Chennai 2
India Kolkata 3
---------------------
USA New York 2
USA Utah 3
---------------------
I have given a sample from a table. From the table, I am trying to query all the countries without id 1. I wrote this(Country was not included in the Where condition since it needs to apply to all the countries of the table).
Select * from Countries
WHERE id<>1
I got this.
-----------------------
country | city | ids
-----------------------
India Chennai 2
India Kolkata 3
---------------------
USA New York 2
USA Utah 3
---------------------
But I need the output to contain only USA(which does not have id=1). Is there any workaround for this?
SELECT * from Countries WHERE country not in
(SELECT country from Countries WHERE id=1)
use NOT EXISTS
Select *
from Countries c
where not exists
(
select *
from Countries x
where x.country = c.country
and x.id = 1
)
You need to group by Country like below :
SELECT C.Country
FROM City
WHERE C.Country NOT IN
(SELECT country FROM City WHERE id=1)
SQL Fiddle Demo
OR
SELECT
C.Country
FROM
City C
GROUP BY C.Country
HAVING C.Country NOT IN
(
SELECT Country FROM City WHERE Id =1
)
SQL Fiddle Demo