获取数据 employee table with 1 id and multiple transaction id
fetch data employee table with 1 id and multiple transaction id
此处交易 table 一名员工有多项交易。我想要 1 名特定员工的所有交易。但员工 ID 应在列中打印一次。这里是格式
1.here一名员工有多个交易id .id应该显示1次并显示所有交易
期望的结果:员工详细信息应显示在一行中,但交易 ID 应显示在多行中
错误 ID 重复 ID:
从评论中复制:
select
b.TransactionId,
a.Name
from
TransactionRecharge b
left JOIN Customer a on a.Id=b.CustomerId
where
b.CustomerId=101282
这不是好的做法,我很确定“我会因为帮助你而得到负面评价”,不管怎样,我只是按照你的要求做了。
Below Query using ROW_NUMBER will help you get what you need exactly.
CREATE TABLE #customer(Id INT PRIMARY KEY,Name VARCHAR(255))
CREATE TABLE #TransactionRecharge(TransactionId VARCHAR(255),CustomerId INT FOREIGN KEY REFERENCES #customer(id))
INSERT INTO #customer
VALUES (1,'Raj'),(2,'Bala'),(3,'Chandra')
INSERT INTO #TransactionRecharge
VALUES ('reansaction1',1),('reansaction2',1),
('reansaction3',1),('reansaction4',1),
('reansaction5',1),('reansaction6',1),
('reansaction7',1),('reansaction8',2)
SELECT
CASE
WHEN ROW_NUMBER() OVER ( PARTITION BY a.id ORDER BY b.TransactionId,a.Id) = '1' THEN CONVERT(VARCHAR,a.Id)
ELSE ''
END AS Customer,
CASE
WHEN ROW_NUMBER() OVER ( PARTITION BY a.id ORDER BY b.TransactionId,a.Id) = '1' THEN Name
ELSE ''
END AS Name,
b.TransactionId
FROM
#TransactionRecharge b
LEFT JOIN
#customer a ON a.Id = b.CustomerId
WHERE 1 = 1
--AND b.CustomerId = 1
DROP TABLE #customer
DROP TABLE #TransactionRecharge
此处交易 table 一名员工有多项交易。我想要 1 名特定员工的所有交易。但员工 ID 应在列中打印一次。这里是格式
1.here一名员工有多个交易id .id应该显示1次并显示所有交易
期望的结果:员工详细信息应显示在一行中,但交易 ID 应显示在多行中
错误 ID 重复 ID:
从评论中复制:
select
b.TransactionId,
a.Name
from
TransactionRecharge b
left JOIN Customer a on a.Id=b.CustomerId
where
b.CustomerId=101282
这不是好的做法,我很确定“我会因为帮助你而得到负面评价”,不管怎样,我只是按照你的要求做了。
Below Query using ROW_NUMBER will help you get what you need exactly.
CREATE TABLE #customer(Id INT PRIMARY KEY,Name VARCHAR(255))
CREATE TABLE #TransactionRecharge(TransactionId VARCHAR(255),CustomerId INT FOREIGN KEY REFERENCES #customer(id))
INSERT INTO #customer
VALUES (1,'Raj'),(2,'Bala'),(3,'Chandra')
INSERT INTO #TransactionRecharge
VALUES ('reansaction1',1),('reansaction2',1),
('reansaction3',1),('reansaction4',1),
('reansaction5',1),('reansaction6',1),
('reansaction7',1),('reansaction8',2)
SELECT
CASE
WHEN ROW_NUMBER() OVER ( PARTITION BY a.id ORDER BY b.TransactionId,a.Id) = '1' THEN CONVERT(VARCHAR,a.Id)
ELSE ''
END AS Customer,
CASE
WHEN ROW_NUMBER() OVER ( PARTITION BY a.id ORDER BY b.TransactionId,a.Id) = '1' THEN Name
ELSE ''
END AS Name,
b.TransactionId
FROM
#TransactionRecharge b
LEFT JOIN
#customer a ON a.Id = b.CustomerId
WHERE 1 = 1
--AND b.CustomerId = 1
DROP TABLE #customer
DROP TABLE #TransactionRecharge