日期:2014-05-18  浏览次数:20631 次

着急问阿,在线等啊!!!!SQL问题
我现在的SQL是这样的:
SELECT   Operator,   Flag,   SUM(TotalMoney)   AS   TotalMoney,   COUNT(*)   AS   COUNTS
FROM   PawnM
WHERE   (FromDate   > =   '2003-01-1 ')   AND   (ToDate   <=   '2007-7-17 ')
GROUP   BY   Operator,   Flag  

查询结果:
Operator     Flag     TotalMoney     Counts
操作员1         0             100                 12
操作员2         1             200                 13
操作员1         1             300                 14
操作员1         2             400                 15  

我想实现的查询结果如下:
Operator   TotalMoney     Counts   TotalMoney2     Counts2     TotalMoney3     Counts3  
操作员1         0                     100                 300             14                   400                 15
操作员2         1                     200                 0                   0                   0                       0

Flag:0--9
“TotalMoney2     Counts2     TotalMoney3     Counts3   ”这些字段PawnM表中没有
请问在一个SQL语句中能实现吗?怎么写?

------解决方案--------------------
SELECT Operator, SUM(TotalMoney) AS TotalMoney1, COUNT(*) AS COUNTS1,
' ' AS TotalMoney2, ' ' AS COUNTS2,
' ' AS TotalMoney3, ' ' AS COUNTS3

FROM PawnM
WHERE (FromDate > = '2003-01-1 ') AND (ToDate <= '2007-7-17 ') anf flag =0
GROUP BY Operator, Flag

union

SELECT Operator, ' ' as TotalMoney1, ' 'AS COUNTS1,
SUM(TotalMoney) AS TotalMoney2, COUNT(*) AS COUNTS2,
' ' AS TotalMoney3, ' ' AS COUNTS3
FROM PawnM
WHERE (FromDate > = '2003-01-1 ') AND (ToDate <= '2007-7-17 ') anf flag =1
GROUP BY Operator, Flag

union

SELECT Operator, ' ' as TotalMoney1, ' 'AS COUNTS1,// 不行的话,把 ' '改为0
' ' AS TotalMoney2, ' ' AS COUNTS2
SUM(TotalMoney) AS TotalMoney3, COUNT(*) AS COUNTS3,
FROM PawnM
WHERE (FromDate > = '2003-01-1 ') AND (ToDate <= '2007-7-17 ') anf flag =2
GROUP BY Operator, Flag

union

...

就这样写,自己试试


------解决方案--------------------
select Operator, SUM(TotalMoney1) AS TotalMoney1, COUNT(COUNTS1) AS COUNTS1,
SUM(TotalMoney2) AS TotalMoney2, COUNT(COUNTS2) AS COUNTS2,
SUM(TotalMoney3) AS TotalMoney3, COUNT(COUNTS23) AS COUNTS3

from
(


SELECT Operator, SUM(TotalMoney) A