verify that xml node has a child node with a given value tsql - sql-server

I have the following tables
A (ID, relatedID, typeId )
B (ID, leftID, leftTypeId)
I want to join the two tables like this
select * from A
inner join B on A.TypeId=B.LeftTypeId and {condition}
where condition should verify id the leftID would match a value from relatedID, where relatedId is a xml column. Eg. relatedID=<Id>1</Id>
Is there a optimal way to do this?
UPDATE
relatedID can contain several Ids. Eg Eg. relatedID=<Id>1</Id><Id>2</Id>

You may use
... and A.relatedID.value('(/Id[1]/text())[1]', 'int') = B.leftID
or
... and A.relatedID.exist('(/Id[1]/text())[1] = sql:column("B.leftID")') = 1
Though exist is recommended over value for predicates, depending on whether the XML column is xml-indexed or not and what type of indexes it has, one of the two above may perform better.
upd. for the case when relatedID can contain set of Ids you may try
select ...
from A
cross apply A.relatedID.nodes('/Id') r(id)
inner join B on A.TypeId=B.LeftTypeId
and r.id.value('text()[1]', 'int') = B.leftID
or
select ...
from A
cross apply A.relatedID.nodes('/Id') r(id)
inner join B on A.TypeId=B.LeftTypeId
and r.id.exist('text()[1]=sql:column("B.leftID")') = 1
or even
select ...
from A
inner join B on A.TypeId=B.LeftTypeId
and A.relatedID.exist('/Id[text()[1]=sql:column("B.leftID")]') = 1

Related

RIGHT\LEFT Join does not provide null values without condition

I have two tables one is the lookup table and the other is the data table. The lookup table has columns named cycleid, cycle. The data table has SID, cycleid, cycle. Below is the structure of the tables.
If you check the data table, the SID may have all the cycles and may not have all the cycles. I want to output the SID completed as well as missed cycles.
I right joined the lookup table and retrieved the missing as well as completed cycles. Below is the query I used.
SELECT TOP 1000 [SID]
,s4.[CYCLE]
,s4.[CYCLEID]
FROM [dbo].[data] s3 RIGHT JOIN
[dbo].[lookup_data] s4 ON s3.CYCLEID = s4.CYCLEID
The query is not displaying me the missed values when I query for all the SID's. When I specifically query for a SID with the below query i am getting the correct result including the missed ones.
SELECT TOP 1000 [SID]
,s4.[CYCLE]
,s4.[CYCLEID]
FROM [dbo].[data] s3 RIGHT JOIN [dbo].[lookup_data] s4
ON s3.CYCLEID = s4.CYCLEID
AND s3.SID = 101002
ORDER BY [SID], s4.[CYCLEID]
As I am supplying this query into tableau I cannot provide the sid value in the query. I want to return all the sid's and from tableau I will be do the rest of the things.
The expected output that i need is as shown below.
I wrote a cross join query like below to acheive my expected output
SELECT DISTINCT
tab.CYCLEID
,tab.SID
,d.CYCLE
FROM ( SELECT d.SID
,d.[CYCLE]
,e.CYCLEID
FROM ( SELECT e.sid
,e.CYCLE
FROM [db_temp].[dbo].[Sheet3$] e
) d
CROSS JOIN [db_temp].[dbo].[Sheet4$] e
) tab
LEFT OUTER JOIN [db_temp].[dbo].[Sheet3$] d
ON d.CYCLEID = tab.CYCLEID
AND d.SID = tab.SID
ORDER BY tab.SID
,tab.CYCLEID;
However I am not able to use this query for more scenarios as my data set have nearly 20 to 40 columns and i am having issues when i use the above one.
Is there any way to do this in a simpler manner with only left or right join itself? I want the query to return all the missing values and the completed values for the all the SID's instead of supplying a single sid in the query.
You can create a master table first (combine all SID and CYCLE ID), then right join with the data table
;with ctxMaster as (
select distinct d.SID, l.CYCLE, l.CYCLEID
from lookup_data l
cross join data d
)
select d.SID, m.CYCLE, m.CYCLEID
from ctxMaster m
left join data d on m.SID = d.SID and m.CYCLEID = d.CYCLEID
order by m.SID, m.CYCLEID
Fiddle
Or if you don't want to use common table expression, subquery version:
select d.SID, m.CYCLE, m.CYCLEID
from (select distinct d.SID, l.CYCLE, l.CYCLEID
from lookup_data l
cross join data d) m
left join data d on m.SID = d.SID and m.CYCLEID = d.CYCLEID
order by m.SID, m.CYCLEID

Conditional JOIN Statement SQL Server

