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
Related
I am little bit new to SQL. I am trying to run below shown dynamic query
SET #SQL ='
select Distinct count(*) TotalCount,
sum(case when ap.FormStatus = ''open'' then 1 else 0 end) AS Totalopenstatus,
sum(case when ap.FormStatus = ''Pending'' then 1 else 0 end) AS TotalPendingstatus,
sum(case when ap.FormStatus = ''Awaiting L1 Review'' then 1 else 0 end) AS TotalAssociates,
sum(case when ap.FormStatus = ''Awaiting L2 Review'' then 1 else 0 end) AS TotalL1reviews,
sum(case when ap.FormStatus = ''Awaiting Delivery Head'' then 1 else 0 end) AS TotalL2reviews
from [dbo].[Appraisals] ap
LEFT join [dbo].[FinalReviewClosures] fi ON ap.FinalReviewClosure_Id = fi.id
join [dbo].[employees] emp ON fi.Employee_Id = emp.id
where fi.FinancialYear ='+#FinancialYear +' AND ap.Quarter = '+#Quarter+' '
but I get this error:
Msg 245, Level 16, State 1, Procedure sp_GetReviewStatus, Line 51 [Batch Start Line 11]
Conversion failed when converting the nvarchar value
Code:
select Distinct count(*) TotalCount,
sum(case when ap.FormStatus = 'open' then 1 else 0 end) AS Totalopenstatus,
sum(case when ap.FormStatus = 'Pending' then 1 else 0 end) AS TotalPendingstatus,
sum(case when ap.FormStatus = 'Awaiting L1 Review' then 1 else 0 end) AS TotalAssociates,
sum(case when ap.FormStatus = 'Awaiting L2 Review' then 1 else 0 end) AS TotalL1reviews,
sum(case when ap.FormStatus = 'Awaiting Delivery Head' then 1 else 0 end) AS TotalL2reviews
from [dbo].[Appraisals] ap
LEFT join [dbo].[FinalReviewClosures] fi ON ap.FinalReviewClosure_Id = fi.id
join [dbo].[employees] emp ON fi.Employee_Id = emp.id
where fi.FinancialYear =2020-2021 AND ap.Quarter = Q3 AND emp.office = ' to data type int.
Please help me fix this problem.
The reason what you have is failing is as has been mentioned. You are injecting values instead of parametrising and these values are creating invalid syntax in your "dynamic" query. (Read on to see why I say "dynamic".)
fi.FinancialYear ='+#FinancialYear +' becomes fi.FinancialYear =2020-2021, which is effectively fi.FinancialYear = -1 (what year is -1?). This will cause an error if fi.FinancialYear isn't able to be converted implicitly to an int.
ap.Quarter = '+#Quarter becomes ap.Quarter = Q3 and unless you have a column called Q3 then you're going to get an invalid column error.
If you are using dynamic SQL then never inject unsanitised values; quote your object names using QUOTENAME and parametrise your parameters (which you can do with sys.sp_executesql).
What you have, however, has no need to be a "dynamic" query; there is nothing dynamic about it. Just use a non-dynamic parametrised query and the query will work fine:
SELECT --DISTINCT --Not needed, as the query will only return 1 row.
--Even if you did have a GROUP BY the DISTINCT isn't needed,
--as the GROUP BY will already put the data in DISTINCT sets.
COUNT(*) AS TotalCount,
SUM(CASE WHEN ap.FormStatus = 'open' THEN 1 ELSE 0 END) AS Totalopenstatus,
SUM(CASE WHEN ap.FormStatus = 'Pending' THEN 1 ELSE 0 END) AS TotalPendingstatus,
SUM(CASE WHEN ap.FormStatus = 'Awaiting L1 Review' THEN 1 ELSE 0 END) AS TotalAssociates,
SUM(CASE WHEN ap.FormStatus = 'Awaiting L2 Review' THEN 1 ELSE 0 END) AS TotalL1reviews,
SUM(CASE WHEN ap.FormStatus = 'Awaiting Delivery Head' THEN 1 ELSE 0 END) AS TotalL2reviews
FROM [dbo].[Appraisals] ap
LEFT JOIN [dbo].[FinalReviewClosures] fi ON ap.FinalReviewClosure_Id = fi.id
JOIN [dbo].[employees] emp ON fi.Employee_Id = emp.id
WHERE fi.FinancialYear = #FinancialYear --I assume fi.FinancialYear and #FinancialYear have the same data type?
AND ap.Quarter = #Quarter; --I assume ap.Quarter and #Quarter have the same data type?
Where clause should be
where fi.FinancialYear ='2020-2021' AND ap.Quarter = 'Q3' AND emp.office = ' to data type int.'
instead of
where fi.FinancialYear =2020-2021 AND ap.Quarter = Q3 AND emp.office = ' to data type int.
Please also let me know the data type of #financialYear and #Quarter
I Have Query where I need to compare a amount field which has grouped by debit credit, I want to get the out put where the amount of credit is not equal to amount of debit the query is
select t_vocno,
sum(t_amt),
dc_type
from accotran
where f_yr = '1718'
and comp_cd = 'skl'
group by t_vocno,
dc_type
order by t_vocno
which gives output
1 215452.1600 D
1 215452.1600 C
2 207586.0000 D
2 207586.0000 C
3 248789.0000 D
3 248789.0000 C
I have very bid data so I want to put a having condition and get the data where debit <> credit
I have tried
select t_vocno,
sum(t_amt),
dc_type
from accotran
where f_yr = '1718'
and comp_cd = 'skl'
group by t_vocno,
dc_type
having case when dc_type= 'c' and t_vocno = t_vocno then sum(t_amt) end <>
case when dc_type= 'd' and t_vocno = t_vocno then sum(t_amt) end
order by t_vocno
You can GROUP BY just t_vocno and use conditional aggregation to calculate credit / debit sums:
select t_vocno,
sum(case when dc_type= 'c' then t_amt else 0 end) as c_sum,
sum(case when dc_type= 'd' then t_amt else 0 end) as d_sum
from accotran
where f_yr = '1718'
and comp_cd = 'skl'
group by t_vocno
having sum(case when dc_type= 'c' then t_amt else 0 end) <>
sum(case when dc_type= 'd' then t_amt else 0 end)
order by t_vocno
The below SQL query creates a table with n number of columns named in the next line.
...., curr_amount, tax_amount, ....
I am having a very tough time updating the below query to create a new column called total and position it exactly after tax_amount column and the total column should contain the values that are obtained after sum of curr_amount & tax_amount.
I have been working on this from more than one day but couldn't figure it out.
P.S. Still a noob here. Thanks alot for your time.
.
SELECT Isnull(t.total_month, 'Total') total_month,
t.tax_amount,
t.curr_amount,
t.usage_qty,
t.kh_qty,
t.bill_cnt
FROM (SELECT dbo.Sigmadf(bm.posted_date, 'YYYY-MM') total_month,
Sum(CASE
WHEN rr.usage_qty IS NULL THEN 0
ELSE Cast (rr.usage_qty AS NUMERIC(18, 2))
END) usage_qty,
Sum(CASE
WHEN bm.curr_amount IS NULL THEN 0
ELSE bm.curr_amount
END) curr_amount,
Sum(CASE
WHEN bm.adj_amount IS NULL THEN 0
ELSE bm.adj_amount
END) adj_amount,
Sum(CASE
WHEN bm.bal_fwd_amount IS NULL THEN 0
ELSE bm.bal_fwd_amount
END) bal_forward,
Sum(CASE
WHEN bm.tax_amount IS NULL THEN 0
ELSE bm.tax_amount
END) tax_amount,
Sum(CASE
WHEN bm.due_amount IS NULL THEN 0
ELSE bm.due_amount
END) due_amount,
Sum(CASE
WHEN bm.last_total_paid_amount IS NULL THEN 0
ELSE bm.last_total_paid_amount * -1
END) paid_amount,
Sum(CASE
WHEN bm.bill_print = 'Y' THEN 1
ELSE 0
END) pdf_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '0' THEN 1
ELSE 0
END) reg_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '1' THEN 1
ELSE 0
END) ftime_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '9999' THEN 1
ELSE 0
END) ltime_cnt,
Count(*) bill_cnt,
Sum(CASE
WHEN bill_status = '01' THEN 1
ELSE 0
END) canc_cnt,
Sum(CASE
WHEN bill_status = '01' THEN
CASE
WHEN rr.usage_qty IS NULL THEN 0
ELSE Cast (rr.usage_qty AS NUMERIC(18, 2))
END
ELSE 0
END) canc_usg,
Sum(CASE
WHEN vis.kh_qty IS NULL THEN 0
ELSE Cast(vis.kh_qty AS NUMERIC(18, 2))
END) kh_qty
FROM bill_master bm WITH (nolock)
INNER JOIN (SELECT bill_no,
Sum(CASE
WHEN vpb.recurr_charge_type IN ( 'T4',
'SLF' )
THEN
CASE
WHEN vpb.print_qty = 'Y'
AND vpb.usage_qty IS NOT NULL
THEN
Cast (vpb.usage_qty AS
NUMERIC(18, 2))
ELSE 0
END
ELSE 0
END) usage_qty
FROM v_print_bills_all vpb
GROUP BY bill_no) rr
ON rr.bill_no = bm.bill_no
LEFT OUTER JOIN vis_bill_master_cr vis WITH (nolock)
ON bm.bill_no = vis.bill_no
WHERE 1 = 1
AND dbo.Trunc(bm.posted_date) >= '20150101'
AND dbo.Trunc(bm.posted_date) <= '20151124'
AND bm.posted_date IS NOT NULL
AND bm.cust_id NOT IN (SELECT cc.code_type cust_id
FROM code_table cc WITH (nolock)
WHERE cc.code_tabname = 'RptExclCust'
AND cc.code_value = 'cust_id')
GROUP BY dbo.Sigmadf(bm.posted_date, 'YYYY-MM') WITH rollup)t
I must say that the explanation is not so clear.
From my understanding, you want the total of two columns.
So, wrap all your query between parenthesis, call it subQuery, and make the sum of the two columns on top:
SELECT subQuery.total_month as bill_date,
subQuery.curr_amount as amount,
subQuery.tax_amount tax,
subQuery.curr_amount + subQuery.tax_amount as [total],
...
FROM
(..your entire query here..) as subQuery
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
I am getting a divide by zero error in my script.
Can anyone please help.
I am trying to divide two records and one of them has zero in it. I dont want to lose the row, please advise.
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) as "Paid in 24hrs",
COUNT([MONTH_OF_SUSPENSION])/sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) as "Achieved"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
You can introduce a second case to check the result of the sum:
case
when sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) > 0
then COUNT([MONTH_OF_SUSPENSION])/sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end)
else 0 /* a default value that makes sense to you */
end as "Achieved"
Looking at your code I could assume you are using MSSQL and therefore you could use nullif which returns null if two arguments are equal. So for example your code could look like :
COUNT([MONTH_OF_SUSPENSION])/nullif(sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end),0) as "Achieved"
What it does is if the value of the sum operator is equal 0 then the divisor is turn from zero into null and that will result in the entire equation to become null.
use another case statement to check the result of your sum
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "Paid in 24hrs",
case
when sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) = 0 then 'whatever you want in this case'
else COUNT([MONTH_OF_SUSPENSION])/sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "Achieved"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
although this is pretty nasty looking, so you could tidy it up a bit with a sub-select:
select
year,
month,
request,
PaidIn24hrs,
case
when PaidIn24hrs = 0 then 'whatever you want in this case'
else request/PaidIn24hrs
end as "Achieved"
from
(
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "PaidIn24hrs"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
)