麻烦帮看下下面sql语句那里错了,可以的话,顺便教下怎么写sql语句查询northwind数据库每个季度的销售情况,谢谢!
select year(ShippedDate) as 年份,
第一季度=(select count(UnitPrice*Quantity*(1-Discount)) 
from Orders,[Order Details] 
where Orders.OrderID=[Order Details].OrderID 
and month(ShippedDate) in (1,2,3)),
--第二季度=,--
--第三季度=,--
--第四季度=,--
from Orders
group by 年份
结果提示:消息 156,级别 15,状态 1,第 9 行
关键字 'from' 附近有语法错误。

解决方案 »

  1.   

    select year(ShippedDate) as 年份,
    第一季度=(select count(UnitPrice*Quantity*(1-Discount))  
    from Orders,[Order Details]  
    where Orders.OrderID=[Order Details].OrderID  
    and month(ShippedDate) in (1,2,3))
    --第二季度=,--
    --第三季度=,--
    --第四季度=,--
    from Orders
    group by 年份
      

  2.   

    select year(ShippedDate) as 年份,
    第一季度=(select count(UnitPrice*Quantity*(1-Discount))  
    from Orders,[Order Details]  
    where Orders.OrderID=[Order Details].OrderID  
    and month(ShippedDate) in (1,2,3)),--多个逗号
    --第二季度=,--
    --第三季度=,--
    --第四季度=,--
    from Orders
    group by 年份
      

  3.   

    不对.这样.select year(ShippedDate) as 年份,
           sum(case month(ShippedDate) when 1 then UnitPrice*Quantity*(1-Discount) else 0 end) [1],
           sum(case month(ShippedDate) when 2 then UnitPrice*Quantity*(1-Discount) else 0 end) [2],
           sum(case month(ShippedDate) when 3 then UnitPrice*Quantity*(1-Discount) else 0 end) [3],
           sum(case month(ShippedDate) when 4 then UnitPrice*Quantity*(1-Discount) else 0 end) [4]
    from Orders,[Order Details]  
    where Orders.OrderID=[Order Details].OrderID  
    group by year(ShippedDate)
      

  4.   

    1,2楼都没仔细看你的内容.后面不能group by 年份,得用group by year(ShippedDate)
      

  5.   

    需求好像不对.
    罗斯文数据库:
    select year(ShippedDate) as 年份,
    第一季度=(select count(UnitPrice*Quantity*(1-Discount))  
    from Orders,[Order Details]  
    where Orders.OrderID=[Order Details].OrderID  
    and month(ShippedDate) in (1,2,3))
    --第二季度=,--
    --第三季度=,--
    --第四季度=,--
    from Orders
    group by year(ShippedDate)
    /*
    年份          第一季度
    ----------- -----------
    1998        686
    1996        686
    1997        686
    NULL        686(4 行受影响)*/
      

  6.   

    select year(ShippedDate) as 年份,(month(ShippedDate)-1)%3+1 嫉妒,
    销售=sum(UnitPrice*Quantity*(1-Discount)
    from Orders,[Order Details]   
    where Orders.OrderID=[Order Details].OrderID
    group by year(ShippedDate),(month(ShippedDate)-1)%3
      

  7.   

    select year(a.OrderDate) as 年份,
    第一季度=sum(case when month(a.OrderDate) in (1,2,3) then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end),
    第二季度=sum(case when month(a.OrderDate) in (4,5,6) then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end),
    第三季度=sum(case when month(a.OrderDate) in (7,8,9) then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end),
    第四季度=sum(case when month(a.OrderDate) in (10,11,12) then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end)
    from Orders a inner join [Order Details] b on a.orderid=b.orderid
    group by year(OrderDate)
    /*
    年份          第一季度                   第二季度                   第三季度                   第四季度
    ----------- ---------------------- ---------------------- ---------------------- ----------------------
    1998        298491.552920341       142132.312679291       0                      0
    1996        0                      0                      79728.5699167252       128355.399736404
    1997        138288.925440788       143177.045089722       153937.770341873       181681.463493347(3 行受影响)*/