sql server: left join 重覆數據

来源:http://www.cnblogs.com/geovindu/archive/2017/09/27/7602775.html
-Advertisement-
Play Games

--顯示專員內容的統計 --香港 Geovin Du 塗聚文 select count(*) as 'Tatol',BrandService,ServerName,Qualified=isnull(count(case IsSucces when '合格' then IsSucces end),0)... ...


---塗聚文 2017-9-28
SELECT VipExamMailProjectId,VipExamMailStaffID FROM VipExamMailRecord 
 WHERE
    VipExamMailProjectId = 1 
GROUP BY VipExamMailStaffID,VipExamMailProjectId  HAVING COUNT(1)>1
ORDER BY VipExamMailStaffID
GO

--192
SELECT * FROM 
(SELECT BranchStaffID,BranchStaffCountryID,BranchStaffIs,BranchStaffArea,BranchStaffCode,BranchStaffNO,BranchStaffName,BranchStaffServerNo FROM VipBranchStaff) a
LEFT JOIN (SELECT VipExamMailProjectId,VipExamMailStaffID FROM VipExamMailRecord WHERE VipExamMailProjectId = 1 
GROUP BY VipExamMailStaffID,VipExamMailProjectId  HAVING COUNT(1)>1) b
ON a.BranchStaffID = b.VipExamMailStaffID
 WHERE a.BranchStaffCountryID=1 
GO

--153 香港 
SELECT * FROM VipBranchStaff WHERE BranchStaffCountryID=1 AND BranchStaffIs=1
GO

--View_VipExamMailRecordProject

SELECT a.*,b.VipExamMailProjectId AS 'ExamProjectId',[dbo].getVipExamBranchSimpleName(a.BranchStaffCode) as 'BranchAreaName',[dbo].getAreaName(a.BranchStaffArea) as 'AreaName',dbo.getVipExamBrandServiceName(dbo.getVipExamBrandServiceNo(a.BranchStaffCode)) AS 'ServerName',
 dbo.getVipExamBrandServiceNo(a.BranchStaffCode) AS 'BrandService',[dbo].getVipExamQuestionsIs (b.VipExamMailProjectId,a.BranchStaffID) as 'IsSucces',dbo.getStaffProjectTimes(b.VipExamMailProjectId,a.BranchStaffID) AS 'ExamTimes',dbo.getVipExamMailDate(b.VipExamMailProjectId,a.BranchStaffID) AS 'VipExamMailDate'
 FROM 
(SELECT BranchStaffID,BranchStaffCountryID,BranchStaffIs,BranchStaffArea,BranchStaffCode,BranchStaffNO,BranchStaffName,BranchStaffServerNo FROM VipBranchStaff WHERE BranchStaffCountryID=1 AND BranchStaffIs=1) a
LEFT JOIN (SELECT VipExamMailProjectId,VipExamMailStaffID FROM VipExamMailRecord WHERE VipExamMailProjectId = 1 
GROUP BY VipExamMailStaffID,VipExamMailProjectId  HAVING COUNT(1)>1) b
ON a.BranchStaffID = b.VipExamMailStaffID
 --WHERE a.BranchStaffCountryID=1  
GO


--國內
--2425
SELECT * FROM VipBranchStaff WHERE BranchStaffCountryID=2 AND BranchStaffIs=1
GO

SELECT a.*,b.VipExamMailProjectId AS 'ExamProjectId',[dbo].getVipExamBranchSimpleName(a.BranchStaffCode) as 'BranchAreaName',[dbo].getAreaName(a.BranchStaffArea) as 'AreaName',dbo.getVipExamBrandServiceName(dbo.getVipExamBrandServiceNo(a.BranchStaffCode)) AS 'ServerName',
 dbo.getVipExamBrandServiceNo(a.BranchStaffCode) AS 'BrandService',[dbo].getVipExamQuestionsIs (b.VipExamMailProjectId,a.BranchStaffID) as 'IsSucces',dbo.getStaffProjectTimes(b.VipExamMailProjectId,a.BranchStaffID) AS 'ExamTimes' FROM 
(SELECT BranchStaffID,BranchStaffCountryID,BranchStaffIs,BranchStaffArea,BranchStaffCode,BranchStaffNO,BranchStaffName,BranchStaffServerNo FROM VipBranchStaff WHERE BranchStaffCountryID=2 AND BranchStaffIs=1) a
LEFT JOIN (SELECT VipExamMailProjectId,VipExamMailStaffID FROM VipExamMailRecord WHERE VipExamMailProjectId =2 
GROUP BY VipExamMailStaffID,VipExamMailProjectId  HAVING COUNT(1)>1) b
ON a.BranchStaffID = b.VipExamMailStaffID 
GO

