Query min value

martedì 02 novembre 2010 - 16.07
Tag Elenco Tags  SQL Server 2008  |  SQL Server 2005  |  SQL Server 2000  |  SQL Server Express

ciccioherz Profilo | Junior Member

ciao, ho un problema che non riesco a risolvere.
ho una tabella con 2 campi: nome - numeroAttivita
in realta la tabella è il risultato di una query.
ora io, devo estrarre da tale tabella (query) il record con il valore numeroAttivita piu basso, esempio:
-------------
pippo - 3
pluto - 7
alex - 1
-------------

io devo estrapolare: alex - 1

questa è la mia query che restituisce la prima tabella e da cui devo estrapolare il recordo con min value:
select utente, count(utente) as NumeroAttivita
from tabellaUtente
where utente in (select utente from utenti where ruolo = 'dipendente')
group by utente

lbenaglia Profilo | Guru

>ora io, devo estrarre da tale tabella (query) il record con il
>valore numeroAttivita piu basso, esempio:
>-------------
>pippo - 3
>pluto - 7
>alex - 1
>-------------
>
>io devo estrapolare: alex - 1

Osserva il seguente esempio:

USE tempdb; CREATE TABLE dbo.foo( nome varchar(10) NOT NULL, numeroAttivita int NOT NULL ); INSERT dbo.foo VALUES ('pippo', 3) , ('pluto', 7) , ('alex', 1); WITH CTE_GetMinActivities(ActivitiesNo) AS ( SELECT MIN(numeroAttivita) FROM dbo.foo ) SELECT F.* FROM dbo.foo AS F JOIN CTE_GetMinActivities AS A ON F.numeroAttivita = A.ActivitiesNo; /* Output: nome numeroAttivita ---------- -------------- alex 1 (1 row(s) affected) */ DROP TABLE dbo.foo;

Pertanto la tua query dovrebbe essere qualcosa del genere (ovviamente non posso provarla dato che non è possibile riprodurla con i dati che hai postato):

WITH CTE_Users(Utente, NumeroAttivita) AS ( SELECT U1.Utente, COUNT(*) FROM dbo.tabellaUtente AS U1 JOIN dbo.Utenti AS U2 ON U1.Utente = U2.Utente WHERE U2.Ruolo = 'dipendente' GROUP BY U1.Utente ), CTE_GetMinActivities(ActivitiesNo) AS ( SELECT MIN(NumeroAttivita) FROM CTE_Users ) SELECT U.* FROM CTE_Users AS U JOIN CTE_GetMinActivities AS A ON U.NumeroAttivita = A.ActivitiesNo;

Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/

ciccioherz Profilo | Junior Member

grazie per la risposta intanto..non ho capito molto in realtà di cio che hai scritto, nel frattempo sono riuscito a risolvere in questo modo:

select top 1 * from
(select utente as Dipendente, count(utente) as [Totali Attività]
from fase
where utente in (select utente from utenti where ruolo = 'dipendente')
group by utente) as temp
order by temp.[Totali Attività] asc

ugk111 Profilo | Junior Member

ti propongo questa facile soluzione (se ho capito il problema) basata su due query

prima query di selezione dei campi
SELECT TabellaA.Id_tabellaA, TabellaA.Nome, TabellaA.Numero_attivita
FROM TabellaA;

seconda query basata sulla prima
SELECT DISTINCTROW First([TabellaA Query].Nome) AS PrimoDiNome, Min([TabellaA Query].Numero_attivita) AS [Min Di Numero_attivita]
FROM [TabellaA Query];

lbenaglia Profilo | Guru

>seconda query basata sulla prima
>SELECT DISTINCTROW First([TabellaA Query].Nome) AS PrimoDiNome,
>Min([TabellaA Query].Numero_attivita) AS [Min Di Numero_attivita]
>FROM [TabellaA Query];

SQL Server non ha la keyword DISTINCTROW e la funzione First.

Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5