Difficulty in constructing SQL query for Redshift












0















I don't have much experience in SQL and I am finding difficult to construct the query for the following problem.



Let's say I have sales per month data as follows and is saved in Redshift



ProductId   Year    Month   Sales
A 2018 1 345
A 2018 2 3425
A 2018 3 56545
A 2018 4 234
A 2018 5 23
B 2018 1 645
B 2018 2 2324
B 2018 3 123
B 2018 4 700
B 2018 5 1410
....
....
....


I want to extract the following information from the above table. The query in plain English looks as follows.




  1. Select the product (group by) where sales are greater than 800 for the month 2018/3. For this query, product "A" data should be returned with the sales value for month 2018/3


  2. Select the product (group by) where the sale of month 2018/5 is 100% greater than 2018/4. Here only product "B" is matched and sales data of both months should be included in the result.



Edit: Expected result added.



For query 1



ProductId   Year    Month   Sales
A 2018 3 56545


For query 2 (Grouped)



ProductId   Increase    Year    Month   Sales
B 101.42 % 2018 5 1410
2018 4 700









share|improve this question

























  • Could you please edit your question to show the sample output you would expect from the input data you have shown?

    – John Rotenstein
    Nov 22 '18 at 5:32











  • @JohnRotenstein Expected results added.

    – Pavan Kumar
    Nov 26 '18 at 3:34
















0















I don't have much experience in SQL and I am finding difficult to construct the query for the following problem.



Let's say I have sales per month data as follows and is saved in Redshift



ProductId   Year    Month   Sales
A 2018 1 345
A 2018 2 3425
A 2018 3 56545
A 2018 4 234
A 2018 5 23
B 2018 1 645
B 2018 2 2324
B 2018 3 123
B 2018 4 700
B 2018 5 1410
....
....
....


I want to extract the following information from the above table. The query in plain English looks as follows.




  1. Select the product (group by) where sales are greater than 800 for the month 2018/3. For this query, product "A" data should be returned with the sales value for month 2018/3


  2. Select the product (group by) where the sale of month 2018/5 is 100% greater than 2018/4. Here only product "B" is matched and sales data of both months should be included in the result.



Edit: Expected result added.



For query 1



ProductId   Year    Month   Sales
A 2018 3 56545


For query 2 (Grouped)



ProductId   Increase    Year    Month   Sales
B 101.42 % 2018 5 1410
2018 4 700









share|improve this question

























  • Could you please edit your question to show the sample output you would expect from the input data you have shown?

    – John Rotenstein
    Nov 22 '18 at 5:32











  • @JohnRotenstein Expected results added.

    – Pavan Kumar
    Nov 26 '18 at 3:34














0












0








0








I don't have much experience in SQL and I am finding difficult to construct the query for the following problem.



Let's say I have sales per month data as follows and is saved in Redshift



ProductId   Year    Month   Sales
A 2018 1 345
A 2018 2 3425
A 2018 3 56545
A 2018 4 234
A 2018 5 23
B 2018 1 645
B 2018 2 2324
B 2018 3 123
B 2018 4 700
B 2018 5 1410
....
....
....


I want to extract the following information from the above table. The query in plain English looks as follows.




  1. Select the product (group by) where sales are greater than 800 for the month 2018/3. For this query, product "A" data should be returned with the sales value for month 2018/3


  2. Select the product (group by) where the sale of month 2018/5 is 100% greater than 2018/4. Here only product "B" is matched and sales data of both months should be included in the result.



Edit: Expected result added.



For query 1



ProductId   Year    Month   Sales
A 2018 3 56545


For query 2 (Grouped)



ProductId   Increase    Year    Month   Sales
B 101.42 % 2018 5 1410
2018 4 700









share|improve this question
















I don't have much experience in SQL and I am finding difficult to construct the query for the following problem.



Let's say I have sales per month data as follows and is saved in Redshift



ProductId   Year    Month   Sales
A 2018 1 345
A 2018 2 3425
A 2018 3 56545
A 2018 4 234
A 2018 5 23
B 2018 1 645
B 2018 2 2324
B 2018 3 123
B 2018 4 700
B 2018 5 1410
....
....
....