SELECT a.*,b.VipExamMailProjectId AS 'ExamProjectId',[dbo].getVipExamBranchSimpleName(a.BranchStaffCode) as 'BranchAreaName',[dbo].getAreaName(a.BranchStaffArea) as 'AreaName',dbo.getVipExamBrandServiceName(dbo.getVipExamBrandServiceNo(a.BranchStaffCode)) AS 'ServerName',
 dbo.getVipExamBrandServiceNo(a.BranchStaffCode) AS 'BrandService',[dbo].getVipExamQuestionsIs (b.VipExamMailProjectId,a.BranchStaffID) as 'IsSucces',dbo.getStaffProjectTimes(b.VipExamMailProjectId,a.BranchStaffID) AS 'ExamTimes',dbo.getVipExamMailDate(b.VipExamMailProjectId,a.BranchStaffID) AS 'VipExamMailDate' FROM 
(SELECT BranchStaffID,BranchStaffCountryID,BranchStaffIs,BranchStaffArea,BranchStaffCode,BranchStaffNO,BranchStaffName,BranchStaffServerNo FROM VipBranchStaff WHERE BranchStaffCountryID=2 AND BranchStaffIs=1) a
LEFT JOIN (SELECT VipExamMailProjectId,VipExamMailStaffID FROM VipExamMailRecord WHERE VipExamMailProjectId =2 
GROUP BY VipExamMailStaffID,VipExamMailProjectId  HAVING COUNT(1)>1) b
ON a.BranchStaffID = b.VipExamMailStaffID
WHERE dbo.getVipExamBrandServiceNo(a.BranchStaffCode)='013180'  
ORDER BY a.BranchStaffCode
GO

--2017-09-27修改 合格詳情列表
IF EXISTS (select * from sysobjects where [name] = '[proc_Select_HKQualifiedNoDetails]')
DROP PROCEDURE proc_Select_HKQualifiedNoDetails
GO
CREATE PROCEDURE [dbo].proc_Select_HKQualifiedNoDetails
(
	@ProjectID int,
	@CountryID int,
	@BrandService VARCHAR(50),
	@search nvarchar(100)
)
as
declare @sql nvarchar(4000),@where nvarchar(4000)
set @sql='SELECT a.*,b.VipExamMailProjectId AS ''ExamProjectId'',[dbo].getVipExamBranchSimpleName(a.BranchStaffCode) as ''BranchAreaName'',[dbo].getAreaName(a.BranchStaffArea) as ''AreaName'',dbo.getVipExamBrandServiceName(dbo.getVipExamBrandServiceNo(a.BranchStaffCode)) AS ''ServerName'',
 dbo.getVipExamBrandServiceNo(a.BranchStaffCode) AS ''BrandService'',[dbo].getVipExamQuestionsIs (b.VipExamMailProjectId,a.BranchStaffID) as ''IsSucces'',dbo.getStaffProjectTimes(b.VipExamMailProjectId,a.BranchStaffID) AS ''ExamTimes'',dbo.getVipExamMailDate(b.VipExamMailProjectId,a.BranchStaffID) AS ''VipExamMailDate'' FROM 
(SELECT BranchStaffID,BranchStaffCountryID,BranchStaffIs,BranchStaffArea,BranchStaffCode,BranchStaffNO,BranchStaffName,BranchStaffServerNo FROM VipBranchStaff WHERE BranchStaffIs=1 and BranchStaffCountryID='+ cast(@CountryID as varchar)+') a'
+' LEFT JOIN (SELECT VipExamMailProjectId,VipExamMailStaffID FROM VipExamMailRecord WHERE VipExamMailProjectId ='+ cast(@ProjectID as varchar)+' GROUP BY VipExamMailStaffID,VipExamMailProjectId  HAVING COUNT(1)>1) b ON a.BranchStaffID = b.VipExamMailStaffID'
set @where=''
IF @BrandService<>'' AND @Search=''
	SET  @where=@where+' where dbo.getVipExamBrandServiceNo(a.BranchStaffCode) like ''%'+@BrandService +'%'''
