1

I want to get values of TIMESTAMP and STRING_VALUE based on selected ID. Suppose My Selected ID is 4259,4226 and 4259 Then It should select TIMESTAMP and STRING_VALUE for selected ID using CASE Statement. I have tried Below query but returning Into Error

CREATE TABLE "DRL_FTO3_DI1_A0"
(
  "VARIABLE" integer,
  "CALCULATION" integer,
  "TIMESTAMP_S" integer,
  "TIMESTAMP_MS" integer,
  "VALUE" double precision,
  "STATUS" integer,
  "GUID" character(36),
  "STRVALUE" character varying(50)
)

INSERT INTO "DRL_FTO3_DI1_A0"(
            "VARIABLE", "CALCULATION", "TIMESTAMP_S", "TIMESTAMP_MS", "VALUE", 
            "STATUS", "GUID", "STRVALUE")
    VALUES (4226, 0, 1451120925, 329,0 , 1078067200, '', 'BATCH 1'),
           (4306, 0, 1451120925, 329,0 , 1078067200, '', 'BATCH 2'),
           (4311, 0, 1451120925, 329,0 , 1078067200, '', '2')

Now Suppose Out of three Variable(4226,4306,4311) I want to select 4226 and 4311

SELECT ((TIMESTAMP WITHOUT Time Zone 'epoch' + "TIMESTAMP_S" * INTERVAL '1 second') AT TIME ZONE 'UTC')::TIMESTAMP WITHOUT Time Zone,
SUM(CASE WHEN "VARIABLE" = 4226 Then "STRVALUE" END) as 'A',
SUM(CASE WHEN "VARIABLE" = 4311 Then "STRVALUE" END) as 'B'
FROM "DRL_FTO3_DI1_A0"
GROUP BY "TIMESTAMP_S"
ORDER BY "TIMESTAMP_S";


 TIMESTAMP_S                         A            B
 2015-12-26 14:38:45                BATCH_1       2
6
  • 1
    I don't understand what you're trying to do. Perhaps example data (and error message) would help? Commented Dec 26, 2015 at 10:49
  • There is my Output Too Commented Dec 26, 2015 at 11:14
  • Will the crosstab() function do what you want? stackoverflow.com/questions/3002499/postgresql-crosstab-query/… Commented Dec 26, 2015 at 11:27
  • Can you please tell me how can i write the query for crosstab in my case Commented Dec 26, 2015 at 11:30
  • This is what i have tried using crosstab but resulting into error SELECT * FROM crosstab( 'SELECT "VARIABLE","TIMESTAMP_S","STRVALUE" FROM "DRL_FTO3_DI1_A0" ORDER BY 2') AS ct ("TIMESTAMP_S" Integer, "A" text, "B" text); Commented Dec 26, 2015 at 11:46

1 Answer 1

1

This Is the Query using crosstab and It Works

SELECT *
    FROM  crosstab (
        $$SELECT "VARIABLE", "TIMESTAMP_S", "STRVALUE"
         FROM   "DRL_FTO3_DI1_A0"
         WHERE  "VARIABLE" = ANY (array[4306,4226])
         ORDER  BY 1,2$$
    ) 
        AS
    t (            
        "TIMESTAMP_S" integer,                
        "A" character varying,
        "B" character varying
    );

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.