I want to extract the following information from the above table. The query in plain English looks as follows.




  1. Select the product (group by) where sales are greater than 800 for the month 2018/3. For this query, product "A" data should be returned with the sales value for month 2018/3


  2. Select the product (group by) where the sale of month 2018/5 is 100% greater than 2018/4. Here only product "B" is matched and sales data of both months should be included in the result.



Edit: Expected result added.



For query 1



ProductId   Year    Month   Sales
A 2018 3 56545


For query 2 (Grouped)



ProductId   Increase    Year    Month   Sales
B 101.42 % 2018 5 1410
2018 4 700






sql amazon-redshift aggregation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:33







Pavan Kumar

















asked Nov 22 '18 at 4:42









Pavan KumarPavan Kumar

5471520




5471520













  • Could you please edit your question to show the sample output you would expect from the input data you have shown?

    – John Rotenstein
    Nov 22 '18 at 5:32











  • @JohnRotenstein Expected results added.

    – Pavan Kumar
    Nov 26 '18 at 3:34



















  • Could you please edit your question to show the sample output you would expect from the input data you have shown?

    – John Rotenstein
    Nov 22 '18 at 5:32











  • @JohnRotenstein Expected results added.

    – Pavan Kumar
    Nov 26 '18 at 3:34

















Could you please edit your question to show the sample output you would expect from the input data you have shown?

– John Rotenstein
Nov 22 '18 at 5:32





Could you please edit your question to show the sample output you would expect from the input data you have shown?

– John Rotenstein
Nov 22 '18 at 5:32













@JohnRotenstein Expected results added.

– Pavan Kumar
Nov 26 '18 at 3:34





@JohnRotenstein Expected results added.

– Pavan Kumar
Nov 26 '18 at 3:34












2 Answers
2






active

oldest

votes


















1














Query 1: Select the product where sales are greater than 800 for the month 2018/3.



SELECT
productid,
year,
month,
sales
FROM table
WHERE year = 2018
AND month = 3
AND sales > 800


Query 2: Select the product where the sale of month 2018/5 is 100% greater than 2018/4.



SELECT
productid
a.year as previous_month_year,
a.month as previous_month,
a.sales as previous_month_sales,
b.year as year,
b.month as month,
b.sales as sales,
to_char(100 * b.sales / a.sales,'999D99%') as increase
FROM table a
JOIN table b
WHERE a.year = 2018
AND b.year = 2018
AND a.month = 4
AND b.month = 5
AND b.sales > a.sales * 2
AND a.productid = b.productid


I'm not sure what you meant by "group by" in your requirements, since no aggregates are required.






share|improve this answer
























  • Regarding group by - Maybe I didn't express the intent properly. The query would be "I need to know products (i.e., A, B, etc) who match the given condition. The condition would be dynamic combinations of "query 1" or "query 2" For example: SELECT fields WHERE query_1_for(2018/3) AND query_1_for(2018/5) OR query_2_for(2018/4, 2018/6) AND so on.

    – Pavan Kumar
    Nov 26 '18 at 7:38











  • I don't know how to think SQL way regarding above problem, but in my mind I thought query like this - each product has row for each months sales. So my thought was to group rows by product_id and inside each group appply the conditions. if all conditions are true select that product. I am imagining a tree structure here (Hang over from JSON / NO-SQL world)

    – Pavan Kumar
    Nov 26 '18 at 7:45











  • The above queries should provide the expected output that you listed in your question. Are you now saying that this was not the output that you actually wanted? If so, please update your question, or create a new question.

    – John Rotenstein
    Nov 26 '18 at 11:32



















1














You can try below queries



select * from tablename
where year=2018 and month=3 and and Sales>800

select * from tablename where year=2018 and month in (4,5)
and (case when year=2018 and month=5 then sales end)>(case when year=2018 and month=4 then sales end)*2





