我对SQL查询还很陌生,可以使用一些帮助。
我有两个不同的SQL查询,我希望两个结果都在一个结果中。
查询1:
SELECT
gbkmut.bkstnr_sub,
MAX(orkrg.refer) AS oms25,
MAX(gbkmut.docnumber) AS YourRef,
MIN(gbkmut.datum) AS datum,
cicmpy.debcode AS delv_debnr,
MAX(cicmpy.cmp_name) AS cmp_name,
MAX(gbkmut.datum) AS latest_deldat,
FROM
gbkmut
INNER JOIN
orkrg ON gbkmut.bkstnr_sub = orkrg.ordernr
INNER JOIN
cicmpy ON orkrg.verzdebnr = cicmpy.debnr
INNER JOIN
items ON gbkmut.artcode = items.itemcode AND items.type <> 'P'
INNER JOIN
magaz ON gbkmut.warehouse = magaz.magcode AND magaz.blokkeer = 0
WHERE
(gbkmut.transsubtype IN ('B','H')
AND gbkmut.freefield1 = 'V'
AND gbkmut.Blockitem = 0
AND gbkmut.afldat <= GETDATE())
AND gbkmut.Checked = 1
AND (orkrg.ordbv_afdr = 0 OR (orkrg.ordbv_afdr = 1 AND orkrg.ordbv_afgd = 1))
GROUP BY
gbkmut.bkstnr_sub,
gbkmut.afldat,
cicmpy.debcode
ORDER BY
gbkmut.bkstnr_sub DESC
查询2:
SELECT TOP 1000
[Debtor code],
[Outstanding Amount]
FROM
[456].[dbo].[GRV_DebtorOutstanding]
[债务人代码]和cicmpy.debcode对于联接是相同的
两个查询的一些示例结果:
查询1列:
Ordernumber, Order description, Your reference, Orderdate, Debtor code, Debtor name, Order delivery date
20093 NULL Bestel.: 20093 2018-07-31 00:00:00.000 60089 Restaurant Enkhuizen 2018-07-31 00:00:00.000
20092 NULL Bestel.: 20092 2018-07-31 00:00:00.000 60089 Restaurant Enkhuizen 2018-07-31 00:00:00.000
20088 NULL Bestel.: 20088 2018-07-05 00:00:00.000 60105 Brasserie Pigalle 2018-07-05 00:00:00.000
查询2:
列:债务人代码,未偿还总额
Debtor code Outstanding Amount
60086 -30,65
60092 -121
60089 -5000
60130 1000
我想将总未付金额添加到第一个查询中。
我尝试了一次子选择,但出现错误:
子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
由于列数不同,工会可能也不可能。
我想将此用于报告目的,当然可以通过Excel和vlookup解决它,但是我想在SQL中进行操作以了解有关SQL的更多信息。
任何帮助都会很棒。谢谢
答案 0 :(得分:1)
将它们放在子查询中,然后将它们加入在一起
SELECT * FROM
(
SELECT
gbkmut.bkstnr_sub,
MAX(orkrg.refer) AS oms25,
MAX(gbkmut.docnumber) AS YourRef,
MIN(gbkmut.datum) AS datum,
cicmpy.debcode AS delv_debnr,
MAX(cicmpy.cmp_name) AS cmp_name,
MAX(gbkmut.datum) AS latest_deldat,
FROM
gbkmut
INNER JOIN
orkrg ON gbkmut.bkstnr_sub = orkrg.ordernr
INNER JOIN
cicmpy ON orkrg.verzdebnr = cicmpy.debnr
INNER JOIN
items ON gbkmut.artcode = items.itemcode AND items.type <> 'P'
INNER JOIN
magaz ON gbkmut.warehouse = magaz.magcode AND magaz.blokkeer = 0
WHERE
(gbkmut.transsubtype IN ('B','H')
AND gbkmut.freefield1 = 'V'
AND gbkmut.Blockitem = 0
AND gbkmut.afldat <= GETDATE())
AND gbkmut.Checked = 1
AND (orkrg.ordbv_afdr = 0 OR (orkrg.ordbv_afdr = 1 AND orkrg.ordbv_afgd = 1))
GROUP BY
gbkmut.bkstnr_sub,
gbkmut.afldat,
cicmpy.debcode
ORDER BY
gbkmut.bkstnr_sub DESC
) T1
JOIN
(
SELECT TOP 1000
[Debtor code],
[Outstanding Amount]
FROM
[456].[dbo].[GRV_DebtorOutstanding]
) T2 ON T2.[Debtor code] = T1.delv_debnr
答案 1 :(得分:0)
您可以通过将第二个查询设为公用表表达式来实现此目的:
WITH agg
AS (SELECT TOP 1000
[Debtor code]
, [Outstanding Amount]
FROM [456].[dbo].[GRV_DebtorOutstanding])
SELECT gbkmut.bkstnr_sub
, MAX(orkrg.refer) AS oms25
, MAX(gbkmut.docnumber) AS YourRef
, MIN(gbkmut.datum) AS datum
, cicmpy.debcode AS delv_debnr
, MAX(cicmpy.cmp_name) AS cmp_name
, MAX(gbkmut.datum) AS latest_deldat
, ISNULL(agg.[Outstanding Amount],0) AS [Outstanding Amount]
FROM gbkmut
INNER JOIN orkrg ON gbkmut.bkstnr_sub = orkrg.ordernr
INNER JOIN cicmpy ON orkrg.verzdebnr = cicmpy.debnr
INNER JOIN items ON gbkmut.artcode = items.itemcode
AND items.type <> 'P'
INNER JOIN magaz ON gbkmut.warehouse = magaz.magcode
AND magaz.blokkeer = 0
LEFT OUTER JOIN agg ON agg.[Debtor code] = cicmpy.debcode
WHERE (
gbkmut.transsubtype IN ( 'B', 'H' )
AND gbkmut.freefield1 = 'V'
AND gbkmut.Blockitem = 0
AND gbkmut.afldat <= GETDATE()
)
AND gbkmut.Checked = 1
AND
(
orkrg.ordbv_afdr = 0
OR
(
orkrg.ordbv_afdr = 1
AND orkrg.ordbv_afgd = 1
)
)
GROUP BY gbkmut.bkstnr_sub
, gbkmut.afldat
, cicmpy.debcode
, agg.[Outstanding Amount]
ORDER BY gbkmut.bkstnr_sub DESC;
答案 2 :(得分:0)
Select * from (
SELECT
gbkmut.bkstnr_sub,
MAX(orkrg.refer) AS oms25,
MAX(gbkmut.docnumber) AS YourRef,
MIN(gbkmut.datum) AS datum,
cicmpy.debcode AS delv_debnr,
MAX(cicmpy.cmp_name) AS cmp_name,
MAX(gbkmut.datum) AS latest_deldat,
FROM
gbkmut
INNER JOIN
orkrg ON gbkmut.bkstnr_sub = orkrg.ordernr
INNER JOIN
cicmpy ON orkrg.verzdebnr = cicmpy.debnr
INNER JOIN
items ON gbkmut.artcode = items.itemcode AND items.type <> 'P'
INNER JOIN
magaz ON gbkmut.warehouse = magaz.magcode AND magaz.blokkeer = 0
WHERE
(gbkmut.transsubtype IN ('B','H')
AND gbkmut.freefield1 = 'V'
AND gbkmut.Blockitem = 0
AND gbkmut.afldat <= GETDATE())
AND gbkmut.Checked = 1
AND (orkrg.ordbv_afdr = 0 OR (orkrg.ordbv_afdr = 1 AND orkrg.ordbv_afgd = 1))
GROUP BY
gbkmut.bkstnr_sub,
gbkmut.afldat,
cicmpy.debcode
Union all
SELECT TOP 1000
bkstnr_sub as '',
oms25 as 0,
YourRef as 0,
datum as 0,
delv_debnr as 0,
cmp_name as ''
[Debtor code],
latest_deldat as NULL
[Outstanding Amount]
FROM
[456].[dbo].[GRV_DebtorOutstanding]
) as outertable