我正在使用 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