How to hide a column in output result in Mysql? - mysql

select historic_betfair_win_prices.sf_name,historic_runners.finish_position,historic_runners.official_rating,historic_betfair_win_prices.date, historic_betfair_win_prices.bsp,
if(finish_position = 1 , #b:=#b+1,#b:=#b) opening_position,
if(finish_position = 1 , #last_op:=official_rating,#last_op:=#last_op)last_opening_position,
cast(if(#b = 0 or finish_position = 1,0,#last_op - official_rating) as decimal(10,2)) diff
from historic_runners
inner join historic_betfair_win_prices on historic_betfair_win_prices.runner_id = historic_runners.runner_id and historic_betfair_win_prices.sf_race_id = historic_runners.race_id
cross join (select #b:=0, #last_op :=0) b
where historic_betfair_win_prices.sf_name = "Camanche Grey"
limit 50
I want to hide
opening_position, last_opening_position
columns from the output result. My Mysql version 8.0.18

Use a subquery
select sf_name,finish_position,official_rating,date,bsp,diff from
(
select historic_betfair_win_prices.sf_name,historic_runners.finish_position,historic_runners.official_rating,historic_betfair_win_prices.date, historic_betfair_win_prices.bsp,
if(finish_position = 1 , #b:=#b+1,#b:=#b) opening_position,
if(finish_position = 1 , #last_op:=official_rating,#last_op:=#last_op)last_opening_position,
cast(if(#b = 0 or finish_position = 1,0,#last_op - official_rating) as decimal(10,2)) diff
from historic_runners
inner join historic_betfair_win_prices on historic_betfair_win_prices.runner_id = historic_runners.runner_id and historic_betfair_win_prices.sf_race_id = historic_runners.race_id
cross join (select #b:=0, #last_op :=0) b
where historic_betfair_win_prices.sf_name = "Camanche Grey"
limit 50
)A

select historic_betfair_win_prices.sf_name,
historic_runners.finish_position,
historic_runners.official_rating,
historic_betfair_win_prices.date,
historic_betfair_win_prices.bsp,
cast(if((#b := if(finish_position = 1 ,
#b+1,
#b))= 0 or finish_position = 1,
0,
(#last_op := if(finish_position = 1 ,
official_rating,
#last_op)) - official_rating) as decimal(10,2)) difffrom historic_runners
inner join historic_betfair_win_prices on historic_betfair_win_prices.runner_id = historic_runners.runner_id and historic_betfair_win_prices.sf_race_id = historic_runners.race_id
cross join (select #b:=0, #last_op :=0) b
where historic_betfair_win_prices.sf_name = "Camanche Grey"
limit 50

Related

how to display the value fields in one line but separate

SELECT bio.nama_lengkap AS nama,bio.jenis_pegawai,bio.progdi,bio.jabatan as jabatan1,bio.cabang,bio.tahun,
GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END) AS nilai,
round (avg(rata_rata),2) as rata
FROM tb_biodata as bio
INNER JOIN tb_penilaian ON tb_biodata.id_biodata = tb_penilaian.id_biodata
INNER JOIN tb_user ON tb_penilaian.id_user = tb_user.id_user
WHERE tb_biodata.jenis_pegawai = 'Tenaga Kependidikan' and tb_penilaian.tahun='2020' and tb_biodata.cabang = 'Majapahit 605' GROUP BY nama'
SELECT ... ,
SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END), ',', 1) AS nilai1,
SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END), ',', 2), ',', -1) AS nilai2,
SUBSTRING_INDEX(GROUP_CONCAT(CASE WHEN tb_penilaian.id_user = tb_user.id_user THEN rata_rata END), ',', -1) AS nilai3,
...
Alternatively:
SELECT bio.nama_lengkap AS nama,
bio.jenis_pegawai,
bio.progdi,
bio.jabatan as jabatan1,
bio.cabang,
bio.tahun,
CASE WHEN tb_penilaian.id_user = tb_user.id_user and numners.num = 1
THEN ANY_VALUE(rata_rata)
END AS nilai1,
CASE WHEN tb_penilaian.id_user = tb_user.id_user and numners.num = 2
THEN ANY_VALUE(rata_rata)
END AS nilai2,
CASE WHEN tb_penilaian.id_user = tb_user.id_user and numners.num = 3
THEN ANY_VALUE(rata_rata)
END AS nilai3,
round (avg(rata_rata),2) as rata
FROM tb_biodata as bio
INNER JOIN tb_penilaian ON tb_biodata.id_biodata = tb_penilaian.id_biodata
INNER JOIN tb_user ON tb_penilaian.id_user = tb_user.id_user
INNER JOIN (SELECT 1 num UNION SELECT 2 UNION SELECT 3) numbers
WHERE tb_biodata.jenis_pegawai = 'Tenaga Kependidikan'
and tb_penilaian.tahun='2020'
and tb_biodata.cabang = 'Majapahit 605'
GROUP BY nama'

MySQL SELECT query with another SELECT

I have the next sql query:
SELECT CONCAT(v.p_sery, v.p_id) AS sery,
(SELECT COUNT(1) FROM v where p_delivery_result = 1) AS delivery_count,
(SELECT COUNT(1) FROM v where p_delivery_result = 2) AS ND1,
(SELECT COUNT(1) FROM v where p_delivery_result = 3) AS ND2,
(SELECT COUNT(1) FROM v where p_delivery_result = 4) AS ND3,
(SELECT COUNT(1) FROM v where p_delivery_result = 5) AS ND4,
(SELECT COUNT(1) FROM v where p_delivery_result = 6) AS ND5,
(SELECT COUNT(1) FROM v where p_delivery_result = 7) AS ND6,
(SELECT COUNT(1) FROM v where p_delivery_result = 8) AS ND7
FROM (
SELECT p_sery, p_id, d.p_delivery_result
FROM registries AS a, registry_regulations r, delivery d
WHERE a.p_id = r.registry_id AND d.p_id = r.regulation_id AND (SELECT
STR_TO_DATE(a.p_date_created, '%Y-%m-%d') BETWEEN '2017-04-01' AND '2017-06-01')
) as v;
But this not working.
Error: Table v doesn't exist
What I do wrong?
I have this tables:
And I want get the count of one of the status in table delivery
You cannot access the derived table from the context of a correlated subquery. Try this query instead:
SELECT CONCAT(v.p_sery, v.p_id) AS sery,
COUNT(CASE WHEN p_delivery_result = 1 THEN 1 END) AS delivery_count,
COUNT(CASE WHEN p_delivery_result = 2 THEN 1 END) AS ND1,
COUNT(CASE WHEN p_delivery_result = 3 THEN 1 END) AS ND2,
COUNT(CASE WHEN p_delivery_result = 4 THEN 1 END) AS ND3,
COUNT(CASE WHEN p_delivery_result = 5 THEN 1 END) AS ND4,
COUNT(CASE WHEN p_delivery_result = 6 THEN 1 END) AS ND5,
COUNT(CASE WHEN p_delivery_result = 7 THEN 1 END) AS ND6,
COUNT(CASE WHEN p_delivery_result = 8 THEN 1 END) AS ND7
FROM (
SELECT p_sery, p_id, d.p_delivery_result
FROM registries AS a
JOIN registry_regulations r ON a.p_id = r.registry_id
JOIN delivery d d.p_id = r.regulation_id
WHERE STR_TO_DATE(a.p_date_created, '%Y-%m-%d')
BETWEEN '2017-04-01' AND '2017-06-01') as v;
Note: Always uses modern, explicit JOIN syntax instead of old-fashioned, implicit syntax.
just remove as before V
FROM (
SELECT p_sery, p_id, d.p_delivery_result
FROM registries AS a, registry_regulations r, delivery d
WHERE a.p_id = r.registry_id AND d.p_id = r.regulation_id AND (SELECT
STR_TO_DATE(a.p_date_created, '%Y-%m-%d') BETWEEN '2017-04-01' AND '2017-06-01')
) v

Any other way to perform joins on same column?

I was just rewriting my question as this answer wont work for me. I have a stored procedure as follows:
--GetAllFilteredProjects
Alter PROCEDURE [GetAllFilteredProjects]
#UserId Int,
#ServerGroupId int,
#ProjectIDs UDT_ProjectIDs Readonly,
#ProjectDesc NVARCHAR(2000),
#TopRecords INT,
#StartProjectID INT,
#CustomerGroupId INT,
#SearchStates dbo.[UDT_SearchStates] Readonly
AS
BEGIN
SET NOCOUNT ON;
DECLARE #StartProjectNumber INT
DECLARE #AcceptUnAcceptFilterNumberv INT
DECLARE #IgnoreRestrictedNumberv INT
DECLARE #SearchProjectIDsCount INT
DECLARE #SearchProjectDesc VARCHAR
SET #IgnoreRestrictedNumberv=0
SET #AcceptUnAcceptFilterNumberv =0
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('StartProjectIDFilter')
where USERID = #UserId
SET #ProjectDesc = REPLACE(#ProjectDesc, '*', '%')
SET #ProjectDesc = REPLACE(#ProjectDesc, '?', '_')
Print(#ProjectDesc)
Select #SearchProjectIDsCount=count(*) from #ProjectIDs;
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterUnAccept','ProjectFilterAccept')
where USERID = #UserId
IF(#AcceptUnAcceptFilterNumberv = 1)
BEGIN
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterAccept')
where USERID = #UserId
IF #AcceptUnAcceptFilterNumberv = 0
BEGIN
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterUnAccept')
where USERID = #UserId
IF(#AcceptUnAcceptFilterNumberv = 1) -- UnAccepted
BEGIN
SET #AcceptUnAcceptFilterNumberv = 3
END
END
END
select #IgnoreRestrictedNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterIgnoreRestricted')
where USERID = #UserId
IF OBJECT_ID('TEMPDB..#PROJECTS') IS NOT NULL DROP TABLE #PROJECTS
CREATE TABLE #PROJECTS
(
TMP_PROJECT_ID INT,
TMP_SERVERGROUP_ID INT,
TMP_DESCRIPTION NVARCHAR(1000),
TMP_PROJECT_STATE_ID INT,
TMP_ACCEPTED_STATE_ID INT,
TMP_ISRESTRICTED_ID INT
)
IF OBJECT_ID('TEMPDB..#SELECTED_STATES') IS NOT NULL DROP TABLE #SELECTED_STATES
CREATE TABLE #SELECTED_STATES
(
TMP_PREFSET_STATEID INT
)
-- All the project accepted for server group and unaccepted for server group which are promoted to
-- Validation or Pilot or Factory states
IF #IgnoreRestrictedNumberv=1
BEGIN
INSERT INTO #PROJECTS
SELECT p.projectid,servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted
FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=#ServerGroupId
WHERE P.CustomerGroupId = #CustomerGroupId AND P.StateId IN(Select StateID From #SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=#ServerGroupId)
--added by shankar, regarding the filtering functionality
--in projects tab of a server group
and (#SearchProjectIDsCount =0 Or p.projectid in
(1673))
AND (Upper(p.Description) LIKE '%' +Upper(#ProjectDesc) + '%')
AND p.projectid>=#StartProjectID
AND p.IsRestricted = 0
END
ELSE
BEGIN
INSERT INTO #PROJECTS
SELECT p.projectid,servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted
FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=#ServerGroupId
WHERE P.CustomerGroupId = #CustomerGroupId AND P.StateId IN(Select StateID From #SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=#ServerGroupId)
--added by shankar, regarding the filtering functionality
--in projects tab of a server group
and (#SearchProjectIDsCount =0 Or p.projectid in
(1673))
AND (Upper(p.Description) LIKE '%' +Upper(#ProjectDesc) + '%')
AND p.projectid>=#StartProjectID
END
-- State selected by user to filter the projects
INSERT INTO #SELECTED_STATES
select ap.AFPStateId
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
inner join AFPState ap on 'ProjectFilter' + ap.AFPStateDesc = PropertyName
where USERID = #UserId
IF #AcceptUnAcceptFilterNumberv = 1 -- Accepted
BEGIN
SELECT TOP(#TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,
(case
when TMP_PROJECT_STATE_ID = 1 then 'Development'
when TMP_PROJECT_STATE_ID = 2 then 'Validation'
when TMP_PROJECT_STATE_ID = 3 then 'Pilot'
when TMP_PROJECT_STATE_ID = 4 then 'Factory'
end) as State,
(case [1] when 2 then 1
else 0
end) as Validation,
(case
when [1] = 3 then 1
when [2] = 3 then 1
else 0
end) as Pilot,
(case
when [1] = 4 then 1
when [2] = 4 then 1
when [3] = 4 then 1
else 0
end) as Factory,PL.LockedBy,PA.CreatedBy,
(CASE
WHEN TMP_ISRESTRICTED_ID = 0 THEN 1
ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = #ServerGroupId )
END
)AS IsUnRestrictedServerGroup
FROM (
SELECT #PROJECTS.TMP_PROJECT_ID,
#PROJECTS.TMP_DESCRIPTION,
#PROJECTS.TMP_SERVERGROUP_ID,
#PROJECTS.TMP_PROJECT_STATE_ID,
#PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM, #PROJECTS.TMP_ISRESTRICTED_ID
FROM #PROJECTS
WHERE #PROJECTS.TMP_PROJECT_ID IN
(
SELECT DISTINCT #PROJECTS.TMP_PROJECT_ID
FROM #PROJECTS
GROUP BY #PROJECTS.TMP_PROJECT_ID HAVING MAX(#PROJECTS.TMP_ACCEPTED_STATE_ID) IN
--WHERE #PROJECTS.TMP_ACCEPTED_STATE_ID IN
(
SELECT #SELECTED_STATES.TMP_PREFSET_STATEID
FROM #SELECTED_STATES
)
)
) A
PIVOT
(
MAX(TMP_ACCEPTED_STATE_ID)
FOR ROWNUM IN ([1],[2],[3],[4])
)B
LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID
LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID
END
ELSE IF #AcceptUnAcceptFilterNumberv = 2 -- Both
BEGIN
SELECT TOP(#TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,
(case
when TMP_PROJECT_STATE_ID = 1 then 'Development'
when TMP_PROJECT_STATE_ID = 2 then 'Validation'
when TMP_PROJECT_STATE_ID = 3 then 'Pilot'
when TMP_PROJECT_STATE_ID = 4 then 'Factory'
end) as State,
(case
when [1]=2 then 1
else 0
end) as Validation,
(case
when [1] = 3 then 1
when [2] = 3 then 1
else 0
end) as Pilot,
(case
when [1] = 4 then 1
when [2] = 4 then 1
when [3] = 4 then 1
else 0
end) as Factory,PL.LockedBy,PA.CreatedBy,
(CASE
WHEN TMP_ISRESTRICTED_ID = 0 THEN 1
ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = #ServerGroupId )
END
)AS IsUnRestrictedServerGroup
FROM (
SELECT #PROJECTS.TMP_PROJECT_ID,
#PROJECTS.TMP_DESCRIPTION,
#PROJECTS.TMP_SERVERGROUP_ID,
#PROJECTS.TMP_PROJECT_STATE_ID,
#PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID
FROM #PROJECTS
WHERE #PROJECTS.TMP_PROJECT_ID in
(
SELECT DISTINCT PJ.TMP_PROJECT_ID
FROM #PROJECTS PJ
--WHERE isnull(PJ.TMP_ACCEPTED_STATE_ID,0) <=
GROUP BY PJ.TMP_PROJECT_ID HAVING MAX(isnull(PJ.TMP_ACCEPTED_STATE_ID,0)) <=
(
SELECT max(#SELECTED_STATES.TMP_PREFSET_STATEID)
FROM #SELECTED_STATES
)
)
) A
PIVOT
(
MAX(TMP_ACCEPTED_STATE_ID)
FOR ROWNUM IN ([1],[2],[3],[4])
)B
LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID
LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID
END
ELSE IF #AcceptUnAcceptFilterNumberv = 3 -- UnAccepted
BEGIN
SELECT TOP(#TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,
(case
when TMP_PROJECT_STATE_ID = 1 then 'Development'
when TMP_PROJECT_STATE_ID = 2 then 'Validation'
when TMP_PROJECT_STATE_ID = 3 then 'Pilot'
when TMP_PROJECT_STATE_ID = 4 then 'Factory'
end) as State,
(case [1] when 2 then 1
else 0
end) as Validation,
(case
when [1] = 3 then 1
when [2] = 3 then 1
else 0
end) as Pilot,
(case
when [1] = 4 then 1
when [2] = 4 then 1
when [3] = 4 then 1
else 0
end) as Factory,PL.LockedBy,PA.CreatedBy,
(CASE
WHEN TMP_ISRESTRICTED_ID = 0 THEN 1
ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = #ServerGroupId )
END
)AS IsUnRestrictedServerGroup
FROM (
SELECT #PROJECTS.TMP_PROJECT_ID,
#PROJECTS.TMP_DESCRIPTION,
#PROJECTS.TMP_SERVERGROUP_ID,
#PROJECTS.TMP_PROJECT_STATE_ID,
#PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID
FROM #PROJECTS
WHERE #PROJECTS.TMP_PROJECT_ID IN
(
SELECT DISTINCT p.TMP_PROJECT_ID
FROM #PROJECTS p
WHERE (SELECT max(ISNULL(PJ.TMP_ACCEPTED_STATE_ID,0))
FROM #PROJECTS PJ
GROUP BY PJ.TMP_PROJECT_ID HAVING PJ.TMP_PROJECT_ID = p.TMP_PROJECT_ID
) <
(
SELECT MAX(#SELECTED_STATES.TMP_PREFSET_STATEID)
FROM #SELECTED_STATES
)
)
) A
PIVOT
(
MAX(TMP_ACCEPTED_STATE_ID)
FOR ROWNUM IN ([1],[2],[3],[4])
)B
LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID
LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID
END
END
Now while inserting values into my table projects I have a condition where p.IsRestrictedID=0.
Here I have one more thing which I need to consider. I need to insert the projects which have IsRestricted=1 where this could be executed when this statement returns 1
SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = #ServerGroupId
Means I need to consider the project which has IsRestricted=1 when the above select query returns 1.
I wanted to add this to that Insert into project query.
How can I do that?
Just add the prefix of the table alias every place you use the column name, to prevent the ambiguous column error:
SELECT
p.projectid,
pa.servergroupid, -- Here I added "pa." as a prefix
p.description,
. . .
I got the solution for this I just need to add an OR condition while inserting Projects.
The modified one is as follows:
--GetAllFilteredProjects
Alter PROCEDURE [GetAllFilteredProjects]
#UserId Int,
#ServerGroupId int,
#ProjectIDs UDT_ProjectIDs Readonly,
#ProjectDesc NVARCHAR(2000),
#TopRecords INT,
#StartProjectID INT,
#CustomerGroupId INT,
#SearchStates dbo.[UDT_SearchStates] Readonly
AS
BEGIN
SET NOCOUNT ON;
DECLARE #StartProjectNumber INT
DECLARE #AcceptUnAcceptFilterNumberv INT
DECLARE #IgnoreRestrictedNumberv INT
DECLARE #SearchProjectIDsCount INT
DECLARE #SearchProjectDesc VARCHAR
SET #IgnoreRestrictedNumberv=0
SET #AcceptUnAcceptFilterNumberv =0
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('StartProjectIDFilter')
where USERID = #UserId
SET #ProjectDesc = REPLACE(#ProjectDesc, '*', '%')
SET #ProjectDesc = REPLACE(#ProjectDesc, '?', '_')
Print(#ProjectDesc)
Select #SearchProjectIDsCount=count(*) from #ProjectIDs;
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterUnAccept','ProjectFilterAccept')
where USERID = #UserId
IF(#AcceptUnAcceptFilterNumberv = 1)
BEGIN
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterAccept')
where USERID = #UserId
IF #AcceptUnAcceptFilterNumberv = 0
BEGIN
select #AcceptUnAcceptFilterNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterUnAccept')
where USERID = #UserId
IF(#AcceptUnAcceptFilterNumberv = 1) -- UnAccepted
BEGIN
SET #AcceptUnAcceptFilterNumberv = 3
END
END
END
select #IgnoreRestrictedNumberv = COUNT(PropertyName)
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
and PropertyName in('ProjectFilterIgnoreRestricted')
where USERID = #UserId
IF OBJECT_ID('TEMPDB..#PROJECTS') IS NOT NULL DROP TABLE #PROJECTS
CREATE TABLE #PROJECTS
(
TMP_PROJECT_ID INT,
TMP_SERVERGROUP_ID INT,
TMP_DESCRIPTION NVARCHAR(1000),
TMP_PROJECT_STATE_ID INT,
TMP_ACCEPTED_STATE_ID INT,
TMP_ISRESTRICTED_ID INT
)
IF OBJECT_ID('TEMPDB..#SELECTED_STATES') IS NOT NULL DROP TABLE #SELECTED_STATES
CREATE TABLE #SELECTED_STATES
(
TMP_PREFSET_STATEID INT
)
-- All the project accepted for server group and unaccepted for server group which are promoted to
-- Validation or Pilot or Factory states
IF #IgnoreRestrictedNumberv=1
BEGIN
INSERT INTO #PROJECTS
SELECT p.projectid, servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted
FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=#ServerGroupId
WHERE P.CustomerGroupId = #CustomerGroupId AND P.StateId IN(Select StateID From #SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=#ServerGroupId)
--added by shankar, regarding the filtering functionality
--in projects tab of a server group
and (#SearchProjectIDsCount =0 Or p.projectid in
(1673))
AND (Upper(p.Description) LIKE '%' +Upper(#ProjectDesc) + '%')
AND p.projectid>=#StartProjectID
AND p.IsRestricted = 0
OR p.IsRestricted IN (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = p.projectid AND PR.ServerGroupId = #ServerGroupId )
END
ELSE
BEGIN
INSERT INTO #PROJECTS
SELECT p.projectid,servergroupid,p.description,p.StateId AS ProjectState, pa.stateid ,p.IsRestricted
FROM v_Project p left outer join ProjectAcception pa ON p.ProjectId = pa.ProjectId and pa.ServerGroupId=#ServerGroupId
WHERE P.CustomerGroupId = #CustomerGroupId AND P.StateId IN(Select StateID From #SearchStates) and P.StateId in (select ss.AFPStateId from ServerGroupStateSettings ss where ss.servergroupid=#ServerGroupId)
--added by shankar, regarding the filtering functionality
--in projects tab of a server group
and (#SearchProjectIDsCount =0 Or p.projectid in
(1673))
AND (Upper(p.Description) LIKE '%' +Upper(#ProjectDesc) + '%')
AND p.projectid>=#StartProjectID
END
-- State selected by user to filter the projects
INSERT INTO #SELECTED_STATES
select ap.AFPStateId
from UserPreferenceSet u inner join PreferenceSet p on u.PreferenceID=p.ID
inner join AFPState ap on 'ProjectFilter' + ap.AFPStateDesc = PropertyName
where USERID = #UserId
IF #AcceptUnAcceptFilterNumberv = 1 -- Accepted
BEGIN
SELECT TOP(#TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,
(case
when TMP_PROJECT_STATE_ID = 1 then 'Development'
when TMP_PROJECT_STATE_ID = 2 then 'Validation'
when TMP_PROJECT_STATE_ID = 3 then 'Pilot'
when TMP_PROJECT_STATE_ID = 4 then 'Factory'
end) as State,
(case [1] when 2 then 1
else 0
end) as Validation,
(case
when [1] = 3 then 1
when [2] = 3 then 1
else 0
end) as Pilot,
(case
when [1] = 4 then 1
when [2] = 4 then 1
when [3] = 4 then 1
else 0
end) as Factory,PL.LockedBy,PA.CreatedBy,
(CASE
WHEN TMP_ISRESTRICTED_ID = 0 THEN 1
ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = #ServerGroupId )
END
)AS IsUnRestrictedServerGroup
FROM (
SELECT #PROJECTS.TMP_PROJECT_ID,
#PROJECTS.TMP_DESCRIPTION,
#PROJECTS.TMP_SERVERGROUP_ID,
#PROJECTS.TMP_PROJECT_STATE_ID,
#PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM, #PROJECTS.TMP_ISRESTRICTED_ID
FROM #PROJECTS
WHERE #PROJECTS.TMP_PROJECT_ID IN
(
SELECT DISTINCT #PROJECTS.TMP_PROJECT_ID
FROM #PROJECTS
GROUP BY #PROJECTS.TMP_PROJECT_ID HAVING MAX(#PROJECTS.TMP_ACCEPTED_STATE_ID) IN
--WHERE #PROJECTS.TMP_ACCEPTED_STATE_ID IN
(
SELECT #SELECTED_STATES.TMP_PREFSET_STATEID
FROM #SELECTED_STATES
)
)
) A
PIVOT
(
MAX(TMP_ACCEPTED_STATE_ID)
FOR ROWNUM IN ([1],[2],[3],[4])
)B
LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID
LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID
END
ELSE IF #AcceptUnAcceptFilterNumberv = 2 -- Both
BEGIN
SELECT TOP(#TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,
(case
when TMP_PROJECT_STATE_ID = 1 then 'Development'
when TMP_PROJECT_STATE_ID = 2 then 'Validation'
when TMP_PROJECT_STATE_ID = 3 then 'Pilot'
when TMP_PROJECT_STATE_ID = 4 then 'Factory'
end) as State,
(case
when [1]=2 then 1
else 0
end) as Validation,
(case
when [1] = 3 then 1
when [2] = 3 then 1
else 0
end) as Pilot,
(case
when [1] = 4 then 1
when [2] = 4 then 1
when [3] = 4 then 1
else 0
end) as Factory,PL.LockedBy,PA.CreatedBy,
(CASE
WHEN TMP_ISRESTRICTED_ID = 0 THEN 1
ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = #ServerGroupId )
END
)AS IsUnRestrictedServerGroup
FROM (
SELECT #PROJECTS.TMP_PROJECT_ID,
#PROJECTS.TMP_DESCRIPTION,
#PROJECTS.TMP_SERVERGROUP_ID,
#PROJECTS.TMP_PROJECT_STATE_ID,
#PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID
FROM #PROJECTS
WHERE #PROJECTS.TMP_PROJECT_ID in
(
SELECT DISTINCT PJ.TMP_PROJECT_ID
FROM #PROJECTS PJ
--WHERE isnull(PJ.TMP_ACCEPTED_STATE_ID,0) <=
GROUP BY PJ.TMP_PROJECT_ID HAVING MAX(isnull(PJ.TMP_ACCEPTED_STATE_ID,0)) <=
(
SELECT max(#SELECTED_STATES.TMP_PREFSET_STATEID)
FROM #SELECTED_STATES
)
)
) A
PIVOT
(
MAX(TMP_ACCEPTED_STATE_ID)
FOR ROWNUM IN ([1],[2],[3],[4])
)B
LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID
LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID
END
ELSE IF #AcceptUnAcceptFilterNumberv = 3 -- UnAccepted
BEGIN
SELECT TOP(#TopRecords) TMP_PROJECT_ID AS ProjectId,TMP_SERVERGROUP_ID as ServerGroupId,TMP_DESCRIPTION as Description,
(case
when TMP_PROJECT_STATE_ID = 1 then 'Development'
when TMP_PROJECT_STATE_ID = 2 then 'Validation'
when TMP_PROJECT_STATE_ID = 3 then 'Pilot'
when TMP_PROJECT_STATE_ID = 4 then 'Factory'
end) as State,
(case [1] when 2 then 1
else 0
end) as Validation,
(case
when [1] = 3 then 1
when [2] = 3 then 1
else 0
end) as Pilot,
(case
when [1] = 4 then 1
when [2] = 4 then 1
when [3] = 4 then 1
else 0
end) as Factory,PL.LockedBy,PA.CreatedBy,
(CASE
WHEN TMP_ISRESTRICTED_ID = 0 THEN 1
ELSE (SELECT COUNT(*) FROM dbo.ProjectRestriction PR WHERE PR.ProjectId = TMP_PROJECT_ID AND PR.ServerGroupId = #ServerGroupId )
END
)AS IsUnRestrictedServerGroup
FROM (
SELECT #PROJECTS.TMP_PROJECT_ID,
#PROJECTS.TMP_DESCRIPTION,
#PROJECTS.TMP_SERVERGROUP_ID,
#PROJECTS.TMP_PROJECT_STATE_ID,
#PROJECTS.TMP_ACCEPTED_STATE_ID , row_number() OVER(PARTITION BY #PROJECTS.TMP_PROJECT_ID, #PROJECTS.TMP_SERVERGROUP_ID ORDER BY #PROJECTS.TMP_ACCEPTED_STATE_ID ASC) as ROWNUM , #PROJECTS.TMP_ISRESTRICTED_ID
FROM #PROJECTS
WHERE #PROJECTS.TMP_PROJECT_ID IN
(
SELECT DISTINCT p.TMP_PROJECT_ID
FROM #PROJECTS p
WHERE (SELECT max(ISNULL(PJ.TMP_ACCEPTED_STATE_ID,0))
FROM #PROJECTS PJ
GROUP BY PJ.TMP_PROJECT_ID HAVING PJ.TMP_PROJECT_ID = p.TMP_PROJECT_ID
) <
(
SELECT MAX(#SELECTED_STATES.TMP_PREFSET_STATEID)
FROM #SELECTED_STATES
)
)
) A
PIVOT
(
MAX(TMP_ACCEPTED_STATE_ID)
FOR ROWNUM IN ([1],[2],[3],[4])
)B
LEFT JOIN dbo.ProjectLock PL ON PL.ProjectID=TMP_PROJECT_ID
LEFT JOIN dbo.ProjectAutomaticAcceptance PA ON PA.ProjectID=TMP_PROJECT_ID
END
END

how to calculate from each row value in sql

I have a table named general_ledger from which I need to show dr_amount, cr_amount and the balance between them as running_balance. That's why I have written a query that is given below. But I am getting the result of each query like the balance only of current row. But I need to produce the result with the remaining balance. Suppose First row dr_balance is 20000 and cr_balance is 5000 and remaining balance is 15000. In second row only cr_balance is 5000. Now the result should be 10000 with the deduction but my result is -5000. I have no idea how to fix this. Can anyone please help me on this? I need your help very much. Here is my query given below :
SELECT '' AS cost_center_id
, '' AS cost_center_name
, '' AS office_code
, CONVERT('2013-02-01',DATETIME) AS transaction_date
, '' AS accounts_head_id
, '' AS account_name
, '' AS opposite_accounts_head_id
, '' AS opposite_account_name
, 'Opening Balance' AS particulars
, tempOpeningBalance.dr_amount
, tempOpeningBalance.cr_amount
, '' AS voucher_no
, '' AS vin
FROM (SELECT IFNULL(mcoa.account_code,'1101010101100321') AS account_code
, IFNULL(mcoa.account_name,'Cash') AS account_name
, IFNULL(mcoa.account_type,'ASSET') AS accountType
, CAST(IFNULL(SUM(IFNULL(maingl.dr_balance,0)),0) AS DECIMAL(27,5)) AS dr_amount
, CAST(IFNULL(SUM(IFNULL(maingl.cr_balance,0)),0) AS DECIMAL(27,5)) AS cr_amount
FROM master_chart_of_accounts AS mcoa
INNER JOIN chart_of_accounts AS coa ON (mcoa.id = coa.master_chart_of_accounts_id AND mcoa.id = 80)
LEFT JOIN general_ledger AS maingl ON (coa.id = maingl.accounts_head_id AND coa.account_code='1101010101100321')
INNER JOIN
( SELECT gl.accounts_head_id, MAX(gl.gl_id) AS max_gl_id, gl.office_code, gl.office_type, gl.country_id,gl.cost_center_id
FROM general_ledger AS gl
-- INNER JOIN voucher_info AS vi ON (gl.voucher_info_id = vi.id)
-- WHERE vi.posting_date < '2013-02-01' AND
WHERE gl.transaction_date < '2013-02-01' AND
gl.cost_center_id IN ('BI0000000000000000000001') AND
gl.country_id IN (1) AND
gl.office_code IN ('UG500013') AND
1=1
GROUP BY gl.accounts_head_id, gl.office_code, gl.office_type, gl.country_id,gl.cost_center_id
ORDER BY gl.accounts_head_id
) AS tmpgl
ON ( maingl.office_code = tmpgl.office_code
AND maingl.office_type = tmpgl.office_type
AND maingl.accounts_head_id = tmpgl.accounts_head_id
AND maingl.country_id = tmpgl.country_id
AND maingl.cost_center_id = tmpgl.cost_center_id
AND maingl.gl_id = tmpgl.max_gl_id
)
WHERE mcoa.account_status_id = 1 AND
coa.account_status_id = 1
) AS tempOpeningBalance
UNION
SELECT vi.cost_center_id
, cc.center_name AS cost_center_name
, gl.office_code
, vi.posting_date AS transaction_date
, vd.accounts_head_id
, (SELECT chart_of_accounts.account_name FROM chart_of_accounts WHERE chart_of_accounts.id = vd.accounts_head_id) AS account_name
, vd.opposite_accounts_head_id
, (SELECT chart_of_accounts.account_name FROM chart_of_accounts WHERE chart_of_accounts.id = vd.opposite_accounts_head_id) AS opposite_account_name
, vd.particulars
, gl.dr_amount AS dr_amount -- here to check
, gl.cr_amount AS cr_amount
, vi.voucher_no
, vi.vin
FROM general_ledger AS gl
INNER JOIN voucher_info AS vi
ON (gl.voucher_info_id = vi.id)
INNER JOIN cost_center AS cc
ON (vi.cost_center_id = cc.id)
INNER JOIN voucher_details AS vd
ON (vi.id = vd.voucher_info_id)
INNER JOIN chart_of_accounts AS coa
ON (vd.accounts_head_id = coa.id)
WHERE vi.posting_date BETWEEN '2013-02-01' AND'2013-02-28'
AND vi.voucher_status_id = 3
AND vd.status_id = 1
AND vi.office_code = 'UG500063'
AND coa.account_code='1101010101100321'
AND coa.cost_center_id = 'BI0000000000000000000001'
ORDER BY cost_center_name
, office_code
, transaction_date;
Use a variable like this
SET #running_balance=0;
SELECT dr_amount AS dr_amount
, cr_amount AS cr_amount
, #running_balance := (#running_balance + dr_amount - cr_amount)
FROM general_ledger

How to display result and handle errors # 1242?

I want to:
if the value qty_out! = 0, then set qty_out = 0
Help me to display results
SELECT
SUBSTR(stok_control.tgl_faktur,9,2) AS 'tanggal',
SUBSTR(stok_control.tgl_faktur,6,2) AS 'bulan',
CONCAT(
(SELECT(
IFNULL(stok_control.faktur_beli,''))
)
,
(SELECT(
IFNULL(stok_control.faktur_jual,''))
)
) AS 'faktur',
bahan.id AS 'kode_bahan',
bahan.nm_bahan AS 'nama_bahan',
stok_control.qty_in AS 'masuk',
(
SELECT IF(stok_control.qty_out != '',0,0)
FROM
stok_control
WHERE
stok_control.faktur_beli !=''
) AS 'keluar'
FROM
stok_control
LEFT JOIN bahan ON stok_control.id_bahan=bahan.id
#eggyal, I have changed your code to be:
SELECT
SUBSTR(sc.tgl_faktur, 9, 2) AS 'tanggal',
SUBSTR(sc.tgl_faktur, 6, 2) AS 'bulan',
CONCAT(
IFNULL(sc.faktur_beli, ''),
IFNULL(sc.faktur_jual, '')
) AS 'faktur',
b.id AS 'kode_bahan',
b.nm_bahan AS 'nama_bahan',
(SELECT IFNULL(sc.qty_in,0)) AS 'masuk',
(SELECT
(
IF(faktur_beli <> '',
0,
(SELECT sc.qty_out)
)
)
) AS 'qty_out'
FROM
stok_control sc LEFT JOIN bahan b ON sc.id_bahan = b.id
I've tried to reference an existing but still error...
I want to:
if the value qty_out! = 0, then set qty_out = 0
Do you mean that you want qty_out to be 0 irrespective of its original value?
SELECT SUBSTR(sc.tgl_faktur, 9, 2) AS tanggal,
SUBSTR(sc.tgl_faktur, 6, 2) AS bulan,
CONCAT(
IFNULL(sc.faktur_beli, ''),
IFNULL(sc.faktur_jual, '')
) AS faktur,
b.id AS kode_bahan,
b.nm_bahan AS nama_bahan,
sc.qty_in AS masuk,
0 AS qty_out
FROM stok_control sc LEFT JOIN bahan b ON sc.id_bahan = b.id