从SQL获取数据

时间:2013-10-04 07:41:33

标签: sql sql-server-2008

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列Vat 5%rate Amount = vat type id 2和Vat 15%rate Amount = vat Type 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

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

我希望此数量显示在列

中的以下数据中

表关系是

在表BillItem表中,我有Item Id和TableOrder Id 在项目表中,我有VatTypeId

1 个答案:

答案 0 :(得分: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 

了解这对您有何帮助。