简体   繁体   English

从 SQL 服务器中的另一个表创建一个表

[英]Create a table from another table in SQL Server

I am trying to create the following table in SQL Server:我正在尝试在 SQL 服务器中创建下表:

Id   Had_appointment      Date        Month     Year   Clinic
1          1           2019-01-03    January    2019      A
1          1           2019-01-05    January    2019      B
5          1           2019-04-03     April     2019      C

From the following:从以下:

Id    Admin_codes         Date        Clinic
1         AAA2         2019-01-03       A
1         D22S         2019-01-03       A
1         FFD3         2019-01-05       B
1         E222         2019-01-05       B
5         EEE1         2019-04-03       C
5         P332         2019-04-03       C
5         AA33         2019-04-03       C
5         XC22         2019-04-03       C
6         A000         2019-02-19       C
7         A999         2019-03-11       C

How can I do this?我怎样才能做到这一点? I don't want to include any individuals in my table who 1) did not have appointments & 2) have specific Admin_codes such as 'A000' and 'A999'.我不想在我的表中包括任何 1) 没有约会和 2) 具有特定 Admin_codes 的个人,例如“A000”和“A999”。 Thanks in advance.提前致谢。

We can do this by selecting distinct records for the appointments (treating every admin_code other than 'A000' and 'A999' as an appointment).我们可以通过为约会选择不同的记录来做到这一点(将除“A000”和“A999”之外的每个 admin_code 视为约会)。

SELECT DISTINCT t.Id, '1' AS 'Had_appointment', t.[date], datename(month, [date]) [Month], year([date]) [Year], t.Clinic
FROM @t t   
WHERE Admin_codes NOT IN ('A000', 'A999')

Please see demo here .在此处查看演示。

By "table" I'm assuming you mean query/resultset.通过“表”,我假设您的意思是查询/结果集。 In addition to @sacse's Answer, you can also accomplish the same with GROUP BY.除了@sacse 的答案,您还可以使用 GROUP BY 完成相同的操作。

DECLARE @Data TABLE ( id INT, Admin_codes VARCHAR(4), [Date] DATE, Clinic VARCHAR(1) );
INSERT INTO @Data ( id, Admin_codes, [Date], Clinic ) VALUES
    ( 1, 'AAA2', '2019-01-03', 'A' ),( 1, 'D22S', '2019-01-03', 'A' ),( 1, 'FFD3', '2019-01-05', 'B' ),
    ( 1, 'E222', '2019-01-05', 'B' ),( 5, 'EEE1', '2019-04-03', 'C' ),( 5, 'P332', '2019-04-03', 'C' ),
    ( 5, 'AA33', '2019-04-03', 'C' ),( 5, 'XC22', '2019-04-03', 'C' ),( 6, 'A000', '2019-02-19', 'C' ),
    ( 7, 'A999', '2019-03-11', 'C' );

SELECT
    id,
    1  AS Had_appointment,
    [Date], 
    DATENAME ( month, [Date] ) AS [Month], 
    YEAR ( [Date] ) AS [Year], 
    Clinic
FROM @Data
WHERE
    Admin_codes NOT IN ( 'A000', 'A999' )
GROUP BY
    id, [Date], Clinic
ORDER BY
    id, [Date];

Returns退货

+----+-----------------+------------+---------+------+--------+
| id | Had_appointment |    Date    |  Month  | Year | Clinic |
+----+-----------------+------------+---------+------+--------+
|  1 |               1 | 2019-01-03 | January | 2019 | A      |
|  1 |               1 | 2019-01-05 | January | 2019 | B      |
|  5 |               1 | 2019-04-03 | April   | 2019 | C      |
+----+-----------------+------------+---------+------+--------+

Using GROUP BY will come in handy in the event you plan to do any additional aggregating (eg, SUM, AVG, etc.).如果您计划进行任何其他聚合(例如,SUM、AVG 等),使用 GROUP BY 将派上用场。

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

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