Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
tempus
Starting Member
47 Posts |
Posted - 2009-06-30 : 03:26:39
|
| Hello, i am having a table like this :name desca1 1a1 2a1 3b1 1b1 2b1 3b1 6b1 7c1 1c1 2d1 1d1 2d1 3d1 4d1 5d1 6etcwhat i would like to do in a script is the following :return the name(s) where all the descriptions of a1 are found in all the collumn name. (all 3 of them must exist in the other else there should not be any value returned). also if more names contain 1,2,3 then a list of them would be perfect. it doesn't matter if names contain more than 1,2,3. the condidition should be at least them.The result from the upper table example should be :------b1d1------i really have problems thinking this one and i would apreciate any help. of course the table is quite big reaching thousand of rows.thanks in advance to everyone who will respond. Regards, BogdanPS: this is my first time here, i hope i posted corectly. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-30 : 03:43:32
|
[code]DECLARE @Sample TABLE ( name CHAR(2), dsc int )INSERT @SampleSELECT 'a1', 1 UNION ALLSELECT 'a1', 2 UNION ALLSELECT 'a1', 3 UNION ALLSELECT 'b1', 1 UNION ALLSELECT 'b1', 2 UNION ALLSELECT 'b1', 3 UNION ALLSELECT 'b1', 6 UNION ALLSELECT 'b1', 7 UNION ALLSELECT 'c1', 1 UNION ALLSELECT 'c1', 2 UNION ALLSELECT 'd1', 1 UNION ALLSELECT 'd1', 2 UNION ALLSELECT 'd1', 3 UNION ALLSELECT 'd1', 4 UNION ALLSELECT 'd1', 5 UNION ALLSELECT 'd1', 6DECLARE @Source CHAR(2)SET @Source = 'a1'SELECT t.nameFROM ( SELECT dsc, COUNT(*) OVER () AS c FROM @Sample WHERE name = @Source ) AS sINNER JOIN ( SELECT name, dsc, COUNT(*) OVER (PARTITION BY name) AS c FROM @Sample WHERE name <> @Source ) AS t ON t.dsc = s.dscWHERE s.c <= t.cGROUP BY t.nameHAVING COUNT(*) >= MAX(s.c)[/code]EDIT: Better control N 56°04'39.26"E 12°55'05.63" |
 |
|
|
tempus
Starting Member
47 Posts |
Posted - 2009-06-30 : 03:50:01
|
| Dear Peso, im gonna try that right now on my table. thank you verry much for your quick reply.Best regards, Bogdan |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-30 : 03:54:12
|
See my edited code. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
tempus
Starting Member
47 Posts |
Posted - 2009-06-30 : 04:26:07
|
| Dear Peso, i have created a test run that for exaclty my table: SELECT t.MP61002FROM ( SELECT MP61005, COUNT(*) OVER () AS c FROM MP610100 WHERE MP61002 = 'PF0520/R1' ) AS sINNER JOIN ( SELECT MP61002, MP61005, COUNT(*) OVER (PARTITION BY MP61002) AS c FROM MP610100 WHERE MP61002 <> 'PF0520/R1' ) AS t ON t.MP61002 = s.MP61005WHERE s.c <= t.cGROUP BY t.MP61002HAVING COUNT(*) >= MAX(s.c)where MP61002 is the column name and MP61005 is the column descand MP610100 is the table namealso i have created in the system a exact copy of PF0520/R1 named PF0520/R2 with an aditional component. attached bellow is the content table:MP61002 MP61005PF0520/R1 01 0001 MPPEM PF0520/R1 01 0002 MPRED PF0520/R1 01 0003 MPWAX PF0520/R1 01 0004 MPIRGANOX PF0520/R2 01 0001 MPPEM PF0520/R2 01 0002 MPRED PF0520/R2 01 0003 MPWAX PF0520/R2 01 0004 MPIRGANOX PF0520/R2 01 0005 MPIOKAL please ignore the second and third column. the script returns in this case only the column name : "MP61002" with no result. still it should be PF0520/R2. Im sure i am doing something wrong here...please forgive my noob'ness |
 |
|
|
tempus
Starting Member
47 Posts |
Posted - 2009-06-30 : 05:58:23
|
| Dear Peso,thank you verry much. i have retested and it's working. thank you verry much for your help. i really apreciate it! Best regards, Bogdan. |
 |
|
|
|
|
|
|
|