Jump to content

SQL Server - Query


krabby

Recommended Posts

Boa tarde,

Precisava de ajuda. Tenho uma tabela com a seguinte estrutura:

ID 1, ID 2, Type, ServerTime

1 325 7 2012-06-04 12:33:22.493

1 324 7 2012-06-03 12:33:22.493

2 333 7 2012-06-01 12:33:22.493

2 334 7 2012-05-31 12:33:22.493

Precisava de listar por exemplo, dentro dos id's que existem (ID 1) o mais antigo de cada.

Ou seja, da tabela acima, o que queria obter era:

ID 1, ID 2, Type, ServerTime

1 324 7 2012-06-03 12:33:22.493

2 334 7 2012-05-31 12:33:22.493

Consigo fazer com o top 1 mas apenas para 1 registo.

Alguem me dá uma ajuda?

cumprimentos,

Link to comment
Share on other sites

podes fazer varias coisas.

faz um group by ServerTime order by Servertime asc, e apanhas o primeiro valor da tabela.

Ou podes fazer tambem com um Select dentro do Select

Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender.

A beleza de um código está em decompor problemas complexos em pequenos blocos simples.

"learn how to do it manually first, then use the wizzy tool to save time."

"Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."

Link to comment
Share on other sites

Para facilitar cria uma view com o tempo minimo

create view mintime as select id1, min(OCCASION) as m from TABELA group by id1;

Depois usa essa view para filtar as linhas da TABELA

select TABELA.* from TABELA, mintime where TABELA.id1 = mintime.id1 and TABELA.OCCASION = mintime.m;

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.