Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

sql server - sql query to get distinct rows in let table in simple two tables

CREATE TABLE [dbo].[tbl_Travel](
    [TE_ID] [int] IDENTITY(1,1) NOT NULL,
    [TRAVEL_TYPE] [varchar](12) NULL,
    [TRAVEL_MODE] [varchar](35) NULL,
    [TRAVEL_CLASS] [nchar](10) NULL)

SET PRIMARY KEY TO TE_ID

INSERT INTO [TAMSMVC].[dbo].[tbl_Travel] VALUES  ('Return', 'Airlines', 'Economy')
INSERT INTO [TAMSMVC].[dbo].[tbl_Travel] VALUES  ('Single', 'Airlines', 'Business')
INSERT INTO [TAMSMVC].[dbo].[tbl_Travel] VALUES  ('Return', 'Airlines', 'Business')
INSERT INTO [TAMSMVC].[dbo].[tbl_Travel] VALUES  ('Single', 'Railway', 'Second')
INSERT INTO [TAMSMVC].[dbo].[tbl_Travel] VALUES  ('Return', 'Railway', 'First')


CREATE TABLE [dbo].[tbl_Journey](
    [JOURNET_ID] [int] IDENTITY(1,1) NOT NULL,
    [TE_ID] [int] NULL,
    [JOURNEY_FROM] [varchar](30) NULL,
    [JOURNEY_TO] [varchar](30) NULL)

SET PRIMARY KEY TO [JOURNET_ID]

INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (1,'Mumbai','PUNE')
INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (1,'PUNE','Mumbai')
INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (2,'BANGALORE','GOA')
INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (3,'CHENNAI','PANAJI')
INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (3,'PANAJI','CHENNAI')
INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (4,'DELHI','KOLKATA')
INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (5,'BHOPAL','SHIMALA')
INSERT INTO [TAMSMVC].[dbo].[tbl_Journey] VALUES (5,'SHIMALA','BHOPAL')

AND RESULT SHOULD BE i want distinct rows in the table

Journey_ID  TE_ID   Journey_From    Journey_To  TRAVEL_TYPE [TRAVEL_MODE    TRAVEL_CLASS
  1          1       Mumbai          PUNE         Return      Airlines        Economy   
  3          2       BANGALORE       GOA          Single      Airlines         Business  
  4          3       CHENNAI        PANAJI        Return      Airlines         Business  
  6          4       DELHI          KOLKATA       Single      Railway          Second    
  7          5       BHOPAL         SHIMALA       Return      Railway          First     

AND RESULT SHOULD BE i want distinct rows in the table i want to remove duplicate rows in second table and min it should show min journey_id contained rows

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Journey_ID  TE_ID   Journey_From    Journey_To  TRAVEL_TYPE [TRAVEL_MODE    TRAVEL_CLASS    CountofTEID
1   1   Mumbai  PUNE        Return  Airlines    Economy     2
3   2   BANGALORE   GOA         Single  Airlines    Business    1
4   3   CHENNAI PANAJI      Return  Airlines    Business    2
6   4   DELHI   KOLKATA     Single  Railway Second      1
7   5   BHOPAL  SHIMALA     Return  Railway First       2
null    6   null    null    Return  Airlines    Economy     0
null    7   null    null    Return  Airlines    Business    0

In result Iwant all rows from Travel table , count of all journeys and single record with minimum journey_id


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...