Related
I have a table 'PurchaseOrder'. I want select data when column's description is not null. and then replace the column's name with it's description.
table PurchaseOrder
ID | Product | Use | Price | Comment
-----------------------------------------------------
1 | Cellphone | true | 100 |
2 | Car | false | 10 |
3 | Table | true | 200 |
Column
Name | Description
---------------------------
ID | Index
Produce | Name
Use |
Price | Sale
Comment |
The Select result will be
Index | Name | Sale
--------------------------------
1 | Cellphone | 100
2 | Car | 10
3 | Table | 200
Now, I just get the column with description
if object_id('tempdb..#dt') is not null drop table #dt
select
sc.column_id [Position],
sc.name [Column],
sc.max_length [Length],
sep.value [Description]
into #dt
from sys.tables st
inner join sys.columns sc on st.object_id = sc.object_id
left join sys.extended_properties sep on st.object_id = sep.major_id
and sc.column_id = sep.minor_id
and sep.name = 'MS_Description'
where st.name = 'PurchaseOrder' and sep.value is not null
How to generate the final data table?
DECLARE
#TABLE_NAME NVARCHAR(50) = 'PurchaseOrderDetail',
#TABLE_SCHEMA Nvarchar(50) = 'Purchasing',
#COL_LIST NVARCHAR(MAX),
#SQL NVARCHAR(4000),
#COL NVARCHAR(500)
DECLARE c_cursor CURSOR FOR
SELECT sc.name + ' as ' + QUOTENAME(CAST(sep.value AS varchar)) AS Expr1
FROM sys.tables AS st INNER JOIN
sys.columns AS sc ON st.object_id = sc.object_id INNER JOIN
sys.extended_properties AS sep ON st.object_id = sep.major_id AND sc.column_id = sep.minor_id AND sep.name = 'MS_Description'
WHERE st.name = #TABLE_NAME AND sep.value IS NOT NULL
OPEN c_cursor;
FETCH NEXT
FROM c_cursor INTO #COL;
WHILE ( ##FETCH_STATUS = 0 )
BEGIN
SELECT #COL_LIST = COALESCE(#COL_LIST+',' ,'') + #COL
FETCH NEXT FROM c_cursor
INTO #col;
END CLOSE c_cursor;
DEALLOCATE c_cursor;
SET #SQL = ' SELECT ' + #COL_LIST +' FROM ' + QUOTENAME(#TABLE_SCHEMA)+'.'+QUOTENAME(#TABLE_NAME)
PRINT #SQL;
EXEC (#sql);
I have a table which has records of user's vacation days.
A Sample of that would be:
+---------+-----------+---------+------------+
| country | user_name | user_id | vac_date |
+---------+-----------+---------+------------+
| canada | James | 1111 | 2015-02-13 |
| canada | James | 1111 | 2015-02-17 |
| canada | James | 1111 | 2015-02-18 |
| canada | James | 1111 | 2015-02-10 |
| canada | James | 1111 | 2015-02-11 |
+---------+-----------+---------+------------+
With the above data, the count would be 3 from feb 13th to feb 18th, because 14th and 15th are weekends and the 16th is a holiday here in Canada. So essentially, I am trying to hold and continue the count if the user took the next working day off. I also have a table that has all the holidays which includes the country and the date of the holiday. Sample data for the holiday table would be:
+---------+-------------+-------------+
| country | holidayDesc | holidayDate |
+---------+-------------+-------------+
| canada | Family Day | 2015-02-16 |
+---------+-------------+-------------+
Currently i have a query in SQL that counts the the dates normally, so it only counts whatever is in the vacation table. For example: if a user took march 3rd 2015, march 4th 2015, and march 5th 2015 off, then it will have a count of 3, but for that above table example, it would only have a count of 1 for feb 13th and 2 from feb 17th to feb 18th.
SELECT DISTINCT user_name
,min(vac_date) as startDate
,max(vac_date) as endDate
,datediff(day, min(vac_date), max(vac_date)) as consecutiveCount
FROM (
SELECT user_name
,vac_date
,user_id
,groupDate = DATEADD(DAY, - ROW_NUMBER() OVER (
PARTITION BY user_id ORDER BY vac_date
), vac_date)
FROM mytable
WHERE country = 'canada'
AND vac_date BETWEEN '20150101'
AND '20151231'
) z
GROUP BY user_name
,groupDate
HAVING datediff(day, min(vac_date), max(vac_date)) >= 0
ORDER BY user_name
,min(vac_date);
This is what it currently outputs from the above sample data:
+-----------+------------+------------+------------------+
| user_name | startDate | endDate | consecutiveCount |
+-----------+------------+------------+------------------+
| James | 2015-02-10 | 2015-02-11 | 2 |
| James | 2015-02-13 | 2015-02-13 | 1 |
| James | 2015-02-17 | 2015-02-18 | 2 |
+-----------+------------+------------+------------------+
Ideally i would like it to be:
+-----------+------------+------------+------------------+
| user_name | startDate | endDate | consecutiveCount |
+-----------+------------+------------+------------------+
| James | 2015-02-10 | 2015-02-11 | 2 |
| James | 2015-02-13 | 2015-02-18 | 3 |
+-----------+------------+------------+------------------+
But i don't know if that is possible with pure SQL. I can also try to incorporate it into C#.
If it helps I am also using C# and SQL Server Management Studio. Any help would be appreciated. Thanks in advance
I try to go a different route, but then found the fix for John Cappelletti solution.
First you need to add weekend dates to your holiday table.
Get a list of dates between two dates using a function
Then UNION ALL vacation days with holidays, but add a description field so you can difference between both.
There are some CROSS JOIN so you can have holiday and weekends for each country and user (need testing)
SELECT [country],
[user_name], [user_id], [vac_date], 'vacation' as description
FROM vacations
UNION ALL
SELECT c.[country],
u.[user_name],
u.[user_id],
[holidayDate],
'holiday' as description
FROM holidays
CROSS JOIN (SELECT DISTINCT [country] FROM vacations) c
CROSS JOIN (SELECT DISTINCT [user_name], [user_id] FROM vacations) u
Then the final query is the same as John suggested, but this time you only count vacation days.
WITH joinDates as (
SELECT [country],
[user_name], [user_id], [vac_date], 'vacation' as description
FROM vacations
UNION ALL
SELECT c.[country],
u.[user_name],
u.[user_id],
[holidayDate],
'holiday' as description
FROM holidays
CROSS JOIN (SELECT DISTINCT [country] FROM vacations) c
CROSS JOIN (SELECT DISTINCT [user_name], [user_id] FROM vacations) u
)
Select user_name
,startDate = min(vac_date)
,endDate = max(vac_date)
,consecutiveCount = count(*)
From (
Select *
,Grp = Day(vac_date) - Row_Number() over (Partition By country,user_id
Order by vac_date)
From joinDates S
) A
WHERE description = 'vacation' -- only count vacation days ignore holiday/weekend
Group By user_name, Grp
Having count(*)>1
ORDER BY startDate
SQL DEMO
OUTPUT
RAW OUTPUT
here you can see the data before the group by
This seems like a classic Gaps & Islands with a little twist.
Declare #YourTable table (country varchar(25),user_name varchar(25),user_id varchar(25),vac_date date)
Insert Into #YourTable values
('canada','James','1111','2015-02-13'),
('canada','James','1111','2015-02-17'),
('canada','James','1111','2015-02-18'),
('canada','James','1111','2015-02-10'),
('canada','James','1111','2015-02-11')
Declare #Holiday table (country varchar(25),holidayDate date)
Insert Into #Holiday values
('canada','2015-02-16')
Select user_name
,startDate = min(vac_date)
,endDate = max(vac_date)
,consecutiveCount = sum(DayCnt)
From (
Select *
,Grp = Day(vac_date) - Row_Number() over (Partition By country,user_id Order by vac_date)
From (Select Country,user_name,user_id,vac_date,DayCnt=1 from #YourTable
Union All
Select A.Country,user_name,user_id,vac_date=b.holidayDate,DayCnt=1
From #YourTable A
Join #Holiday B on A.country=B.country and abs(DateDiff(DD,vac_date,holidayDate))=1
Union All
Select A.Country,user_name,user_id,vac_date=b.retval,DayCnt=0
From #YourTable A
Join (
Select * From [dbo].[udf-Range-Date]('2015-01-01','2017-12-31','DD',1) where DateName(WEEKDAY,RetVal) in ('Saturday','Sunday')
) B on abs(DateDiff(DD,vac_date,RetVal))=1
) S
) A
Group By user_name,Grp
Having Sum(DayCnt)>1
Returns
user_name startDate endDate consecutiveCount
James 2015-02-10 2015-02-11 2
James 2015-02-16 2015-02-18 3
The UDF to generate dynamic Date Ranges -- could be your own query
CREATE FUNCTION [dbo].[udf-Range-Date] (#R1 datetime,#R2 datetime,#Part varchar(10),#Incr int)
Returns Table
Return (
with cte0(M) As (Select 1+Case #Part When 'YY' then DateDiff(YY,#R1,#R2)/#Incr When 'QQ' then DateDiff(QQ,#R1,#R2)/#Incr When 'MM' then DateDiff(MM,#R1,#R2)/#Incr When 'WK' then DateDiff(WK,#R1,#R2)/#Incr When 'DD' then DateDiff(DD,#R1,#R2)/#Incr When 'HH' then DateDiff(HH,#R1,#R2)/#Incr When 'MI' then DateDiff(MI,#R1,#R2)/#Incr When 'SS' then DateDiff(SS,#R1,#R2)/#Incr End),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
cte3(N,D) As (Select 0,#R1 Union All Select N,Case #Part When 'YY' then DateAdd(YY, N*#Incr, #R1) When 'QQ' then DateAdd(QQ, N*#Incr, #R1) When 'MM' then DateAdd(MM, N*#Incr, #R1) When 'WK' then DateAdd(WK, N*#Incr, #R1) When 'DD' then DateAdd(DD, N*#Incr, #R1) When 'HH' then DateAdd(HH, N*#Incr, #R1) When 'MI' then DateAdd(MI, N*#Incr, #R1) When 'SS' then DateAdd(SS, N*#Incr, #R1) End From cte2 )
Select RetSeq = N+1
,RetVal = D
From cte3,cte0
Where D<=#R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1)
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1)
*/
OK, my understanding of the question is that what you want to do is count spans of days off as only one day. Many businesses call this an "occurrence of absence" to differentiate absences by cause. In this case, you're trying to treat holidays as a continuance of the holiday (for time purposes) and if a holiday occurs on a Friday but the person takes Monday off, that should be one contiguous time out.
Personally, I'd do this in C# because of properties of the DateTime object that could make this a lot easier than trying to make a frankenquery. The code below assumes that you have an object called an Employee that contains its own record of DateTimes, like so:
public class Employee
{
public int ID {get;set;}
public string Name {get;set;}
public List<DateTime> DaysIWasOut {get;set;}
}
public static int TimeOut(IEnumerable employees)
{
int totalOutInstances = 0;
DataTable dt = HolidaysPlease(); //this refers to another method
//to fill the table. Just a basic SQLAdapter.Fill kind of thing.
//Basic so I won't waste time on it here.
foreach(var e in employees)
{
var holidays = dt.AsEnumerable().Where(t => Convert.ToDateTime(t[3]) == d) //holidays now has all of the holidays the employee had off.
totalOutInstances = e.DaysIWasOut.Count();
foreach(var d in e.DaysIWasOut)
{
int daystolook = 0;
if (d.DayOfWeek == DayOfWeek.Friday)
daystolook +=3;
else
daystolook +=1;
if(e.DaysIWasOut.Contains(d.AddDays(daystolook))
{totalOutInstances --; } //don't count that day
}
}
return totalOutInstances;
}
Given the following SQL table:
GroupAvailable, GroupAssigned, Title
----------------------------------------
GroupA,GroupA,Widget1
GroupA,GroupB,Widget1
GroupB,GroupA,Widget1
Need a report to look like:
Widget1
GroupA GroupB
GroupA X X
GroupB X
Widget2
etc.
Is this possible to do in query? Or something pretty close? Or would a combination of code and sql be better?
The PIVOT function will get you close to the result:
select name,
coalesce(GroupA, '') GroupA,
coalesce(GroupB, '') GroupB
from
(
select title +' '+ groupavailable name,
groupassigned,
'X' as flag
from yourtable
) d
pivot
(
max(flag)
for groupassigned in (GroupA, GroupB)
) piv;
See SQL Fiddle with Demo.
If you have an unknown number of groups, then you could use dynamic SQL to get the result:
DECLARE #cols AS NVARCHAR(MAX),
#colsNull AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(GroupAvailable)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select #colsNull = STUFF((SELECT distinct ', coalesce(' + QUOTENAME(GroupAvailable)+', '''') as '+QUOTENAME(GroupAvailable)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT name,' + #colsNull + '
from
(
select title +'' ''+ groupavailable name,
groupassigned,
''X'' as flag
from yourtable
) x
pivot
(
max(flag)
for groupassigned in (' + #cols + ')
) p '
execute(#query);
See SQL Fiddle with Demo
Both versions will give a result:
| NAME | GROUPA | GROUPB |
------------------------------------
| Widget1 GroupA | X | X |
| Widget1 GroupB | X | |
| Widget2 GroupB | X | |
Trying to pivot dynamic data using LINQ or LAMBDA in C#/MVC4 and have pretty much come to the conclusion that its very difficult to do..
This is basically what I want to do:
I have been able to get this to work with known column names using this example: http://geekswithblogs.net/malisancube/archive/2009/04/21/using-lambda-or-linq-for-pivot-tables.aspx
But I cant find any examples for doing this with dynamic columns.
By dynamic columns I mean that there could be a new row with a different Name and FieldType that has not been in the table before at any time that also needs to be turned into a column.. any pointers?.
I don't know LINQ so I will give you a version that can be used in a SQL Server stored procedure. This type of data transformation is known as a PIVOT. Since you are using SQL Server 2008+, you can use the function.
If you know the values that you want to transform, then you can hard-code the values:
SELECT nodeid, rowid,[FirstName], [LastName], [Title]
FROM
(
SELECT nodeid, rowid, name, value
FROM yourTable
) x
PIVOT
(
max(value)
for name in ([FirstName], [LastName], [Title])
)p
See SQL Fiddle with Demo
Then if you have an unknown number of values, you can implement dynamic SQL:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(name)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT nodeid, rowid,' + #cols + ' from
(
SELECT nodeid, rowid, name, value
FROM yourTable
) x
pivot
(
max(value)
for name in (' + #cols + ')
) p '
execute(#query)
See SQL Fiddle with Demo
Both return the results:
| NODEID | ROWID | FIRSTNAME | LASTNAME | TITLE |
--------------------------------------------------
| 1 | 1 | Alfred | Beagle | (null) |
| 1 | 2 | Freddy | (null) | (null) |
| 1 | 3 | (null) | Grey | Sir. |
Assume a table with the fields TransactionId, ItemId, Code, EffectiveDate, and CreateDate.
+---------------+--------+------+------------------+------------------+
| TransactionId | ItemId | Code | EffectiveDate | CreateDate |
+---------------+--------+------+------------------+------------------+
| 1| 1| 8| 12/2/2009 1:13 PM| 12/2/2009 1:13 PM|
+---------------+--------+------+------------------+------------------+
| 4| 1| 51|12/2/2009 11:08 AM| 12/3/2009 9:01 AM|
+---------------+--------+------+------------------+------------------+
| 2| 1| 14|12/2/2009 11:09 AM|12/2/2009 11:09 AM|
+---------------+--------+------+------------------+------------------+
| 3| 1| 61| 12/3/2009 8:33 AM| 12/3/2009 8:33 AM|
+---------------+--------+------+------------------+------------------+
| 5| 1| 28| 12/3/2009 9:33 AM| 12/3/2009 9:33 AM|
+---------------+--------+------+------------------+------------------+
| 6| 1| 9| 12/3/2009 1:58 PM| 12/3/2009 1:58 PM|
+---------------+--------+------+------------------+------------------+
I need to get the set of records where the sequence 51, 61, 9 occurs for a given ItemId, sorted by EffectiveDate. There could be other records with other codes in between these records.
In this case, I would return TransactionId's 4, 3, and 6, as shown below.
+---------------+--------+------+------------------+------------------+
| TransactionId | ItemId | Code | EffectiveDate | CreateDate |
+---------------+--------+------+------------------+------------------+
| 4| 1| 51|12/2/2009 11:08 AM| 12/3/2009 9:01 AM|
+---------------+--------+------+------------------+------------------+
| 3| 1| 61| 12/3/2009 8:33 AM| 12/3/2009 8:33 AM|
+---------------+--------+------+------------------+------------------+
| 6| 1| 9| 12/3/2009 1:58 PM| 12/3/2009 1:58 PM|
+---------------+--------+------+------------------+------------------+
Note that:
This isn't the only sequence I'll need to identify, but it illustrates the problem.
Records can be inserted into the table out of order; that is, the 61 record could have been inserted first, followed by the 51, and then the 9. You can see this in the example where for the code 51 record the CreateDate is later than the EffectiveDate.
The order of the sequence matters. So, the sequence 61, 9, 51 would not return any records, but 51, 61, 9 would.
A DB approach is ideal if it's simple (i.e. no cursors or overly complicated stored procedure), but a code approach could also work, although it would result in a significant amount of data transfer out of the DB.
The environment is SQL Server 2005 and C#/.NET 3.5.
Actually, you could get a couple of fairly simple solutions leveraging ranking/windowing functions and/or CTEs and recursive CTEs.
Create a procedure that accepts a character-based comma-separated list of Code values you are looking for in the sequence you want them in - use any of a dozen possible ways to split this list into a table/set that is made up of the sequence and Code value, resulting in a table with a structure like this:
declare #sequence table (sequence int not null, Code int not null);
Once you have this, it's simply a matter of sequencing the source set based on joining the sequenced table to the source table on the same Code values for a given ItemId - once you have the source set filtered and sequenced, you can simply join again based on the matching sequence values - this is sounding complex, but in reality it would be a single query like this:
with srcData as (
select row_number() over(order by t.EffectiveDate) as rn,
t.TransactionId, t.ItemId, t.Code, t.EffectiveDate, t.CreateDate
from #TableName t
join #sequence s
on t.Code = s.Code
where t.ItemId = #item_id
)
select d.TransactionId, d.ItemId, d.Code, d.EffectiveDate, d.CreateDate
from srcData d
join #sequence s
on d.rn = s.sequence
and d.Code = s.Code
order by d.rn;
This alone won't guarantee that you get a result-set that is identical to what you are looking for, but staging the data into a temp table and adding a few simple checks around the code would do the trick (for example, add a checksum validation and a sum of the code values)
declare #tempData table (rn int, TransactionId smallint, ItemId smallint, Code smallint, EffectiveDate datetime, CreateDate datetime);
with srcData as (
select row_number() over(order by t.EffectiveDate) as rn,
t.TransactionId, t.ItemId, t.Code, t.EffectiveDate, t.CreateDate
from #TableName t
join #sequence s
on t.Code = s.Code
where t.ItemId = #item_id
)
insert #tempData
(rn, TransactionId, ItemId, Code, EffectiveDate, CreateDate)
select d.rn, d.TransactionId, d.ItemId, d.Code, d.EffectiveDate, d.CreateDate
from srcData d
join #sequence s
on d.rn = s.sequence
and d.Code = s.Code;
-- Verify we have matching hash/sums
if
(
( (select sum(Code) from #sequence) = (select sum(Code) from #tempData) )
and
( (select checksum_agg(checksum(sequence, Code)) from #sequence) = (select checksum_agg(checksum(rn, Code)) from #tempData) )
)
begin;
-- Match - return the resultset
select d.TransactionId, d.ItemId, d.Code, d.EffectiveDate, d.CreateDate
from #tempData d
order by d.rn;
end;
If you want to do it all inline, you could use a different approach leveraging CTEs and recursion to perform a running sum/total and OrdPath-like comparison as well (though you'd still need to parse the sequence character data out into a dataset)
-- Sequence data with running total
with sequenceWithRunningTotal as
(
-- Anchor
select s.sequence, s.Code, s.Code as runningTotal, cast(s.Code as varchar(8000)) as pth,
sum(s.Code) over(partition by 1) as sumCode
from #sequence s
where s.sequence = 1
-- Recurse
union all
select s.sequence, s.Code, b.runningTotal + s.Code as runningTotal,
b.pth + '.' + cast(s.Code as varchar(8000)) as pth,
b.sumCode as sumCode
from #sequence s
join sequenceWithRunningTotal b
on s.sequence = b.sequence + 1
),
-- Source data with sequence value
srcData as
(
select row_number() over(order by t.EffectiveDate) as rn,
t.TransactionId, t.ItemId, t.Code, t.EffectiveDate, t.CreateDate,
sum(t.Code) over(partition by 1) as sumCode
from #TableName t
join #sequence s
on t.Code = s.Code
where t.ItemId = #item_id
),
-- Source data with running sum
sourceWithRunningSum as
(
-- Anchor
select t.rn, t.TransactionId, t.ItemId, t.Code, t.EffectiveDate, t.CreateDate,
t.Code as runningTotal, cast(t.Code as varchar(8000)) as pth,
t.sumCode
from srcData t
where t.rn = 1
-- Recurse
union all
select t.rn, t.TransactionId, t.ItemId, t.Code, t.EffectiveDate, t.CreateDate,
s.runningTotal + t.Code as runningTotal,
s.pth + '.' + cast(t.Code as varchar(8000)) as pth,
t.sumCode
from srcData t
join sourceWithRunningSum s
on t.rn = s.rn + 1
)
select d.TransactionId, d.ItemId, d.Code, d.EffectiveDate, d.CreateDate
from sourceWithRunningSum d
join sequenceWithRunningTotal s
on d.rn = s.sequence
and d.Code = s.Code
and d.runningTotal = s.runningTotal
and d.pth = s.pth
and d.sumCode = s.sumCode
order by d.rn;
A DB approach is ideal if it's simple (i.e. no cursors or overly complicated stored procedure)
I don't believe a pure DB approach ("pure" meaning only using SQL SELECT) is practical because the type of SQL I envision would require very convoluted self-joins, field concatenation, MAX() functions, etc. This type of SQL might be a fun academic answer to a puzzle in Joe Celko's "SQL for Smarties" book but I don't think that's appropriate for production code.
I think the realistic approach is to write some kind of loop that keeps track of state. Your problem in the general sense is very similar to writing code for stateful inspection of TCPIP packets for spam filtering or scanning credit-card transactions for fraudulent patterns. All these problems share similar characteristics: the actions you take on the current row(record) depends on what records you saw previously (the context)... and that aspect requires holding state variables.
If you want to avoid round-tripping the data for analysis, it looks like Transact-SQL is the best way for performance. Or use hosted CLR to take advantage of C# syntax while still keeping the processing within the database engine.
This is just off the top of my head and is untested, so it may need some tweaking:
SELECT DISTINCT
T.TransactionID,
T.ItemID,
T.Code,
T.EffectiveDate,
T.CreateDate
FROM
My_Table T
INNER JOIN (
SELECT
T1.TransactionID,
T2.TransactionID,
T3.TransactionID
FROM
My_Table T1
INNER JOIN My_Table T2 ON
T2.ItemID = T1.ItemID AND
T2.Code = 61 AND
T2.EffectiveDate > T1.EffectiveDate
INNER JOIN My_Table T3 ON
T3.ItemID = T1.ItemID AND
T3.Code = 9 AND
T3.EffectiveDate > T2.EffectiveDate
WHERE
T1.Code = 51
) SQ ON
SQ.TransactionID = T1.TransactionID OR
SQ.TransactionID = T2.TransactionID OR
SQ.TransactionID = T3.TransactionID