| Author |
Topic |
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 07:15:14
|
| I need to build following query:Each item has one or more manufacturers:Is any way to get all the items with all manufacturers where one of manufacturers fit required expression.I will try to explain in example: items | manufacturers------------------------------ABC1 | golfABC1 | toyotaABC1 | citroenABC2 | skodaABC2 | subaruABC2 | ladaABC3 | peugeotABC3 | renaultABC3 | dodgeExample for Input: *olf*Output: ABC1 | golfABC1 | toyotaABC1 | citroenExample for Input: *aul*Output: ABC3 | peugeotABC3 | renaultABC3 | dodgeThanks |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2007-09-13 : 07:22:56
|
| select * from tablex where items in(select distinct items from tablex where manufacturers like '%olf%')PS: If anyone is interested in a SQL Server Job in Connecticut with excellent pay send me a resume to ValterBorges@msn.com |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 07:26:14
|
[code]DECLARE @Sample TABLE (Items SYSNAME, Manufacturers SYSNAME)INSERT @SampleSELECT 'ABC1', 'golf' union allSELECT 'ABC1', 'toyota' union allSELECT 'ABC1', 'citroen' union allSELECT 'ABC2', 'skoda' union allSELECT 'ABC2', 'subaru' union allSELECT 'ABC2', 'lada' union allSELECT 'ABC3', 'peugeot' union allSELECT 'ABC3', 'renault' union allSELECT 'ABC3', 'dodge'SELECT s.Items, s.ManufacturersFROM @Sample AS sINNER JOIN ( SELECT Items FROM @Sample WHERE Manufacturers LIKE '%olf%' GROUP BY Items ) AS r ON r.Items = s.Items[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 07:27:23
|
| Dear Peso, ABC1 and etc. is only example, my database has ~10 thousand records.May you advise some common solution.I put in in Access.Thanks |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 07:28:42
|
There is an MS Access for Access queries.You use it much the same way. Replace % with *. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 07:30:30
|
| Dear Peso, ABC1 and etc. is only example, my database has ~10 thousand records.May you advise some common solution.Thanks |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 07:33:29
|
The first part is only to mimic YOUR environment!This is the part you needSELECT s.Items, s.ManufacturersFROM {Your table name here} AS sINNER JOIN ( SELECT Items FROM {Your table name here} WHERE Manufacturers LIKE '%olf%' GROUP BY Items ) AS r ON r.Items = s.Items E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 07:42:59
|
| peso, I'm new in SQL, so be gentle :)After I wrote it and run I 'm getting dialog box with a request for "items" and "s.items".Instead it I need to enter only part of manufacturer and to get requested output (for examples: Manufacturers LIKE '%olf%)SELECT s.Items, s.ManufacturersFROM Table1 AS sINNER JOIN ( SELECT Items FROM Table1 WHERE Manufacturers LIKE '%olf%' GROUP BY Items ) AS r ON r.Items = s.Items |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2007-09-13 : 07:45:25
|
| Does your table have a column called items?You must use the names defined in your table or access will think it's a parameter.PS: If anyone is interested in a SQL Server job in Connecticut with excellent pay please send me your resume to ValterBorges@msn.com |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 07:47:11
|
| I have three columns:1.ID2.Items3.Manufacturers..and only "Manufacturers" must get criteia. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 07:49:34
|
Did you REPLACE the "{Your table name here}" part with your actual table name in your database? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 07:52:39
|
| Yes, I wroteSELECT s.Items,s.ManufacturersFROM Table1 AS sINNER JOIN (SELECT ItemsFROM Table1WHERE Manufacturers LIKE '%olf%'GROUP BY Items) AS r ON r.Items = s.ItemsMy table is table1 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 07:55:09
|
And your TABLE1 table has these three columns/fields?1.ID2.Items3.Manufacturers E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 10:25:01
|
| I got an error:"The Micrsoft Jet database engine cannot find the input table or query.."Please, try to simulate my "Table1" and "Query1" in your Access. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 10:36:07
|
We can't see images stored on your local disk. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 10:41:42
|
| You're BIG!!!Thanks.., just replaced '%' by '*'. That's all..Thanks again! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 10:44:50
|
Didn't I write that three hours ago?See post at 09/13/2007 : 07:28:42 I am happy things worked out for you. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 11:05:37
|
| Dear Peso,How to display all records, but without record that fit category.From previous example, show all , except "golf".Thanks |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 11:09:30
|
[code]DECLARE @Sample TABLE (Items SYSNAME, Manufacturers SYSNAME)INSERT @SampleSELECT 'ABC1', 'golf' union allSELECT 'ABC1', 'toyota' union allSELECT 'ABC1', 'citroen' union allSELECT 'ABC2', 'skoda' union allSELECT 'ABC2', 'subaru' union allSELECT 'ABC2', 'lada' union allSELECT 'ABC3', 'peugeot' union allSELECT 'ABC3', 'renault' union allSELECT 'ABC3', 'dodge'SELECT s.Items, s.ManufacturersFROM @Sample AS sLEFT JOIN ( SELECT Items FROM @Sample WHERE Manufacturers LIKE '%olf%' GROUP BY Items ) AS r ON r.Items = s.ItemsWHERE r.Items IS NULL[/code]And this is the code you need[code]SELECT s.Items, s.ManufacturersFROM {Your table name here} AS sLEFT JOIN ( SELECT Items FROM {Your table name here} WHERE Manufacturers LIKE '*olf*' GROUP BY Items ) AS r ON r.Items = s.ItemsWHERE r.Items IS NULL[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jonny
Starting Member
12 Posts |
Posted - 2007-09-13 : 11:21:06
|
| It gives me nothing.For *olf* I want to get following: ABC1 | toyotaABC1 | citroen(ABC1 | golf is not showing) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 11:22:23
|
Nothing? It givesItems ManufacturersABC2 skodaABC2 subaruABC2 ladaABC3 peugeotABC3 renaultABC3 dodge for me... E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Next Page
|