Sum inside cast function in Mysql gives error - mysql

SELECT CASE WHEN SUM(Amount) > 0 THEN CONCAT(CAST((SUM(Amount) AS VARCHAR(20) ) , ' Cr')
WHEN SUM(Amount) < 0 THEN CONCAT(CAST(ABS(SUM(Amount)) AS VARCHAR(20) ) , ' Dr')
ELSE '0.00'
END
FROM amount_transactions
WHERE Account_ID = 1 ;
It gives me an error.INVALID syntax.

You could minimize your query as
SELECT CASE WHEN SUM(Amount) > 0
THEN CONCAT(SUM(Amount) , ' Cr')
WHEN SUM(Amount) < 0
THEN CONCAT(ABS(SUM(Amount)), ' Dr')
ELSE '0.00'
END
FROM amount_transactions
WHERE Account_ID = 1 ;
If you really need to CAST your value you can use CHAR instead of varchar

Related

Explain me ways to understand this complex query

Can anyone help me to understand this big sql query. How do I break it down in small chunks to understand it ?
select t.Instrument as Instrument ,ClearingId as ClearingId,
ISNULL( PrevQty ,0) AS PrevQty,SettlePrice,
ISNULL(TodayBuyQty,0) as TodayBuyQty,
ISNULL( TodaySellQty ,0) AS TodaySellQty,
ISNULL(PrevQty +TodayBuyQty-TodaySellQty,0) as NetQty,
TodayBuyPrice, TodaySellPrice,LTP,PnL,Token
from
(
select Instrument,w.ClearingId as ClearingId,
ISNULL( PrevQty ,0) AS PrevQty,ISNULL(TodayBuyQty,0) as TodayBuyQty,
ISNULL( TodaySellQty ,0) AS TodaySellQty,
TodayAvgBuyPrice as TodayBuyPrice,TodayAvgSellPrice as TodaySellPrice,
LTP,PnL,w.Token
from
(
select Symbol as Instrument, ClearingId,
ISNULL(TodayBuyQty,0) as TodayBuyQty,
TodayAvgBuyPrice,
ISNULL( -TodaySellQty ,0) AS TodaySellQty,
TodayAvgSellPrice, NULL as LTP ,NULL as PnL ,
w.Token as Token
from
(
select Token, sum(Qty) as NetPositionQty, ClearingId,
sum(CASE WHEN Qty < 0 THEN Qty ELSE 0 END) as TodaySellQty,
sum(CASE WHEN Qty > 0 THEN Qty ELSE 0 END) as TodayBuyQty,
sum(CASE WHEN Qty < 0 THEN Qty * Price ELSE 0 END)
/
NULLIF(sum(CASE WHEN Qty < 0 THEN Qty ELSE 0 END), 0) as TodayAvgSellPrice,
sum(CASE WHEN Qty > 0 THEN Qty * Price ELSE 0 END)
/
NULLIF(sum(CASE WHEN Qty > 0 THEN Qty ELSE 0 END), 0) as TodayAvgBuyPrice
from
(
select m.Token,
(CASE WHEN ClearingId = 'SATP' THEN 'STRAITS' ELSE CASE WHEN ClearingId = 'BATP' THEN 'BPI' ELSE 'UOB' END END ) as ClearingId ,
Price/CAST(Multiplier AS float ) as Price,Qty
from
(
select Token , Exchange as ClearingId ,
LastTradePrice as Price ,
CASE WHEN Side = 'S' THEN -LastTradeQuantity ELSE LastTradeQuantity END as Qty
from Strategy_Orders
where ExchangeStatus in (9,10) )m
JOIN TokenMap t ON ( m.Token = t.Token)
UNION ALL
select m.Token, (CASE WHEN ClearingId = 'SATP' THEN 'STRAITS' ELSE CASE WHEN ClearingId = 'BATP' THEN 'BPI' ELSE 'UOB' END END ) as ClearingId ,
Price/CAST(Multiplier AS float ) as Price,
Qty
from
(
select Token , Exchange as ClearingId ,
LastTradePrice as Price ,
CASE WHEN Side = 'S' THEN -LastTradeQuantity ELSE LastTradeQuantity END as Qty
from Manual_Orders
where ExchangeStatus in (9,10) )m
JOIN TokenMap t ON ( m.Token = t.Token)
UNION ALL
select Token , ClearingId , TodayBuyPrice ,
TodayBuyQty as Qty
from EOD_Holdings
where CurrentDate =
( select top 1 CurrentDAte from EOD_Holdings
order by CurrentDAte desc
)
UNION ALL
select Token , ClearingId , TodaySellPrice ,
TodaySellQty as Qty
from EOD_Holdings
where CurrentDate = (
select top 1 CurrentDAte from EOD_Holdings
order by CurrentDAte desc
)
) x
group by Token,ClearingId) w
JOIN(select Token, Symbol from TokenMAp ) h on w.Token = h.Token
) w
FULL OUTER JOIN(
select Token, PrevQty , ClearingId
from EOD_Holdings
where CurrentDate = ( select top 1 CurrentDAte from EOD_Holdings order by CurrentDAte desc
)
) h
on w.Token = h.Token and w.ClearingId = h.ClearingId
)t
JOIN (
select * from LatestSettlePrices
) sp
on t.Instrument = sp.Instrument
You can break the query into chunks by looking at each subquery ("select ..." ) separately and running them to see the results. You need to start with the innermost queries that do not have other select statements in the "from" or "where" clause.
Also note that this query does not seem to be a clear, neither an optimal solution.
You would want to avoid using full outer joins and union all statements for the best performance.

SQL Server Row totals in pivot query

I am trying to make a row in the end of the result set that shows the totals.
My query is this:
SELECT
[ ] = ISNULL(CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente, 'TOTAL'),
[2016-11-01] = MAX([2016-11-01]),
[2016-11-02] = MAX([2016-11-02]),
[2016-11-03] = MAX([2016-11-03]),
[2016-11-04] = MAX([2016-11-04]),
TOTAL = COUNT([2016-11-01]) + COUNT([2016-11-02]) + COUNT([2016-11-03]) + COUNT([2016-11-04])
FROM
(
SELECT GEN_Paciente.GEN_idPaciente,COALESCE(GEN_ape_paternoPaciente, '')+' '+COALESCE(GEN_ape_maternoPaciente, '')+' '+COALESCE(GEN_nombrePaciente, '') AS nombrePaciente,HOS_fechaCategorizacion,HOS_nivel_riesgoCategorizacion+CAST(HOS_nivel_dependenciaCategorizacion AS VARCHAR) as riesgoDependencia
FROM HOS_Categorizacion
INNER JOIN HOS_Hospitalizacion
ON HOS_Hospitalizacion.HOS_idHospitalizacion = HOS_Categorizacion.HOS_idHospitalizacion
INNER JOIN GEN_Paciente
ON GEN_Paciente.GEN_idPaciente = HOS_Hospitalizacion.GEN_idPaciente
WHERE HOS_nivel_riesgoCategorizacion IS NOT NULL
) src
PIVOT
(
MAX(riesgoDependencia)
for HOS_fechaCategorizacion in ([2016-11-01],[2016-11-02],[2016-11-03],[2016-11-04])
) p
GROUP BY
ROLLUP(CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente)
This gives me this result:
But as you can see the totals for the rows are right but the totals for the columns are wrong because I am using MAX instead of COUNT, but I only need COUNT in the TOTAL row, the others have to be MAX, so I wrote this query:
SELECT
[ ] = ISNULL(CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente, 'TOTAL'),
[2016-11-01] = CASE WHEN CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente IS NOT NULL THEN MAX([2016-11-01]) ELSE COUNT([2016-11-01]) END,
[2016-11-02] = CASE WHEN CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente IS NOT NULL THEN MAX([2016-11-02]) ELSE COUNT([2016-11-02]) END,
[2016-11-03] = CASE WHEN CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente IS NOT NULL THEN MAX([2016-11-03]) ELSE COUNT([2016-11-03]) END,
[2016-11-04] = CASE WHEN CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente IS NOT NULL THEN MAX([2016-11-04]) ELSE COUNT([2016-11-04]) END,
TOTAL = COUNT([2016-11-01]) + COUNT([2016-11-02]) + COUNT([2016-11-03]) + COUNT([2016-11-04])
FROM
(
SELECT GEN_Paciente.GEN_idPaciente,COALESCE(GEN_ape_paternoPaciente, '')+' '+COALESCE(GEN_ape_maternoPaciente, '')+' '+COALESCE(GEN_nombrePaciente, '') AS nombrePaciente,HOS_fechaCategorizacion,HOS_nivel_riesgoCategorizacion+CAST(HOS_nivel_dependenciaCategorizacion AS VARCHAR) as riesgoDependencia
FROM HOS_Categorizacion
INNER JOIN HOS_Hospitalizacion
ON HOS_Hospitalizacion.HOS_idHospitalizacion = HOS_Categorizacion.HOS_idHospitalizacion
INNER JOIN GEN_Paciente
ON GEN_Paciente.GEN_idPaciente = HOS_Hospitalizacion.GEN_idPaciente
WHERE HOS_nivel_riesgoCategorizacion IS NOT NULL
) src
PIVOT
(
MAX(riesgoDependencia)
for HOS_fechaCategorizacion in ([2016-11-01],[2016-11-02],[2016-11-03],[2016-11-04])
) p
GROUP BY
ROLLUP(CAST(GEN_idPaciente AS VARCHAR)+'-'+nombrePaciente)
But that is not working
Thanks for your help!!
If I understand this correctly you want to count all columns which are not null. In this case you should just look at the condition IS NULL and not at the actual value at all. Try this:
DECLARE #tbl TABLE(ID INT IDENTITY, val1 VARCHAR(100),val2 VARCHAR(100),val3 VARCHAR(100));
INSERT INTO #tbl VALUES
('row1_val1','row1_val2',NULL)
,('row2_val1','row2_val2','row2_val3')
,(NULL,'row2_val2',NULL)
,(NULL,NULL,'row2_val3')
,(NULL,NULL,NULL);
SELECT *
,CASE WHEN val1 IS NULL THEN 0 ELSE 1 END
+CASE WHEN val2 IS NULL THEN 0 ELSE 1 END
+CASE WHEN val3 IS NULL THEN 0 ELSE 1 END AS CountOfValNotNull
FROM #tbl
UPDATE: Add a final Totals Row
You'd need ugly fiddling with a CTE, an additional sort column, UNION ALL to add another row and a sub_select.
Use the outer-most ORDER BY to get the artificial Totals-Row to the end
hint: Use the #tbl variable from above!
WITH SortedRows AS
(
SELECT ROW_NUMBER() OVER(ORDER BY ID) AS SortColumn
,*
,CASE WHEN val1 IS NULL THEN 0 ELSE 1 END
+CASE WHEN val2 IS NULL THEN 0 ELSE 1 END
+CASE WHEN val3 IS NULL THEN 0 ELSE 1 END AS CountOfValNotNull
FROM #tbl
)
SELECT tbl1.*
FROM
(
SELECT * FROM SortedRows
UNION ALL
SELECT 999999,0,'','','',(SELECT SUM(CountOfValNotNull) FROM SortedRows)
) AS tbl1
ORDER BY tbl1.SortColumn

return a SUM zero(0) instead of null

SELECT SUM( IF( userId = '123456', amount, 0 ) ) AS 'amount'
FROM `amountInfo`
userId 123456 is not present in table amountInfo, in that case it is returning null i want 0(numerical)
Use IFNULL:
SELECT IFNULL(SUM(IF(userId = '123456', amount, 0)), 0) AS amount
You can use coalesce for this
UPDATE : I was missing the argument to set 0 instead of NULL, pointed by #Barmar
coalesce( SUM( IF( userId = '123456', amount, 0 ) ),0 )
Select COALESCE(sum(amount),0) as amount from amountInfo where userId='123456'
You can read on http://www.w3schools.com/sql/sql_isnull.asp

Migrating a PostgreSQL stored proc to MYSQL

I have the following PostgreSQL which I want to re-write to make it compatible for MySQL stored function.
CREATE OR REPLACE FUNCTION test.yearly_demand_function(IN paramstartdate date,
OUT network character varying,
OUT season_number integer,
OUT week_trans numeric,
OUT month_trans numeric,
OUT year_trans numeric )
RETURNS SETOF record AS
$BODY$
BEGIN
RETURN QUERY
(select qq.network::varchar,
qq.season_number,
qq.week_trans::numeric,
qq.month_trans::numeric,
qq.year_trans::numeric
from
(
SELECT coalesce(nullif(mpf.studio,''),fi.name) AS network,
coalesce(mpf.season_number, mpf.reported_season_number) AS season_number ,
sum(case when activity_date >= date_trunc('week', paramStartDate::timestamp) - interval '7 day' and activity_date <= paramStartDate then mpf.units_sold else 0 END) as week_trans,
sum(case when activity_date >= date_trunc('month', paramStartDate::timestamp) and activity_date <= paramStartDate then mpf.units_sold else 0 END) as month_trans,
sum(case when activity_date >= date_trunc('year', paramStartDate::timestamp) and activity_date <= paramStartDate then mpf.units_sold else 0 END) as year_trans
FROM customer.dim_product_view mpf
left join customer.feed_indicator fi on mpf.series_name = fi.indicator_value
and mpf.series_name = fi.indicator_value
left join
(
select p.series_name,p.season_number,count(*) as episode_count
from product p
where source in ('Amway','FifthThird')
and p.episode_number is not null
group by 1,2) as pec on mpf.series_name = pec.series_name
and mpf.season_number = pec.season_number
WHERE mpf.activity_date BETWEEN date_trunc('year', paramStartDate::timestamp) AND paramStartDate
AND 1=1
AND ( mpf.demographic is null or '' not in ( '' ) or ('' in ( '' ) and mpf.demographic = 'Persons') )
AND mpf.customer_product_id not ilike '%unallocated%'
GROUP BY 1,2,3,7,34,35,36
)qq
);
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
ALTER FUNCTION yearly_demand_function(date);
Now Im not sure how to write the RETURNS SETOF record AS from above PostgreSQL (RETURN QUERY part) for MYSQL
Although I have started writing the stored proc for MY SQL as below
-- DROP FUNCTION IF EXISTS looptest;
DELIMITER $$
CREATE FUNCTION test.yearly_demand_function( IN paramstartdate date,
OUT network varchar(256),
OUT season_number integer,
OUT week_value numeric,
OUT month_value numeric,
OUT year_value numeric
) RETURNS <What to Write >
LANGUAGE SQL
BEGIN
RETURN QUERY
(select qq.network::varchar,
qq.season_number,
qq.week_value::numeric,
qq.month_value::numeric,
qq.year_value::numeric
from
(
SELECT coalesce(nullif(mm.studio,''),fi.name) AS network,
coalesce(mm.season_number, mm.reported_season_number) AS season_number ,
sum(case when final_date >= date_trunc('week', paramStartDate::timestamp) - interval '7 day' and final_date <= paramStartDate then mm.units_sold else 0 END) as week_value,
sum(case when final_date >= date_trunc('month', paramStartDate::timestamp) and final_date <= paramStartDate then mm.units_sold else 0 END) as month_value,
sum(case when final_date >= date_trunc('year', paramStartDate::timestamp) and final_date <= paramStartDate then mm.units_sold else 0 END) as year_value
FROM customer.product_view mm
left join customer.f_indicator fi on mm.series_name = fi.indicator_value
and mm.series_name = fi.indicator_value
left join
(
select p.name,p.s_number,count(*) as episode_count
from product p
where source in ('Amway','FifthThird')
and p.e_number is not null
group by 1,2) as tt on mm.series_name = tt.series_name
and mm.season_number = tt.season_number
WHERE mm.final_date BETWEEN date_trunc('year', paramStartDate::timestamp) AND paramStartDate
AND 1=1
AND ( mm.demographic is null or '' not in ( '' ) or ('' in ( '' ) and mm.demographic = 'Persons') )
AND mm.customer_product_id not ilike '%unallocated%'
GROUP BY 1,2,3,7,34,35,36
)qq
);
END;
$$
DELIMITER
how to write the RETURNS SETOF record AS from above PostgreSQL (RETURN QUERY part) for MYSQL
Instead of CREATE FUNCTION use the CREATE PROCEDURE syntax. You can then write a normal SELECT statement inside that block. To execute the stored procedure you created use the CALL syntax (i.e. CALL test.yearly_demand_function('2013-01-01')). You also don't need to specify your 5 OUT parameters. The value of the 5 OUT parameters will just correspond to the 5 columns that you will specify in your SELECT statement.

Counting Distinct values from Sql Table

Following is a view of my table...
I am getting a hard time in getting desired result from a query.
My requirement looks like below image
It should be conditioned as follows --
1)Within start date and end date.
2)No as Pending .
3)Yes as Completed.
4)No+Yes as Total.
5)Based On only one survey Type.
This is what i have tried and got result for above mentioned 1 to 4 condition , but how to impliment 5 condition ?
SELECT DISTINCT Userid
,CASE
WHEN [YES] IS NULL
THEN 0
ELSE [YES]
END AS Completed
,CASE
WHEN [NO] IS NULL
THEN 0
ELSE [NO]
END AS Pending
,(
CASE
WHEN [YES] IS NULL
THEN 0
ELSE [YES]
END + CASE
WHEN [NO] IS NULL
THEN 0
ELSE [NO]
END
) AS Total
FROM (SELECT DISTINCT Userid
,SurveyStatus
,COUNT(ParcelId) AS cnt
FROM ParcelAllocationsurvivor
WHERE DateAllocated >= '2013-08-01'
AND DateAllocated <= '2013-08-07'
GROUP BY Userid
,SurveyStatus
) AS p
PIVOT(max(cnt) FOR surveystatus IN ([YES],[NO])) AS pvt
ORDER BY Userid
Can anybody help me out in it.
thanks in advance////
You would add a where clause into the subquery:
SELECT Userid, (case when [YES] is null then 0 else [YES] end) as Completed,
(case when [NO] is null then 0 else [NO] end) as Pending,
(case when [YES] is null then 0 else [YES] end +
case when [NO] is null then 0 else [NO] end
) as Total
FROM (SELECT Userid, SurveyStatus, COUNT(ParcelId) as cnt
FROM ParcelAllocationsurvivor
WHERE DateAllocated >= '2013-08-01' and DateAllocated <='2013-08-07' and
SurveyType = 'Survey1'
group by Userid, SurveyStatus
) AS p
PIVOT(max(cnt) FOR surveystatus IN([YES],[NO])) AS pvt order by Userid;
By the way, the select distinct are redundant. You do not need them for this query.