if @Search<>'' AND @BrandService=''
begin
	set @where=@where+' where (a.BranchStaffCode like ''%'+@search +'%'' or [dbo].getVipExamBranchSimpleName(a.BranchStaffCode) like ''%'+@search +'%'' or [dbo].getAreaName(a.BranchStaffArea) like ''%'+@search +'%''  or dbo.getVipExamBrandServiceName(dbo.getVipExamBrandServiceNo(a.BranchStaffCode))  like ''%'+@search +'%'' or [dbo].getVipExamQuestionsIs (b.VipExamMailProjectId,a.BranchStaffID) like ''%'+@search +'%''  or dbo.getVipExamBrandServiceNo(a.BranchStaffCode) like ''%'+@search +'%'')'
END
IF @BrandService<>'' AND @Search<>''
BEGIN
   SET  @where=@where+' where dbo.getVipExamBrandServiceNo(a.BranchStaffCode) like ''%'+@BrandService +'%'''
   set @where=@where+' and (a.BranchStaffCode like ''%'+@search +'%'' or [dbo].getVipExamBranchSimpleName(a.BranchStaffCode) like ''%'+@search +'%'' or [dbo].getAreaName(a.BranchStaffArea) like ''%'+@search +'%''  or dbo.getVipExamBrandServiceName(dbo.getVipExamBrandServiceNo(a.BranchStaffCode))  like ''%'+@search +'%'' or [dbo].getVipExamQuestionsIs (b.VipExamMailProjectId,a.BranchStaffID) like ''%'+@search +'%''  or dbo.getVipExamBrandServiceNo(a.BranchStaffCode) like ''%'+@search +'%'')'
END 
set @sql=@sql+@where+' order by ExamTimes'
print @sql
exec(@sql)
GO

EXEC proc_Select_HKQualifiedNoDetails 1,1,'',''
GO

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 安裝oracle11g客戶端,再安裝oacle12c odac插件。應將oracle11g的環境變數放到12c環境變數前面。 ...
  • 標簽:SQL SERVER/MSSQL SERVER/資料庫/DBA/ 概述 SQLServer2016發佈版本到現在已有一年多的時間了,目前最新的穩定版本是SP1版本。接下來就開看看2016在Alwyson上做了哪些改進,記得之前我在寫2014Alwayson的時候提到過幾個需要改進的問題在201 ...
  • MySQL更改線上配置方案思想:原則上,需要備機、備份工作准備到位,有參數調優配置方案、有配置回退方案、有應急切換備機方案。以上方案評審無問題,然後可以和客戶約定實施的時間。服務中斷時間,先向客戶側申請30分鐘吧。如果客戶承受不了,就讓客戶自己給個時間。在規定時間內,我們做得到就做,做不到就立即回退 ...
  • 我們可能經常安裝和部署資料庫伺服器,但是可能突然忘記了某個設置,為後來的運維造成隱患。下麵是國外大牛整理的的檢查列表。 其實也包含了很多我們平時資料庫配置的最佳實踐。比如TEMPDB 文件的個數,比如資料庫文件,日誌文件如何存放,最大記憶體的設置等等。如果有補充的歡迎留言 ...
  • UNPIVOT UNPIVOT則相反,把數據從列旋轉到行 SELECT * INTO product_vlues FROM ( SELECT NAME , YEAR(shijian) AS shijian , jiage FROM dbo.product ) AS ov PIVOT ( SUM(ov ...
  • 安裝MySQL碰到的超級新手問題 看了各種教程從官網下載了mysql-5.7.17.msi,安裝碰到問題。 .紅框位置提示沒安裝Microsoft visual c++ 2013 runtime ,很是鬱悶剛開始沒註意看直接下一步了。因為這個問題,導致MySQL Server安裝不成功。 但是MyS ...
  • 第五章:事務,視圖,索引,備份和恢復 1.事務是指將一系列數據操作捆綁成為一個整體進行統一管理. 一、事務語法: 1.開始事務 語法: BEGIN 或 START TRAINSACTION; 2.提交事務:執行的所有數據更新將永久地保存在資料庫數據文件中 語法: COMMIT; 3.回滾事務:將數據 ...
  • CREATE DATABASE `mybank`;/*創建表*/USE mybank;CREATE TABLE `bank`( `customerName` CHAR(10), #用戶名 `currentMoney` DECIMAL(10,2) #當前餘額);/*插入數據*/INSERT INTO ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...