2010-01-11 7 views
14

Non so nemmeno se sto facendo questa query nel modo giusto. C'è una tabella Sandwiches con 7 campi e 2 di essi sono caselle combinate (Type e Bread).Una query di selezione che seleziona un'istruzione select

così ho fatto una query che unisce tutti i valori caselle combinate in una query, in questo modo:

SELECT TypesAndBreads.TBName, TypesAndBreads.Type 
FROM (SELECT [Sandwiches Types].[Sandwich Type] As TBName, "Sandwich Type" As Type 
    FROM [Sandwiches Types] 
UNION ALL 
    SELECT Breads.Bread As TBName, "Bread" As Type 
    FROM Breads) AS TypesAndBreads; 

Ho l'appartamento valori delle tabelle ora voglio contare tutti i panini sotto ogni TypesAndBreads.TBName. Ho questo, giusto per assicurarsi che funziona con tutti i panini:

SELECT TypesAndBread.Type, TypesAndBread.TBName, 
     (SELECT Count(Sandwiches.[SandwichID]) As SandwichCount 
     FROM Sandwiches) As SandwichCount 
FROM TypesAndBread; 

Ma voglio fare riferimento al tipo di corrente e tbName all'interno della subquery. Qualcosa del genere:

SELECT TypesAndBread.Type, TypesAndBread.TBName, 
     (SELECT Count(Sandwiches.[SandwichID]) As SandwichCount 
     FROM Sandwiches 
     WHERE Sandwiches.[TypesAndBread.Type] = Sandwiches.[TypesAndBread.TBName]) As SandwichCount 
FROM TypesAndBread; 

Ma ovviamente questo non funziona. Non pensavo che fosse così, ho pensato di provarlo. Stavo pensando di costruire la query con VBA quando aprono il Report su cui si baserà questa query.

Quindi penso che la mia domanda sia: C'è un modo per fare riferimento ai campi selezionati in una sottoquery? O c'è un modo diverso di avvicinarsi a questo?

Grazie per l'aiuto

EDIT: mia struttura della tabella è come questo:

s' Sandwiches campi

| SandwichID | Name | Date Added | Chef | Sandwich Type | Bread | Reviewed By | 

dove Sandwich Type e Bread sono campi di ricerca per queste tabelle:

'Campi s

| Sandwich Type | 

Breads' Sandwiches Types campi s

| Bread | 

La query TypesAndBreads combinati i panini Tipi e tabelle di pane, ma la ragione per questo è così che posso ottenere il conteggio di tutti i panini che hanno quel tipo o pane. Un risultato simile a questo:

+=============================================+ 
|  Type  | TBName | SandwichCount | 
+=============================================+ 
| Sandwich Type | Turkey Club |   10 | 
| Bread   | Italian  |    5 | 
| Bread   | Garlic  |    8 | 
+---------------------------------------------+ 

prima riga del risultato esempio dice che fondamentalmente ci sono 10 panini record con il campo Tipo Sandwich pari a Turchia Club.

Spero che lo spieghi meglio.

+0

Sto trovando questo un po 'di confusione. Puoi pubblicare la struttura della tabella e l'output desiderato? – RedFilter

+0

Ho aggiunto la struttura della tabella e l'output desiderato, speriamo che questo aiuti. Grazie per il commento. –

risposta

17

Non sono sicuro se Access supporta, ma nella maggior parte dei motori (tra cui SQL Server) questo è chiamato una subquery correlata e funziona bene:

SELECT TypesAndBread.Type, TypesAndBread.TBName, 
     (
     SELECT Count(Sandwiches.[SandwichID]) As SandwichCount 
     FROM Sandwiches 
     WHERE (Type = 'Sandwich Type' AND Sandwiches.Type = TypesAndBread.TBName) 
       OR (Type = 'Bread' AND Sandwiches.Bread = TypesAndBread.TBName) 
     ) As SandwichCount 
FROM TypesAndBread 

questo può essere reso più efficiente l'indicizzazione Type e Bread e la distribuzione del subqueries su UNION:

SELECT [Sandwiches Types].[Sandwich Type] As TBName, "Sandwich Type" As Type, 
     (
     SELECT COUNT(*) As SandwichCount 
     FROM Sandwiches 
     WHERE Sandwiches.Type = [Sandwiches Types].[Sandwich Type] 
     ) 
FROM [Sandwiches Types] 
UNION ALL 
SELECT [Breads].[Bread] As TBName, "Bread" As Type, 
     (
     SELECT COUNT(*) As SandwichCount 
     FROM Sandwiches 
     WHERE Sandwiches.Bread = [Breads].[Bread] 
     ) 
FROM [Breads] 
+0

Jet/ACE ha sempre supportato sottoquery correlate. Tuttavia, spesso non sono ottimizzati in termini di utilizzo dell'indice. –

4

Mi stavo complicando troppo. Dopo aver preso una lunga pausa e tornare, l'output desiderato potrebbe essere realizzato da questa semplice query:

SELECT Sandwiches.[Sandwich Type], Sandwich.Bread, Count(Sandwiches.[SandwichID]) AS [Total Sandwiches] 
FROM Sandwiches 
GROUP BY Sandwiches.[Sandwiches Type], Sandwiches.Bread; 

Grazie per la risposta, ha aiutato il mio treno di pensiero.