SQL query - not null - sql

I have sql query:
Select id
,MAX(CASE WHEN (pos = "10") THEN date ELSE NULL END) position10
,MAX(CASE WHEN (pos = "18") THEN date ELSE NULL END) position18
,MAX(CASE WHEN (pos = "20") THEN date ELSE NULL END) position20
FROM table
WHERE
date BETWEEN "${od=2021-05-01 00:00:01}" AND "${do=2021-07-01 00:00:01}"
AND pos IN('10','18','20')
group by id
I got this table
id
position10
position20
position30
1
2021-05-02
2021-05-02
2021-05-3
2
2021-05-15
2021-05-16
2021-05-18
3
2021-07-08
NULL
NULL
And I want lines where NULL values are not picked up.
A try this:
SELECT * FROM (
Select id
,MAX(CASE WHEN (pos = "10") THEN date ELSE NULL END) position10
,MAX(CASE WHEN (pos = "18") THEN date ELSE NULL END) position18
,MAX(CASE WHEN (pos = "20") THEN date ELSE NULL END) position20
FROM table
WHERE
date BETWEEN "${od=2021-05-01 00:00:01}" AND "${do=2021-07-01 00:00:01}"
AND pos IN('10','18','20')
group by id )T
WHERE (position10 IS NOT NULL)
OR (position18 IS NOT NULL)
OR (position20 IS NOT NULL)
But it still chooses to me where the NULL value appears somewhere.

You need to change OR to AND in WHERE clause
SELECT * FROM (
Select id
,MAX(CASE WHEN (pos = "10") THEN date ELSE NULL END) position10
,MAX(CASE WHEN (pos = "18") THEN date ELSE NULL END) position18
,MAX(CASE WHEN (pos = "20") THEN date ELSE NULL END) position20
FROM table
WHERE
date BETWEEN "${od=2021-05-01 00:00:01}" AND "${do=2021-07-01 00:00:01}"
AND pos IN('10','18','20')
group by id )T
WHERE position10 IS NOT NULL
AND position18 IS NOT NULL
AND position20 IS NOT NULL

Related

Pivot function returning null values

I want to show all three rows in 1st row so i used pivot in the query
**output pic is attached **
SELECT [Container Number],ReadTimee,Locationn,Trailer FROM
(SELECT [Container Number],ReadTime,Location FROM #temp )as Tab1
PIVOT
(
MAX(ReadTime) FOR Location IN (Trailer,ReadTimee,Locationn)) AS Tab2
But using above query returning null
.The output pic is shown below
I want a output Like this
ReadTime Location Trailer Container Number ReadTime Location Trailer Container Number ReadTime Location Trailer Container Number
2019-02-27 03:17:21.033 CUSTOM 1 ZIMU1374787,TRHU3437713 2019-02-27 06:10:35.470 ZERO 1 ZIMU1374787,TRHU3437713 2019-02-27 07:30:47.407 CFS 1 ZIMU1374787,TRHU3437713
Try this:
SELECT Trailer, [Container Number], [CUSTOM] = MAX(CASE WHEN Location = 'CUSTOM' THEN ReadTime ELSE NULL END),
[ZERO] = MAX(CASE WHEN Location = 'ZERO' THEN ReadTime ELSE NULL END),
[CFS] = MAX(CASE WHEN Location = 'CFS' THEN ReadTime ELSE NULL END)
FROM #tmp
GROUP BY Trailer, [Container Number]
Above query returns:
Trailer Container Number CUSTOM ZERO CFS
1 ZIMU1374787,TRHU3437713 2019-02-27 03:17:21.033 2019-02-27 06:10:35.470 2019-02-27 07:30:47.407
You can use MAX method to get necessary columns:
SELECT
MAX(CASE WHEN q.RowNumber = 1 THEN q.ReadTime ELSE NULL END) AS ReadTime
, MAX(CASE WHEN q.RowNumber = 1 THEN q.Location ELSE NULL END) AS Location
, MAX(CASE WHEN q.RowNumber = 1 THEN q.Trailer ELSE NULL END) AS Trailer
, MAX(CASE WHEN q.RowNumber = 1 THEN q.ContainerNumber ELSE NULL END) AS ContainerNumber
, MAX(CASE WHEN q.RowNumber = 2 THEN q.ReadTime ELSE NULL END) AS ReadTime
, MAX(CASE WHEN q.RowNumber = 2 THEN q.Location ELSE NULL END) AS Location
, MAX(CASE WHEN q.RowNumber = 2 THEN q.Trailer ELSE NULL END) AS Trailer
, MAX(CASE WHEN q.RowNumber = 2 THEN q.ContainerNumber ELSE NULL END) AS ContainerNumber
, MAX(CASE WHEN q.RowNumber = 3 THEN q.ReadTime ELSE NULL END) AS ReadTime
, MAX(CASE WHEN q.RowNumber = 3 THEN q.Location ELSE NULL END) AS Location
, MAX(CASE WHEN q.RowNumber = 3 THEN q.Trailer ELSE NULL END) AS Trailer
, MAX(CASE WHEN q.RowNumber = 3 THEN q.ContainerNumber ELSE NULL END) AS ContainerNumber
FROM
(
SELECT
*
, ROW_NUMBER() OVER(PARTITION BY T.Trailer ORDER BY T.Trailer) AS RowNumber
FROM #Table AS T
)AS q
Let me show an example:
DECLARE #Table TABLE
(
ReadTime DATETIME,
[Location] VARCHAR(50),
Trailer INT,
ContainerNumber VARCHAR(50)
)
INSERT INTO #Table
(
ReadTime,
Location,
Trailer,
ContainerNumber
)
VALUES
( '2019-02-23 12:22:35.490', 'CUSTOM', 1, 'ZIMU1' )
, ( '2019-02-24 12:22:35.490', 'ZERO', 1, 'ZIMU2' )
, ( '2019-02-25 12:22:35.490', 'CFS', 1, 'ZIMU3')
And use the above query to get the desired output:

Need to break down into day..Right now its rolled up into months

SELECT A.SPLIT,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 1 END) AS AHT
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015' AND A.CALENDAR_DATE <= '9/30/2016' AND A.SPLIT IN (9,26,23,43,48,33,56,70,71,72,74,50) AND ( A.ent_split IS NOT NULL)
Group BY A.SPLIT
Heres is some of the data my code puts out.
Split CH CO AHT
9 957157 1156319 1156461
You mean this?
SELECT A.SPLIT, A.CALENDAR_DATE,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 1 END) AS AHT
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015' AND A.CALENDAR_DATE <= '9/30/2016' AND A.SPLIT IN (9,26,23,43,48,33,56,70,71,72,74,50) AND ( A.ent_split IS NOT NULL)
Group BY A.SPLIT, A.CALENDAR_DATE

