编辑---
SQL FIDDLE sqlfiddle.com/#!18/f08bd/4 由于无法使用SQL FIDDLE,我无法将所有数据加载到其中,但是以少量示例数据在某种程度上结果是正确的,但是在我的数据库上它以某种方式翻了一番。
我正在尝试显示产品的总数,重量和价格。 结果来自4个不同的表。
当我不使用join进行操作时,我得到了适当的值,但是当我使用Join时,在所有输出上都得到了双精度值。
如果没有连接,我将无法实现,因为这些结果需要全部放在一个表中。
我尝试不使用Join,而只将那些表包括在“ FROM”中,但是这引发了有关转换为数字的问题的错误。我也尝试了Union,但是没有用。
当不使用联接并且不尝试显示所有值时,我得到了所需的输出,但是缺少一些列,我故意为此进行了测试。
SELECT PriceListTest.Description, COUNT(ItemCode) AS Quantity,
SUM(Weight) AS 'Weight', Item.Pieces, PriceListTest.Price,
CAST(SUM( PriceListTest.Price * Weight) as
DECIMAL(10,2)) as 'Unit Price', CAST(SUM(PriceListTest.Price * Weight) as
DECIMAL(10,2))
AS 'Nett Amount'
FROM StockItems
INNER JOIN PriceListTest ON
StockItems.ItemCode = PriceListTest.Description
INNER JOIN Item ON StockItems.ItemCode = Item.ShortCode
WHERE Barcode IN (SELECT DISTINCT Barcode FROM StockOuttbl
WHERE ContainedID = 'isr5063' AND Status ='' GROUP BY Barcode) AND
PriceListTest.CustomerID = (SELECT DISTINCT CustomerID From Customerstbl
WHERE CustomerID ='1')
GROUP BY PriceListTest.Description, Item.Pieces, PriceListTest.Price;
在这种情况下,状态为空,所以这不是问题
我得到这些值:
Description Quantity Weight Pieces Price Unit Price Nett Amount
MAJ 52 20242 0 1.23 24897.66 24897.66
FLOCK 50 17206 0 1.23 21163.38 21163.38
这是我正在寻找的输出:
Description Quantity Weight Pieces Price Unit Price Nett Amount
MAJ 26 10121 0 1.23 12448.83 12448.83
FLOCK 25 8603 0 1.23 10581.69 10581.69
当我在Join中不使用PriceListTest时,我没有得到双打,但是那并不是我想要的。
我得到:
Description Quantity Weight Pieces
MAJ 26 10121 0
FLOCK 25 8603 0
编辑-添加了表格的数据
PriceListTest---
OID ShortCode Description CustomerID Price
7372 MAJ MAJ 1 1.23
7373 FLOCK FLOCK 1 1.23
StockItems---
TimeStamp DateStamp ItemCode Barcode ID Weight
104414357 20190701 MAJ 20190701104413935 7198 302
125350401 20190701 MAJ 20190701125349979 7220 360
125507063 20190703 MAJ 20190703125506641 7513 336
StockOutTbl---
ID AddedTimeStamp Quant Line UserID Weight Barcode Status Type StockoutTimeStamp StockoutUser TerminalStockOut TerminalAdded AddedDateStamp StockOutDateStamp ContainedID
41 115020205 NULL NULL NULL 336 20190703125506641 NULL 115020208 user 1 TC20 NULL 20190704 20190704 isr5063
Item Table ----
OID ShortCode ScreenCode Description AdminOid Kilos Pieces Inactive CategoryTitleStr BigBale
203 MAJ MAJ MAJ NULL 0 0 0 45 1
204 FLOCK FLOCK FLOCK NULL 0 0 0 45 1
不好意思,请格式化
对此我将不胜感激。预先感谢!
答案 0 :(得分:0)
使用子查询和子查询中第二个表的不同prcelist,然后使用join
并且不需要GROUP BY Barcode
插入子查询,因为您已经使用了与众不同的
SELECT PriceListTest.Description, COUNT(ItemCode) AS Quantity,
SUM(Weight) AS 'Weight', Item.Pieces, PriceListTest.Price,
CAST(SUM( PriceListTest.Price * Weight) as
DECIMAL(10,2)) as 'Unit Price', CAST(SUM(PriceListTest.Price * Weight) as
DECIMAL(10,2))
AS 'Nett Amount'
FROM StockItems
INNER JOIN( select distinct * from PriceListTest) as PriceListTest ON
StockItems.ItemCode = PriceListTest.Description
INNER JOIN Item ON StockItems.ItemCode = Item.ShortCode
WHERE Barcode IN (SELECT DISTINCT Barcode FROM StockOuttbl
WHERE ContainedID = 'isr5063' AND Status ='' ) AND
PriceListTest.CustomerID = (SELECT DISTINCT CustomerID From Customerstbl
WHERE CustomerID ='1')
GROUP BY PriceListTest.Description, Item.Pieces, PriceListTest.Price;