假设我有两张桌子
我想选择产品价值的总和乘以税表中各自的增值税值,作为产品日期小于税收日期且mDate
小于产品日期的百分比,按增值税分组或ttid
例如
Taxes products
ttid date value mDate Vat value date
0 02-06-11 20 03-07-11 0 15 01-07-11
1 03-07-11 10 03-08-11 0 15 01-07-11
0 03-07-11 14 03-08-11 0 15 02-08-11
1 03-08-11 15 03-09-11 1 10 04-07-11
1 10 06-08-11
1 10 08-09-11 --this will not include
所以我的结果会是这样的。
TTId Value
0 3+3+2.1=8.1
1 1.5+1=2.5
到目前为止我写的查询。
select sum
(
(cast(t.Value as float)
*
(cast(x.value as float)/100)
)
as 'Vat'
,x.ttid
from prducts t
,taxes x where x.ttid=t.vat and x.mDate>t.date and x.date<=t.date
group by x.ttid
有人会帮助我吗?
答案 0 :(得分:2)
这里有一些问题:
正如评论中所述,您已+
后跟*
在税表中date
的值始终小于mdate
。您的WHERE
测试有x.date>t.datee and x.mDate<=t.date
,表示x.date
大于x.mDate
。这将永远是假的
你在t.date
条款中加了WHERE
个额外的e
您拼错了products
作为表格的名称。
此外,使用t
作为products
和x
的别名作为tax
的别名实在令人困惑。
答案 1 :(得分:1)
SELECT
SUM( t.Value *
ISNULL((SELECT TOP 1 value FROM taxes x where x.ttid=t.vat and t.date BETWEEN x.date AND x.mDate order by t.datee), 100)/100) as 'Vat'
FROM prducts t
答案 2 :(得分:0)
select sum(Vat)'Vat',itemvat,sum(productvalue)'ProductValue' from
(
SELECT
t.vat,
t.value,
isnull(t.value *
(SELECT TOP 1 value FROM #TMP x where x.ttid=t.vat and
convert(varchar,cast(x.effectiveDate as datetime),3)>convert(varchar,cast(t.date as datetime),3)
and convert(varchar,cast(x.mDate as datetime),3)<=convert(varchar,cast(t.date as datetime),3)
)/100,'5')
as 'Vat'
FROM products t
)
as t where Vat<>'5' group by itemvat