Come posso trovare il numero totale di record creati in un determinato giorno usando T-SQL?

StackOverflow https://stackoverflow.com/questions/1208518

  •  05-07-2019
  •  | 
  •  

Domanda

Devo scoprire il numero totale di record che sono stati creati in un determinato giorno.

per es.

ID CreatedDate
1 17/07/2009
2 12/07/2009
3 17/07/2009
4 05/07/2009
5 12/07/2009
6 17/07/2009

Output:

3 record sono stati creati il ??17/07/2009
2 Record creati il ??12/07/2009
1 Il record è stato creato il 05/07/2009

Modifica

Dopo aver testato il secondo suggerimento di Chris Roberts di includere la formattazione nell'SQL, ottengo questo errore:

Syntax error converting the varchar value ' Records were created on ' to a column of data type int.

Qualcuno sa come rielaborare la soluzione in modo che funzioni?

È stato utile?

Soluzione

Dovresti essere in grado di ottenere i dati che stai cercando con il seguente SQL ...

SELEZIONA COUNT (ID), CreatedDate DA MyTable GROUP BY CreatedDate

Oppure - se si desidera eseguire anche la formattazione in SQL ...

SELECT CONVERT (varchar, COUNT (ID)) + 'I record sono stati creati su' + CONVERT (varchar, CreatedDate) DA MyTable GROUP BY CreatedDate

Buona fortuna!

Altri suggerimenti

La colonna è in realtà un timestamp? Nel qual caso dovrai applicare una funzione per rimuovere la componente temporale, ad es .:

SELEZIONA COUNT (*), data (CreatedDate) DA MyTable GROUP BY date (CreatedDate)

Non so quale sia la funzione in T-SQL, è date () in MySQL e trunc () in Oracle. Potrebbe anche essere necessario eseguire un to_string e rimuovere la fine della stringa e del gruppo in tal caso se questo non è presente.

Spero che questo aiuti.

select count(*), CreatedDate from table group by CreatedDate order by count(*) DESC
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top