Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear sir,

i have these record
how to select
top 8 distnict record.
when i select then under line record repet (
same ZoneFrom and ZoneTo does not repeit.)

CSS
ZoneFrom    ZoneTo  ZoneFromName    ZoneToName  CreatedOn
19          18       MEG             MED         2011-12-30 03:59:13.070
41          38      FAR EAST          WCI       2011-12-30 50.700
38          31      WCI             SE ASIA     2011-12-30 03:58:30.327
19           31       MEG             SE ASIA   2011-12-30 03:58:11.327
19          38       MEG                WCI     2011-12-30 03:57:47.370
41          14      FAR EAST           ECI     2011-12-29 09:33:12.193
19           18     MEG               MED      2011-12-29 08:40:25.623
41           38      FAR EAST      WCI 2011-12-29 08:39:58.620
38          31        WCI          SE ASIA 2011-12-29 08:39:36.110
19          31       MEG          SE ASIA 2011-12-29 08:39:06.687
19           38  MEG                WCI      2011-12-29 08:38:36.690
41          14  FAR              EAST    ECI 2011-12-27 07:13:15.937
41          14  FAR EAST         ECI 2011-12-27 03:41:49.897
19           43  MEG            ARA 2011-12-22 08:21:43.150
41           38  FAR EAST            WCI 2011-12-22 08:21:20.870
38          31  WCI              SE ASIA 2011-12-22 08:20:54.040
19          31  MEG               SE ASIA 2011-12-22 08:20:28.893
19            38  MEG           WCI 2011-12-22 08:18:58.717
19            18  MEG           MED 2011-12-07 08:38:11.787
41        38  FAR EAST         WCI 2011-12-07 08:37:45.487
38         41  WCI            FAR EAST    2011-12-07 08:37:19.667
19         41  MEG         FAR EAST    2011-12-07 08:36:54.240
19         38  MEG            WCI 2011-12-07 08:36:27.380
Posted
Comments
AmitGajjar 30-Dec-11 5:07am    
which query you are using ? can you post it here..

SQL
DECLARE @ZoneInfo TABLE
(ZoneFrom   Bigint, ZoneTo  Bigint ,
ZoneFromName    NVARCHAR(50) , ZoneToName NVARCHAR(50) , CreatedOn DATETIME )


INSERT INTO @ZoneInfo
select 19,18,'MEG','MED','2011-12-30 03:59:13.070'
INSERT INTO @ZoneInfo
select 41,38,'FAR EAST','WCI','2011-12-30'
INSERT INTO @ZoneInfo
select 38,31,'WCI','SE ASIA','2011-12-30 03:58:30.327'
INSERT INTO @ZoneInfo
select 19,31,'MEG','SE ASIA','2011-12-30 03:58:11.327'
INSERT INTO @ZoneInfo
select 19,38,'MEG','WCI','2011-12-30 03:57:47.370'
INSERT INTO @ZoneInfo
select 41,14,'FAR EAST','ECI','2011-12-29 09:33:12.193'
INSERT INTO @ZoneInfo
select 19,18,'MEG','MED','2011-12-29 08:40:25.623'
INSERT INTO @ZoneInfo
select 41,38,'FAR EAST','WCI','2011-12-29 08:39:58.620'
INSERT INTO @ZoneInfo
select 38,31,'WCI','SE ASIA','2011-12-29 08:39:36.110'
INSERT INTO @ZoneInfo
select 19,31,'MEG','SE ASIA','2011-12-29 08:39:06.687'
INSERT INTO @ZoneInfo
select 19,38,'MEG','WCI','2011-12-29 08:38:36.690'
INSERT INTO @ZoneInfo
select 41,14,'FAR','EAST    ECI','2011-12-27 07:13:15.937'
INSERT INTO @ZoneInfo
select 41,14,'FAR EAST','ECI','2011-12-27 03:41:49.897'
INSERT INTO @ZoneInfo
select 19,43,'MEG','ARA','2011-12-22 08:21:43.150'
INSERT INTO @ZoneInfo
select 41,38,'FAR EAST','WCI','2011-12-22 08:21:20.870'
INSERT INTO @ZoneInfo
select 38,31,'WCI','SE ASIA','2011-12-22 08:20:54.040'
INSERT INTO @ZoneInfo
select 19,31,'MEG','SE ASIA','2011-12-22 08:20:28.893'
INSERT INTO @ZoneInfo
select 19,38,'MEG','WCI','2011-12-22 08:18:58.717'
INSERT INTO @ZoneInfo
select 19,18,'MEG','MED','2011-12-07 08:38:11.787'
INSERT INTO @ZoneInfo
select 41,38,'FAR EAST','WCI','2011-12-07 08:37:45.487'
INSERT INTO @ZoneInfo
select 38,41,'WCI','FAR EAST','2011-12-07 08:37:19.667'
INSERT INTO @ZoneInfo
select 19,41,'MEG','FAR EAST','2011-12-07 08:36:54.240'
INSERT INTO @ZoneInfo
select 19,38,'MEG','WCI','2011-12-07 08:36:27.380'


SELECT Top 8 * FROM (
select Distinct ZoneFrom,ZoneTo,ZoneFromName,ZoneToName  from
@ZoneInfo) ZITbl
 
Share this answer
 
Comments
RaviRanjanKr 30-Dec-11 18:37pm    
5+
RDBurmon 1-Jan-12 3:12am    
Thanks Ravi , Happy New Year 2012
Hi,
Try This One..

SQL
SELECT a.* FROM tablename AS a
  LEFT JOIN tablename AS a2
    ON 
a.ZoneFrom = a2.ZoneFrom AND 
a.ZoneTo = a2.ZoneTo AND
a.ZoneFromName = a2.ZoneFromName AND
a.ZontToName = a2.ZoneToName AND
a.CreatedOn = a2.CreatedOn
HAVING COUNT(*) <= 8;
 
Share this answer
 
v2
when you select all columns for distinct then your Zonefromname column will contain repeated names cause your createdon contain differetn value
better to consider any one column for distinct, decide which column you wnat dictinct.
SQL
SELECT distinct top 8  Zonefromname FROM tablename
 
Share this answer
 
Comments
DINESH K MAURYA 30-Dec-11 5:12am    
thanks for try to solve but
i want to select which record wher zone from and zone to does not match
zone to repet or zone from repet does not matter only zone to and zone from bothe not sam
If these are in a single table and you want top based on CreatedOn, perhaps something like
SQL
SELECT TOP 8 yt.ZoneFrom, yt.ZoneTo, yt.CreatedOn, ...
FROM YourTable yt
WHERE NOT EXISTS (SELECT 1
                  FROM YourTable yt2
                  WHERE yt2.ZoneTo = yt.ZoneTo
                  AND   yt2.ZoneFrom = yt.ZoneFrom
                  AND   yt2.CreatedOn < yt.CreatedOn)
ORDER BY yt.CreatedOn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900