Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select statement

Author  Topic 

jastc
Starting Member

1 Post

Posted - 2008-09-30 : 22:01:36
I'm having an issue with a select statement. Is there a way to select four columns of data from 3 columns and have the records grouped by the other 2: 1st column is a count of priext with a specific select(count field = ''), 2nd is a count of priext with a different select(count field <> ''), the 3rd is the loggeddate, and the forth is priext. The data look like:
________________________________Count__
______Priext______________________field______loggeddate
_012___206______18:58__00:07;54____________20080927__P9P1112223333
_012___206______19:08__00:14;30__00;07_____20080927
_012___206______19:22__00:03;23____________20080927__P9P1112223333
_018___208______19:43__00:11;19__00;15_____20080927
_012___206______00:09__00:00;53____________20080928__P9P1112223333

What the output should look like:

date: 20080927
ext: 206
inbound calls: 1
outbound calls: 2

date: 20080927
ext: 208
inbound calls: 1
outbound calls: 0

date: 20080928
ext: 206
inbound calls: 0
outbound calls: 1

Any help would be appreciated.

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-30 : 23:51:43
Didnt get what you're looking at clearly. could you please elaborate?
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-10-01 : 00:14:52
try something like this

DECLARE @T TABLE
(
Priext INT,
field1 VARCHAR(100),
field2 VARCHAR(100),
loggeddate VARCHAR(100)
)
INSERT INTO @T
SELECT 206, '00:07;54', NULL, '20080927__P9P1112223333' UNION ALL
SELECT 206, '00:14;30', '00;07', '20080927' UNION ALL
SELECT 206, '00:03;23', NULL, '20080927__P9P1112223333' UNION ALL
SELECT 208, '00:11;19', '00;15', '20080927' UNION ALL
SELECT 206, '00:00;53', NULL, '20080928__P9P1112223333'

SELECT DISTINCT SUBSTRING(T.loggeddate, 1, 8) AS 'date'
, T.Priext AS 'ext'
, ISNULL(TIN.Inbound, 0) AS 'inbound calls'
, ISNULL(TOT.Outbound, 0) AS 'outbound calls'
FROM @T T
LEFT JOIN
(
SELECT SUBSTRING(loggeddate, 1, 8) AS 'date', Priext AS 'ext', COUNT(Priext) AS 'Inbound'
FROM @T
WHERE field2 IS NOT NULL
GROUP BY SUBSTRING(loggeddate, 1, 8), Priext
) TIN ON T.Priext = TIN.ext AND TIN.date = SUBSTRING(T.loggeddate, 1, 8)
LEFT JOIN
(
SELECT SUBSTRING(loggeddate, 1, 8) AS 'date', Priext AS 'ext', COUNT(Priext) AS 'Outbound'
FROM @T
WHERE field1 IS NOT NULL AND field2 IS NULL
GROUP BY SUBSTRING(loggeddate, 1, 8), Priext
) TOT ON T.Priext = TOT.ext AND TOT.date = SUBSTRING(T.loggeddate, 1, 8)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 00:27:43
Another way:-

SELECT LEFT(loggeddate,CASE WHEN CHARINDEX('_',loggedate)>0 THEN CHARINDEX('_',loggeddate)-1 ELSE LEN(loggeddate) END) AS date,
Priext AS Ext,
SUM(CASE WHEN NULLIF(Countfield,'') IS NOT NULL THEN 1 ELSE 0 END) AS InboundCalls,
SUM(CASE WHEN NULLIF(Countfield,'') IS NULL THEN 1 ELSE 0 END) AS OutboundCalls
FROM YourTable
GROUP BY LEFT(loggeddate,CASE WHEN CHARINDEX('_',loggedate)>0 THEN CHARINDEX('_',loggeddate)-1 ELSE LEN(loggeddate) END),Priext
Go to Top of Page
   

- Advertisement -