share|improve this answer
























  • Regarding the second query. How to make comparisons where Product Id is same. For example, in the second query, sales of product A form month 2018/5 can get compared to product B sales form month 2018/4. But product A sales should be compared to itself in a different month.

    – Pavan Kumar
    Nov 26 '18 at 3:27












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424012%2fdifficulty-in-constructing-sql-query-for-redshift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Query 1: Select the product where sales are greater than 800 for the month 2018/3.



SELECT
productid,
year,
month,
sales
FROM table
WHERE year = 2018
AND month = 3
AND sales > 800


Query 2: Select the product where the sale of month 2018/5 is 100% greater than 2018/4.



SELECT
productid
a.year as previous_month_year,
a.month as previous_month,
a.sales as previous_month_sales,
b.year as year,
b.month as month,
b.sales as sales,
to_char(100 * b.sales / a.sales,'999D99%') as increase
FROM table a
JOIN table b
WHERE a.year = 2018
AND b.year = 2018
AND a.month = 4
AND b.month = 5
AND b.sales > a.sales * 2
AND a.productid = b.productid


I'm not sure what you meant by "group by" in your requirements, since no aggregates are required.






share|improve this answer
























  • Regarding group by - Maybe I didn't express the intent properly. The query would be "I need to know products (i.e., A, B, etc) who match the given condition. The condition would be dynamic combinations of "query 1" or "query 2" For example: SELECT fields WHERE query_1_for(2018/3) AND query_1_for(2018/5) OR query_2_for(2018/4, 2018/6) AND so on.

    – Pavan Kumar
    Nov 26 '18 at 7:38











  • I don't know how to think SQL way regarding above problem, but in my mind I thought query like this - each product has row for each months sales. So my thought was to group rows by product_id and inside each group appply the conditions. if all conditions are true select that product. I am imagining a tree structure here (Hang over from JSON / NO-SQL world)

    – Pavan Kumar
    Nov 26 '18 at 7:45











  • The above queries should provide the expected output that you listed in your question. Are you now saying that this was not the output that you actually wanted? If so, please update your question, or create a new question.

    – John Rotenstein
    Nov 26 '18 at 11:32
















1














Query 1: Select the product where sales are greater than 800 for the month 2018/3.



SELECT
productid,
year,
month,
sales
FROM table
WHERE year = 2018
AND month = 3
AND sales > 800


Query 2: Select the product where the sale of month 2018/5 is 100% greater than 2018/4.



SELECT
productid
a.year as previous_month_year,
a.month as previous_month,
a.sales as previous_month_sales,
b.year as year,
b.month as month,
b.sales as sales,
to_char(100 * b.sales / a.sales,'999D99%') as increase
FROM table a
JOIN table b
WHERE a.year = 2018
AND b.year = 2018
AND a.month = 4
AND b.month = 5
AND b.sales > a.sales * 2
AND a.productid = b.productid


I'm not sure what you meant by "group by" in your requirements, since no aggregates are required.






share|improve this answer
























  • Regarding group by - Maybe I didn't express the intent properly. The query would be "I need to know products (i.e., A, B, etc) who match the given condition. The condition would be dynamic combinations of "query 1" or "query 2" For example: SELECT fields WHERE query_1_for(2018/3) AND query_1_for(2018/5) OR query_2_for(2018/4, 2018/6) AND so on.

    – Pavan Kumar
    Nov 26 '18 at 7:38











  • I don't know how to think SQL way regarding above problem, but in my mind I thought query like this - each product has row for each months sales. So my thought was to group rows by product_id and inside each group appply the conditions. if all conditions are true select that product. I am imagining a tree structure here (Hang over from JSON / NO-SQL world)

    – Pavan Kumar
    Nov 26 '18 at 7:45











  • The above queries should provide the expected output that you listed in your question. Are you now saying that this was not the output that you actually wanted? If so, please update your question, or create a new question.

    – John Rotenstein
    Nov 26 '18 at 11:32














1












1








1







Query 1: Select the product where sales are greater than 800 for the month 2018/3.



SELECT
productid,
year,
month,
sales
FROM table
WHERE year = 2018
AND month = 3
AND sales > 800


