J'ai une table qui ressemble à ceci:
Tableau:
CUSTTRIPID TPTRIPID CUSTOMERID ENTRY_TOLLTXNID EXIT_TOLLTXNID ENTRYTRIPDATETIME EXITTRIPDATETIME TRIPIDENTMETHOD TRIPCHARGETYPE ENTRYLANEID ENTRYPLAZAID EXITLANEID EXITPLAZAID VEHICLENUMBER VEHICLESTATE VEHICLECLASS VEHICLEID TAGREFID TOLLAMOUNT FEEAMOUNTS DISCOUNTSAMOUNT OUTSTANDINGAMOUNT TRIPSTAGEID TRIPSTATUSID TRIPSTATUSDATE POSTEDDATE PAYMENTSTATUSID CREATEDDATE CREATEDUSER UPDATEDDATE UPDATEDUSER REFLINKTRIPID PLAZA
39038181 34040337 20272679 35126165 35126165 2017-05-04 08:15:54.000 2017-05-04 08:15:54.000 980 P NULL NULL NULL NULL KA03D8435 KA VC10 272614 34161FA8203289720249F4C0 30.00 0.00 0.00 30.00 3 1 2017-05-04 08:31:54.657 2017-05-04 08:31:54.657 456 2017-05-04 08:32:11.130 TRIPPOST 2017-05-04 08:32:11.130 TRIPPOST 0 BangaloreNelamangalaTollPlaza
Je veux récupérer CUSTOMERID
plus de 2 enregistrements avec le temps, j'ai donc écrit cette requête et obtenu le résultat.
Requête principale:
SELECT TOP (10) [CUSTOMERID] , [ENTRYTRIPDATETIME]
FROM [ISSUER].[TOLLPLUS].[TP_CUSTOMERTRIPS]
GROUP BY [CUSTOMERID],[ENTRYTRIPDATETIME]
HAVING COUNT(*) > 2
Résultat:
CUSTOMERID ENTRYTRIPDATETIME
20063381 2016-12-08 00:24:35.000
20010438 2016-12-16 22:16:55.000
20015278 2017-01-28 05:18:20.000
20108092 2017-02-10 00:37:09.000
20013275 2017-01-06 07:42:05.000
20018833 2017-01-27 04:53:32.000
20113994 2017-01-27 01:13:44.000
20199820 2017-01-27 05:34:24.000
20124122 2017-02-01 02:47:01.000
20240710 2017-03-21 13:02:18.000
Mais mon exigence est de diviser la date et l'heure dans le résultat d'abobe, je veux dire qu'il devrait y avoir 3 colonnes, donc ma question est de savoir où je pourrai écrire la requête ci-dessous dans mon Requête principale?
CONVERT(VARCHAR(10),[ENTRYTRIPDATETIME],101) as DatePart,
CONVERT(VARCHAR(10),[ENTRYTRIPDATETIME],108) as TimePart
Vous avez presque compris:
SELECT TOP (10) [CUSTOMERID],
CONVERT(VARCHAR(20), [ENTRYTRIPDATETIME], 101) AS DATEPART,
CONVERT(VARCHAR(20), [ENTRYTRIPDATETIME], 108) AS TIMEPART
FROM [TP_CUSTOMERTRIPS]
GROUP BY [CUSTOMERID],[ENTRYTRIPDATETIME]
HAVING COUNT(*) > 2;
SELECT TOP (10) [CUSTOMERID], CONVERT(VARCHAR(20), [ENTRYTRIPDATETIME], 101) AS DATEPART, CONVERT(VARCHAR(20), [ENTRYTRIPDATETIME], 108) AS TIMEPART FROM [TP_CUSTOMERTRIPS] GROUP BY [CUSTOMERID],[ENTRYTRIPDATETIME] HAVING COUNT(*) > 2; GO
CUSTOMERID | DATEPART | TIMEPART ---------: | : ------ : ------- 20063381 | 12/08/2016 | 00:24:35 20010438 | 16/12/2016 | 22:16:55 20013275 | 01/06/2017 | 07:42:05 20113994 | 27/01/2017 | 01:13:44 20018833 | 27/01/2017 | 04:53:32 20199820 | 27/01/2017 | 05:34:24 20015278 | 28/01/2017 | 05:18:20 20124122 | 02/01/2017 | 02:47:01 20108092 | 02/10/2017 | 00:37:09 20240710 | 21/03/2017 | 13:02:18
db <> violon --- (ici
Autrement:
SELECT TOP (10) [CUSTOMERID],
[DATEPART] = CONVERT(date, [ENTRYTRIPDATETIME]),
[TIMEPART] = CONVERT(time, [ENTRYTRIPDATETIME])
FROM [ISSUER].[TOLLPLUS].[TP_CUSTOMERTRIPS]
GROUP BY [CUSTOMERID],[ENTRYTRIPDATETIME]
HAVING COUNT(*) > 2;
Aussi, pourquoi la couche de présentation ne peut-elle pas faire cela?