5
Select 
   Id, 
   ROW_NUMBER() over(Order By(Select 1)) as SNo,  
   Tableno as 'Table Number',  
   convert(Date, tableorder.Date) as Date,  
   (LTRIM(RIGHT(CONVERT(VARCHAR(20), tableorder.Date, 100), 7))) as Time,  
   case 
       when TableOrder.Status = 1 then 'Open' 
       when TableOrder.Status = 0  then 'Close'
       else 'Undifined'
   end As 'Order Status', 
   KotNO as 'Kot Number', 
   (Select SUM(NetAmount) 
    from Bill 
    where Bill.OrderId = TableOrder.Id) as 'Total  Amount' 
from 
    TableOrder 
Where 
    IsActive = '1' And IsDelete = '0'

此查询返回以下数据:

Id  SNo  Table Number  Date        Time     Order Status  Kot Number  Total  Amount
318 1    1             4/3/2013    12:00AM  Close         1218        270
319 2    1             4/3/2013    12:00AM  Close         7581        335
320 3    1             4/3/2013    12:00AM  Close         7582        110
321 4    1             4/3/2013    12:00AM  Close         7585        165
323 5    4             4/3/2013    12:00AM  Close         7586        80
324 6    1             4/3/2013    12:00AM  Close         7587        45
325 7    3             4/3/2013    12:00AM  Close         7588        150
326 8    1             4/3/2013    12:00AM  Close         7589        145
327 9    1             4/3/2013    12:00AM  Close         7590        70
328 10   4             4/3/2013    12:00AM  Close         7591        120

我想在此查询中添加 2 列增值税 5% 税率金额 = 增值税类型 id 2 和增值税 15% 税率金额 = 增值税类型 id 4

我在这个查询中有第二个这样的查询我使用 TableOrder id = 319

Select 
    Vt.Id, Vt.Description,
    abs(Vt.Rate) as VRate,
    Sum((( ItemPrice * Qty) - NetAmount)) as VatAmount 
from 
    BillItem1 as B1 
Left JOIN 
    ItemDescription ItD ON ItD.Id = B1.itemId 
Left Join 
    VatType Vt on Vt.Id = ItD.TaxId 
where 
    B1.IsActive = 1 and B1.IsDelete = 0 
    and B1.OrderId = 319 
Group By 
    Vt.Id, Vt.Rate, Vt.Description 
Order By 
    SUM((ItemPrice*Qty) - NetAmount) DESC

输出:

Id  Description  VRate  VatAmount
2   Food         5      8.8094
4   Cold drinks  15     7.143

在此查询中,增值税类型 ID = 2 的增值税为 5%,增值税类型 ID = 4 的增值税为 15%,在上述数据 390 TableOrder Id 显示总金额 270 和增值税金额为 (8.8094+7.143)

我希望此金额显示在以下列的数据中

表关系是

在表 BillItem 表中我有 Item Id 和 TableOrder Id
在 Item 表中我有 VatTypeId

4

1 回答 1

0

我想我明白你在追求什么。尝试以下...

SELECT
    Id, 
    ROW_NUMBER() OVER(ORDER BY(SELECT 1)) as SNo,  
    Tableno AS 'Table Number',  
    convert(DateTime, tableorder.Date) AS [Date],  
    (LTRIM(RIGHT(CONVERT(VARCHAR(20), tableorder.Date, 100), 7))) as [Time],  
    CASE
        WHEN TableOrder.Status = 1 THEN 'Open' 
        WHEN TableOrder.Status = 0 THEN 'Close'
        ELSE 'Undefined'
    END AS 'Order Status', 
    KotNO AS 'Kot Number', 
    SUM(Bill.NetAmount) AS 'Total Amount', 
    SUM(Bill.NetAmount) * 0.05 AS 'Tax 5', 
    SUM(Bill.NetAmount) * 0.15 AS 'Tax 15' 

FROM
    TableOrder INNER JOIN 
    Bill 
        ON TableOrder.Id = Bill.OrderId 

WHERE 
    IsActive = '1' And 
    IsDelete = '0' 

GROUP BY 
    Id, 
    Tableno,  
    TableOrder.[Date],  
    TableOrder.Status, 
    KotNO 

看看它是如何为你工作的。

于 2013-10-04T17:43:48.867 回答