Breakdown of data

SELECT SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) AS CH
,SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) + SUM(CASE
WHEN abandon_time IS NOT NULL
AND answer_time IS NULL
THEN 1
ELSE 0
END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015'
AND A.CALENDAR_DATE <= '4/30/2016'
AND A.SPLIT IN (
35
,55
,73
)
AND (A.ent_split IS NOT NULL)
Which in return rolled up all 3 split information to
CH 175282
CO 209452
My problem is I need the data to show individual fr each one of the split IN
"Data for each split" sounds like you need to use GROUP BY. Have you tried something like this?
SELECT A.SPLIT,
SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) AS CH
,SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) + SUM(CASE
WHEN abandon_time IS NOT NULL
AND answer_time IS NULL
THEN 1
ELSE 0
END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015'
AND A.CALENDAR_DATE <= '4/30/2016'
AND A.SPLIT IN (
35
,55
,73
)
AND (A.ent_split IS NOT NULL)
GROUP BY A.SPLIT
ORDER BY A.SPLIT

How to break my results down into Months

SELECT
COUNT(CALL_KEY) AS CONTACTS,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
CAST((CAST(SUM(A.talk_time)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END) AS float) + CAST(SUM(A.hold_time) AS float)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END)) + CAST(SUM(A.work_time) AS float)
/ CAST(COUNT(CASE WHEN A.answer_time > 0 THEN 1 ELSE 1 END) AS numeric(8, 2)) AS Int) AS AHT,
SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS Total_ABN,
SUM(A.answer_speed) AS ASA,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '5/31/2016' AND A.CALENDAR_DATE <= '7/19/2016' AND A.SPLIT IN (43) AND ( A.ent_split IS NOT NULL)
Here's My Results
CONTACTS CH AHT Total_ABN ASA CO
17199 16744 640 448 580643 17192
Assuming that the month can be obtained from the A.CALENDAR_DATE column
SELECT Month(A.CALENDAR_DATE) as Month,
COUNT(CALL_KEY) AS CONTACTS,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
CAST((CAST(SUM(A.talk_time)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END) AS float) + CAST(SUM(A.hold_time) AS float)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END)) + CAST(SUM(A.work_time) AS float)
/ CAST(COUNT(CASE WHEN A.answer_time > 0 THEN 1 ELSE 1 END) AS numeric(8, 2)) AS Int) AS AHT,
SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS Total_ABN,
SUM(A.answer_speed) AS ASA,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '5/31/2016' AND A.CALENDAR_DATE <= '7/19/2016' AND A.SPLIT IN (43) AND ( A.ent_split IS NOT NULL)
Group by Month(A.CALENDAR_DATE)

