久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Tsql聯合查詢

Tsql union query(Tsql聯合查詢)
本文介紹了Tsql聯合查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在尋找一種查詢表的有效方法.表結構為:

I’m looking for an efficient way to query a table. The table structure is:

CREATE TABLE [dbo].[CaseManager](
    [CaseID] [int] IDENTITY(1,1) NOT NULL,
    [SystemUserCreatedBy] [int] NULL,
    [SystemUserAssignee] [int] NULL,
     CONSTRAINT [PK_Case] PRIMARY KEY CLUSTERED 
(
    [CaseID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] 

查詢應該為每個 caseID 和 userid(userid 可以是 SystemUserCreatedBy 或 SystemUserAssignee)位列返回,顯示使用是 Createdby 還是 Assignee我設法這樣寫:

The query should return for every caseID and userid (userid can be either SystemUserCreatedBy or SystemUserAssignee) bit columns that show if the use is Createdby or Assignee I managed to write it like this:

select  CaseID,UserID,
       max(CaseUser.IsAssignee) as IsAssignee,
       max(CaseUser.IsCreator) as IsCreator


  FROM
  (
  select CMassignee.CaseID,
         CMassignee.SystemUserAssignee as UserID,
         1 as IsAssignee ,
         0 as IsCreator 
      from CaseManager CMassignee 
  where  CMassignee.SystemUserAssignee is not null
  UNION
   select CMCreator.CaseID,
          CMCreator.SystemUserCreatedBy as UserID,
          0 as IsAssignee ,
          1 as IsCreator 
      from CaseManager CMCreator
  where  CMCreator.SystemUserCreatedBy is not null
    ) CaseUser
  group by CaseID,UserID

我很確定有一種更好的方法來編寫一次掃描該表.在那個例子中,我只顯示了兩列(SystemUserCreatedBy as SystemUserAssignee),但實際上我有五列需要添加.

I’m pretty sure there is a better way to write it with scanning that table once .In that example I show only two columns (SystemUserCreatedBy as SystemUserAssignee) but actually I have five that need to be added.

請看數據示例:

SET IDENTITY_INSERT dbo.casemanager ON;
insert into casemanager(caseid,SystemUserCreatedBy,SystemUserAssignee)
values
(1,2222,3333)
SET IDENTITY_INSERT dbo.casemanager OFF;

在那種情況下,我希望得到:

In that case I’m looking to get :

CaseID      UserID      IsAssignee  IsCreator
----------- ----------- ----------- -----------
1           2222        0           1
1           3333        1           0

(2 row(s) affected)

推薦答案

CaseID 是主鍵,因此不需要聚合(如果兩列中只有一列不是 NULL):

The CaseID is the Primary Key, so there's no need for aggregation (if only one of both columns is NOT NULL):

SELECT
   CaseID,
   COALESCE(SystemUserAssignee, SystemUserCreatedBy) AS UserID,
   CASE WHEN SystemUserAssignee IS NOT NULL THEN 1 ELSE 0 end AS IsAssignee,
   CASE WHEN SystemUserCreatedBy IS NOT NULL THEN 1 ELSE 0 end AS IsCreator
FROM CaseManager CMassignee 

根據最新的評論,兩列都可以有數據,而且可能是同一個用戶,所以你的原始查詢是可以的(即使它掃描了兩次表),你唯一需要改變的是UNION ALL 而不是 UNION.

Based on the latest comments both columns can have data and it might be the same user, so your original query is ok (even if it scans the table twice), the only thing you need to change is UNION ALL instead of UNION.

但是對于 5 個用戶,@Amit 的答案應該是最好的.

But for 5 userid @Amit's answer should be the best.

這篇關于Tsql聯合查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 欧美区日韩区 | 日操操夜操操 | 日本一二三区在线观看 | 免费国产视频在线观看 | 欧美四虎 | 国产一级在线 | 欧美日韩黄色一级片 | 毛片毛片毛片毛片 | 精品国产1区2区3区 一区二区手机在线 | 久久激情视频 | 天堂色| 中文字幕一区二区三区在线视频 | 亚洲欧洲一区二区 | 国产成人一区二区三区 | 一级片免费视频 | 四虎影院一区二区 | 亚洲一二三区不卡 | 日韩欧美在线免费观看 | 婷婷开心激情综合五月天 | 午夜丰满寂寞少妇精品 | 欧美一区二区三区在线 | 欧美精品一区二区在线观看 | 国产精品一区视频 | 国产中文 | 精品国产精品三级精品av网址 | 99免费在线观看 | 日韩 欧美 二区 | 欧美 日韩 国产 成人 在线 | 亚洲精选一区二区 | 精品免费国产一区二区三区四区介绍 | 在线观看你懂的网站 | 99亚洲视频 | 亚洲欧美日韩成人在线 | 在线成人免费视频 | 日韩在线精品强乱中文字幕 | 国产日批| 国产精品久久国产精品 | www.操.com | 精品国产18久久久久久二百 | 国产乱码一区 | 国产精品一二三区在线观看 |