将两个查询组合并在一个表中显示结果?

时间:2014-06-23 18:23:49

标签: sql

大家好,我遇到了一个非常简单的问题,即以下两个查询工作正常,但我想在一个表中查询两个查询的结果,那么加入这两个查询的可能方法是什么,以便它们将结合起来并将结果放在一个表中,我试了UNION但它没有用。请告诉我解决方案。谢谢!

Select ITEM_DETAILS.ITEM_MODEL,
       ITEM_DETAILS.ITEM_NAME,
       ITEM_DETAILS.ITEM_DESCRIPTION,
       ITEM_DETAILS.VENDOR_NAME,
       ITEM_DETAILS.INVOICE_NUM,
       ITEMS_MASTER.QUANTITY 
       from ITEM_DETAILS
       inner join ITEMS_MASTER 
       on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL

Select RATE=(CASE WHEN Discount IS NULL OR Discount=0 THEN RATE ELSE RATE-(RATE*(Discount/100))END),              
       AMOUNT=(CASE WHEN Discount IS NULL OR Discount=0 THEN AMOUNT ELSE AMOUNT-(AMOUNT*(Discount/100)) END)
       from ITEM_DETAILS    

2 个答案:

答案 0 :(得分:0)

如果它是 sql server 你是否尝试过这样的

Select ITEM_DETAILS.ITEM_MODEL,
       ITEM_DETAILS.ITEM_NAME,
       ITEM_DETAILS.ITEM_DESCRIPTION,
       ITEM_DETAILS.VENDOR_NAME,
       ITEM_DETAILS.INVOICE_NUM,
       ITEMS_MASTER.QUANTITY,
       CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.RATE ELSE ITEM_DETAILS.RATE-(ITEM_DETAILS.RATE*(ITEM_DETAILS.Discount/100))END AS 'RATE',
       CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.AMOUNT ELSE ITEM_DETAILS.AMOUNT-(ITEM_DETAILS.AMOUNT*(ITEM_DETAILS.Discount/100)) END AS 'AMOUNT'
       from ITEM_DETAILS
       inner join ITEMS_MASTER 
       on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL

我不确定您是想要ITEM_DETAILS表中的所有费率和金额,还是只需要满足连接条件的费用和金额

答案 1 :(得分:0)

通过查看两个查询,您需要合并结果。在这种情况下,您不需要UNION。您只需要将列组合如下

Select ITEM_DETAILS.ITEM_MODEL,
       ITEM_DETAILS.ITEM_NAME,
       ITEM_DETAILS.ITEM_DESCRIPTION,
       ITEM_DETAILS.VENDOR_NAME,
       ITEM_DETAILS.INVOICE_NUM,
       ITEMS_MASTER.QUANTITY,
       RATE=(CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.RATE ELSE ITEM_DETAILS.RATE-(ITEM_DETAILS.RATE*(ITEM_DETAILS.Discount/100))END),
       AMOUNT=(CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.AMOUNT ELSE ITEM_DETAILS.AMOUNT-(ITEM_DETAILS.AMOUNT*(ITEM_DETAILS.Discount/100)) END) 
FROM ITEM_DETAILS
       inner join ITEMS_MASTER 
       on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL