Visualizzare x record

venerdì 27 giugno 2008 - 16.02

marconi82 Profilo | Newbie

Ciao a tutti ho questo tipo di problema:
ho un tabella che ho chiamato news, questa è suddivisa in 4 campi che sono:
Tabella news=
Campo1 = id
Campo2= testo
Campo3= lega
Campo4= orario

Ora se faccio questa istruzione sql:

SELECT news.lega, Last(news.testo) AS testo, Last(news.orario) AS orario
FROM news
GROUP BY news.lega;

Ho come risultato solo le ultime notizie inserite per ogni lega, a me servirebbe una istruzione che mi visualizzasse le ultime 2 notizie di ogni lega...come faccio?

lbenaglia Profilo | Guru

>a me servirebbe una istruzione che mi visualizzasse le ultime
>2 notizie di ogni lega...come faccio?

Su SQL Server utilizzerei una soluzione simile a questa:

USE tempdb; CREATE TABLE dbo.News( NewsID int NOT NULL IDENTITY PRIMARY KEY, Testo varchar(10) NOT NULL, Lega varchar(10) NOT NULL, Orario smalldatetime NOT NULL ); INSERT dbo.News VALUES('Testo 1', 'Lega 1', '19000101 1:00'); INSERT dbo.News VALUES('Testo 2', 'Lega 1', '19000101 2:00'); INSERT dbo.News VALUES('Testo 3', 'Lega 1', '19000101 3:00'); INSERT dbo.News VALUES('Testo 4', 'Lega 2', '19000101 4:00'); INSERT dbo.News VALUES('Testo 5', 'Lega 2', '19000101 5:00'); INSERT dbo.News VALUES('Testo 6', 'Lega 3', '19000101 6:00'); GO CREATE FUNCTION dbo.ufn_Last2News( @Lega varchar(10) ) RETURNS TABLE RETURN ( SELECT TOP 2 * FROM dbo.News WHERE Lega = @Lega ORDER BY Orario DESC ); GO WITH CTE_Leghe AS ( SELECT DISTINCT Lega FROM dbo.News ) SELECT U.* FROM CTE_Leghe CROSS APPLY dbo.ufn_Last2News(Lega) AS U ORDER BY NewsID; /* Output: NewsID Testo Lega Orario ----------- ---------- ---------- ----------------------- 2 Testo 2 Lega 1 1900-01-01 02:00:00 3 Testo 3 Lega 1 1900-01-01 03:00:00 4 Testo 4 Lega 2 1900-01-01 04:00:00 5 Testo 5 Lega 2 1900-01-01 05:00:00 6 Testo 6 Lega 3 1900-01-01 06:00:00 (5 row(s) affected) */ DROP FUNCTION dbo.ufn_Last2News; DROP TABLE dbo.News;

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

marconi82 Profilo | Newbie

Bene Grazie, solo he c'è un problemino!!!!!!!!

Non mi serve sql server, io programmo in asp devo richiamare un recordset, mi serve solo l'istruione sql da inserire nel recordsert...grazie mille

lbenaglia Profilo | Guru

>Non mi serve sql server, io programmo in asp devo richiamare
>un recordset, mi serve solo l'istruione sql da inserire nel recordsert...grazie
>mille
Un recordset popolato con una query che si connette a quale DBMS?

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

marconi82 Profilo | Newbie

E' una pagina asp che si collega ad un database access...
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