1

我正在使用 postgres,但我也知道 T-SQL,所以任何人都可以给我的任何答案都将不胜感激并能够进行转换。我正在尝试为我的汽车销售公司运行查询。他们主要在 OR 和 WA 在美国各地销售汽车。我需要提供一份报告,允许我返回 OR 的新销售和 Wa 的新销售,然后我遇到的问题是所有其他非 OR 或 Wa 的州的销售。所以现在我运行一个与组的联合,其中一个查询是针对 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

4 回答 4

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 
于 2012-08-28T17:37:24.960 回答
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_otherbuyerstate,但我认为使用新名称更清楚,所以很清楚哪些地方指的是什么。)

于 2012-08-28T17:32:55.007 回答
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 
于 2012-08-28T17:37:12.203 回答
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

对于第三部分,只需使用某些东西而不是buyerstate,并且不要根据它进行分组,它应该会有所帮助。

于 2012-08-28T17:30:58.977 回答