I am trying to pull data from 3 different tables and my result set is not what I'm expecting.
SELECT mdp.ReportDate
, mdp.PolicyNumber
, Company
, StateCode
, LOB
, mdp.AccountReference
, EffectiveDate
, EquityDate
, AccountBalance
, TermPremium
, DelinquentAmount
, PolicyStatus
, dcbpt.PolicyTermExtendedData
, TermsInDays
, dcba.AccountId
FROM Bil_MonthlyDelinquentPayments mdp
INNER JOIN DC_BIL_Account AS dcba
ON PolicyNumber = dcba.AccountReference
AND ReportDate = (
SELECT Max(ReportDate)
FROM Bil_MonthlyDelinquentPayments maxmdp
WHERE Year(maxmdp.ReportDate) = 2017
AND Month(maxmdp.ReportDate) = 01
)
LEFT JOIN DC_BIL_PolicyTerm AS dcbpt
ON dcba.AccountId = dcbpt.PrimaryAccountId
AND PolicyTermEffectiveDate = (
SELECT Max(PolicyTermEffectiveDate)
FROM DC_BIL_PolicyTerm
)
ORDER BY AccountId
In my result set the column dcbpt.PolicyTermExtendedData is being returned as a null value. This column contains data in the table and I would expect my result set to contain that data but it doesn't.
That null value is from the second table used in your left join. Left join will return all the results from the 1st table (on the left) and if it does not find a match in your 2nd table to join on, it will pair the first table with a null value. Take a look at what you ware matching on.
Related
I have tables:
Result containing 5 columns: result_id, num_1, num_2, num_3, num_4
Ref containing 4 columns: num_1, num_2, num_3, num_4
Columns num contain random int in range of 1-9
Aim of exercise is to display all result_id from Result table which have num values combination present in Ref table and to display result_id which have not met combination criteria.
I've been trying left joining ref to result, but unfortunately no success. Could you please share some light how to deal with it?
If you want the result_id for which combination exists in the ref table then use following JOIN query:
select distinct r.result_id
from results r
join ref on r.num_1 = ref.num_1 and r.num_2 = ref.num_2
and r.num_3 = ref.num_3 and r.num_4 = ref.num_4
If you want the result_id for which combination do not exists in REF table then use the LEFT JOIN as follows:
select r.result_id
from results r
left join ref on r.num_1 = ref.num_1 and r.num_2 = ref.num_2
and r.num_3 = ref.num_3 and r.num_4 = ref.num_4
where ref.num_1 is null -- or use PK / Not nullable column of REF table here
Assuming you want the columns to "line up" and you want to add a flag to the result_id in the first table, then use exists:
select t1.*,
(case when exists (select 1
from table2 t2
where t2.n1 = t1.n1 and t2.n2 = t1.n2 and t2.n3 = t1.n3 and t2.n4
)
then 'present' else 'not present'
end) as flag
from t2;
I am trying to pull data where a specific column is completely not null. It should only return the data if ALL of the rows in the column meet that requirement. Doing simply IS NOT NULL will not work. In short, I am trying to find contracts where all of the products on that contract has been terminated and to only return that data.
Here is what I have so far, its barebone:
SELECT
T0.CustomerCode
, T0.CustomerName
, T1.ContractID
, T1.StartDate
, T1.TerminationDate
, T2.ProductRecordID
, T2.ProductSN
, T2.CompanySN
, T2.ProductRecordStatus
FROM
T0
INNER JOIN T1 ON T0.ContractID = T1.ContractID
INNER JOIN T2 ON T1.ProductRecordID = T2.ProductRecordID
WHERE T0.ProductRecordStatus = 'A'
A solution can be to count null value in the specified column and check if this number is equal to zero.
DECLARE #nullCount int = 1;
SELECT
#nullCount = COUNT(1)
FROM
[your_table]
WHERE
[your_column] IS NULL;
IF #nullCount = 0
SELECT
T0.CustomerCode , T0.CustomerName , T1.ContractID , T1.StartDate , T1.TerminationDate ,
T2.ProductRecordID , T2.ProductSN , T2.CompanySN , T2.ProductRecordStatus
FROM T0
INNER JOIN T1 ON T0.ContractID = T1.ContractID INNER JOIN T2 ON T1.ProductRecordID =
T2.ProductRecordID
WHERE T0.ProductRecordStatus = 'A';
in this way if you have one or more null values the query is not even performed.
I dont't get it. I changed some of the code. In the WPLEVENT Table are a lot of Events per person. In the Persab-Table are the Persons with their History. Now I need the from the Persab Table just that row wich matches the persab.gltab Date nearest to the WPLEVENT.vdat Date. So all rows from the WPLEVENT, but just the one matching row from the PERSAB-Table.
SELECT
persab.name,
persab.vorname,
vdat,
eventstart,
persab.rc1,
persab.rc2
FROM wplevent
INNER JOIN
persab ON WPLEVENT.PersID = persab.PRIMKEY
INNER JOIN
(SELECT TOP 1 persab.rc1
FROM PERSAB
WHERE persab.gltab <= getdate() --/ Should be wplevent.vdat instead of getdate()
) NewTable ON wplevent.persid = persab.primkey
WHERE
persid ='100458'
ORDER BY vdat DESC
Need to use the MAX() function with the proper syntax by supplying an expression like MAX(persab.rc1). Also need to use GROUP BY for the second column rc2 in the subquery (although it looks like you do not need it). Finally you are missing the ON clause for the final INNER JOIN. I can update the answer to fix the query if you provide that information.
SELECT
Z1PERS.NAME
, Z1PERS.VORNAME
, WPLEVENT.VDat
, WPLEVENT.EventStart
, WPLEVENT.EventStop
, WPLEVENT.PEPGROUP
, Z1SGRP.TXXT
, PERSAB.GLTAB
, Z1PERS.PRIMKEY AS Expr1
, PERSAB.PRIMKEY
FROM
Z1PERS
INNER JOIN
WPLEVENT ON Z1PERS.PRIMKEY = WPLEVENT.PersID
INNER JOIN
Z1SGRP ON WPLEVENT.PEPGROUP = Z1SGRP.GRUPPE
INNER JOIN
(
SELECT MAX(Persab.rc1) --Fixed MAX expression
, persab.rc2
FROM
persab
GROUP BY
persab.rc2 --Need to group on rc2 if you want that column in the query otherwise remove this AND the rc2 column from select list
WHERE
WPLEVENT.PersID = PERSAB.PRIMKEY
AND WPLEVENT.VDat <= PERSAB.GLTAB
) --Missing ON clause for the INNER JOIN here
WHERE z1pers.vorname = 'henning'
I have 1 big table with 12 columns , 4 of this columns have fields such as : VersionID , StatusID,Owner ID etc..
I have 3 small tables for Version,Status,etc...this table contain information such as : "VersionID "1 = Active" ; VersionID 2 = Disabled...etc" the same goes for StatusID and OwnerID .
I created a Query which displays Information from the ,,Big Table" and the information that is contained in the VersionID , StatusID etc. should be displayed from the smaller tables , for example : If In the big table the VersionID displayed is "1" i want it to show the definition ..in this case "Active".
I managed to do this by using the following code:
SELECT
Object.Type, Object.ID, Object.Key, Object.IsInactive,
Version.Version AS VersionID,
Status.StatusText AS StatusID,
ObjectDetail.BalancePosition, ObjectDetail.FrequencyID,
ObjectDetail.FrequencyTimeLagID, ObjectDetail.ObjectName,
ObjectDetail.Description, ObjectDetail.ValueSpecification,
ObjectDetail.Computation, ObjectDetail.StorageSystemID,
ObjectDetail.StorageSystemField, ObjectDetail.TableFunctionalDatamodel,
ObjectDetail.OwnerID
FROM
Version
INNER JOIN
(
[Object]
INNER JOIN
(
ObjectDetail
LEFT JOIN
Status
ON ObjectDetail.[StatusID] = Status.[ID]
)
ON Object.ID = ObjectDetail.ObjectID
)
ON Version.ID = ObjectDetail.VersionID
WHERE (((Object.Type)=0) AND ((Object.ID) Is Null))
OR (((Object.Type)=0) AND ((Object.Key) Is Null))
OR (((Object.Type)=0) AND ((Object.IsInactive) Is Null))
OR (((Object.Type)=0) AND ((Version.Version) Is Null) AND ((ObjectDetail.VersionID) Is Null))
OR (((Object.Type)=0) AND ((Status.StatusText) Is Null))
OR (((ObjectDetail.BalancePosition) Is Null))
OR (((ObjectDetail.FrequencyID) Is Null))
OR (((ObjectDetail.FrequencyTimeLagID) Is Null));
This works just fine,the problem is that faulty entries such us "999" for example in the StatusID - "999" has no definition in the small table ..are not displayed. My wish is for the query to display only the fields where there are no entries or entries which do not have a definition in the small table.
so you basically wan't to find rows which have invalid values within their columns?
You can achieve this by using a left join, and only including the null results:
SELECT a.x, b.y FROM BigTable a
LEFT JOIN SmallTable1 b ON b.id = a.id
WHERE b.id IS NULL
I have a query to find not null on 2 columns on a table which is a view , hence it is taking a lot of time for execution.
The query is : Query1
SELECT [Table1].M, [[Table1]].B, [Table1].P
FROM [Table1]
WHERE ((([[Table1]].B) Is Not Null) AND (([[Table1]].P) Is Not Null));
Does the below query does the same function as Query1 with faster execution time ?
SELECT [Table1].M, [[Table1]].B, [Table1].P
FROM [Table1]
WHERE COALESCE (([[Table1]].B),([[Table1]].P)) Is Not Null
Any help would be of great help and thanks in advance.
The view query
select dbo.TABLE1.[COL1]
, dbo.TABLE1.[COL2]
, RIGHT(dbo.TABLE1.M, 12) as M
, dbo.TABLE2.[MD]
, dbo.TABLE1.[COL3]
, dbo.TABLE1.[COL4]
, dbo.TABLE3.COL1
, dbo.TABLE3.[COL2]
, dbo.TABLE3.[COL3]
, dbo.TABLE4.[COL1]
, dbo.TABLE5.[COL1]
, dbo.TABLE6.[COL1]
, dbo.TABLE7.[COL1] as [BA]
, dbo.TABLE8.[COL1]
, dbo.TABLE3.[COL4]
, dbo.TABLE3.[COL5]
, dbo.TABLE3.[COL6]
from dbo.TABLE1
left outer join dbo.TABLE2
on dbo.TABLE1.M = dbo.TABLE2.M
left outer join dbo.TABLE3
on dbo.TABLE1.M = dbo.TABLE3.M
left outer join dbo.TABLE5
on dbo.TABLE3.[OBJ_NR] = dbo.TABLE5.OBJ
left outer join dbo.TABLE6
on dbo.TABLE3.[OBJ_NR] = dbo.TABLE6.OBJ
left outer join dbo.TABLE7
on dbo.TABLE3.[OBJ_NR] = dbo.TABLE7.OBJ
left outer join dbo.TABLE4
on dbo.TABLE3.[OBJ_NR] = dbo.TABLE4.OBJ
left outer join dbo.TABLE8
on dbo.TABLE3.[OBJ_NR] = dbo.TABLE8.OBJ
where (
(
dbo.TABLE1.[COL1] not in (
'XX'
, 'YY'
)
)
and (dbo.TABLE1.COL5 = 'x')
)
No, both queries aren't equivalent.
The WHERE clause in the second one is equivalent to
WHERE [[Table1]].B Is Not Null OR [[Table1]].P Is Not Null
COALESCE will evaluate the first parameter and return it if not null. Otherwise, it will return the second one if not null, and so on, until reaching the last parameter, which will be returned whatever its value. So COALESCE(...) IS NOT NULL needs only one not null value to return true, not all.
I've tried this out on a table in my development DB. Here are the results:
with only PK index: 2 minutes for 4 million selected records out of 8 million table
with index on 3 selected columns (none of them PK) 1.8 seconds.
You might need to do some testing to get the right indexes for your setup but here is the sample of what i changed:
select [col1]
, [col2]
, [col3]
from [dbo].[tbl]
where col2 is not null
and col3 is not null
create nonclustered index [idx_test] on [dbo].[tbl] (
[col2] asc
, [col3] asc
) INCLUDE ([col1])