SQL组的结果不相等

时间:2012-08-28 17:20:07

标签: sql tsql postgresql

我正在使用postgres,但我也知道T-SQL,所以任何人都可以给我生病的答案,感激并能够进行转换。我正在尝试为我的公司运行查询,这是一家汽车销售公司。他们主要在美国东部和西澳大利亚州销售汽车。我需要提出一份报告,允许我返回OR新的销售和Wa的新销售,然后我遇到的问题是所有其他状态不是OR或Wa的销售。所以现在我正在运行与group的联合,其中一个查询用于WA,另一个语句用于OR。我希望结果看起来非常简单,只有3行。 1 OR 1 WA和第3行所有其他状态相结合。我无法弄清楚如何将不等于OR或WA组合成一个单独的值。这甚至可能吗?

感谢您抽出宝贵时间来看看这个

我的查询还将包括二手销售汽车,但是一旦我弄清楚如何解决这个问题,我就可以整合它,这就是为什么查询有多个联盟

这是我现在的查询:

SELECT buyerstate        AS "State", 
       Count(buyerstate) AS "Acura", 
       saletype          AS "N/U" 
FROM   lydeal 
WHERE  stocklocationid = '8' 
       AND buyerstate = 'OR' 
       AND saledate > '8/01/12' 
       AND saletype = 'N' 
GROUP  BY buyerstate, 
          stocklocationid, 
          saletype 
UNION 
SELECT buyerstate        AS "State", 
       Count(buyerstate) AS "Acura", 
       saletype          AS "N/U" 
FROM   lydeal 
WHERE  stocklocationid = '8' 
       AND buyerstate = 'OR' 
       AND saledate > '8/01/12' 
       AND saletype = 'U' 
GROUP  BY buyerstate, 
          stocklocationid, 
          saletype 
UNION 
SELECT buyerstate        AS "State", 
       Count(buyerstate) AS "Acura", 
       saletype          AS "N/U" 
FROM   lydeal 
WHERE  stocklocationid = '8' 
       AND buyerstate = 'WA' 
       AND saledate > '8/01/12' 
       AND saletype = 'N' 
GROUP  BY buyerstate, 
          stocklocationid, 
          saletype 
UNION 
SELECT buyerstate        AS "State", 
       Count(buyerstate) AS "Acura", 
       saletype          AS "N/U" 
FROM   lydeal 
WHERE  stocklocationid = '8' 
       AND buyerstate = 'WA' 
       AND saledate > '8/01/12' 
       AND saletype = 'U' 
GROUP  BY buyerstate, 
          stocklocationid, 
          saletype 

4 个答案:

答案 0 :(得分:3)

试试这个

SELECT CASE WHEN buyerstate IN ('OR','Wa') THEN buyerstate ELSE 'OTHER' END   AS "State", 
   Count(buyerstate) AS "Acura", 
   saletype          AS "N/U" 
FROM   lydeal 
WHERE  stocklocationid = '8' 
   AND saledate > '8/01/12' 
   AND saletype = 'N' 
GROUP  BY CASE WHEN buyerstate IN ('OR','Wa') THEN buyerstate ELSE 'OTHER' END, 
      stocklocationid, 
      saletype 

答案 1 :(得分:1)

您可以使用CASE表达式:

SELECT CASE WHEN buyerstate IN ('OR', 'WA')
            THEN buyerstate
            ELSE 'other'
        END AS OR_or_WA_or_other,
       ...
  FROM lydeal
 WHERE ...
 GROUP
    BY CASE WHEN buyerstate IN ('OR', 'WA')
            THEN buyerstate
            ELSE 'other'
        END,
       ...
 ORDER
    BY OR_or_WA_or_other
;

(您实际上可以将OR_or_WA_or_other更改为buyerstate,但我认为使用新名称更清楚,因此很清楚哪些地方指的是什么。)

答案 2 :(得分:1)

我认为你应该这样做:

SELECT buyerstate        AS "State", 
       Count(buyerstate) AS "Acura",
       saletype          AS "N/U" 
FROM   lydeal 
WHERE  stocklocationid = '8' 
       AND (
          (buyerstate in ('OR', 'WA') AND saledate > '8/01/12' AND saletype in ('N', 'U')) OR //Only new sales for Or and Wa
          (buyerstate not in ('OR', 'WA')) //all other sales
       )
       AND saletype = 'N' 
GROUP  BY buyerstate, 
          stocklocationid, 
          saletype 

答案 3 :(得分:0)

您可以尝试这样的事情:

select buyerstate as "State",count(buyerstate) as "Acura", saletype as "N/U"
from lydeal
where stocklocationid = '8' and buyerstate = 'OR' and saledate > '8/01/12' and saletype = 'N'
group by buyerstate,stocklocationid,saletype
union

select buyerstate as "State",count(buyerstate) as "Acura", saletype as "N/U"
from lydeal
where stocklocationid = '8' and buyerstate = 'WA' and saledate > '8/01/12' and saletype = 'N'
group by buyerstate,stocklocationid,saletype

union
select 'Others' as "State",count(buyerstate) as "Acura", saletype as "N/U"
from lydeal
where stocklocationid = '8' and buyerstate <> 'WA' and buyerstate <> 'OR'  and saledate > '8/01/12' and saletype = 'N'
group by stocklocationid,saletype

对于第三部分,只使用一些东西而不是买主,并且不要在其上创建一个组,它应该有所帮助。