I would like a function which returns 0 if the result of the calculation in the function is 0 (or NULL). If the result is larger than 0 and below 0.1 then function should return 0.1 - otherwise the result should just be the result of the calculation. Help is very much appreciated. I have this function:
DECLARE output REAL;
BEGIN
select
case
when (select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'TRUE') > 0 and p.peer_review = 'TRUE' then /* Samarbejde */
case
when p.type_classification_id in (57360320, 57360322, 57360324, 57360326) /* Tidsskriftsartikel, Konferenceartikel i tidsskrift, Letter, Review */ then 1*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (57360348, 57360352, 57360350) /* Bidrag til bog, Bidrag til rapport, Konferenceartikel i proceeding */
then
case
when bs.id is null then 0.5*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when bs.id is not null then 1*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
end
when p.type_classification_id in (57360336, 57360340) /* Bog, Rapport */ then 5*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (57360338, 57360330) /*Antologi, Leder*/ then 0.5*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
end
when (select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'TRUE') > 0 and p.peer_review is null then /* Samarbejde */
case
when p.type_classification_id in (57360342) /* Doktorafhandling */ then 5*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (57360390) /* Patent */ then 2*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (415542856) /* Preprint */ then 0.5*1.25*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
end
when (select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'TRUE') = 0 and p.peer_review = 'TRUE' then /*Ikke Samarbejde */
case
when p.type_classification_id in (57360320, 57360322, 57360324, 57360326) /* Tidsskriftsartikel, Konferenceartikel i tidsskrift, Letter, Review */ then 1*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (57360348, 57360352, 57360350) /* Bidrag til bog, Bidrag til rapport, Konferenceartikel i proceeding */
then
case
when bs.id is null then 0.5*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when bs.id is not null then 1*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
end
when p.type_classification_id in (57360336, 57360340) /* Bog, Rapport */ then 5*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (57360338, 57360330) /*Antologi, Leder*/ then 0.5*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
end
when (select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'TRUE') = 0 and p.peer_review is null then /*Ikke Samarbejde */
case
when p.type_classification_id in (57360342) /* Doktorafhandling */ then 5*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (57360390) /* Patent */ then 2*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
when p.type_classification_id in (415542856) /* Preprint */ then 0.5*((select count(caa1.id) from classified_author_assoc caa1 where caa1.publication_id = p.id and caa1.remote_origin = 'FALSE')::real / (select count(caa2.id) from classified_author_assoc caa2 where caa2.publication_id = p.id))
end
else
0
end
from
publication p join publication_status ps on (p.id = ps.publication_id) left join book_series bs on (bs.publication_id = p.id)
where p.id = $1
INTO output;
RETURN output;
END;
Unless I misunderstand you, you just need to change your final line that says RETURN output; to this:
return case
when output is null or output = 0 then 0
when output > 0 and output < 0.1 then 0.1
else output end;
I did, however notice you did not account for negative values. In the statement above, it will return any negative as-is.
Related
I am trying to pull data out and chuck it into a Stimulsoft report. The problem I am having is that I need it to output to two columns. I also need every "manager" record to show even if the count assigned to said record is NULL.
This is what i have at the moment:
DECLARE #ManagerCount INT = (( SELECT Count(*) FROM AM WHERE dbo.AM.AMCurrent = 1 AND dbo.AM.OmitInReport = 0 ) + 1) / 2
DECLARE #tmp_AM1 TABLE (AMID INT, AMName NVARCHAR(100), ID INT)
INSERT INTO #tmp_AM1 SELECT AMID, AMName, row_number() over (order by AMID ) FROM AM
WHERE dbo.AM.AMCurrent = 1 AND dbo.AM.OmitInReport = 0
SELECT * FROM (
SELECT ta.id AS id1, ta.AMName AS ManagerName1, COUNT(*) AS ManagerCount1 FROM #tmp_AM1 tA INNER JOIN Job J ON tA.AMID = j.AMID
WHERE ta.ID BETWEEN 1 AND #ManagerCount AND j.jobStatusID != 5
GROUP BY ta.ID, ta.AMName
) a
LEFT JOIN
(
SELECT ta.id AS id2,ta.AMName AS ManagerName2, COUNT(*) AS ManagerCount2 FROM #tmp_AM1 tA INNER JOIN Job J ON tA.AMID = j.AMID
WHERE ta.ID > #ManagerCount AND j.jobStatusID != 5
GROUP BY ta.AMName, ta.ID
) b ON a.id1 + #ManagerCount = b.id2
Which ends up returning something like:
There are 18 managers so 9 per column, but this code doesn't show them all since anything that doesn't have a count in the first left join, won't show, and therefore the same row in column 2 doesn't show.
Results of SELECT * FROM #tmp_AM1:
DECLARE #tmp_AM1 TABLE (AMID INT, AMName NVARCHAR(100), ID INT)
INSERT INTO #tmp_AM1 SELECT AMID, AMName, row_number() over (order by AMID ) FROM AM
WHERE dbo.AM.AMCurrent = 1 AND dbo.AM.OmitInReport = 0
SELECT * FROM (
SELECT ta.id AS id1, ta.AMName AS ManagerName1, COUNT(*) AS ManagerCount1 FROM #tmp_AM1 tA INNER JOIN Job J ON tA.AMID = j.AMID
WHERE ta.ID BETWEEN 1 AND #ManagerCount AND j.jobStatusID != 5
GROUP BY ta.ID, ta.AMName
) a
LEFT OUTER JOIN
(
SELECT ta.id AS id2,ta.AMName AS ManagerName2, COUNT(*) AS ManagerCount2 FROM #tmp_AM1 tA INNER JOIN Job J ON tA.AMID = j.AMID
WHERE ta.ID > #ManagerCount AND j.jobStatusID != 5
GROUP BY ta.AMName, ta.ID
) b ON a.id1 + #ManagerCount = b.id2 where ManagerName2 IS Null and ManagerCount2 IS NULL
just you want to use LEFT OUTER JOIN for select row even there is have any null values.,
Since the two subqueries are pretty much identical, except the where-statement, I would consiter rewriting it into one single query. I'm not sure why you need the same columns outputed into different columns in the result, but something like this might work:
WITH cte AS (
SELECT
ta.id AS id
,ta.AMName AS ManagerName
,COUNT(*) AS ManagerCount
,CASE WHEN ta.ID BETWEEN 1 AND #ManagerCount THEN 0 ELSE 1 END AS something
FROM
#tmp_AM1 tA
INNER JOIN Job J ON tA.AMID = j.AMID
WHERE
j.jobStatusID != 5
GROUP BY
ta.ID
,ta.AMName
,CASE WHEN ta.ID BETWEEN 1 AND #ManagerCount THEN 0 ELSE 1 END
)
SELECT
CASE WHEN something = 0 THEN cte.id ELSE null END AS id1
,CASE WHEN something = 0 THEN cte.ManagerName ELSE null END AS ManagerName1
,CASE WHEN something = 0 THEN cte.ManagerCount ELSE null END AS ManagerCount1
,CASE WHEN something = 1 THEN cte.id ELSE null END AS id2
,CASE WHEN something = 1 THEN cte.ManagerName ELSE null END AS ManagerName2
,CASE WHEN something = 1 THEN cte.ManagerCount ELSE null END AS ManagerCount2
FROM
cte
Probably not the best approach but i got the correct output using:
DECLARE #ManagerCount INT = (( SELECT Count(*) FROM AM WHERE dbo.AM.AMCurrent = 1 AND dbo.AM.OmitInReport = 0 ) + 1) / 2
DECLARE #tmp_AM1 TABLE (AMID INT, AMName NVARCHAR(100), ID INT)
INSERT INTO #tmp_AM1 SELECT AMID, AMName, row_number() over (order by AMID ) FROM AM
WHERE dbo.AM.AMCurrent = 1 AND dbo.AM.OmitInReport = 0
ORDER By AMName
SELECT ManagerName1, ManagerName2, ManagerCount1, ManagerCount2 FROM (
SELECT AMID, ta.id AS id1, ta.AMName AS ManagerName1 FROM #tmp_AM1 tA
WHERE (ta.ID BETWEEN 1 AND #ManagerCount)
) a
LEFT JOIN
(
SELECT AMID, ISNULL(COUNT(*), 0) AS ManagerCount1 FROM Job j
INNER JOIN tblJobOutcome jO ON j.JobOutcomeID = jo.JobOutcomeID AND jO.JobOutcomeID != 5
GROUP BY AMID
) a1 ON a.AMID = a1.AMID
LEFT JOIN
(
SELECT AMID, ta.id AS id2, ta.AMName AS ManagerName2 FROM #tmp_AM1 tA
WHERE (ta.ID > #ManagerCount)
) b ON a.id1 + #ManagerCount = b.id2
LEFT JOIN
(
SELECT AMID, ISNULL(COUNT(*), 0) AS ManagerCount2 FROM Job j
INNER JOIN tblJobOutcome jO ON j.JobOutcomeID = jo.JobOutcomeID AND jO.JobOutcomeID != 5
GROUP BY AMID
) b1 ON b.AMID = b1.AMID
Gives me the correct output in two columns.
gives me this:
My query is:
SELECT
id,
CASE WHEN EXISTS(
SELECT
data_od
FROM
bp_stan_produkt
WHERE
id_produkt = bp_produkt.id AND data_do IS NULL AND id_stan_produkt = 313
) THEN 'TAK' ELSE 'NIE'
END AS "313"
FROM
bp_produkt
WHERE
id IN(21048528)
Is it possible to put data_od to column 313 instead of TAK or do I have to create function, make SELECT data_od into some_variable and then some_variable?
Yes it is possible:
select id,
case when exists (select data_od from bp_stan_produkt
where id_produkt = bp_produkt.id and data_do is null
and id_stan_produkt = 313)
then (select to_char(data_od, 'YYYY-MM-DD')
from bp_stan_produkt
where id_produkt = bp.id
and data_do is null
and id_stan_produkt = 313)
else 'NIE'
end as "313"
from bp_produkt
where id in(21048528);
EDIT:
SELECT bp.id, COALESCE(t.data_od_t, 'NIE') AS "313"
FROM bp_produkt bp
LEFT JOIN LATERAL (select to_char(data_od, 'YYYY-MM-DD') AS data_od_t
from bp_stan_produkt
where id_produkt = bp.id
and data_do is null
and id_stan_produkt = 313) t
ON TRUE
WHERE bp.id in(21048528)
I have procedure like this and I get error in If clause. I think it is because COUNT. But my clause have to be like that so I don't know how to solve it. Maybe it would be good to create a function or something similar. Rest of the code is okay
CREATE PROCEDURE DohvatiSveUgovore #zavodId int
AS
BEGIN
DECLARE #TempUgovori TABLE
(
Id int,
UstrojstvenaJedinica nvarchar(100),
VrstaUgovora nvarchar(100),
KlasaUgovora nvarchar(100),
UrudzbeniBrojUgovora nvarchar(100),
DatumPocetkaUgovora nvarchar(10),
DatumIstekaUgovora nvarchar(10)
)
INSERT INTO #TempUgovori(Id, UstrojstvenaJedinica, VrstaUgovora, KlasaUgovora, UrudzbeniBrojUgovora, DatumPocetkaUgovora, DatumIstekaUgovora)
SELECT
u.Id,
ISNULL(STRING_AGG(LTRIM(RTRIM(z.SkraceniNaziv)), ', '), '') AS 'UstrojstvenaJedinica',
vu.Naziv AS 'VrstaUgovora',
ISNULL(u.KlasaUgovora, '') AS 'KlasaUgovora',
ISNULL(u.UrudzbeniBrojUgovora, '') AS 'UrudzbeniBrojUgovora',
(SELECT ISNULL(convert(varchar(10), u.DatumPocetkaUgovora, 104), '')) AS 'DatumPocetkaUgovora',
(SELECT ISNULL(convert(varchar(10), u.DatumIstekaUgovora, 104), '')) AS 'DatumIstekaUgovora'
FROM Ugovor AS u
LEFT JOIN VezaUgovorUstrojstvenaJedinica AS vuu
ON u.Id = vuu.UgovorId
INNER JOIN SifVrstaUgovora AS vu
ON u.VrstaUgovoraId = vu.Id
LEFT JOIN [TEST_MaticniPodaci2].hcphs.SifZavod AS z
ON vuu.UstrojstvenaJedinicaId = z.Id
if( (SELECT COUNT(UstrojstvenaJedinicaId) FROM VezaUgovorUstrojstvenaJedinica WHERE UstrojstvenaJedinicaId = 'HCPHS') = 1)
begin
(SELECT * FROM VezaUgovorUstrojstvenaJedinica WHERE UstrojstvenaJedinicaId = 'HCPHS')
end
ELSE
(SELECT * FROM VezaUgovorUstrojstvenaJedinica WHERE Isdeleted = 0 and UstrojstvenaJedinicaId = #zavodId)
end
ERROR is here in Group by and I don't know why.
GROUP BY u.Id, vu.Naziv, u.KlasaUgovora, u.UrudzbeniBrojUgovora, u.DatumPocetkaUgovora, u.DatumIstekaUgovora
SELECT
tu.Id,
tu.UstrojstvenaJedinica AS 'UstrojstvenaJedinica',
tu.VrstaUgovora AS 'VrstaUgovora',
tu.KlasaUgovora AS 'KlasaUgovora',
tu.UrudzbeniBrojUgovora AS 'UrudzbeniBrojUgovora',
tu.DatumIstekaUgovora AS 'DatumPocetkaUgovora',
tu.DatumIstekaUgovora AS 'DatumIstekaUgovora',
ISNULL(STRING_AGG(LTRIM(RTRIM(p.Naziv)), ', '), '') as 'Partner'
FROM #TempUgovori AS tu
LEFT JOIN VezaUgovorPartner AS vup
on tu.Id = vup.UgovorId
LEFT JOIN [TEST_MaticniPodaci2].dbo.Partner as p
ON vup.PartnerId = p.PartnerID
GROUP BY tu.Id, tu.UstrojstvenaJedinica, tu.VrstaUgovora, tu.KlasaUgovora, tu.UrudzbeniBrojUgovora, tu.DatumPocetkaUgovora, tu.DatumIstekaUgovora
END
EXEC [TEST_Ugovori_Prod].[dbo].[DohvatiSve] 6;
GO
I am sorry for too much code but without it I can't run query.
It looks like you're trying to tell SQL Server to join to VezaUgovorUstrojstvenaJedinica differently depending on the circumstances. The sql engine can't figure that out. For a very brief discussion on how sql evaluates a query, here's a start:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/70efeffe-76b9-4b7e-b4a1-ba53f5d21916/order-of-execution-of-sql-queries?forum=transactsql
Maybe something like this will work for you?
CREATE PROCEDURE DohvatiSveUgovore #zavodId int
AS
BEGIN
DECLARE #TempUgovori TABLE
(
Id int,
UstrojstvenaJedinica nvarchar(100),
VrstaUgovora nvarchar(100),
KlasaUgovora nvarchar(100),
UrudzbeniBrojUgovora nvarchar(100),
DatumPocetkaUgovora nvarchar(10),
DatumIstekaUgovora nvarchar(10)
);
DECLARE #HCPHS integer;
SELECT
#HCPHS = COUNT(UstrojstvenaJedinicaId)
FROM
VezaUgovorUstrojstvenaJedinica
WHERE
UstrojstvenaJedinicaId = 'HCPHS';
IF #HCPHS = 1
INSERT INTO #TempUgovori(Id, UstrojstvenaJedinica, VrstaUgovora, KlasaUgovora, UrudzbeniBrojUgovora, DatumPocetkaUgovora, DatumIstekaUgovora)
SELECT
u.Id,
ISNULL(STRING_AGG(LTRIM(RTRIM(z.SkraceniNaziv)), ', '), '') AS 'UstrojstvenaJedinica',
vu.Naziv AS 'VrstaUgovora',
ISNULL(u.KlasaUgovora, '') AS 'KlasaUgovora',
ISNULL(u.UrudzbeniBrojUgovora, '') AS 'UrudzbeniBrojUgovora',
(SELECT ISNULL(convert(varchar(10), u.DatumPocetkaUgovora, 104), '')) AS 'DatumPocetkaUgovora',
(SELECT ISNULL(convert(varchar(10), u.DatumIstekaUgovora, 104), '')) AS 'DatumIstekaUgovora'
FROM Ugovor AS u
LEFT JOIN VezaUgovorUstrojstvenaJedinica AS vuu
ON u.Id = vuu.UgovorId
INNER JOIN SifVrstaUgovora AS vu
ON u.VrstaUgovoraId = vu.Id
LEFT JOIN [TEST_MaticniPodaci2].hcphs.SifZavod AS z
ON vuu.UstrojstvenaJedinicaId = z.Id
<JOIN OF SOME KIND>
(SELECT * FROM VezaUgovorUstrojstvenaJedinica WHERE UstrojstvenaJedinicaId = 'HCPHS')
ON <SOME CRITERIA>
GROUP BY u.Id, vu.Naziv, u.KlasaUgovora, u.UrudzbeniBrojUgovora, u.DatumPocetkaUgovora, u.DatumIstekaUgovora
ELSE
INSERT INTO #TempUgovori(Id, UstrojstvenaJedinica, VrstaUgovora, KlasaUgovora, UrudzbeniBrojUgovora, DatumPocetkaUgovora, DatumIstekaUgovora)
SELECT
u.Id,
ISNULL(STRING_AGG(LTRIM(RTRIM(z.SkraceniNaziv)), ', '), '') AS 'UstrojstvenaJedinica',
vu.Naziv AS 'VrstaUgovora',
ISNULL(u.KlasaUgovora, '') AS 'KlasaUgovora',
ISNULL(u.UrudzbeniBrojUgovora, '') AS 'UrudzbeniBrojUgovora',
(SELECT ISNULL(convert(varchar(10), u.DatumPocetkaUgovora, 104), '')) AS 'DatumPocetkaUgovora',
(SELECT ISNULL(convert(varchar(10), u.DatumIstekaUgovora, 104), '')) AS 'DatumIstekaUgovora'
FROM Ugovor AS u
LEFT JOIN VezaUgovorUstrojstvenaJedinica AS vuu
ON u.Id = vuu.UgovorId
INNER JOIN SifVrstaUgovora AS vu
ON u.VrstaUgovoraId = vu.Id
LEFT JOIN [TEST_MaticniPodaci2].hcphs.SifZavod AS z
ON vuu.UstrojstvenaJedinicaId = z.Id
<JOIN OF SOME KIND>
(SELECT * FROM VezaUgovorUstrojstvenaJedinica WHERE Isdeleted = 0 and UstrojstvenaJedinicaId = #zavodId)
ON <SOME CRITERIA>
GROUP BY u.Id, vu.Naziv, u.KlasaUgovora, u.UrudzbeniBrojUgovora, u.DatumPocetkaUgovora, u.DatumIstekaUgovora
this is my PostgreSQL query. Here I pass hard coded merchant id (merchant_id = 11).but I need to dynamical pass one merchant id from set of merchant id list. but I don't Know how to pass id as a variable or array by using PostgreSQL function.
select s.enabled from loyalty_scheme L
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) WHERE ((SELECT demoting_time_period from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = 11)GROUP BY scheme_id)< to_char((SELECT (DATE_PART('year', CURRENT_DATE::date) - DATE_PART('year', (select max(t.date_time) As maxdat FROM loyalty_scheme L
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) where (T.unipoint_customer_ref_id = ( select unipoint_customer_ref_id FROM subscribe_merchant where schemerefid = (SELECT scheme_id from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = 11)GROUP BY scheme_id) ) )) ::date)) * 12 +
(DATE_PART('month', CURRENT_DATE::date) - DATE_PART('month', (select max(t.date_time) As maxdat FROM loyalty_scheme L
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) where (T.unipoint_customer_ref_id = ( select unipoint_customer_ref_id FROM subscribe_merchant where schemerefid = (SELECT scheme_id from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = 11)GROUP BY scheme_id) ) ))::date))),'FM999MI'))
here is other query to find list of merchant id
select array_agg(DISTINCT merchant.merchant_id)
from merchant
this are the id Array {3,6,7,11,23,32,34,40,41,43}
in first code I hard coded 11,but i need to pass all those id one by one.Is any one know how to do it. please help me to do this
finally I found the the way to do this
here is my CODE
CREATE OR REPLACE FUNCTION getdata() RETURNS CHAR AS $enables$ DECLARE enables CHAR; my_arry INTEGER[] := (
select array_agg(DISTINCT merchant.merchant_id)
from merchant
); BEGIN FOREACH my_arry SLICE 1 IN ARRAY my_arry LOOP select s.enabled INTO enables from loyalty_scheme L JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid) JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) WHERE ((SELECT demoting_time_period from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = ANY(my_arry) )GROUP BY scheme_id)< to_char((SELECT (DATE_PART('year', CURRENT_DATE::date) - DATE_PART('year', (select max(t.date_time) As maxdat FROM loyalty_scheme L JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid) JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) where (T.unipoint_customer_ref_id = ( select unipoint_customer_ref_id FROM subscribe_merchant where schemerefid = (SELECT scheme_id from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = ANY(my_arry) )GROUP BY scheme_id) ) )) ::date)) * 12 + (DATE_PART('month', CURRENT_DATE::date) - DATE_PART('month', (select max(t.date_time) As maxdat FROM loyalty_scheme L JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid) JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id) where (T.unipoint_customer_ref_id = ( select unipoint_customer_ref_id FROM subscribe_merchant where schemerefid = (SELECT scheme_id from loyalty_scheme where minimum_purchase_amount =(SELECT min(minimum_purchase_amount) from loyalty_scheme where merchant_id = ANY(my_arry))GROUP BY scheme_id) ) ))::date))),'FM999MI')) ;
END LOOP; RETURN enables; END; $enables$ LANGUAGE plpgsql;
Is there a query I can run that will tell me which of my fields in all of the database tables that are integer primary keys are NOT marked as auto-increment? Thanks.
Try this:
SELECT
pk_column_name = c.name,
table_name = o.name
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.syscolumns c
ON ic.object_id = c.id AND COL_NAME(ic.object_id, ic.column_id) = c.name
INNER JOIN sys.sysobjects o
ON o.id = c.id
WHERE i.is_primary_key = 1 -- the column is part of a primary key
AND c.status < 128 -- the column is NOT identity enabled
AND o.xtype = 'U' -- the object is a user table
AND c.xtype = 56 -- the column type is int
I am including columns that are of the type 'int'. If you want to include types like smallint as well, you can look up its xtype by doing a select on the sys.systypes DMV:
SELECT name, xtype FROM sys.systypes
Here is an alternative way of doing this using information schema views:
SELECT
pk_column_name = c.COLUMN_NAME,
table_name = c.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS c
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k
ON c.TABLE_NAME = k.TABLE_NAME
AND c.TABLE_SCHEMA = k.TABLE_SCHEMA
AND c.TABLE_CATALOG = k.TABLE_CATALOG
AND c.COLUMN_NAME = k.COLUMN_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS const
ON k.TABLE_NAME = const.TABLE_NAME
AND k.CONSTRAINT_CATALOG = const.CONSTRAINT_CATALOG
AND k.CONSTRAINT_SCHEMA = const.CONSTRAINT_SCHEMA
AND k.CONSTRAINT_NAME = const.CONSTRAINT_NAME
WHERE COLUMNPROPERTY(OBJECT_ID(c.TABLE_NAME), c.COLUMN_NAME, 'IsIdentity') = 0
AND const.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND c.DATA_TYPE = 'int'
Hope this helps. Note that this only works for SQL Server 2005 and above.