如何避免在sql查询中重复重复的名称 [英] How to avoid the repeation of duplicate names in sql query

查看:72
本文介绍了如何避免在sql查询中重复重复的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询如下



 选择 b.cmn_minor_code < span class =code-keyword> as 课程,c.cmn_minor_desc  as  Eligbility, convert  char  12 ),b.cbm_batch_start_dt, 106  as  Startdt,
convert char 12 ),b.cbm_batch_end_dt, 106 as Enddt 来自 tb_rankwise_coursesettings as a,CO_BATCH_MASTER as b,CO_MINOR_MASTER as c
其中​​ a。 course = b.cmn_minor_code a。 Rank = ' CHENGR' b。 cbm_active<> ' d'
b.cmn_minor_code = c.cmn_minor_code
a.Active<> ' d' b.cbm_batch_start_dt < span class =code-keyword> ' 2015-08-01 00:00:00.000'
' 2015- 09-30 00:00:00.000'





当我运行上述查询输出时如下



课程资格Startdt Enddt 
REO首席工程师2015年8月10日2015年8月21日
REO首席工程师2015年8月24日2015年9月4日b $ b REO首席工程师2015年12月14日2015年12月25日





但我希望输出如下



课程资格能力Startdt Enddt 
REO CHIE ENGINEER 2015年8月10日2015年8月21日
2015年8月24日2015年9月4日
2015年12月14日2015年12月25日





如何避免重复课程和资格使用我的上述查询

请帮助我。



我尝试过:



重复上述内容。



我的查询如下



选择b.cmn_minor_code作为课程,c.cmn_minor_desc作为Eligbility,转换(char(12),b.cbm_batch_start_dt,106)作为Startdt,

convert(char( 12),b.cbm_batch_end_dt,106)作为一个来自tb_rankwise_coursesettings的Enddt作为a,CO_BATCH_MASTER作为b,CO_MINOR_MASTER作为c

其中a.course = b.cmn_minor_code和a.Rank ='CHENGR'和b。 cbm_active<> 'd'

和b.cmn_minor_code = c.cmn_minor_code

和a.Active<> 'd'和b.cbm_batch_start_dt介于'2015-08-01 00:00:00.000'和

'2015-09-30 00:00:00.000'



当我运行上面的查询输出如下



课程资格Startdt Enddt

REO首席工程师2015年8月10日2015年8月21日

REO首席工程师2015年8月24日2015年9月4日

REO首席工程师2015年12月14日2015年12月25日



但我希望输出如下



课程资格Startdt Enddt

REO CHIE ENGINEER 2015年8月10日2015年8月21日
2015年8月24日2015年9月4日

2015年12月14日2015年12月25日



如何避免课程和资格在使用我的上述查询重复

请帮助我。

解决方案

你想要的是或多或少的用户界面的工作。获得数据后,请确保使用正确的UI控件/工具,以便按照您希望的方式显示它。


我强烈建议在解决方案1中提到的内容。我也是我觉得这是用户界面(Crystal Reports,Excel等等)的责任来决定细节的显示方式。



但是如果需要的话在数据库方面,你也可以使用类似的方法,如下所示。



  CREATE   TABLE  #TestTable(
课程 VARCHAR 10 NOT NULL
,资格 VARCHAR 20 NOT NULL
,StartDate SMALLDATETIME NOT NULL
,EndDate SMALLDATETIME NOT NULL


