SQL Join issue with 3 tables and a subquery - sqlplus

Tables Involved:
account, user, service, accesshist
I want to include all records in the account table, and only the data from the other tables when it exists.
Count from account: 5064
rows returned from query below: 4915
select u.last_name, u.first_name, a.username, ll.mxlogin, si.servicename, a.islockedout
from account a
join service si on a.serviceid = si.serviceid
left outer join user u on u.loginid = a.username
left outer join(select max(loginattemptdate) as MxLogin, usernameattempted from accesshist where isloginsuccessful = 1
group by usernameattempted) ll
on a.username = ll.usernameattempted
where a.isenabled = 1
order by ll.mxlogin, u.last_name
I've narrowed it down that the subquery join is the part causing the number of rows to be reduced, but I am unsure how to correct it. Any insight is greatly appreciated!

Have you tried changing the first join to a left outer join?
select u.last_name, u.first_name, a.username, ll.mxlogin, si.servicename, a.islockedout
from account a
left outer join service si on a.serviceid = si.serviceid

Related

I want to use mysql cross join between examination tbl & Contineous Assesment tbl with a WHERE clause but it always turns to inner join

[examTableCAtableI want to use mysql cross join between examination tbl & Contineous Assesment tbl with a WHERE clause.
I want to get record of a student from both tables. I try left join exam on CA and it works but the issue is when the student wrote CA but doesn't have exam, his CA will not be counted.
I want to join the two tables so that it can count for student with either exam or CA or both.
This is my left join query:
SELECT examscore.stuid,
examscore.subcode,
examscore.score AS examscore,
csscore.score,
SUM(examscore.score + IFNULL(csscore.score, 0)) AS totalscore
FROM examscore
LEFT JOIN csscore on examscore.stuid = csscore.stuid
and examscore.subcode = csscore.subcode
where examscore.term = 'FIRST'
and examscore.class = 'jss1a'
GROUP BY examscore.stuid;
The above query works only when the student has written the exam.
I try cross join but if i use where clause, it turns to inner join.
Please how can i achieved it.
Thanks

Optimizing SQL query using JOIN instead of NOT IN

I have a sql query that I'd like to optimize. I'm not the designer of the database, so I have no way of altering structure, indexes or stored procedures.
I have a table that consists of invoices (called faktura) and each invoice has a unique invoice id. If we have to cancel the invoice a secondary invoice is created in the same table but with a field ("modpartfakturaid") referring to the original invoice id.
Example of faktura table:
invoice 1: Id=152549, modpartfakturaid=null
invoice 2: Id=152592, modpartfakturaid=152549
We also have a table called "BHLFORLINIE" which consists of services rendered to the customer. Some of the services have already been invoiced and match a record in the invoice (FAKTURA) table.
What I'd like to do is get a list of all services that either does not have an invoice yet or does not have an invoice that's been cancelled.
What I'm doing now is this:
`SELECT
dbo.BHLFORLINIE.LeveringsDato AS treatmentDate,
dbo.PatientView.Navn AS patientName,
dbo.PatientView.CPRNR AS patientCPR
FROM
dbo.BHLFORLINIE
INNER JOIN dbo.BHLFORLOEB
ON dbo.BHLFORLOEB.BhlForloebID = dbo.BHLFORLINIE.BhlForloebID
INNER JOIN dbo.PatientView
ON dbo.PatientView.PersonID = dbo.BHLFORLOEB.PersonID
INNER JOIN dbo.HENVISNING
ON dbo.HENVISNING.BhlForloebID = dbo.BHLFORLOEB.BhlForloebID
LEFT JOIN dbo.FAKTURA
ON dbo.BHLFORLINIE.FakturaId = FAKTURA.FakturaId
WHERE
(dbo.BHLFORLINIE.LeveringsDato >= '2017-01-01' OR dbo.BHLFORLINIE.FakturaId IS NULL) AND
dbo.BHLFORLINIE.ProduktNr IN (110,111,112,113,8050,4001,4002,4003,4004,4005,4006,4007,4008,4009,6001,6002,6003,6004,6005,6006,6007,6008,7001,7002,7003,7004,7005,7006,7007,7008) AND
((dbo.FAKTURA.FakturaType = 0 AND
dbo.FAKTURA.FakturaID NOT IN (
SELECT FAKTURA.ModpartFakturaID FROM FAKTURA WHERE FAKTURA.ModpartFakturaID IS NOT NULL
)) OR
dbo.FAKTURA.FakturaType IS NULL)
GROUP BY
dbo.PatientView.CPRNR,
dbo.PatientView.Navn,
dbo.BHLFORLINIE.LeveringsDato`
Is there a smarter way of doing this? Right now the added the query performs three times slower because of the "not in" subquery.
Any help is much appreciated!
Peter
You can use an outer join and check for null values to find non matches
SELECT customer.name, invoice.id
FROM invoices i
INNER JOIN customer ON i.customerId = customer.customerId
LEFT OUTER JOIN invoices i2 ON i.invoiceId = i2.cancelInvoiceId
WHERE i2.invoiceId IS NULL

getting the count of another table values in existing query Rails

Currently I am doing this to get data from my tables as
cameras = Camera.joins("left JOIN users on cameras.owner_id = users.id")
.joins("left JOIN vendor_models vm on cameras.model_id = vm.id")
.joins("left JOIN vendors v on vm.vendor_id = v.id")
.where(condition).order(sorting(col_for_order, order_for)).decorate
And this generates such query as
SELECT "cameras".* FROM "cameras" left JOIN users on cameras.owner_id = users.id left JOIN vendor_models vm on cameras.model_id = vm.id left JOIN vendors v on vm.vendor_id = v.id
I have an other table as camera_shares on which I have relation of camera's table as there is a camera_id which is present in camera_shares table as well, in each camera_shares there is an camera_id which tell us that the share is for which camera, I want to calculate the count of camera_shares, For example if there is a camera with id 12 and there are also 20 camera_shares with camera_id = 12 then I want to count the total of it. I want to do that in Rails query as I have shown above? How is that possible to do?
Seems rather straightforward. Suppose you want to find the number of camera_shares for #camera:
CameraShare.where(camera: #camera).count

Solr5.4 indexing using DataImportHandler(joins)

I am using Solr 5.4. Working on indexing entities using Data Import Handler which are having inner joins and left joins as below.
Does solr5.4 supports inner joins and left joins?
I am having a query which has relationship between 7 tables. I am doing inner joins for few and left joins for few tables.
below is my query declared in data-config.xml file.
SELECT rel.*,
PRODUCT_SRC_SYS.PRODUCT_SRC_NME AS sourceName
FROM (SELECT REF.*,
PRODUCT_RLSP.OBJ_ID AS relObjId
FROM (SELECT prdct.*,
PRODUCT_CATEGORY.OBJ_ID AS refObjId
PRODUCT_CATEGORY_MAPG.category_mapng_Name
AS refCategoryMapngName,
PRODUCT_CATEGORY_LIFE_CYL.LIFE_CYL_STAT_TYP
AS refStatus
FROM (SELECT PRODUCT.RGSTRY_ID
AS id
PRODUCT.OBJ_ID
AS obj_id,
PRODUCT.PRODUCT_NAME
As product_name,
PRODUCT_STAT.PRODUCT_STAT_TYP
AS status
FROM PRODUCT
LEFT JOIN PRODUCT_STAT
ON PRODUCT.OBJ_ID =
PRODUCT_STAT.OBJ_ID)
prdct
LEFT JOIN PRODUCT_CATEGORY
ON prdct.product_name =
PRODUCT_CATEGORY.category_name
INNER JOIN PRODUCT_CATEGORY_MAPG
ON PRODUCT_CATEGORY.OBJ_ID =
PRODUCT_CATEGORY_MAPG.OBJ_ID
INNER JOIN PRODUCT_CATEGORY_LIFE_CYL
ON PRODUCT_CATEGORY.OBJ_ID =
PRODUCT_CATEGORY_LIFE_CYL.OBJ_ID)
REF
LEFT JOIN PRODUCT_RLSP
ON REF.obj_id =
PRODUCT_RLSP.OBJ_ID) rel
LEFT JOIN PRODUCT_SRC_SYS
ON rel.obj_id = PRODUCT_SRC_SYS.OBJ_ID
Any help is highly appreciated!!

Number of joins in select query

I have a business table and in that we have 50 foreign key columns which refers other master data tables.
to fetch all the data my query has to join all the 50 reference tables like
select ct.id , ct.name , ct.description , st.value , pr.value , sv.value , ....
from
core_table ct
left outer join domain_value st on ct.status_fk = st.id
left outer join domain_value pr on ct.priority_fk = pr.id
left outer join domain_value svon ct.severity_fk = sv.id
.......
.......
so like this i need to make 50 left outer joins.
is this right to do 50 left outer joins like this or do we have any other optimized way to achieve this ?
Is too many Left Joins a code smell?
It's a perfectly legitimate solution for some designs.

Resources