Case statement with grouping

Can you help me write a SELECT statement that returns a single row having columns for each TypeId involved for the transaction number 55?
CREATE TABLE Types
(
Id INT IDENTITY(1,1) NOT NULL,
Name VARCHAR(100) NOT NULL,
CONSTRAINT PK_Types PRIMARY KEY CLUSTERED(Id)
)
GO
INSERT INTO Types
VALUES
('Type1')
,('Type2')
,('Type3')
,('Type4')
,('Type5')
GO
CREATE TABLE Transactions
(
Id INT IDENTITY(1000,1) NOT NULL,
TypeId INT NULL,
TransactionNumber INT NOT NULL,
Amount MONEY NULL,
DateRecorded DATETIME2 NOT NULL,
CONSTRAINT PK_Transactions PRIMARY KEY CLUSTERED(Id),
CONSTRAINT FK_Types FOREIGN KEY(TypeId) REFERENCES Types(Id)
)
GO
INSERT INTO Transactions
VALUES
(1,55,2555.50,SYSDATETIME())
,(3,55,3555.50,SYSDATETIME())
,(4,55,4555.50,SYSDATETIME())
,(5,55,5555.50,SYSDATETIME())
GO
I need a single row returned for each transaction number in the Transactions table.
What I already tried:
SELECT TransactionNumber
,(CASE WHEN TypeId = 1 THEN Amount ELSE 0 END) AS Type1
,(CASE WHEN TypeId = 2 THEN Amount ELSE 0 END) AS Type2
,(CASE WHEN TypeId = 3 THEN Amount ELSE 0 END) AS Type3
,(CASE WHEN TypeId = 4 THEN Amount ELSE 0 END) AS Type4
,(CASE WHEN TypeId = 5 THEN Amount ELSE 0 END) AS Type5
FROM Transactions
SELECT TransactionNumber
,sum(CASE WHEN TypeId = 1 THEN Amount ELSE 0 END) AS Type1
,sum(CASE WHEN TypeId = 2 THEN Amount ELSE 0 END) AS Type2
,sum(CASE WHEN TypeId = 3 THEN Amount ELSE 0 END) AS Type3
,sum(CASE WHEN TypeId = 4 THEN Amount ELSE 0 END) AS Type4
,sum(CASE WHEN TypeId = 5 THEN Amount ELSE 0 END) AS Type5
FROM Transactions group by TransactionNumber;
I think you can do a PIVOT to get required output:
SELECT *
FROM (
SELECT ty.NAME, ISNULL(Amount,0)Amount
FROM TRANSACTIONS t
INNER JOIN Types ty ON ty.Id = t.TypeId
WHERE TransactionNumber = 55
) sub
PIVOT(
SUM(Amount) FOR [Name] IN ([Type1], [Type2], [Type3], [Type4], [Type5])
) AS pivottable
SQL Fiddle
With conditional aggregation:
select Id,
sum(case TypeId when 1 then Amount end) Type1,
sum(case TypeId when 2 then Amount end) Type2,
...
from Transactions
group by Id
I think this would be the query you are looking for:
SELECT
COUNT(CASE WHEN TypeID = 1 THEN 1 ELSE NULL END) '# Transactions Type 1'
, COUNT(CASE WHEN TypeID = 2 THEN 1 ELSE NULL END) '# Transactions Type 2'
, COUNT(CASE WHEN TypeID = 3 THEN 1 ELSE NULL END) '# Transactions Type 3'
, COUNT(CASE WHEN TypeID = 4 THEN 1 ELSE NULL END) '# Transactions Type 4'
, COUNT(CASE WHEN TypeID = 5 THEN 1 ELSE NULL END) '# Transactions Type 5'
FROM TRANSACTIONS
Where TransactionNumber = 55
You can find a working SQLFiddle here.
EDIT:
After you comment I think what you are actually looking for is SUM().
SELECT TransactionNumber
, SUM(CASE WHEN TypeId = 1 THEN Amount ELSE 0 END) AS Type1
, SUM(CASE WHEN TypeId = 2 THEN Amount ELSE 0 END) AS Type2
, SUM(CASE WHEN TypeId = 3 THEN Amount ELSE 0 END) AS Type3
, SUM(CASE WHEN TypeId = 4 THEN Amount ELSE 0 END) AS Type4
, SUM(CASE WHEN TypeId = 5 THEN Amount ELSE 0 END) AS Type5
FROM Transactions
GROUP BY TransactionNumber
SQLFiddle for 2nd query here.