Query 2: Select the product where the sale of month 2018/5 is 100% greater than 2018/4.



SELECT
productid
a.year as previous_month_year,
a.month as previous_month,
a.sales as previous_month_sales,
b.year as year,
b.month as month,
b.sales as sales,
to_char(100 * b.sales / a.sales,'999D99%') as increase
FROM table a
JOIN table b
WHERE a.year = 2018
AND b.year = 2018
AND a.month = 4
AND b.month = 5
AND b.sales > a.sales * 2
AND a.productid = b.productid


I'm not sure what you meant by "group by" in your requirements, since no aggregates are required.






share|improve this answer













Query 1: Select the product where sales are greater than 800 for the month 2018/3.



SELECT
productid,
year,
month,
sales
FROM table
WHERE year = 2018
AND month = 3
AND sales > 800


Query 2: Select the product where the sale of month 2018/5 is 100% greater than 2018/4.



SELECT
productid
a.year as previous_month_year,
a.month as previous_month,
a.sales as previous_month_sales,
b.year as year,
b.month as month,
b.sales as sales,
to_char(100 * b.sales / a.sales,'999D99%') as increase
FROM table a
JOIN table b
WHERE a.year = 2018
AND b.year = 2018
AND a.month = 4
AND b.month = 5
AND b.sales > a.sales * 2
AND a.productid = b.productid


I'm not sure what you meant by "group by" in your requirements, since no aggregates are required.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 6:50









John RotensteinJohn Rotenstein

77.6k786138




77.6k786138













  • Regarding group by - Maybe I didn't express the intent properly. The query would be "I need to know products (i.e., A, B, etc) who match the given condition. The condition would be dynamic combinations of "query 1" or "query 2" For example: SELECT fields WHERE query_1_for(2018/3) AND query_1_for(2018/5) OR query_2_for(2018/4, 2018/6) AND so on.

    – Pavan Kumar
    Nov 26 '18 at 7:38











  • I don't know how to think SQL way regarding above problem, but in my mind I thought query like this - each product has row for each months sales. So my thought was to group rows by product_id and inside each group appply the conditions. if all conditions are true select that product. I am imagining a tree structure here (Hang over from JSON / NO-SQL world)

    – Pavan Kumar
    Nov 26 '18 at 7:45











  • The above queries should provide the expected output that you listed in your question. Are you now saying that this was not the output that you actually wanted? If so, please update your question, or create a new question.

    – John Rotenstein
    Nov 26 '18 at 11:32



















  • Regarding group by - Maybe I didn't express the intent properly. The query would be "I need to know products (i.e., A, B, etc) who match the given condition. The condition would be dynamic combinations of "query 1" or "query 2" For example: SELECT fields WHERE query_1_for(2018/3) AND query_1_for(2018/5) OR query_2_for(2018/4, 2018/6) AND so on.

    – Pavan Kumar
    Nov 26 '18 at 7:38











  • I don't know how to think SQL way regarding above problem, but in my mind I thought query like this - each product has row for each months sales. So my thought was to group rows by product_id and inside each group appply the conditions. if all conditions are true select that product. I am imagining a tree structure here (Hang over from JSON / NO-SQL world)

    – Pavan Kumar
    Nov 26 '18 at 7:45











  • The above queries should provide the expected output that you listed in your question. Are you now saying that this was not the output that you actually wanted? If so, please update your question, or create a new question.

    – John Rotenstein
    Nov 26 '18 at 11:32

















Regarding group by - Maybe I didn't express the intent properly. The query would be "I need to know products (i.e., A, B, etc) who match the given condition. The condition would be dynamic combinations of "query 1" or "query 2" For example: SELECT fields WHERE query_1_for(2018/3) AND query_1_for(2018/5) OR query_2_for(2018/4, 2018/6) AND so on.

– Pavan Kumar
Nov 26 '18 at 7:38





Regarding group by - Maybe I didn't express the intent properly. The query would be "I need to know products (i.e., A, B, etc) who match the given condition. The condition would be dynamic combinations of "query 1" or "query 2" For example: SELECT fields WHERE query_1_for(2018/3) AND query_1_for(2018/5) OR query_2_for(2018/4, 2018/6) AND so on.

