背景:我需要将此查询修改为仅输出不平衡为零的发票号(可能是+/-)。我还需要输出以包括发票编号上的所有项目,这些项目不会平衡为零(无分组)。
所以,如果发票余额,则将其从输出中抑制。
查询:
SELECT invoices.account, invoices.invoice_no, transact.item, transact.date_time, transact.operator, transact.salespoint, transact.extension
FROM transact INNER JOIN
invoices ON transact.invoice_no = invoices.invoice_no
WHERE (invoices.account = '*GUESTS*') AND (transact.extension <> 0))
ORDER BY invoices.invoice_no DESC
输出:
account invoice_no item date_time operator salespoint extension
Test 1 **TRANS** 1/0/00 12:25 AM SUNNY RTL2 $(2.69)
Test 1 BT_DIET 1/0/00 12:25 AM SUNNY RTL2 $2.69
Test 2 **TRANS** 1/0/00 12:08 AM SUNNY RTL2 $(14.55)
Test 2 **TRANS** 1/0/00 12:08 AM SUNNY RTL2 $(1.00)
Test 2 QUICHE 1/0/00 12:08 AM SUNNY RTL2 $7.01
Test 2 FRUITSALAD 1/0/00 12:08 AM SUNNY RTL2 $7.54
Test 2 **TIPS** 1/0/00 12:08 AM SUNNY RTL2 $1.00
Test 3 **TRANS** 1/0/00 12:07 AM SUNNY RTL2 $(40.67)
Test 3 BURRITO 1/0/00 12:07 AM SUNNY RTL2 $16.17
Test 3 ENGMUFFSAU 1/0/00 12:07 AM SUNNY RTL2 $7.54
Test 3 DANISH 1/0/00 12:07 AM SUNNY RTL2 $4.30
Test 3 SUMPLYJUIC 1/0/00 12:07 AM SUNNY RTL2 $6.47
Test 3 SUMPLYJUIC 1/0/00 12:07 AM SUNNY RTL2 $3.23
Test 3 COFFEE_CUP 1/0/00 12:07 AM SUNNY RTL2 $2.96
Test 4 QUICHE 1/0/00 12:01 AM SUNNY RTL2 $7.01
Test 4 DANISH 1/0/00 12:07 AM SUNNY RTL2 $4.30
期望的输出:
account invoice_no item date_time operator salespoint extension
Test 4 QUICHE 1/0/00 12:01 AM SUNNY RTL2 $7.01
Test 4 DANISH 1/0/00 12:07 AM SUNNY RTL2 $4.30
此致 DH
答案 0 :(得分:0)
我会使用窗口/分析函数,如果这是一个选项。然后你可以通过两个步骤来解决这个问题:
df[cbind(seq(df$var), df$var)]
[1] "3050" "2062" "1036" "4001" "3075" "4083" "1085" "3061"
sum(extension) over (partition by invoice_no) as invoice_balance
答案 1 :(得分:0)
;WITH list AS (
SELECT invoices.account, invoices.invoice_no, transact.item, transact.date_time, transact.operator, transact.salespoint, transact.extension
, SUM(transact.salespoint) OVER (PARTITION BY transact.item ORDER BY transact.item) AS Total
FROM transact INNER JOIN
invoices ON transact.invoice_no = invoices.invoice_no
WHERE (invoices.account = '*GUESTS*') AND (transact.extension <> 0)
)
SELECT *
FROM list
WHERE Total > 0
ORDER BY invoice_no DESC
答案 2 :(得分:0)
问题是扩展名不是数字。您需要编写一个存储函数,该函数可以正确地将任何值从扩展转换为数值。您还需要group by
,因为0的数量是一个聚合术语。所以,你需要这样的东西:
SELECT account, invoice_no, item, date_time, operator, salespoint, extension
FROM transact INNER JOIN
invoices ON transact.invoice_no = invoices.invoice_no
group by invoices.account, invoices.invoice_no, transact.item, transact_date_time, transact.operator, transact.salespoint, myfunction(transact.extension) as extension
having (account = '*GUESTS*') AND (sum(extension) <> 0))
ORDER BY invoice_no DESC