Is it possible to do the following:
IF [a] = 1234 THEN JOIN ON TableA
ELSE JOIN ON TableB
If so, what is the correct syntax?
I think what you are asking for will work by joining the Initial table to both Option_A and Option_B using LEFT JOIN, which will produce something like this:
Initial LEFT JOIN Option_A LEFT JOIN NULL
OR
Initial LEFT JOIN NULL LEFT JOIN Option_B
Example code:
SELECT i.*, COALESCE(a.id, b.id) as Option_Id, COALESCE(a.name, b.name) as Option_Name
FROM Initial_Table i
LEFT JOIN Option_A_Table a ON a.initial_id = i.id AND i.special_value = 1234
LEFT JOIN Option_B_Table b ON b.initial_id = i.id AND i.special_value <> 1234
Once you have done this, you 'ignore' the set of NULLS. The additional trick here is in the SELECT line, where you need to decide what to do with the NULL fields. If the Option_A and Option_B tables are similar, then you can use the COALESCE function to return the first NON NULL value (as per the example).
The other option is that you will simply have to list the Option_A fields and the Option_B fields, and let whatever is using the ResultSet to handle determining which fields to use.
This is just to add the point that query can be constructed dynamically based on conditions.
An example is given below.
DECLARE #a INT = 1235
DECLARE #sql VARCHAR(MAX) = 'SELECT * FROM [sourceTable] S JOIN ' + IIF(#a = 1234,'[TableA] A ON A.col = S.col','[TableB] B ON B.col = S.col')
EXEC(#sql)
--Query will be
/*
SELECT * FROM [sourceTable] S JOIN [TableB] B ON B.col = S.col
*/
You can solve this with union
select a, b
from tablea
join tableb on tablea.a = tableb.a
where b = 1234
union
select a, b
from tablea
join tablec on tablec.a = tableb.a
where b <> 1234
I disagree with the solution suggesting 2 left joins. I think a table-valued function is more appropriate so you don't have all the coalescing and additional joins for each condition you would have.
CREATE FUNCTION f_GetData (
#Logic VARCHAR(50)
) RETURNS #Results TABLE (
Content VARCHAR(100)
) AS
BEGIN
IF #Logic = '1234'
INSERT #Results
SELECT Content
FROM Table_1
ELSE
INSERT #Results
SELECT Content
FROM Table_2
RETURN
END
GO
SELECT *
FROM InputTable
CROSS APPLY f_GetData(InputTable.Logic) T
I think it will be better to think about your query in a different way and treat them more like sets.
I do believe if you make two separate queries then join them using UNION, It will be much better in performance and more readable.

Get a collision free hash for a specific query or a view with SQL Server 2008

I am working on a project where I need to synchronize data from our system to an external system. What I want to achieve, is to periodically send only changed items (rows) from a custom query. This query looks like this (but with many more columns) :
SELECT T1.field1,
T1.field2,
T1.field2,
T1.field3,
CASE WHEN T1.field4 = 'some-value' THEN 1 ELSE 0 END,
T2.field1,
T3.field1,
T4.field1
FROM T1
INNER JOIN T2 ON T2.pk = T2.fk
INNER JOIN T3 ON T3.pk = T2.fk
INNER JOIN T4 ON T4.pk = T2.fk
I want to avoid to have to compare every field one to one between synchronizations. I came with the idea that I could generate a hash for every row from my query, and compare this with the hash from the previous synchronization, which will return only the changed rows. I am aware of the CHECKSUM function, but it is very collision-prone and might miss changes sometimes. However I like the way I could just make a temp table and use CHECKSUM(*), which makes maintenance easier (not having to add fields in the query and in the CHECKSUM) :
SELECT T1.field1,
T1.field2,
T1.field2,
T1.field3,
CASE WHEN T1.field4 = 'some-value' THEN 1 ELSE 0 END,
T2.field1,
T3.field1,
T4.field1
INTO #tmp
FROM T1
INNER JOIN T2 ON T2.pk = T2.fk
INNER JOIN T3 ON T3.pk = T2.fk
INNER JOIN T4 ON T4.pk = T2.fk;
-- get all columns from the query, plus a hash of the row
SELECT *, CHECKSUM(*)
FROM #tmp;
I am aware of HASHBYTES function (which supports sha1, md5, which are less prone to collisions), but it only accept varchar or varbinary, not a list of columns or * the way CHECKSUM does. Having to cast/convert every column from the query is a pain in the ... and opens the door to errors (forget to include a new field for instance)
I also noticed Change Data Capture and Change Tracking features of SQL Server, but they all seems complicated and overkill for what I am doing.
So my question : is there an other method to generate a hash from a query or a temp table that meets my criterias ?
If not, is there an other way to achieve this kind of work (to sync differences from a query)
I found a way to do exactly what I wanted, thanks to the FOR XML clause :
SELECT T1.field1,
T1.field2,
T1.field2,
T1.field3,
CASE WHEN T1.field4 = 'some-value' THEN 1 ELSE 0 END,
T2.field1,
T3.field1,
T4.field1
INTO #tmp
FROM T1
INNER JOIN T2 ON T2.pk = T2.fk
INNER JOIN T3 ON T3.pk = T2.fk
INNER JOIN T4 ON T4.pk = T2.fk;
-- get all columns from the query, plus a hash of the row (converted in an hex string)
SELECT T.*, CONVERT(VARCHAR(100), HASHBYTES('sha1', (SELECT T.* FOR XML RAW)), 2) AS sHash
FROM #tmp AS T;

SQL Select random from multiple table and order by specific criteria on one table

