I am trying to execute the following query and getting a error.
but when i tried to use commented lines its work. but not working with contains function.
select
2015-min(c.Year)+1 as experiance
, sum (case when not c.JudgementID_FK is null then 1 else 0 end ) as reported_Judgements
, sum( case
when
(
Contains( c.Result , 'ACCEPT')
or
Contains( c.Result , 'ALLOW' )
or
contains (c.Result , 'Grant')
-- (
-- c.Result = 'Accepted' or c.Result = 'Allowed'
-- or c.Result='allowed' or c.Result='accepted'
-- or c.Result ='bail allowed(accepted)'
-- or c.Result = 'admitted'
-- or c.Result = 'Accepted.'
-- or c.Result = 'accepted.'
-- )
-- and
--cl.LawyerOf = 'Petitioner'
)
then 1 else 0 end) as win_cases
, sum (case when c.JudgementID_FK is null then 1 else 1 end ) as total_Cases
from CaseTLS c, CaseLawyer cl , Lawyer l
Where
c.CaseId = cl.CaseId
and cl.ComputerCode = l.ComputerCode
group by l.computercode
order by l.computercode
According to this documentation "CONTAINS is a predicate used in the WHERE clause" and you are trying to use it in the SELECT clause.
Try using PATINDEX instead:
select
2015-min(c.Year)+1 as experiance
, sum (case when not c.JudgementID_FK is null then 1 else 0 end ) as reported_Judgements
, sum( case
when
(
PATINDEX( '%ACCEPT%', UPPER(c.Result)) > 0
or
PATINDEX( '%ALLOW%', UPPER(c.Result)) > 0
or
PATINDEX( '%GRANT%', UPPER(c.Result)) > 0
)
then 1 else 0 end) as win_cases
, sum (case when c.JudgementID_FK is null then 1 else 1 end ) as total_Cases
from CaseTLS c, CaseLawyer cl , Lawyer l
Where
c.CaseId = cl.CaseId
and cl.ComputerCode = l.ComputerCode
group by l.computercode
order by l.computercode
Related
I have the following code:
select
FeeEarnerID,
(
select
(select [name] from [User] AS u where u.userid=f.userid)
from
feeearner AS f
where
f.FeeEarnerID=aa.FeeEarnerID
) FeeEarner,
sum(aa.FEES) Fees,
sum(aa.DISB) Disbursements,
sum(aa.CREDITORS) Creditors
from
(
SELECT
FeeEarner.FeeEarnerID,
case when WIPTransaction.WIPTransactionTypeID IN (1,17,18,20,21,25) then WIPTransaction.Amount else 0 end 'FEES',
case when WIPTransaction.WIPTransactionTypeID IN (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,26,27,28,29) then WIPTransaction.Amount else 0 end 'DISB',
case when WIPTransaction.WIPTransactionTypeID IN (24) then WIPTransaction.Amount else 0 end 'CREDITORS'
FROM
(
(
FeeEarner
JOIN
WIPTransaction ON FeeEarner.FeeEarnerID = WIPTransaction.FeeEarnerID
)
JOIN
WIPTransactionType ON WIPTransactionType.WIPTransactionTypeID = WIPTransaction.WIPTransactionTypeID
)
WHERE
(WIPTransaction.TransactionDate BETWEEN '2020-10-01' AND '2020-12-31')
)
AS aa
group by
FeeEarnerID
Used Table names: WIPtransaction, WIPtransactiontype, Feeearner
I want to display two more columns at the end of the output, namely: Invoiced and Uninvoiced.
The "Invoicenumber" field in the "WIPtransaction" database will be tested for this. If the "Invoicenumber" is NULL - the transaction amount will be added to a sum in the uninvoiced column and if "Invoicenumber" contains a number - the transaction amount will be added to a sum in the invoiced column.
What is the code that I would need to write and where would it be placed?
select
FeeEarnerID,
(
select
(select [name] from [User] AS u where u.userid=f.userid)
from
feeearner AS f
where
f.FeeEarnerID=aa.FeeEarnerID
) FeeEarner,
sum(aa.FEES) Fees,
sum(aa.DISB) Disbursements,
sum(aa.CREDITORS) Creditors,
----------
SUM( InvoicedAmount) AS InvoicedAmount,
SUM(UnInvoicedAmount) AS UnInvoicedAmount
----------
from
(
SELECT
FeeEarner.FeeEarnerID,
case when WIPTransaction.WIPTransactionTypeID IN (1,17,18,20,21,25) then WIPTransaction.Amount else 0 end 'FEES',
case when WIPTransaction.WIPTransactionTypeID IN (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,26,27,28,29) then WIPTransaction.Amount else 0 end 'DISB',
case when WIPTransaction.WIPTransactionTypeID IN (24) then WIPTransaction.Amount else 0 end 'CREDITORS',
----------
CASE WHEN WIPTransaction.Invoicenumber IS NOT NULL THEN WIPTransaction.Amount END AS InvoicedAmount,
CASE WHEN WIPTransaction.Invoicenumber IS NULL THEN WIPTransaction.Amount END AS UnInvoicedAmount
----------
FROM
FeeEarner
JOIN
WIPTransaction ON FeeEarner.FeeEarnerID = WIPTransaction.FeeEarnerID
JOIN
WIPTransactionType ON WIPTransactionType.WIPTransactionTypeID = WIPTransaction.WIPTransactionTypeID
WHERE
WIPTransaction.TransactionDate BETWEEN '2020-10-01' AND '2020-12-31'
)
AS aa
group by
FeeEarnerID
You can remove your derived query and combine it all into one. The FeeEarner double sub-query can also be optimized:
select
FeeEarnerID,
(
select [name] from [User] AS u where u.userid=FeeEarner.userid
) FeeEarner,
sum(case when WIPTransaction.WIPTransactionTypeID IN (1,17,18,20,21,25) then WIPTransaction.Amount else 0 end) Fees,
sum(case when WIPTransaction.WIPTransactionTypeID IN (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,26,27,28,29) then WIPTransaction.Amount else 0 end) Disbursements,
sum(case when WIPTransaction.WIPTransactionTypeID IN (24) then WIPTransaction.Amount else 0 end) Creditors,
SUM(CASE WHEN WIPTransaction.Invoicenumber IS NOT NULL THEN WIPTransaction.Amount END) AS InvoicedAmount,
SUM(CASE WHEN WIPTransaction.Invoicenumber IS NULL THEN WIPTransaction.Amount END) AS UnInvoicedAmount
FROM
FeeEarner
JOIN
WIPTransaction ON FeeEarner.FeeEarnerID = WIPTransaction.FeeEarnerID
JOIN
WIPTransactionType ON WIPTransactionType.WIPTransactionTypeID = WIPTransaction.WIPTransactionTypeID
WHERE
WIPTransaction.TransactionDate BETWEEN '2020-10-01' AND '2020-12-31'
group by
FeeEarnerID;
I am trying to add a case expression in my query but it is inside of a select from statement. SQL Server returns multiple errors when I try to execute it.
UNION all
SELECT 'Min WA APR for Canada MPL Receivables' AS [Text], ISNULL([Value],0.0) AS [Value], 'percent' AS [ValueType], [Limit], CASE WHEN [Excess] > 0 THEN 0 ELSE ABS(ISNULL([Excess],0.0)) END AS [Excess]
FROM (
--SELECT [part1]/[part2] AS [Value], [Limit], ([part1] - [Limit]*[part2])/(49.00 - [Limit]) AS [Excess]
SELECT [part1]/[part2] AS [Value], [Limit], ([part1] - [Limit]*[part2])/100.0 AS [Excess]
FROM (
CASE isLPP WHEN 1 THEN
SELECT SUM(RateLPP * OutstandingPrincipal * FAEligibility)
ELSE
SELECT SUM(APR * OutstandingPrincipal * FAEligibility)
END AS [part1]
FROM [dfc_BorrowingBaseRecords] WITH (NOLOCK)
WHERE FAEligibility = 1
AND Region = 'Canada'
AND LoanType = 'MPL'
) first
, (
SELECT 44.90 AS [Limit], SUM(OutstandingPrincipal * FAEligibility) AS [part2]
FROM [dfc_BorrowingBaseRecords] WITH (NOLOCK)
WHERE FAEligibility = 1
AND Region = 'Canada'
AND LoanType = 'MPL'
) second
) third
I need to do one calculation if a flag is set and another if it is not.
I think the following is what you are looking for.
The key point is to use the case expression inside the sum to conditionally determine what value you are summing.
SELECT 'Min WA APR for Canada MPL Receivables' AS [Text], ISNULL([Value],0.0) AS [Value]
, 'percent' AS [ValueType], [Limit]
, CASE WHEN [Excess] > 0 THEN 0 ELSE ABS(ISNULL([Excess],0.0)) END AS [Excess]
FROM (
SELECT [part1]/[part2] AS [Value], [Limit], ([part1] - [Limit]*[part2])/100.0 AS [Excess]
FROM (
SELECT 44.90 AS [Limit]
, SUM(CASE isLPP WHEN 1 THEN RateLPP ELSE APR END * OutstandingPrincipal * FAEligibility) [part1]
, SUM(OutstandingPrincipal * FAEligibility) [part2]
FROM [dfc_BorrowingBaseRecords]
WHERE FAEligibility = 1
AND Region = 'Canada'
AND LoanType = 'MPL'
) X
) Y
I have a query which return a flag wheter a client who made a contract with my company this year is new or returning:
WITH Resultset AS(
SELECT
Cnt = COUNT(*)
,KliRC --personal identification number
FROM dbo.Smlouvy
WHERE VyplacenaCastka > 0
GROUP BY KliRC
)
SELECT
s.KliRC
,CASE WHEN Cnt > 1 THEN 1 ELSE 0 END AS Novy --new client
,CASE WHEN Cnt = 1 THEN 1 ELSE 0 END AS Stavajici --existing client
FROM Resultset JOIN dbo.Smlouvy s ON s.KliRC = resultset.KliRC
WHERE (YEAR(DatumZadosti) = YEAR(GETDATE())) AND (s.KliRC NOT LIKE '%x')
Now, I need to aggregate all the new and existing clients and make a ratio between them.
Any ideas? Thanks in advance.
I think this does what you want:
WITH Resultset AS (
SELECT COUNT(*) as cnt,
KliRC --personal identification number,
(CASE WHEN COUNT(*) > 1 THEN 1 ELSE 0 END) AS Novy --new client
(CASE WHEN COUNT(*) = 1 THEN 1 ELSE 0 END) AS Stavajici
FROM dbo.Smlouvy
WHERE VyplacenaCastka > 0
GROUP BY KliRC
)
SELECT SUM(Novy) / SUM(Stavajici)
FROM Resultset r JOIN
dbo.Smlouvy s
ON s.KliRC = r.KliRC
WHERE YEAR(DatumZadosti) = YEAR(GETDATE()) AND
s.KliRC NOT LIKE '%x';
Your query can be simplified to
SELECT SUM(Novy)*1.0/SUM(Stavajici)
FROM (
SELECT KliRC
,CASE WHEN COUNT(*) OVER(PARTITION BY KliRC) > 1 THEN 1 ELSE 0 END AS Novy --new client
,CASE WHEN COUNT(*) OVER(PARTITION BY KliRC) = 1 THEN 1 ELSE 0 END AS Stavajici --existing client
FROM dbo.Smlouvy
WHERE YEAR(DatumZadosti) = YEAR(GETDATE()) AND KliRC NOT LIKE '%x'
) T
i have a query like this:
SELECT
MIN(F_Exhibition_Name) AS F_Site_name,
(SELECT
SUM(F_Quantity)
FROM T_assets
WHERE T_assets.F_ref_code = T_Item_movement.ItemCode
AND F_State = 'A')
AS openingqty,
CASE
WHEN dbo.T_Item_Movement.F_Status = 1 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END,
CASE
WHEN dbo.T_Item_Movement.F_Status = 2 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END
FROM T_Item_movement
LEFT OUTER JOIN T_L2Category
ON T_L2Category.F_ItemCode = T_Item_movement.ItemCode
LEFT OUTER JOIN T_Exhibition
ON T_Exhibition.F_Exhibition_Code = T_Item_movement.F_Site_Code
WHERE F_Cat_code = 'FN'
--AND F_Cat_code IN ('EC', 'EL', 'FL', 'FN', 'GR', 'MX', 'OT', 'SH')
AND F_L1Cat_code = 'TT'
AND itemcode = 'TT015-BLK'
AND CONVERT(varchar(10), F_datetime, 112) >= '20130915'
AND CONVERT(varchar(10), F_datetime, 112) <= '20150915'
AND F_Exhibition_Name IS NOT NULL
GROUP BY ItemCode,
dbo.T_Item_Movement.F_Status,
dbo.T_Item_Movement.F_Site_Code,
dbo.T_Item_Movement.F_Frm_Site_Code
i am getting out put like this:
I am getting gulf glass 2015 in two column. I want to get in column and show the result in same column only.
Expected output :
Gulf class 2015 6 5
ATM 2015 0 3
It's the GROUP BY columns that are causing it.
You could change your CASE Statement to SUM the result, and therefore remove some of the columns in the GROUP BY:
...
SUM(CASE WHEN dbo.T_Item_Movement.F_Status = 1 AND ...
THEN dbo.T_Item_Movement.F_Quantity
ELSE 0
END),
...
GROUP BY ItemCode
SELECT F_Site_name, openingqty,
SUM(col1) AS col1, SUM(col2) AS col2
FROM
(
SELECT
MIN(F_Exhibition_Name) AS F_Site_name,
(SELECT
SUM(F_Quantity)
FROM T_assets
WHERE T_assets.F_ref_code = T_Item_movement.ItemCode
AND F_State = 'A')
AS openingqty,
CASE
WHEN dbo.T_Item_Movement.F_Status = 1 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END AS col1,
CASE
WHEN dbo.T_Item_Movement.F_Status = 2 AND
dbo.T_Item_Movement.F_Site_Code <> dbo.T_Item_Movement.F_Frm_Site_Code THEN SUM(dbo.T_Item_Movement.F_Quantity)
ELSE 0
END AS col2
FROM T_Item_movement
LEFT OUTER JOIN T_L2Category
ON T_L2Category.F_ItemCode = T_Item_movement.ItemCode
LEFT OUTER JOIN T_Exhibition
ON T_Exhibition.F_Exhibition_Code = T_Item_movement.F_Site_Code
WHERE F_Cat_code = 'FN'
--AND F_Cat_code IN ('EC', 'EL', 'FL', 'FN', 'GR', 'MX', 'OT', 'SH')
AND F_L1Cat_code = 'TT'
AND itemcode = 'TT015-BLK'
AND CONVERT(varchar(10), F_datetime, 112) >= '20130915'
AND CONVERT(varchar(10), F_datetime, 112) <= '20150915'
AND F_Exhibition_Name IS NOT NULL
GROUP BY ItemCode,
dbo.T_Item_Movement.F_Status,
dbo.T_Item_Movement.F_Site_Code,
dbo.T_Item_Movement.F_Frm_Site_Code
) AS t
GROUP BY F_Site_name, openingqty
i want to pivot table but i get some error about this image
this is my code
SELECT ARCBG_Abbrev
,ARCIM_Code
,SUM(CASE WHEN TAR_Code = 'O' THEN ITP_Price
ELSE 0 END) AS O
,SUM(CASE WHEN TAR_Code = 'I' THEN ITP_Price ELSE 0 END)
AS I
,SUM(CASE WHEN TAR_Code = 'F' THEN ITP_Price ELSE 0 END)
AS F
FROM SYSTEM.VS_OrderItem
WHERE (ARCIM_Code = '010004')
GROUP BY ARCBG_Abbrev, ARCIM_Code
i try to distinct column to check value in column is same in every row where ARCIM_Code = 010004 .it's the same value in column ARCBG_Abbrev. Can you check my pivot code plascc. thank you
on the assumption it's an ODBC driver limitation, perhaps nesting the case expressions as a subquery will help.
SELECT
ARCBG_Abbrev
, ARCIM_Code
, SUM(O) AS O
, SUM(I) AS I
, SUM(F) AS F
FROM (
SELECT
ARCBG_Abbrev
, ARCIM_Code
, CASE
WHEN TAR_Code = 'O' THEN
ITP_Price
ELSE
0
END AS O
, CASE
WHEN TAR_Code = 'I' THEN
ITP_Price
ELSE
0
END AS I
, CASE
WHEN TAR_Code = 'F' THEN
ITP_Price
ELSE
0
END AS F
FROM SYSTEM.VS_OrderItem
) AS SQ
WHERE ARCIM_Code = '010004'
GROUP BY
ARCBG_Abbrev
, ARCIM_Code