Sql 在具有匹配id的另一个表的查询中的列中插入值';s
我有两张桌子: Cust360:Sql 在具有匹配id的另一个表的查询中的列中插入值';s,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有两张桌子: Cust360: Customer_id int primary key, Gender nvarchar(50), Basket_count int null, Cust_id int, Transaction_id int, tran_date datetime, total_amt int 交易: Customer_id int primary key, Gender nvarchar(50), Basket_count int null, Cust_id int,
Customer_id int primary key,
Gender nvarchar(50),
Basket_count int null,
Cust_id int,
Transaction_id int,
tran_date datetime,
total_amt int
交易:
Customer_id int primary key,
Gender nvarchar(50),
Basket_count int null,
Cust_id int,
Transaction_id int,
tran_date datetime,
total_amt int
我必须使用
一天内多次购买等于一篮子的不同购买计数
SELECT COUNT(tran_date)
FROM Transactions
GROUP BY tran_date, cust_id
HAVING COUNT(tran_date) > 1
我想在cust360的Basket\u count
列中插入这些值,其中
cust\u id
fromtransactions
与cust360的匹配。您需要在UPDATE
语句中对Customer\u id
和cust\u id
执行内部联接。您已经将问题标记为MySQL
和sqlserver
RDBMS,因此结构略有不同
MySQL示例:
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt;
UPDATE c
SET c.Basket_count = t2.cnt
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt,
c.TotalAmount = t2.total;
UPDATE c
SET c.Basket_count = t2.cnt
, c.TotalAmount = t2.total
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SQL Server(Transact/T-SQL)示例:
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt;
UPDATE c
SET c.Basket_count = t2.cnt
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt,
c.TotalAmount = t2.total;
UPDATE c
SET c.Basket_count = t2.cnt
, c.TotalAmount = t2.total
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
此外,假设您现在想要收集总金额
的SUM()
,您可以在Cust360表中添加一个名为TotalAmount
的列,然后:
MySQL示例:
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt;
UPDATE c
SET c.Basket_count = t2.cnt
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt,
c.TotalAmount = t2.total;
UPDATE c
SET c.Basket_count = t2.cnt
, c.TotalAmount = t2.total
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SQL Server(Transact/T-SQL)示例:
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt;
UPDATE c
SET c.Basket_count = t2.cnt
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
UPDATE Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
SET c.Basket_count = t2.cnt,
c.TotalAmount = t2.total;
UPDATE c
SET c.Basket_count = t2.cnt
, c.TotalAmount = t2.total
FROM Cust360 c
INNER JOIN (
SELECT Cust_id, COUNT(t.tran_date) cnt, SUM(t.total_amt) total
FROM Transactions t
GROUP BY t.tran_date, t.cust_id
HAVING COUNT(tran_date) > 1) t2 ON c.Customer_id = t2.Cust_id
您按交易日期、客户id分组,这意味着同一客户id将有多个结果。但您的客户id是cust36中的主键?您面临哪些问题?两个RDBMS中提到的是哪一个?它是MS SQL SERVER。根据上面的查询,我在更新篮子计数值时遇到了问题。我想设置与客户id匹配的篮子计数行值自从OP确认SQL Server以来我已经更新了我的答案。您能在SQL Server脚本中重新编写一个吗