I need to select a random record from 3 tables and ensure I am ordering by photoOrder
Select TOP 1(a.id), a.mls_number, a.parcel_name, a.property_type, a.ownership_type, b.filename, b.photoOrder, c.county_Name
From property as a
Inner JOIN
listingPhotos as b on a.id = b.ListingID
LEFT JOIN
counties as C on a.county_name = c.id
WHERE a.isCommercial = 'True'
Order By NEWID()
So this query works, but I need to ensure that the b.filename record is ordered by b.photoOrder and thus the b.photoOrder should always be 1.
The b table (listing photos) has multiple photo files per property and I need to only select the photo that is 1st in the photo order.
Thanks
You could subquery your listingPhotos table and limit to WHERE PhotoOrder = 1:
Select TOP 1(a.id), a.mls_number, a.parcel_name, a.property_type, a.ownership_type, b.filename, b.photoOrder, c.county_Name
From property as a
Inner JOIN
(SELECT ListingID , filename, PhotoOrder FROM listingPhotos WHERE PhotoORder = 1
) as b on a.id = b.ListingID
LEFT JOIN
counties as C on a.county_name = c.id
WHERE a.isCommercial = 'True'
Order By NEWID()

How to Merge SQL Query(Help required)

Dear friends, below are my two SQL queries:
select distinct
a_bm.DestProvider_ID,
a_bm.DestCircel_ID,
convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103) as fromdate,
convert(datetime,dbo.fnToDate(a_bm.BM_BillTo),103) as todate,
t_rec.TapInRec as BillRecevable,
t_rec.TapInRec as Billreceied
from Auditdata_BillingMaster a_bm
inner join TapInRecordMaster t_rec
on a_bm.DestProvider_ID = t_rec.DestProviderMaster_ID
and a_bm.DestCircel_ID = t_rec.DestCircelMaster_ID
and convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103)> =
convert(datetime,t_rec.Months)
and convert(datetime,dbo.fnToDate(a_bm.BM_BillTo),103)<=
convert(datetime,t_rec.BillTo)
where a_bm.DestProvider_ID=4
and a_bm.DestCircel_ID=22
and a_bm.typeoffile=1
and convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103)>=
convert(datetime,'6/1/2009')
and convert(datetime,dbo.fnToDate(a_bm.BM_BillFrom),103)<=
convert(datetime,'7/30/2009')
select Temp_tbl.fromdate from Temp_tbl Temp_tbl
inner join (
select
convert(datetime,dbo.fnToDate(BM_BillFrom),103) as a1,
convert(datetime,dbo.fnToDate(BM_BillTo),103) as b1,
count(*) as c1,
am_bm.DestProvider_ID,
am_bm.DestCircel_ID
from Auditdata_BillingMaster am_bm
inner join Temp_tbl tmp
on tmp.Provider_ID=am_bm.DestProvider_ID
and tmp.Circel_ID=am_bm.DestCircel_ID
where convert(datetime,tmp.fromdate)>=
convert(datetime,dbo.fnToDate(am_bm.BM_BillFrom),103)
and convert(datetime,tmp.todate) <=
convert(datetime,dbo.fnToDate(am_bm.BM_BillTo),103)
group by
convert(datetime,dbo.fnToDate(BM_BillFrom),103),
convert(datetime,dbo.fnToDate(BM_BillTo),103),
am_bm.DestProvider_ID,
am_bm.DestCircel_ID
) b
on Temp_tbl.Provider_ID = b.DestProvider_ID
and Temp_tbl.Circel_ID = b.DestCircel_ID
and convert(datetime,Temp_tbl.fromdate,101)>= convert(datetime,(b.a1),101)
and convert(datetime,Temp_tbl.todate) <= convert(datetime,(b.b1),101)
I want to merge above 2 SQL query in SQL Server 2000.
Please help me.
Thanks in advance.
Do you mean to JOIN or UNION both tables?
If you mean to JOIN both query results, simply take both results as input for JOIN statement.
How you join both results is really dependent on your database design. Preferably the join is based on referential integrity enforcing the relationship between the results to ensure data integrity. But since you do not mention the join condition, let me assume you will join based on DestProvider_ID & DestCircel_ID.
select
result1.DestProvider_ID,
result1.DestCircel_ID,
result1.fromdate,
result1.todate,
result1.BillRecevable,
result1.Billreceied,
result2.fromdate
from
( *your first query* ) as result1
inner join
(select
Temp_tbl.fromdate,
am_bm.DestProvider_ID,
am_bm.DestCircel_ID
from Temp_tbl Temp_tbl
*the rest of your second query*
) as result2 on result1.DestProvider_ID = result2.DestProvider_ID
and result1.DestCircel_ID = result2.DestCircel_ID
UNION:
If you want to take multiple select statements and combine them into one result set, UNION statement is the easiest way to go:
SELECT column1a, column2a, column3a FROM tableA
UNION
SELECT column1b, column2b, column3b FROM tableB
This is possible only if:
both queries have same number of columns
Corresponding columns in each query expression must be of the same data type
data type of column1a == column1b
data type of column2a == column2b
data type of column3a == column3b
Since both of your queries do not have same number of columns, you can't merge them, at least with UNION select.

Resources