I have three tables
POS_ITEM ( have 4 columns [ITEM_col1,ITEM_col2,ITEM_col3,ITEM_col4])
POS_MAP ( have 3 columns [MAP_col1, MAP_col2,MAP_col3])
POS_DIS ( have 5 columns [DIS_col1,DIS_col2.DIS_col3,DIS_col4,DIS_col5])
I have to perform a left outer join from POS_ITEM to POS_MAP, which I've been able to do. But now I have to join this result with a third table, POS_DIS.
I tried this
select b.MAP_col2,a.ITEM_col1,a.ITEM_col2,a.ITEM_col3
FROM POS_ITEM as b
left outer JOIN POS_MAP as a on a.ITEM_col1=b.MAP_col2 as h
left Outer JOIN POS_DIS as d on d.DIS_col1=h.MAP_col2 ;
but it isn't working.
I've tried this
(select b.MAP_col2,a.ITEM_col1,a.ITEM_col2,a.ITEM_col3
FROM POS_ITEM as b
left outer JOIN POS_MAP as a on a.ITEM_col1=b.MAP_col2) as h
left Outer JOIN POS_DIS as d on d.DIS_col1=h.MAP_col2 ;
But this fails saying that "(" is not a valid character.
Is this not possible with Sqlite? If so, what am I doing wrong? If not, what are my alternatives?
finally cracked it...the result will be appending...no need to add braces
select b.MAP_col2,a.ITEM_col1,a.ITEM_col2,a.ITEM_col3
FROM POS_ITEM as b
left outer JOIN POS_MAP as a on a.ITEM_col1=b.MAP_col2
left Outer JOIN POS_DIS as d on d.DIS_col1=b.MAP_col2 ;
Related
I have a large query which combines a lot of tables. I don't want to make this question to complicate so I will try to ask it easy:
suppose I have 3 columns:
table1_device_id, table2_device_id, table3_device_id
I know want to achieve this:
Do a left join on table1_device_id, if that column is null, do a left join on table2_device_id,.. (same for table 3..)
I have following example for 1 table now:
left join query_505727 as inside_weather_data on y.datetime_weather = inside_weather_data.time AND (a.monitor_id is null or a.monitor_id = inside_weather_data.device_id)
but how to do that for multiple?
my query:
select y.datetime_weather, a.site_name as site_name,a.name as house_name, a.name1 as floor_name,a.airspace_name as room_name, a.registration,a.monitor_id, device_color_table.device_id as device_color_device_id, inside_weather_data.device_id as weather_device_id, rdi_data.device_id as rdi_device_id, a.monitor_code as monitor_code, a.alarm_type,a.created_at_date as alarm_date,device_color_table.colors as LED_color_status, rdi_data.value as RDI, ROUND(inside_weather_data.min_inside_temp, 2) as min_inside_temp, ROUND(inside_weather_data.max_inside_temp, 2) as max_inside_temp, y.max_temp as max_outside_temp, y.min_temp as min_outside_temp, y.average_temp as avg_temp, a.alarm_type, a.priority, a.translation_code,a.created_at
from query_505641 as y
left join query_505127 as a on a.created_at_date = y.datetime_weather
left join query_505241 as device_color_table on device_color_table.datetime = y.datetime_weather AND device_color_table.device_id = a.monitor_id
left join query_506556 as rdi_data on rdi_data.time = y.datetime_weather and rdi_data.device_id = a.monitor_id AND device_color_table.device_id is null
left join query_505727 as inside_weather_data on y.datetime_weather = inside_weather_data.time AND inside_weather_data.device_id = a.monitor_id AND coalesce(device_color_table.device_id, rdi_data.device_id) is null
If the table that you want to join to these 3 tables is table then use these LEFT joins:
....
from table t
left join table1 t1 on t1.table1_device_id = t.device_id
left join table2 t2 on t2.table2_device_id = t.device_id and t1.table1_device_id is null
left join table3 t3 on t3.table3_device_id = t.device_id and coalesce(t1.table1_device_id, t2.table2_device_id) is null
I have some data about shops that looks something like:
where the unimportant data is omitted.
I have an existing SQLite query:
select s.COMSERNO, s.COMNAME, COMALTNAME, s.COMCODE, s.COMADDR, s.COMPCODE, s.COMCITY, x.lastVisit, y.memoText, l.COMCALL
from TCOMPANY s
left outer join ( select COMSERNO, max(CALEDATE || CALESTART) as lastVisit from TCALENTR where SALMSERN='000000000000019' and (CALEDATE || CALESTART) < strftime('%Y%m%d%H%M', 'now', 'localtime') group by COMSERNO) x on (x.COMSERNO=s.COMSERNO)
left outer join ( select MEMOSERN1 as comSerno, max(MEMOTEXT) as memoText from TMEMO where MEMOTYPE='0' and 0<length(MEMOTEXT) and MEMOSERN2 in ('Notes')group by MEMOSERN1) y on (y.comSerno=s.COMSERNO)
left outer join LSALCOM l on l.COMSERNO=s.COMSERNO
and SALMSERN='000000000000019'
order by s.COMNAME, s.COMSERNO
The above returns 174 rows.
I want to show the 'Chain' for a shop, if it has one, so the data should look something like this:
I have another table, LCOMCOM and for each COMSERNO in my original result set (each shop) I need to determine if this shop is part of a chain and if it is, display that chain.
I join LCOMCOM on COMSERN2, and if the column LCOMATR1 has the value 'Chain' then it means the store COMSERN2 is a chain. I then take COMSERN1 from LCOMCOM and use the original table, TCOMPANY to look up and show the Chain name. Here is my code
case when z.lcomatr1 = 'Chain' then (select p.COMNAME from TCOMPANY p inner join LCOMCOM q on (p.COMSERNO=q.COMSERN2) where q.COMSERN1 = z.comsern1) else '' end as Chain
Here is the LCOMCOM join:
inner join LCOMCOM z on (s.COMSERNO=z.COMSERN2)
I tried adding the above two pieces into my query but it doesn't do what I expected:
select s.COMSERNO, s.COMNAME, COMALTNAME, s.COMCODE, s.COMADDR, s.COMPCODE, s.COMCITY, x.lastVisit, y.memoText, l.COMCALL,
case when z.lcomatr1 = 'Chain' then (select p.COMNAME from TCOMPANY p inner join LCOMCOM q on (p.COMSERNO=q.COMSERN2) where q.COMSERN1 = z.comsern1) else '' end as Chain
from TCOMPANY s
left outer join ( select COMSERNO, max(CALEDATE || CALESTART) as lastVisit from TCALENTR where SALMSERN='000000000000019' and (CALEDATE || CALESTART) < strftime('%Y%m%d%H%M', 'now', 'localtime') group by COMSERNO) x on (x.COMSERNO=s.COMSERNO)
left outer join ( select MEMOSERN1 as comSerno, max(MEMOTEXT) as memoText from TMEMO where MEMOTYPE='0' and 0<length(MEMOTEXT) and MEMOSERN2 in ('Notes')group by MEMOSERN1) y on (y.comSerno=s.COMSERNO)
left outer join LSALCOM l on l.COMSERNO=s.COMSERNO
inner join LCOMCOM z on (s.COMSERNO=z.COMSERN2)
and SALMSERN='000000000000019'
order by s.COMNAME, s.COMSERNO
The above returns 548 rows, not 174. Some shops are now in 4 rows, like so:
And some shops are now in 2 rows, like so:
Adding DISTINCT after the initial SELECT reduces the row count to 348 and it now looks like all the Chain stores are in 2 rows.
I'm not sure what's going on here but presumably referring to TCOMPANY twice in both the SELECTs is wrong. Can anybody tell me how to get 174 rows, with the chain, if appropriate?
The CASE statement wasn't needed and the INNER JOIN was moved into the bracketed SELECT:
select DISTINCT s.COMSERNO, s.COMNAME, COMALTNAME, s.COMCODE, s.COMADDR, s.COMPCODE, s.COMCITY, x.lastVisit, y.memoText, l.COMCALL,
(select p.comname from LCOMCOM q inner join TCOMPANY p on (p.COMSERNO=q.COMSERN1) where q.COMSERN2 = s.comserno and q.lcomatr1 = 'Chain') as Chain
from TCOMPANY s
left outer join ( select COMSERNO, max(CALEDATE || CALESTART) as lastVisit from TCALENTR where SALMSERN='000000000000019' and (CALEDATE || CALESTART) < strftime('%Y%m%d%H%M', 'now', 'localtime') group by COMSERNO) x on (x.COMSERNO=s.COMSERNO)
left outer join ( select MEMOSERN1 as comSerno, max(MEMOTEXT) as memoText from TMEMO where MEMOTYPE='0' and 0<length(MEMOTEXT) and MEMOSERN2 in ('Notes')group by MEMOSERN1) y on (y.comSerno=s.COMSERNO)
left outer join LSALCOM l on l.COMSERNO=s.COMSERNO
and SALMSERN='000000000000019'
order by s.COMNAME, s.COMSERNO
In following query, what is happening is that, the 3rd join is not being done. we are getting pharmacy match and then the display is showing patients in other facilities who share the same pharmacy, can you see why this would be happening?
Insert Into #tblNDC
SELECT ROW_NUMBER() OVER(ORDER BY ID_KEY DESC) AS RN,*
From
(
Select distinct A.PHARMACYNPI,
f.FACILITY_NAME,
ID_KEY,
[BATCH] AS column1,
[IMPORTDATE],
[DATEBILLED],
[RX],
[DATEDISPENSED],
[DAYSUPPLY],
[PAYTYPE],
A.[NPI],
[PHYSICIAN],
[COST],
[QUANTITY],
[MEDICATION],
A.[NDC],
f.FACILITY_ID
FROM [PBM].[T_CHARGES] A
LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON A.[NDC] = B.[NDC]
--Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI
Left Outer Join PBM.PHARMACY_NPI pn on A.PHARMACYNPI = pn.NPI
Inner join PBM.PHARMACY_FACILITY pp on pn.PHARMACY_ID = pp.PHARMACY_ID
Inner Join PBM.FACILITY f on pp.FACILITY_ID = f.FACILITY_ID
Where [STAT] not in (3, 4, 5)
AND [TIER] <> 'T1'
AND f.FACILITY_ID IN
(
select FacilityID from #tblFacility
)
AND f.FACILITY_ID IN
(
SELECT * FROM [PBM].[Split1] (#selectedFacility)
)
--- it seems 3rd condition not being done ----------------------------------
Its hard to see for sure without knowing the data, but my first thoughts are that the Left Outer joins will be giving you joinage you might not want.
Go through each join and remove it until you start to get dodgy records back, if it is the 3rd one when it suddenly goes strange, then i suspect you have multiple IDs for a facility.
I need to join 4 tables based on a common primary key. If sqlite implemented full outer joins it might look something like this (with optimization not taken into account).
SELECT S.pair, C.ball, P.bluejeans, B.checkered
FROM Socks S
FULL OUTER JOIN Caps C
FULL OUTER JOIN Pants P
FULL OUTER JOIN Boxers B
WHERE S.color = C.color AND S.color = P.color AND S.color = B.color;
I've looked long and hard and the best I found was this 2 table sqlite full join implemented with left joins and union alls:
SELECT employee.*, department.*
FROM employee LEFT JOIN department
ON employee.DepartmentID = department.DepartmentID
UNION ALL SELECT employee.*, department.*
FROM department LEFT JOIN employee
ON employee.DepartmentID = department.DepartmentID
WHERE employee.DepartmentID IS NULL;
I'm trying to modify this to work for more than 2 tables but I'm new to SQL and I'm not getting too far. Is it possible to get this result in a reasonable amount of time?
I think I have a correct implementation for 3 tables (it might not be correct) but I still can't seem to get it for 4. Here's what I have for 3:
SELECT S.pair, C.ball, P.bluejeans
FROM Socks S LEFT JOIN Caps C LEFT JOIN Pants P
ON C.color = S.color AND P.color = S.color
UNION ALL
SELECT S.pair, C.ball, P.bluejeans
FROM Socks S LEFT JOIN Caps C LEFT JOIN Pants P
ON S.color = C.color AND S.color = P.color
WHERE S.color IS NULL;
Any help is much appreciated
The general construction for a full outer join between two tables A and B in SQLite indeed is:
SELECT ... FROM A LEFT JOIN B ON ...
UNION ALL
SELECT ... FROM B LEFT JOIN A ON ... WHERE A.key IS NULL
Now create a view SocksCaps for the full outer join between Socks and Caps:
CREATE VIEW SocksCaps AS
SELECT ... FROM Socks LEFT JOIN Caps ON ...
UNION ALL
SELECT ... FROM Caps LEFT JOIN Socks ON ... WHERE Socks.color IS NULL
Do the same for Pants and Boxers.
Then treat these views just like tables and do a full outer join with the same construction:
SELECT ... FROM SocksCaps LEFT JOIN PantsBoxers ON ...
UNION ALL
SELECT ... FROM PantsBoxers LEFT JOIN SocksCaps ON ... WHERE SocksCaps.color IS NULL
The sql statement below will not run in SQLite:
select *
from A
left join (B inner join C on B.fkC = C.pk) on A.optionalfkB = B.pk
I get a sqlException "unknown column B.pk"
According to the documentation # http://www.sqlite.org/lang_select.html this should work, and it will work in all other sql implementations. Am I doing something wrong?
It doesn't work because the "outer" query doesn't know what B is.
select *
from A
left join (B inner join C on B.fkC = C.pk) B on A.optionalfkB = B.pk
The (B inner join C on B.fkC = C.pk) is weird without any select, but the specification does say that it is valid.