简体   繁体   English

SQL 根据其他列值创建表

[英]SQL create table based on other column values

I'm wondering if its possible to create a new table from another table that includes rows based on one column value and only some rows from the same column based on a condition in another column?我想知道是否有可能从另一个表创建一个新表,该表包括基于一个列值的行,以及基于另一列中的条件的同一列中的一些行?

I have a table and I only want to select rows where the 'Tier' column = 1,2 or 3 and has been created this year.我有一张桌子,我只想 select 行,其中“Tier”列 = 1,2 或 3,并且已于今年创建。 I can achieve this with:我可以通过以下方式实现:

SELECT * INTO Table1
FROM Table_v2
WHERE Table_v2.Tier in ('1', '2', '3')
AND CreatedDate > Date()-365;

My issue is that I want to select all rows for 'Tier' 1 & 2 but for 'Tier' 3 I only want rows where the following column 'Type Of Check' = Quality so the desired output would look something like this:我的问题是我想要 select 'Tier' 1 和 2 的所有行,但对于 'Tier' 3 我只想要以下列 'Type Of Check' = Quality 的行,因此所需的 output 看起来像这样:

Desired output example:所需的 output 示例:

在此处输入图像描述

You can do that with an OR for your Type Of Check value.您可以对您Type Of Check值使用OR来做到这一点。 If there are only 3 possible values for Tier then you can also omit Table_v2.Tier = '3' .如果Tier只有 3 个可能的值,那么您也可以省略Table_v2.Tier = '3'

SELECT * INTO Table1
FROM Table_v2
WHERE CreatedDate > Date()-365
AND (Table_v2.Tier IN ('1', '2') OR (Table_v2.Tier = '3' AND [Type Of Check] = 'Quality'));

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

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