sql server – SQL query involving count, group by and substring

sql server – SQL query involving count, group by and substring

SELECT SUBSTRING(ReqPhone, 1, 4),
       DATEADD(DAY,0, DATEDIFF(DAY, 0, SessionID)) AS dayCreated,
       COUNT(*) AS tally
FROM yourTable
GROUP BY SUBSTRING(ReqPhone, 1, 4),
         DATEADD(DAY, 0, DATEDIFF(DAY, 0, SessionID))
SELECT LEFT(ReqPhone, 4),
       DATEADD(DAY,0, DATEDIFF(DAY, 0, SessionID)) AS dayCreated,
       COUNT(*) AS tally
FROM yourTable
GROUP BY LEFT(ReqPhone,4),
         DATEADD(DAY, 0, DATEDIFF(DAY, 0, SessionID))

sql server – SQL query involving count, group by and substring

This will help you to calculate the count of rows group by the ReqPhone type. This query is working successfully in Oracle DB.

SELECT COUNT(SESSIONID), REQP
FROM (SELECT SESSIONID,SUBSTR(REQPHONE,1,4) AS REQP FROM SCHEMA_NAME.TABLE_NAME)
GROUP BY REQP

Note: Please use the column which is unique in the COUNT expression.

Leave a Reply

Your email address will not be published. Required fields are marked *