INSERT INTO #TestTable
(课程
,资格
,StartDate
,EndDate

VALUES
' REO'' CHIEF ENGINEER'' 2015年8月10日'' 2015年8月21日'
,(' REO '' CHIEF ENGINEER'' 2015年8月24日'' 04 -Sep-2015'
,(' REO'' CHIEF ENGINEER'' 2015年12月14日'' 2015年12月25日'
,(' REO'' ENGINEER'' 26- 2015年12月'' 2015年12月28日'
, (' REO'' ENGINEER'' 2015年12月29日'' 2015年12月31日'



SELECT
IIF(A.Sort_ID<> 1,' ',A.Course) AS 课程
,IIF(A.Sort_ID<> ; 1,' ',A.Eligibility) AS 资格
,A.StartDate
,A.EndDate
FROM
SELECT
ROW_NUMBER() OVER PARTITION BY Src.Course,Src.Eligibility ORDER BY Src .Course,Src.Eligibility) AS Sort_ID
,DENSE_RANK() OVER ORDER BY Src.Course,Src.Eligibility) AS Rank_ID

,Src。*
FROM #TestTable AS Src
AS A
ORDER BY A.Rank_ID,A.Sort_ID


My query as follows

select b.cmn_minor_code as Course,c.cmn_minor_desc as Eligbility,convert(char(12),b.cbm_batch_start_dt,106) as Startdt,
convert(char(12),b.cbm_batch_end_dt,106) as Enddt from tb_rankwise_coursesettings as a,CO_BATCH_MASTER as b,CO_MINOR_MASTER as c
where a.course = b.cmn_minor_code and a.Rank = 'CHENGR' and b.cbm_active <> 'd'
and b.cmn_minor_code = c.cmn_minor_code
and a.Active <> 'd' and b.cbm_batch_start_dt between  '2015-08-01 00:00:00.000' and 
 '2015-09-30 00:00:00.000'



when i run the above query output as follows

Course         Eligbility         Startdt         Enddt
REO         CHIEF ENGINEER       10 Aug 2015     21 Aug 2015
REO         CHIEF ENGINEER       24 Aug 2015     04 Sep 2015
REO         CHIEF ENGINEER       14 Dec 2015     25 Dec 2015



But i want the output as follows

Course      Eligbility        Startdt             Enddt
REO        CHIE ENGINEER      10 Aug 2015      21 Aug 2015
                              24 Aug 2015       04 Sep 2015
                             14 Dec 2015       25 Dec 2015



for that how to avoid the Course and Eligbility in repeation using my above query
please help me.

What I have tried:

Repetition of what is above.

My query as follows

select b.cmn_minor_code as Course,c.cmn_minor_desc as Eligbility,convert(char(12),b.cbm_batch_start_dt,106) as Startdt,
convert(char(12),b.cbm_batch_end_dt,106) as Enddt from tb_rankwise_coursesettings as a,CO_BATCH_MASTER as b,CO_MINOR_MASTER as c
where a.course = b.cmn_minor_code and a.Rank = 'CHENGR' and b.cbm_active <> 'd'
and b.cmn_minor_code = c.cmn_minor_code
and a.Active <> 'd' and b.cbm_batch_start_dt between '2015-08-01 00:00:00.000' and
'2015-09-30 00:00:00.000'

when i run the above query output as follows

Course Eligbility Startdt Enddt
REO CHIEF ENGINEER 10 Aug 2015 21 Aug 2015
REO CHIEF ENGINEER 24 Aug 2015 04 Sep 2015
REO CHIEF ENGINEER 14 Dec 2015 25 Dec 2015

But i want the output as follows

Course Eligbility Startdt Enddt
REO CHIE ENGINEER 10 Aug 2015 21 Aug 2015
24 Aug 2015 04 Sep 2015
14 Dec 2015 25 Dec 2015

for that how to avoid the Course and Eligbility in repeation using my above query
please help me.

解决方案

What you want is more or less user interface's job. Once you have the data, make sure you use proper UI control/tool that can display it the way you want.


I strongly suggest what has been mentioned on the Solution 1. That I also feel this is the responsibility of the UI (Crystal Reports, Excel etc...) to decide how the details should be displayed.

But if the requirement to have it on the database side, you also can use a similar method what's shown below.

CREATE TABLE #TestTable(
	Course			VARCHAR(10)		NOT NULL
	,Eligibility	VARCHAR(20)		NOT NULL
	,StartDate		SMALLDATETIME	NOT NULL
	,EndDate		SMALLDATETIME	NOT NULL
)

INSERT INTO #TestTable
        (Course
        ,Eligibility
        ,StartDate
        ,EndDate
        )
VALUES	
('REO','CHIEF ENGINEER','10-Aug-2015','21-Aug-2015')
,('REO','CHIEF ENGINEER','24-Aug-2015','04-Sep-2015')
,('REO','CHIEF ENGINEER','14-Dec-2015','25-Dec-2015')
,('REO','ENGINEER','26-Dec-2015','28-Dec-2015')
,('REO','ENGINEER','29-Dec-2015','31-Dec-2015')



SELECT 
    IIF(A.Sort_ID<>1,'',A.Course) AS Course
	,IIF(A.Sort_ID<>1,'',A.Eligibility) AS Eligibility
    ,A.StartDate
    ,A.EndDate
FROM(
	SELECT  
		ROW_NUMBER() OVER (PARTITION BY Src.Course,Src.Eligibility ORDER BY Src.Course,Src.Eligibility) AS Sort_ID
		,DENSE_RANK() OVER(ORDER BY Src.Course,Src.Eligibility) AS Rank_ID
		
		,Src.*
	FROM #TestTable AS Src
) AS A
ORDER BY A.Rank_ID,A.Sort_ID


这篇关于如何避免在sql查询中重复重复的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