– Pavan Kumar
Nov 26 '18 at 7:38













I don't know how to think SQL way regarding above problem, but in my mind I thought query like this - each product has row for each months sales. So my thought was to group rows by product_id and inside each group appply the conditions. if all conditions are true select that product. I am imagining a tree structure here (Hang over from JSON / NO-SQL world)

– Pavan Kumar
Nov 26 '18 at 7:45





I don't know how to think SQL way regarding above problem, but in my mind I thought query like this - each product has row for each months sales. So my thought was to group rows by product_id and inside each group appply the conditions. if all conditions are true select that product. I am imagining a tree structure here (Hang over from JSON / NO-SQL world)

– Pavan Kumar
Nov 26 '18 at 7:45













The above queries should provide the expected output that you listed in your question. Are you now saying that this was not the output that you actually wanted? If so, please update your question, or create a new question.

– John Rotenstein
Nov 26 '18 at 11:32





The above queries should provide the expected output that you listed in your question. Are you now saying that this was not the output that you actually wanted? If so, please update your question, or create a new question.

– John Rotenstein
Nov 26 '18 at 11:32













1














You can try below queries



select * from tablename
where year=2018 and month=3 and and Sales>800

select * from tablename where year=2018 and month in (4,5)
and (case when year=2018 and month=5 then sales end)>(case when year=2018 and month=4 then sales end)*2





share|improve this answer
























  • Regarding the second query. How to make comparisons where Product Id is same. For example, in the second query, sales of product A form month 2018/5 can get compared to product B sales form month 2018/4. But product A sales should be compared to itself in a different month.

    – Pavan Kumar
    Nov 26 '18 at 3:27
















1














You can try below queries



select * from tablename
where year=2018 and month=3 and and Sales>800

select * from tablename where year=2018 and month in (4,5)
and (case when year=2018 and month=5 then sales end)>(case when year=2018 and month=4 then sales end)*2





share|improve this answer
























  • Regarding the second query. How to make comparisons where Product Id is same. For example, in the second query, sales of product A form month 2018/5 can get compared to product B sales form month 2018/4. But product A sales should be compared to itself in a different month.

    – Pavan Kumar
    Nov 26 '18 at 3:27














1












1








1







You can try below queries



select * from tablename
where year=2018 and month=3 and and Sales>800

select * from tablename where year=2018 and month in (4,5)
and (case when year=2018 and month=5 then sales end)>(case when year=2018 and month=4 then sales end)*2





share|improve this answer













You can try below queries



select * from tablename
where year=2018 and month=3 and and Sales>800

select * from tablename where year=2018 and month in (4,5)
and (case when year=2018 and month=5 then sales end)>(case when year=2018 and month=4 then sales end)*2






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 4:50









fa06fa06

18.5k21019




18.5k21019













  • Regarding the second query. How to make comparisons where Product Id is same. For example, in the second query, sales of product A form month 2018/5 can get compared to product B sales form month 2018/4. But product A sales should be compared to itself in a different month.

    – Pavan Kumar
    Nov 26 '18 at 3:27



















  • Regarding the second query. How to make comparisons where Product Id is same. For example, in the second query, sales of product A form month 2018/5 can get compared to product B sales form month 2018/4. But product A sales should be compared to itself in a different month.

    – Pavan Kumar
    Nov 26 '18 at 3:27

















Regarding the second query. How to make comparisons where Product Id is same. For example, in the second query, sales of product A form month 2018/5 can get compared to product B sales form month 2018/4. But product A sales should be compared to itself in a different month.

– Pavan Kumar
Nov 26 '18 at 3:27





Regarding the second query. How to make comparisons where Product Id is same. For example, in the second query, sales of product A form month 2018/5 can get compared to product B sales form month 2018/4. But product A sales should be compared to itself in a different month.

– Pavan Kumar
Nov 26 '18 at 3:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424012%2fdifficulty-in-constructing-sql-query-for-redshift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?