这很有效。但我还想包括那个日期范围内没有销售的商品。目前仅显示在所选日期范围内销售的商品。我希望它能展示那些没有销售的商品。
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<import type="android.databinding.ObservableMap" />
<variable
name="map"
type="ObservableMap" />
</data>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{(String)map[`text`]}"/>
</layout>
根据评论进行更新:
已售出的商品被标识为SELECT i.item_number, i.item_name, ish.avg_cost, ish.avg_retail, ish.avg_daily_sales,
CASE WHEN i.sold_by_id = '0' THEN 'pcs'
WHEN i.sold_by_id = '1' THEN 'lbs'
WHEN i.sold_by_id = '2' THEN 'lbs'
END [Sold_by]
FROM inventory i
INNER JOIN
(SELECT ish.item_number, AVG(avg_cost) as avg_cost, AVG(avg_retail) as avg_retail, ROUND(AVG(total_sold),0) as avg_daily_sales
FROM inventory_sales_history ish
LEFT JOIN Inventory i ON i.item_number = ish.Item_Number
WHERE ish.the_date BETWEEN '12-11-2016' AND '12-18-2016'
AND ish.item_number IN (SELECT item_number FROM inventory i JOIN department d ON d.department_id = i.department_id WHERE d.category_id = '4')
GROUP BY ish.item_number)ish
ON i.item_number = ish.item_number
ORDER BY i.item_name
表中的商品,但不在inventory
表中
答案 0 :(得分:2)
这与DVT的答案相同,因为它将连接更改为从内连接到左连接的子查询。我只是在查看它时稍微重构了一下这个查询。
您可能需要查看Bad habits to kick : mis-handling date / range queries - Aaron Bertrand。
编辑:将部门联接移动到外部查询
select
i.item_number
, i.item_name
, ish.avg_cost
, ish.avg_retail
, ish.avg_daily_sales
, Sold_by = case
when i.sold_by_id = '0' then 'pcs'
when i.sold_by_id = '1' then 'lbs'
when i.sold_by_id = '2' then 'lbs'
end
from inventory i
inner join department d on d.department_id = i.department_id
and d.category_id='4'
left join (
select
ish.item_number
, avg_cost = avg(avg_cost)
, avg_retail = avg(avg_retail)
, avg_daily_sales = round(avg(total_sold), 0)
from inventory_sales_history ish
where ish.the_date between '12-11-2016' and '12-18-2016'
group by ish.item_number
) as ish on i.item_number = ish.item_number
order by i.item_name
答案 1 :(得分:1)
SELECT i.item_number, i.item_name, ish.avg_cost, ish.avg_retail, ish.avg_daily_sales,
CASE WHEN i.sold_by_id = '0' THEN 'pcs'
WHEN i.sold_by_id = '1' THEN 'lbs'
WHEN i.sold_by_id = '2' THEN 'lbs'
END [Sold_by]
FROM inventory i
LEFT JOIN
(SELECT ish.item_number, AVG(avg_cost) as avg_cost, AVG(avg_retail) as avg_retail, ROUND(AVG(total_sold),0) as avg_daily_sales
FROM inventory_sales_history ish
LEFT JOIN Inventory i ON i.item_number = ish.Item_Number
WHERE ish.the_date BETWEEN '12-11-2016' AND '12-18-2016'
AND ish.item_number IN (SELECT item_number FROM inventory i JOIN department d ON d.department_id = i.department_id WHERE d.category_id = '4')
GROUP BY ish.item_number)ish
ON i.item_number = ish.item_number
ORDER BY i.item_name
答案 2 :(得分:1)
知道了。你的建议指出了我正确的方向。
SELECT i.item_number
, i.item_name
, ish.avg_cost
, ish.avg_retail
, ish.avg_daily_sales
, CASE WHEN i.sold_by_id = '0' THEN 'pcs'
WHEN i.sold_by_id = '1' THEN 'lbs'
WHEN i.sold_by_id = '2' THEN 'lbs'
END [Sold_by]
FROM inventory i LEFT JOIN
(
SELECT ish.item_number
, AVG(avg_cost) as avg_cost
, AVG(avg_retail) as avg_retail
, ROUND(AVG(total_sold),0) as avg_daily_sales
FROM inventory_sales_history ish LEFT JOIN Inventory i
ON i.item_number = ish.Item_Number
WHERE ish.the_date BETWEEN '12-11-2016' AND '12-18-2016'
GROUP BY ish.item_number
)
ish ON i.item_number = ish.item_number
WHERE i.item_number IN
(
SELECT item_number
FROM inventory i JOIN department d
ON d.department_id = i.department_id
WHERE d.category_id = '4'
)
ORDER BY i.item_name