简体   繁体   English

使用SQL中的case语句根据某些条件对列进行分组

[英]Grouping Columns based on some condition using case statement in sql

I am trying to group some columns in certain way based on some criteria. 我试图根据某些条件以某种方式对某些列进行分组。 My criteria in this situation is: if Category_Name is "Performance" i want "Sales" and "Cost" to go in the Grouping column and their values to go in the Amount column. 在这种情况下,我的标准是:如果Category_Name为“ Performance”,我希望将“ Sales”和“ Cost”放入“ Grouping”列,并将其值放入“ Amount”列。 If Category_Name is "Quality" i want the "Rev" to go into Grouping column and its value to go into the Amount column. 如果Category_Name为“质量”,则我希望“版本”进入“分组”列,其值进入“数量”列。 Please note that i am using Netezza as my database. 请注意,我正在使用Netezza作为数据库。 Here is what i have now: CURRENT TABLE OR CURRENT ISSUE: 这是我现在所拥有的:当前表或当前问题:

在此处输入图片说明

DESIRED RESULTS: 所需结果:

在此处输入图片说明

PS I AM USING NETEZZA PS我正在使用NETEZZA

The example and solution below were done with ANSI SQL in SQL Server 2012. This should work fine in NETEZZA if it is ANSI compliant. 下面的示例和解决方案是使用SQL Server 2012中的ANSI SQL完成的。如果它符合ANSI,则在NETEZZA中应该可以正常工作。

First, create a sample table in TEMPDB and load with data. 首先,在TEMPDB中创建一个示例表并加载数据。 This is SQL Server specific. 这是特定于SQL Server的。 Can be any testing database you create. 可以是您创建的任何测试数据库。

-- Use temp db
USE TEMPDB;
GO

-- Create a temp table
CREATE TABLE T1
(
  LOCATION_CODE CHAR(2),
  PART_NUM CHAR(2),
  CATEGORY_NAME VARCHAR(25),
  YEAR_NUM INT,
  SALES_NUM INT,
  COST_NUM INT,
  REV_NUM INT
);

-- Add 3 row of data
INSERT INTO T1 
VALUES ('NY', '1A', 'Performance', 2001, 100, 35, 45);

INSERT INTO T1 
VALUES ('IL', '2E', 'Quality', 2002, 250, 150, 44);

INSERT INTO T1 
VALUES ('IL', '4F', 'Performance', 2003, 355, 280, 33);
GO

-- Show the data
SELECT * FROM T1;
GO

Basically, what you want to do is re-organize the data. 基本上,您要做的是重新组织数据。 Make two different rows for Performance data and one row for Quality data. 为效果数据制作两行,为质量数据制作一行。 Then order all the rows by location and part number. 然后按位置和零件号对所有行进行排序。

The UNION ALL is what you want. UNION ALL是您想要的。 This statement does not remove duplicates between result sets. 该语句不会删除结果集之间的重复项。 Three queries one for each case, a UNION ALL between them, and a ORDER BY at the end will produce the results you are looking for. 对于每种情况,三个查询一个,两个查询之间的一个UNION ALL,最后一个ORDER BY将产生您想要的结果。

http://technet.microsoft.com/en-us/library/ms180026.aspx http://technet.microsoft.com/en-us/library/ms180026.aspx

Here is the sample code. 这是示例代码。

-- Break Performance into 2 rows - A
SELECT 
  LOCATION_CODE,
  PART_NUM,
  CATEGORY_NAME,
  'Sales' as GROUPED,
  SALES_NUM as AMOUNT
FROM T1
WHERE CATEGORY_NAME = 'Performance'
UNION ALL
-- Break Performance into 2 rows - B
SELECT 
  LOCATION_CODE,
  PART_NUM,
  CATEGORY_NAME,
  'Cost' as GROUPED,
  COST_NUM as AMOUNT
FROM T1
WHERE CATEGORY_NAME = 'Performance'
UNION ALL
-- One row for Quality
SELECT 
  LOCATION_CODE,
  PART_NUM,
  CATEGORY_NAME,
  'Rev' as GROUPED,
  REV_NUM as AMOUNT
FROM T1
WHERE CATEGORY_NAME = 'Quality'

-- Order the results
ORDER BY LOCATION_CODE, PART_NUM;

Here is a screen shot of the result window in SSMS. 这是SSMS中结果窗口的屏幕截图。

在此处输入图片说明

This should solve your problem. 这应该可以解决您的问题。

Sincerely, John - The Crafty DBA 真诚的约翰-狡猾的DBA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM