how to show my data as in interval form in sql - sql

I have a Table like that:
ID Class Marks
1 12th 0
2 10th 25
3 09th 24
4 12th 50
5 10th 60
6 09th 70
Desired Output Like that:-
Marks CLass12th class9th class10th
0-25 1 1 1
25-50 1 0 0
50-60 1 0 1
60-70 0 1 1
Total 3 2 3
how can i do same with sql

CREATE TABLE marks
(
id INT,
class VARCHAR(200),
marks INT
);
INSERT INTO marks
VALUES (1,
'12th',
0);
INSERT INTO marks
VALUES (1,
'10th',
25);
INSERT INTO marks
VALUES (1,
'9th',
24);
INSERT INTO marks
VALUES (1,
'12th',
50);
INSERT INTO marks
VALUES (1,
'10th',
60);
INSERT INTO marks
VALUES (1,
'9th',
70);
-----not able to put (0-24) condition in marks as it is actually minus the value as -24 -----
SELECT CASE
WHEN marks >= 0
AND marks < 25 THEN ( 024 )
WHEN marks >= 25
AND marks <= 50 THEN ( 2550 )
WHEN marks >= 51
AND marks < 60 THEN ( 5160 )
WHEN marks >= 60
AND marks < 71 THEN ( 6070 )
ELSE NULL
END AS marks,
Sum(class12th) AS CLass12th,
Sum(class10th) AS CLass9th,
Sum(class9th) AS CLass9th
FROM (SELECT id,
marks,
[12th] AS CLass12th,
[10th] AS CLass10th,
[9th] AS CLass9th
FROM (SELECT id,
class,
marks
FROM marks) AS SourceTable
PIVOT ( Count(class)
FOR class IN ([12th],
[10th],
[9th]) ) AS pivottable)a
GROUP BY marks

Try this :
select '0-25', sum(case when class = '12th' then 1 else 0 end) '12th',sum(case when class = '10th' then 1 else 0 end) '10th',sum(case when class = '9th' then 1 else 0 end) '9th'
from Yourtable where marks >= 0 and marks < = 25
union
select '25-50', sum(case when class = '12th' then 1 else 0 end),sum(case when class = '10th' then 1 else 0 end),sum(case when class = '9th' then 1 else 0 end)
from Yourtable where marks >= 25 and marks < = 50
union
select '50-60', sum(case when class = '12th' then 1 else 0 end),sum(case when class = '10th' then 1 else 0 end),sum(case when class = '9th' then 1 else 0 end)
from Yourtable where marks >= 50 and marks < = 60
union
select '60-70', sum(case when class = '12th' then 1 else 0 end),sum(case when class = '10th' then 1 else 0 end),sum(case when class = '9th' then 1 else 0 end)
from Yourtable where marks >= 60 and marks < = 70
union
select 'total', sum(case when class = '12th' then 1 else 0 end),sum(case when class = '10th' then 1 else 0 end),sum(case when class = '9th' then 1 else 0 end)
from Yourtable

You need to store your ranges our use a CTE and use it in a query like this:
;with ranges as (
select 0 fromMark, 25 toMark, '0-25' title
union all select 25,50, '25-50'
union all select 50,60, '50-60'
union all select 60,70, '60-70'
union all select 0,100, 'Total'
)
select
r.title,
count(case when t.Class = '12th' then 1 end) Class12th,
count(case when t.Class = '09th' then 1 end) Class9th,
count(case when t.Class = '10th' then 1 end) Class10th
from yourTable t
left join ranges r
on t.Marks >= r.fromMark and t.Marks < r.toMark
group by
r.title;

Related

Query to get a output in desired format

Proc type add sub multi div
1 A 1 0 1 1
1 B 2 2 0 1
Output should be in the format
Proc Aadd Asub Amulti Adiv Badd Bsub Bmulti Bdiv
1 1 0 1 1 2 2 0 1
A simple conditional aggregation should do the trick
Select Proc
,Aadd = max( case when type='A' then add else 0 end)
,Asub = max( case when type='A' then sub else 0 end)
,Amuti = max( case when type='A' then multi else 0 end)
,Adiv = max( case when type='A' then div else 0 end)
,Badd = max( case when type='B' then add else 0 end)
,Bsub = max( case when type='B' then sub else 0 end)
,Bmuti = max( case when type='B' then multi else 0 end)
,Bdiv = max( case when type='B' then div else 0 end)
From YourTable
Group By Proc
Another approach using CTE and Joins.
declare #table table(Proce int, type char(1), addi int, sub int, multi int, div int)
insert into #table values
(1,'A', 1, 0, 1, 1),
(1,'B', 2, 2, 0, 1);
;with cte_a as
(
SELECT proce, max(addi) as Aadd, max(sub) as Asub, max(multi) as Amulti, max(div) as Adiv
FROM #table where type = 'A'
group by proce
),cte_b as
(
SELECT proce, max(addi) as Badd, max(sub) as Bsub, max(multi) as Bmulti, max(div) as Bdiv
FROM #table where type = 'B'
group by proce
)
SELECT a.proce,a.aAdd, a.aSub, a.Amulti, a.Adiv,b.BAdd,b.bsub, b.bmulti, b.bdiv
from cte_a as a
join cte_b as b
on a.Proce = b.Proce
proce
aAdd
aSub
Amulti
Adiv
BAdd
bsub
bmulti
bdiv
1
1
0
1
1
2
2
0
1

Adding a dummy identifier to data that varies by position and value

I am working on a project in SQL Server with diagnosis codes and a patient can have up to 4 codes but not necessarily more than 1 and a patient cannot repeat a code more than once. However, codes can occur in any order. My goal is to be able to count how many times a Diagnosis code appears in total, as well as how often it appears in a set position.
My data currently resembles the following:
PtKey
Order #
Order Date
Diagnosis1
Diagnosis2
Diagnosis3
Diagnosis 4
345
1527
7/12/20
J44.9
R26.2
NULL
NULL
367
1679
7/12/20
R26.2
H27.2
G47.34
NULL
325
1700
7/12/20
G47.34
NULL
NULL
NULL
327
1710
7/12/20
I26.2
J44.9
G47.34
NULL
I would think the best approach would be to create a dummy column here that would match up the diagnosis by position. For example, Diagnosis 1 with A, and Diagnosis 2 with B, etc.
My current plan is to rollup the diagnosis using an unpivot:
UNPIVOT ( Diag for ColumnALL IN (Diagnosis1, Diagnosis2, Diagnosis3, Diagnosis4)) as unpvt
However, this still doesn’t provide a way to count the diagnoses by position on a sales order.
I want it to look like this:
Diagnosis
Total Count
Diag1 Count
Diag2 Count
Diag3 Count
Diag4 Count
J44.9
2
1
1
0
0
R26.2
1
1
0
0
0
H27.2
1
0
1
0
0
I26.2
1
1
0
0
0
G47.34
3
1
0
2
0
You can unpivot using apply and aggregate:
select v.diagnosis, count(*) as cnt,
sum(case when pos = 1 then 1 else 0 end) as pos_1,
sum(case when pos = 2 then 1 else 0 end) as pos_2,
sum(case when pos = 3 then 1 else 0 end) as pos_3,
sum(case when pos = 4 then 1 else 0 end) as pos_4
from data d cross apply
(values (diagnosis1, 1),
(diagnosis2, 2),
(diagnosis3, 3),
(diagnosis4, 4)
) v(diagnosis, pos)
where diagnosis is not null;
Another way is to use UNPIVOT to transform the columns into groupable entities:
SELECT Diagnosis, [Total Count] = COUNT(*),
[Diag1 Count] = SUM(CASE WHEN DiagGroup = N'Diagnosis1' THEN 1 ELSE 0 END),
[Diag2 Count] = SUM(CASE WHEN DiagGroup = N'Diagnosis2' THEN 1 ELSE 0 END),
[Diag3 Count] = SUM(CASE WHEN DiagGroup = N'Diagnosis3' THEN 1 ELSE 0 END),
[Diag4 Count] = SUM(CASE WHEN DiagGroup = N'Diagnosis4' THEN 1 ELSE 0 END)
FROM
(
SELECT * FROM #x UNPIVOT (Diagnosis FOR DiagGroup IN
([Diagnosis1],[Diagnosis2],[Diagnosis3],[Diagnosis4])) up
) AS x GROUP BY Diagnosis;
Example db<>fiddle
You can also manually unpivot via UNION before doing the conditional aggregation:
SELECT Diagnosis, COUNT(*) As Total Count
, SUM(CASE WHEN Position = 1 THEN 1 ELSE 0 END) As [Diag1 Count]
, SUM(CASE WHEN Position = 2 THEN 1 ELSE 0 END) As [Diag2 Count]
, SUM(CASE WHEN Position = 3 THEN 1 ELSE 0 END) As [Diag3 Count]
, SUM(CASE WHEN Position = 4 THEN 1 ELSE 0 END) As [Diag4 Count]
FROM
(
SELECT PtKey, Diagnosis1 As Diagnosis, 1 As Position
FROM [MyTable]
UNION ALL
SELECT PtKey, Diagnosis2 As Diagnosis, 2 As Position
FROM [MyTable]
WHERE Diagnosis2 IS NOT NULL
UNION ALL
SELECT PtKey, Diagnosis3 As Diagnosis, 3 As Position
FROM [MyTable]
WHERE Diagnosis3 IS NOT NULL
UNION ALL
SELECT PtKey, Diagnosis4 As Diagnosis, 4 As Position
FROM [MyTable]
WHERE Diagnosis4 IS NOT NULL
) d
GROUP BY Diagnosis
Borrowing Aaron's fiddle, to avoid needing to rebuild the schema from scratch, and we get this:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=d1f7f525e175f0f066dd1749c49cc46d

Trying to fix this -- found ")" (at char 81) expecting a keyword -- error

I'm trying to run this but I keep getting, error
^ found ")" (at char 81) expecting a keyword
The char 81 is in the 2nd SELECT portion AS
There is clearly an keyword AS in there
SELECT full_name, clm_id,
CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '0TO30'
WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END) AS '31TO60'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '61TO90'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '91TO120'
WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END) AS 'GreaterThan120'
ELSE 'OTHERS'
END AS DAYS_RANGE,
COUNT(*) as NUMBER_OF_DAYS
FROM
(
SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%'
AND EXTNL_STAT_CD = 'SUBMITTED'
) A
LEFT JOIN mt_employee_current_vw
ON clm_claim_rds_vw.examined_by_empl_id = mt_employee_current_vw.employee_number
GROUP BY full_name
You have wrong closing ) after each END.. try removing
SELECT full_name, clm_id,
CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '0TO30'
WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END AS '31TO60'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '61TO90'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '91TO120'
WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END AS 'GreaterThan120'
ELSE 'OTHERS'
END AS DAYS_RANGE,
COUNT(*) as NUMBER_OF_DAYS
FROM
(
SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%'
AND EXTNL_STAT_CD = 'SUBMITTED'
) A
LEFT JOIN mt_employee_current_vw
ON clm_claim_rds_vw.examined_by_empl_id = mt_employee_current_vw.employee_number
GROUP BY full_name
I think you want conditional aggregation:
SELECT ec.full_name, ec.clm_id,
SUM(CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS cnt_0TO30,
SUM(CASE WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END) AS cnt_31TO60,
SUM(CASE WHEN OPEN_DAYS BETWEEN 61 AND 90 THEN 1 ELSE 0 END) AS cnt_61TO90,
SUM(CASE WHEN OPEN_DAYS BETWEEN 91 AND 120 THEN 1 ELSE 0 END) AS cnt_91TO120,
SUM(CASE WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END) AS cnt_GreaterThan120
SUM(CASE WHEN OPEN_DAYS IS NULL OR OPEN_DAYS < 0 THEN 1 ELSE 0 END) as cnt_Others,
COUNT(*) as NUMBER_OF_DAYS
FROM (SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%' AND
EXTNL_STAT_CD = 'SUBMITTED'
) ccr LEFT JOIN
mt_employee_current_vw ec
ON ccw.examined_by_empl_id = ec.employee_number
GROUP BY ec.full_name, ec.clm_id;

group by and select max with value null

I have a next problem with query
SELECT
T.DETALLE_BECA_ANIO anio,
T.DETALLE_BECA_MES mes,
T.DETALLE_BECA_NIVEL_EDU_ID edu_id,
T.DETALLE_BECA_TRAMO_ID tr_id,
MAX(
CASE
WHEN T.DETALLE_BECA_TIPO_BENE_ID IS NULL
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) mant ,
MAX(
CASE
WHEN T.DETALLE_BECA_TIPO_BENE_ID = 1
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) tras
FROM
(SELECT DETALLE_BECA_NIVEL_EDU_ID,
DETALLE_BECA_BECA_ID,
DETALLE_BECA_TIPO_BENE_ID,
DETALLE_BECA_VALOR,
DETALLE_BECA_MES,
DETALLE_BECA_REGION_ID,
DETALLE_BECA_PROVINCIA_ID,
DETALLE_BECA_ANIO,
DETALLE_BECA_TRAMO_ID,
DETALLE_BECA_COMUNA_ID
FROM TBL_DETALLE_BECAS
WHERE (DETALLE_BECA_TIPO_BENE_ID = 1
OR DETALLE_BECA_TIPO_BENE_ID IS NULL)
and DETALLE_BECA_BECA_ID = 1
and detalle_beca_mes = 3
) T
GROUP BY T.DETALLE_BECA_BECA_ID,
T.DETALLE_BECA_TRAMO_ID,
T.DETALLE_BECA_REGION_ID,
T.DETALLE_BECA_PROVINCIA_ID,
T.DETALLE_BECA_ANIO,
T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID,
T.DETALLE_BECA_COMUNA_ID
ORDER BY T.DETALLE_BECA_BECA_ID,
T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID
output:
"ANIO" "MES" "EDU_ID" "TR_ID" "MANT" "TRAS"
2017 3 2 0.62 0 NULL
2017 3 3 1.24 6 NULL
2017 3 NULL 1.0 NULL 1
I need that sum value where EDU_ID is null with value 2,3 in TR_ID and replace value null in "tras" with value from EDU is null
"ANIO" "MES" "EDU_ID" "TR_ID" "MANT" "TRAS"
2017 3 2 1.62 0 1
2017 3 3 2.24 6 1
I writed query with min(edu_id) or max(edu_id ) but could not solve my problem.
The other thing that occurred to me is to make a join with the same table
First, this makes more sense as your query:
SELECT T.DETALLE_BECA_ANIO as anio, T.DETALLE_BECA_MES as mes,
T.DETALLE_BECA_NIVEL_EDU_ID as edu_id, T.DETALLE_BECA_TRAMO_ID as tr_id,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID IS NULL
THEN NVL(DETALLE_BECA_VALOR, 0)
ELSE 0
END) as mant ,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID = 1
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) tras
FROM TBL_DETALLE_BECAS
WHERE (DETALLE_BECA_TIPO_BENE_ID = 1 OR DETALLE_BECA_TIPO_BENE_ID IS NULL) AND
DETALLE_BECA_BECA_ID = 1 AND
detalle_beca_mes = 3
GROUP BY T.DETALLE_BECA_ANIO, T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID, T.DETALLE_BECA_TRAMO_ID
ORDER BY T.DETALLE_BECA_BECA_ID, T.DETALLE_BECA_MES, T.DETALLE_BECA_NIVEL_EDU_ID;
This eliminates the subquery (unnecessary) and only aggregates by the columns being returned. A proper query might fix your problem.
But, you seem to want to use NULL to be "all" for the other columns. If so, something like this will work:
WITH t as (
SELECT T.DETALLE_BECA_ANIO as anio, T.DETALLE_BECA_MES as mes,
T.DETALLE_BECA_NIVEL_EDU_ID as edu_id, T.DETALLE_BECA_TRAMO_ID as tr_id,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID IS NULL
THEN NVL(DETALLE_BECA_VALOR, 0)
ELSE 0
END) as mant ,
MAX(CASE WHEN T.DETALLE_BECA_TIPO_BENE_ID = 1
THEN NVL(DETALLE_BECA_VALOR,0)
ELSE 0
END) tras
FROM TBL_DETALLE_BECAS
WHERE (DETALLE_BECA_TIPO_BENE_ID = 1 OR DETALLE_BECA_TIPO_BENE_ID IS NULL) AND
DETALLE_BECA_BECA_ID = 1 AND
detalle_beca_mes = 3
GROUP BY T.DETALLE_BECA_ANIO, T.DETALLE_BECA_MES,
T.DETALLE_BECA_NIVEL_EDU_ID, T.DETALLE_BECA_TRAMO_ID
)
SELECT t.ANIO, t.MES, t.EDU_ID,
COALESCE(t.TR_ID, 0) + COALESCE(tnull.TR_ID, 0) as TR_ID,
t.MANT,
COALESCE(t.TRAS, 0) + COALESCE(tnull.TRAS, 0) as TRAS
FROM t LEFT JOIN
(SELECT t.*
FROM t
WHERE t.edu_id IS NULL
) tnull
ON tnull.ANIO = t.ANIO AND tnull.MES = t.MES
WHERE t.edu_id IS NOT NULL
ORDER BY T.DETALLE_BECA_BECA_ID, T.DETALLE_BECA_MES, T.DETALLE_BECA_NIVEL_EDU_ID;

Sub select & union all

SELECT C.ClientCaseNumber,
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (28, 29, 30) THEN 1
ELSE 0
END) AS [Wezwania],
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (14, 21) THEN 1
ELSE 0
END) AS [Kontakt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 32 THEN 1
ELSE 0
END) AS [SMS],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 44 THEN 1
ELSE 0
END) AS [Zgon],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 49 THEN 1
ELSE 0
END) AS [Areszt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 37 THEN 1
ELSE 0
END) AS [Odmowa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 39 THEN 1
ELSE 0
END) AS [Podważa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 99 THEN 1
ELSE 0
END) AS [Ugoda],
[Adres],
[Numer],
[Mail],
[Powód]
FROM (SELECT Notes AS [Adres]
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 68
UNION ALL
SELECT Info AS [Numer]
FROM CaseActionHistory
WHERE CaseActionDefinitionId IN (54, 55, 56, 58,
59, 60, 61, 62, 63)
UNION ALL
SELECT Notes AS [Mail]
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 66
UNION ALL
SELECT Description AS [Powód]
FROM CaseActionDefinition
JOIN CaseActionHistory AS C
ON DefinitionId = C.CaseActionDefinitionId
WHERE DefinitionId BETWEEN 70 AND 78) AS x
INNER JOIN CaseDetails AS C
ON x.CaseDetailId = C.CaseDetaislId
INNER JOIN CaseActionHistory AS CA
ON C.CaseDetailsId = CA.CaseDetailId
WHERE C.ClientId = '11'
GROUP BY C.ClientCaseNumber
I've got such query. As return shows error of invalid columns "CaseDetailId, CaseDetailsId, Mail, Numer, Powód".
http://oi39.tinypic.com/2vwy44n.jpg
That's more or less how the results should look like.
ClientCaseNumber is taken from table CaseDetails
All the sums are sums of code added to CaseActionHistory table.
Notes/Info are in CaseActionHistory table
Description is placed in CaseActionDefinition table.
Between tables there are such connections:
CaseDetails.CaseDetailId = CaseActionHistory.CaseDetailsId
CaseActionHistory.CaseActionDefinitionId = CaseActionDefinition.DefinitionId
The UNION clause does not work like that.
This query:
select Notes as [Adres] from CaseActionHistory where ...
UNION ALL
select Info as [Numer] from CaseActionHistory where ...
UNION ALL
select Notes as [Mail] from CaseActionHistory where ...
UNION ALL
select Description as [Powód] from CaseActionDefinition join CaseActionHistory ...
will not populate a table with 4 columns. Instead it will be a table with one column, with all the values one after the other. The name of the column will be taken from the first SELECT, i.e.
if the first query returns values 1 and 2,
the second query returns values 3 and 4
the third query returns values 5 and 6
the fourth query returns values 7 and 8
you wont get:
Adres | Numer | Mail | Powód
------------------------------
1 | 3 | 5 | 7
2 | 4 | 6 | 8
but you'll get:
Adres
-------
1
2
3
4
5
6
7
8
Are you not missing the alias 'x' from these fields:
SELECT
C.ClientCaseNumber
,sum(case when CA.CaseActionDefinitionId in (28,29,30) then 1 else 0 end) as [Wezwania]
,sum(case when CA.CaseActionDefinitionId in (14,21) then 1 else 0 end) as [Kontakt]
,sum(case when CA.CaseActionDefinitionId = 32 then 1 else 0 end) as [SMS]
,sum(case when CA.CaseActionDefinitionId = 44 then 1 else 0 end) as [Zgon]
,sum(case when CA.CaseActionDefinitionId = 49 then 1 else 0 end) as [Areszt]
,sum(case when CA.CaseActionDefinitionId = 37 then 1 else 0 end) as [Odmowa]
,sum(case when CA.CaseActionDefinitionId = 39 then 1 else 0 end) as [Podważa]
,sum(case when CA.CaseActionDefinitionId = 99 then 1 else 0 end) as [Ugoda]
,x.[Adres]
,x.[Numer]
,x.[Mail]
,x.[Powód]
FROM
(select Notes as [Adres] from CaseActionHistory where CaseActionDefinitionId = 68
UNION ALL
select Info as [Numer] from CaseActionHistory where CaseActionDefinitionId in (54,55,56,58,59,60,61,62,63)
UNION ALL
select Notes as [Mail] from CaseActionHistory where CaseActionDefinitionId = 66
UNION ALL
select Description as [Powód] from CaseActionDefinition join CaseActionHistory as C on DefinitionId = C.CaseActionDefinitionId where DefinitionId between 70 and 78)
AS x
inner join CaseDetails as C on x.CaseDetailId = C.CaseDetaislId
inner join CaseActionHistory as CA on C.CaseDetailsId = CA.CaseDetailId
where C.ClientId = '11'
GROUP by C.ClientCaseNumber