查询按帐号分配费用

时间:2017-11-22 03:30:29

标签: sql sql-server tsql

Part和Recv_Acct在两个表(A,B)中都是常见的(Join Key)col。需要查询表B记录应该与A.Send_Acc相关联。检查附加的最终输出截图

表A

CREATE TABLE [dbo].[a](
    [id] [int] NOT NULL,
    [part] [nvarchar](150) NOT NULL,
    [Recv_acct] [nvarchar](30) NULL,
    [Send_acct] [nvarchar](30) NULL
) ON [PRIMARY]

GO
INSERT INTO a([id],[part],[Recv_acct],[Send_acct])
VALUES(1,'QR1','6722','307131')
,(2,'QR1','6722','22965')
,(3,'QR2','6700','12345')
,(4,'QR2','6700','54321')
Go

**Table B**
CREATE TABLE [dbo].[b](
    [Recv_acct] [varchar](30) NOT NULL,
    [customer] [varchar](60) NULL,
    [invoice_number] [varchar](40) NOT NULL,
    [PART] [varchar](100) NOT NULL,
    [COST] [decimal](38, 6) NULL
) ON [PRIMARY]
GO

INSERT INTO b([Recv_acct],[customer],[invoice_number],[PART],[COST])
VALUES('6722','ABC','3310','QR1','10500.00')
,('6722','ABC','5969','QR1','22400.00')
,('6722','ABC','5974','QR1','2100.00')
,('6722','ABC','8469','QR1','20300.00')
,('6722','ABC','8470','QR1','2100.00')
,('6700','DEF','5974','QR2','100.00')
,('6700','DEF','8469','QR2','2300.00')
,('6700','DEF','8470','QR2','2000.00')

最终输出截图可用 Final_Output_last_Col

2 个答案:

答案 0 :(得分:1)

这可以解决您的问题:

SELECT b.* 
  , a.Send_acct
FROM table_b AS b
LEFT JOIN table_a AS a
ON b.PART = a.part
 AND b.Recv_acct = a.Recv_acct

答案 1 :(得分:0)

由于没有提到推导预期结果的逻辑,因此假设数据需要在列invoice_number上排序,您可以使用row_number()%(mod)如下所示结果接近你的。

SELECT
  PART,
  Recv_acct,
  customer_Name,
  invoice_number,
  Cost,
  Send_Acct
FROM (SELECT
  t2.PART,
  t2.Recv_acct,
  t2.customer_Name,
  t2.invoice_number,
  t2.Cost,
  t1.Send_Acct,
  ROW_NUMBER() OVER (ORDER BY t2.invoice_number ASC) AS rn
FROM tableB t2
JOIN tableA t1
  ON t2.part = t1.part
  AND t1.recv_acct = t1.recv_acct) t
WHERE rn % 2 = 1
ORDER BY part, invoice_number;

<强>结果:

+------+-----------+---------------+----------------+----------+-----------+
| PART | Recv_acct | customer_Name | invoice_number |   Cost   | Send_Acct |
+------+-----------+---------------+----------------+----------+-----------+
| QR1  |      6722 | ABC           |           3310 | 10500,00 |    307131 |
| QR1  |      6722 | ABC           |           3311 | 22400,00 |     22965 |
| QR1  |      6722 | ABC           |           3312 | 2100,00  |    307131 |
| QR1  |      6722 | ABC           |           3313 | 20300,00 |     22965 |
| QR1  |      6722 | ABC           |           3314 | 3100,00  |    307131 |
| QR2  |      6700 | DEF           |           2233 | 400,00   |     12345 |
| QR2  |      6700 | DEF           |           2244 | 4000,00  |     54321 |
| QR2  |      6700 | DEF           |           2333 | 500,00   |     54321 |
+------+-----------+---------------+----------------+----------+-----------+

<强> DEMO

要获得预期输出中提到的确切结果,您需要在单独的查询中对QR1数据升序和QR2降序进行排序,然后union进行排序,如下所示。

SELECT
  PART,
  Recv_acct,
  customer_Name,
  invoice_number,
  Cost,
  Send_Acct
FROM (SELECT
  t2.PART,
  t2.Recv_acct,
  t2.customer_Name,
  t2.invoice_number,
  t2.Cost,
  t1.Send_Acct,
  ROW_NUMBER() OVER (ORDER BY t2.invoice_number ASC) AS rn
FROM tableB t2
JOIN tableA t1
  ON t2.part = t1.part
  AND t1.recv_acct = t1.recv_acct
WHERE t2.part = 'QR1') t
WHERE rn % 2 = 1

UNION ALL

SELECT
  PART,
  Recv_acct,
  customer_Name,
  invoice_number,
  Cost,
  Send_Acct
FROM (SELECT
  t2.PART,
  t2.Recv_acct,
  t2.customer_Name,
  t2.invoice_number,
  t2.Cost,
  t1.Send_Acct,
  ROW_NUMBER() OVER (ORDER BY t2.invoice_number DESC) AS rn
FROM tableB t2
JOIN tableA t1
  ON t2.part = t1.part
  AND t1.recv_acct = t1.recv_acct
WHERE t2.part = 'QR2') t
WHERE rn % 2 = 1
ORDER BY part, recv_acct;

<强>结果:

+------+-----------+---------------+----------------+----------+-----------+
| PART | Recv_acct | customer_Name | invoice_number |   Cost   | Send_Acct |
+------+-----------+---------------+----------------+----------+-----------+
| QR1  |      6722 | ABC           |           3310 | 10500,00 |    307131 |
| QR1  |      6722 | ABC           |           3311 | 22400,00 |     22965 |
| QR1  |      6722 | ABC           |           3312 | 2100,00  |    307131 |
| QR1  |      6722 | ABC           |           3313 | 20300,00 |     22965 |
| QR1  |      6722 | ABC           |           3314 | 3100,00  |    307131 |
| QR2  |      6700 | DEF           |           2333 | 500,00   |     12345 |
| QR2  |      6700 | DEF           |           2244 | 4000,00  |     12345 |
| QR2  |      6700 | DEF           |           2233 | 400,00   |     54321 |
+------+-----------+---------------+----------------+----------+-----------+

<强> DEMO