Database:
Transaction ProductID
1 1000
2 1000
2 1001
3 1000
3 1002
4 1000
4 1001
5 1003
and L1 table (i keep Frequenties item)
PRODUCTID SUPPORT
1000 4
1001 2
1002 1
1003 1
According to L1 table, how to find this result with a T-SQL statement? And find support value from my Transaction table?
+-------------+-----------+-----------------+
| PRODUCTID1 | PRODUCTID2| SUPPORT |
+-------------+-----------+-----------------+
| 1000 | 1001 | 2 |
+-------------+-----------+-----------------+
| 1000 | 1002 | 1 |
+-------------+-----------+-----------------+
| 1000 | 1003 | 0 |
+-------------+-----------+-----------------+
| 1001 | 1002 | 0 |
+-------------+-----------+-----------------+
| 1001 | 1003 | 0 |
+-------------+-----------+-----------------+
| 1002 | 1003 | 0 |,
+-------------+-----------+-----------------+
Test Table:
create table transactions(
ORDERID INT,
PRODUCTID INT
);
insert into transactions(ORDERID, PRODUCTID)
values ('1', '1000')
,('2', '1000')
,('2', '1001')
,('3', '1000')
,('3', '1002')
,('4', '1000')
,('4', '1001'),
('5', '1003');
CREATE TABLE L1
(PRODUCTID INT,
SUPPORT INT )
INSERT INTO L1 (PRODUCTID,SUPPORT)
SELECT PRODUCTID,COUNT(*) [SUPPORT] FROM transactions
GROUP BY PRODUCTID
ORDER BY PRODUCTID
Thanks in advance.
1 Answer