SQL - create flag in query to highlight order which contain quantity = 1











up vote
3
down vote

favorite
1












I have tried creating a case statement but doesn't seem to give me what i want. Id like to get a split of the table (which is at a product level) and aggregate at an order level of items which contain quantity of 1.



Any ideas on how I would do this?



order id | Product | Quantity 
---------+---------+--------------
11111 | sdsd4 | 1 (single item )
22222 | sasas | 1 (multiple items)
22222 | wertt | 1 (multiple items)


I'd like to get a case statement to add another column to split out orders with quantity = 1 and orders greater 1



Any idea on how I would do this?



The desired outcome would be the column in (brackets)



I could then count the orders and bring in the newly created column as the dimension



More detail here:



enter image description here



Attached is an image of table structure.



Logic, if quantity = 1 and 1 order then single item order
if order has one item but multiples of same item non single item order
if order has more than one product then non single item order










share|improve this question
























  • Specify the expected result as well!
    – jarlh
    Nov 18 at 18:44










  • It is hard to understand your requirement because you are using terms that are unfamiliar to database engineers. Perhaps you can come up with sample data and the expected output, that would help.
    – Laurenz Albe
    Nov 18 at 18:44










  • Ive added desired outcome in pic above and logic. hope this helps.
    – Andrew Poole
    Nov 18 at 19:11










  • It is considered good practice to not use spaces in column names -- I use underscore (_) myself.
    – Hogan
    Nov 18 at 19:21















up vote
3
down vote

favorite
1












I have tried creating a case statement but doesn't seem to give me what i want. Id like to get a split of the table (which is at a product level) and aggregate at an order level of items which contain quantity of 1.



Any ideas on how I would do this?



order id | Product | Quantity 
---------+---------+--------------
11111 | sdsd4 | 1 (single item )
22222 | sasas | 1 (multiple items)
22222 | wertt | 1 (multiple items)


I'd like to get a case statement to add another column to split out orders with quantity = 1 and orders greater 1



Any idea on how I would do this?



The desired outcome would be the column in (brackets)



I could then count the orders and bring in the newly created column as the dimension



More detail here:



enter image description here



Attached is an image of table structure.



Logic, if quantity = 1 and 1 order then single item order
if order has one item but multiples of same item non single item order
if order has more than one product then non single item order










share|improve this question
























  • Specify the expected result as well!
    – jarlh
    Nov 18 at 18:44










  • It is hard to understand your requirement because you are using terms that are unfamiliar to database engineers. Perhaps you can come up with sample data and the expected output, that would help.
    – Laurenz Albe
    Nov 18 at 18:44










  • Ive added desired outcome in pic above and logic. hope this helps.
    – Andrew Poole
    Nov 18 at 19:11










  • It is considered good practice to not use spaces in column names -- I use underscore (_) myself.
    – Hogan
    Nov 18 at 19:21













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I have tried creating a case statement but doesn't seem to give me what i want. Id like to get a split of the table (which is at a product level) and aggregate at an order level of items which contain quantity of 1.



Any ideas on how I would do this?



order id | Product | Quantity 
---------+---------+--------------
11111 | sdsd4 | 1 (single item )
22222 | sasas | 1 (multiple items)
22222 | wertt | 1 (multiple items)


I'd like to get a case statement to add another column to split out orders with quantity = 1 and orders greater 1



Any idea on how I would do this?



The desired outcome would be the column in (brackets)



I could then count the orders and bring in the newly created column as the dimension



More detail here:



enter image description here



Attached is an image of table structure.



Logic, if quantity = 1 and 1 order then single item order
if order has one item but multiples of same item non single item order
if order has more than one product then non single item order










share|improve this question















I have tried creating a case statement but doesn't seem to give me what i want. Id like to get a split of the table (which is at a product level) and aggregate at an order level of items which contain quantity of 1.



Any ideas on how I would do this?



order id | Product | Quantity 
---------+---------+--------------
11111 | sdsd4 | 1 (single item )
22222 | sasas | 1 (multiple items)
22222 | wertt | 1 (multiple items)


I'd like to get a case statement to add another column to split out orders with quantity = 1 and orders greater 1



Any idea on how I would do this?



The desired outcome would be the column in (brackets)



I could then count the orders and bring in the newly created column as the dimension



More detail here:



enter image description here



Attached is an image of table structure.



Logic, if quantity = 1 and 1 order then single item order
if order has one item but multiples of same item non single item order
if order has more than one product then non single item order







sql google-bigquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 19:48









jarlh

27.9k52137




27.9k52137










asked Nov 18 at 18:09









Andrew Poole

184




184












  • Specify the expected result as well!
    – jarlh
    Nov 18 at 18:44










  • It is hard to understand your requirement because you are using terms that are unfamiliar to database engineers. Perhaps you can come up with sample data and the expected output, that would help.
    – Laurenz Albe
    Nov 18 at 18:44










  • Ive added desired outcome in pic above and logic. hope this helps.
    – Andrew Poole
    Nov 18 at 19:11










  • It is considered good practice to not use spaces in column names -- I use underscore (_) myself.
    – Hogan
    Nov 18 at 19:21


















  • Specify the expected result as well!
    – jarlh
    Nov 18 at 18:44










  • It is hard to understand your requirement because you are using terms that are unfamiliar to database engineers. Perhaps you can come up with sample data and the expected output, that would help.
    – Laurenz Albe
    Nov 18 at 18:44










  • Ive added desired outcome in pic above and logic. hope this helps.
    – Andrew Poole
    Nov 18 at 19:11










  • It is considered good practice to not use spaces in column names -- I use underscore (_) myself.
    – Hogan
    Nov 18 at 19:21
















Specify the expected result as well!
– jarlh
Nov 18 at 18:44




Specify the expected result as well!
– jarlh
Nov 18 at 18:44












It is hard to understand your requirement because you are using terms that are unfamiliar to database engineers. Perhaps you can come up with sample data and the expected output, that would help.
– Laurenz Albe
Nov 18 at 18:44




It is hard to understand your requirement because you are using terms that are unfamiliar to database engineers. Perhaps you can come up with sample data and the expected output, that would help.
– Laurenz Albe
Nov 18 at 18:44












Ive added desired outcome in pic above and logic. hope this helps.
– Andrew Poole
Nov 18 at 19:11




Ive added desired outcome in pic above and logic. hope this helps.
– Andrew Poole
Nov 18 at 19:11












It is considered good practice to not use spaces in column names -- I use underscore (_) myself.
– Hogan
Nov 18 at 19:21




It is considered good practice to not use spaces in column names -- I use underscore (_) myself.
– Hogan
Nov 18 at 19:21












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Below is for BigQuery Standard SQL



#standardSQL
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


You can test, play with above using dummy data as below



#standardSQL
WITH `project.dataset.table` AS (
SELECT 11111 order_id, 'sdsd4' Product, 1 Quantity UNION ALL
SELECT 22222, 'sasas', 2 UNION ALL
SELECT 22222, 'wertt', 1
)
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


with result



Row order_id    Product Quantity    Single_or_Multiple   
1 11111 sdsd4 1 Single Item Order
2 22222 sasas 2 Multipple Items Order
3 22222 wertt 1 Multipple Items Order





share|improve this answer





















  • Nailed it! Brilliant Thanks very much for your help. And for everyone who responded.
    – Andrew Poole
    Nov 18 at 20:33






  • 1




    glad it worked for you. also consider voting up the answer if it helped you :o)
    – Mikhail Berlyant
    Nov 18 at 20:33




















up vote
3
down vote













If your database supports analytic functions, then you can use a query like this one:



SELECT *,
CASE WHEN count("Product") OVER (partition by "order id") > 1
THEN 'multiple items' ELSE 'single item'
END As "How many items"
FROM Table1


Demo: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=b659279fc16d2084cb1cf4a3bea361a1






share|improve this answer

















  • 1




    The database im using is BigQuery
    – Andrew Poole
    Nov 18 at 18:49










  • Hi, I tried this but didnt return the desired outcome.
    – Andrew Poole
    Nov 18 at 19:12










  • This answer looks correct to me and should run on BigQuery.
    – Gordon Linoff
    Nov 18 at 20:09


















up vote
2
down vote













If I understand this right, you could use a subquery to get the count of records for an order and flag a record, if this count is larger then 1 and the quantity is equal to 1.



SELECT t1.order_id,
t1.product,
t1.quantity,
CASE
WHEN t1.quantity = 1
AND (SELECT count(*)
FROM elbat t2
WHERE t2.order_id = t1.order_id) > 1 THEN
'flag'
ELSE
'no flag'
END flag
FROM elbat t1;





share|improve this answer





















  • Hey, thanks for the quick reply. I think this is nearly there however, there is just 1 table so no need for a join? Sorry for the lack of info on this. Im a newb
    – Andrew Poole
    Nov 18 at 18:45








  • 1




    @sticky bit You explained a correct way to solve it via corelated subquery. I tried it a while ago but only was close t2.order_id=t1.order_id did the trick. But a simple suggestion would it be better if group bywas used for count even I am a little unsure as group by would again aggregate but we want row by row. Any point on that ?
    – Himanshu Ahuja
    Nov 18 at 20:22













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',
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%2f53363990%2fsql-create-flag-in-query-to-highlight-order-which-contain-quantity-1%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Below is for BigQuery Standard SQL



#standardSQL
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


You can test, play with above using dummy data as below



#standardSQL
WITH `project.dataset.table` AS (
SELECT 11111 order_id, 'sdsd4' Product, 1 Quantity UNION ALL
SELECT 22222, 'sasas', 2 UNION ALL
SELECT 22222, 'wertt', 1
)
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


with result



Row order_id    Product Quantity    Single_or_Multiple   
1 11111 sdsd4 1 Single Item Order
2 22222 sasas 2 Multipple Items Order
3 22222 wertt 1 Multipple Items Order





share|improve this answer





















  • Nailed it! Brilliant Thanks very much for your help. And for everyone who responded.
    – Andrew Poole
    Nov 18 at 20:33






  • 1




    glad it worked for you. also consider voting up the answer if it helped you :o)
    – Mikhail Berlyant
    Nov 18 at 20:33

















up vote
2
down vote



accepted










Below is for BigQuery Standard SQL



#standardSQL
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


You can test, play with above using dummy data as below



#standardSQL
WITH `project.dataset.table` AS (
SELECT 11111 order_id, 'sdsd4' Product, 1 Quantity UNION ALL
SELECT 22222, 'sasas', 2 UNION ALL
SELECT 22222, 'wertt', 1
)
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


with result



Row order_id    Product Quantity    Single_or_Multiple   
1 11111 sdsd4 1 Single Item Order
2 22222 sasas 2 Multipple Items Order
3 22222 wertt 1 Multipple Items Order





share|improve this answer





















  • Nailed it! Brilliant Thanks very much for your help. And for everyone who responded.
    – Andrew Poole
    Nov 18 at 20:33






  • 1




    glad it worked for you. also consider voting up the answer if it helped you :o)
    – Mikhail Berlyant
    Nov 18 at 20:33















up vote
2
down vote



accepted







up vote
2
down vote



accepted






Below is for BigQuery Standard SQL



#standardSQL
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


You can test, play with above using dummy data as below



#standardSQL
WITH `project.dataset.table` AS (
SELECT 11111 order_id, 'sdsd4' Product, 1 Quantity UNION ALL
SELECT 22222, 'sasas', 2 UNION ALL
SELECT 22222, 'wertt', 1
)
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


with result



Row order_id    Product Quantity    Single_or_Multiple   
1 11111 sdsd4 1 Single Item Order
2 22222 sasas 2 Multipple Items Order
3 22222 wertt 1 Multipple Items Order





share|improve this answer












Below is for BigQuery Standard SQL



#standardSQL
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


You can test, play with above using dummy data as below



#standardSQL
WITH `project.dataset.table` AS (
SELECT 11111 order_id, 'sdsd4' Product, 1 Quantity UNION ALL
SELECT 22222, 'sasas', 2 UNION ALL
SELECT 22222, 'wertt', 1
)
SELECT *,
CASE COUNT(DISTINCT Product) OVER(PARTITION BY order_id)
WHEN 1 THEN 'Single Item Order'
ELSE 'Multipple Items Order'
END Single_or_Multiple
FROM `project.dataset.table`


with result



Row order_id    Product Quantity    Single_or_Multiple   
1 11111 sdsd4 1 Single Item Order
2 22222 sasas 2 Multipple Items Order
3 22222 wertt 1 Multipple Items Order






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 20:17









Mikhail Berlyant

53.5k42964




53.5k42964












  • Nailed it! Brilliant Thanks very much for your help. And for everyone who responded.
    – Andrew Poole
    Nov 18 at 20:33






  • 1




    glad it worked for you. also consider voting up the answer if it helped you :o)
    – Mikhail Berlyant
    Nov 18 at 20:33




















  • Nailed it! Brilliant Thanks very much for your help. And for everyone who responded.
    – Andrew Poole
    Nov 18 at 20:33






  • 1




    glad it worked for you. also consider voting up the answer if it helped you :o)
    – Mikhail Berlyant
    Nov 18 at 20:33


















Nailed it! Brilliant Thanks very much for your help. And for everyone who responded.
– Andrew Poole
Nov 18 at 20:33




Nailed it! Brilliant Thanks very much for your help. And for everyone who responded.
– Andrew Poole
Nov 18 at 20:33




1




1




glad it worked for you. also consider voting up the answer if it helped you :o)
– Mikhail Berlyant
Nov 18 at 20:33






glad it worked for you. also consider voting up the answer if it helped you :o)
– Mikhail Berlyant
Nov 18 at 20:33














up vote
3
down vote













If your database supports analytic functions, then you can use a query like this one:



SELECT *,
CASE WHEN count("Product") OVER (partition by "order id") > 1
THEN 'multiple items' ELSE 'single item'
END As "How many items"
FROM Table1


Demo: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=b659279fc16d2084cb1cf4a3bea361a1






share|improve this answer

















  • 1




    The database im using is BigQuery
    – Andrew Poole
    Nov 18 at 18:49










  • Hi, I tried this but didnt return the desired outcome.
    – Andrew Poole
    Nov 18 at 19:12










  • This answer looks correct to me and should run on BigQuery.
    – Gordon Linoff
    Nov 18 at 20:09















up vote
3
down vote













If your database supports analytic functions, then you can use a query like this one:



SELECT *,
CASE WHEN count("Product") OVER (partition by "order id") > 1
THEN 'multiple items' ELSE 'single item'
END As "How many items"
FROM Table1


Demo: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=b659279fc16d2084cb1cf4a3bea361a1






share|improve this answer

















  • 1




    The database im using is BigQuery
    – Andrew Poole
    Nov 18 at 18:49










  • Hi, I tried this but didnt return the desired outcome.
    – Andrew Poole
    Nov 18 at 19:12










  • This answer looks correct to me and should run on BigQuery.
    – Gordon Linoff
    Nov 18 at 20:09













up vote
3
down vote










up vote
3
down vote









If your database supports analytic functions, then you can use a query like this one:



SELECT *,
CASE WHEN count("Product") OVER (partition by "order id") > 1
THEN 'multiple items' ELSE 'single item'
END As "How many items"
FROM Table1


Demo: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=b659279fc16d2084cb1cf4a3bea361a1






share|improve this answer












If your database supports analytic functions, then you can use a query like this one:



SELECT *,
CASE WHEN count("Product") OVER (partition by "order id") > 1
THEN 'multiple items' ELSE 'single item'
END As "How many items"
FROM Table1


Demo: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=b659279fc16d2084cb1cf4a3bea361a1







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 18:40









krokodilko

27.5k32657




27.5k32657








  • 1




    The database im using is BigQuery
    – Andrew Poole
    Nov 18 at 18:49










  • Hi, I tried this but didnt return the desired outcome.
    – Andrew Poole
    Nov 18 at 19:12










  • This answer looks correct to me and should run on BigQuery.
    – Gordon Linoff
    Nov 18 at 20:09














  • 1




    The database im using is BigQuery
    – Andrew Poole
    Nov 18 at 18:49










  • Hi, I tried this but didnt return the desired outcome.
    – Andrew Poole
    Nov 18 at 19:12










  • This answer looks correct to me and should run on BigQuery.
    – Gordon Linoff
    Nov 18 at 20:09








1




1




The database im using is BigQuery
– Andrew Poole
Nov 18 at 18:49




The database im using is BigQuery
– Andrew Poole
Nov 18 at 18:49












Hi, I tried this but didnt return the desired outcome.
– Andrew Poole
Nov 18 at 19:12




Hi, I tried this but didnt return the desired outcome.
– Andrew Poole
Nov 18 at 19:12












This answer looks correct to me and should run on BigQuery.
– Gordon Linoff
Nov 18 at 20:09




This answer looks correct to me and should run on BigQuery.
– Gordon Linoff
Nov 18 at 20:09










up vote
2
down vote













If I understand this right, you could use a subquery to get the count of records for an order and flag a record, if this count is larger then 1 and the quantity is equal to 1.



SELECT t1.order_id,
t1.product,
t1.quantity,
CASE
WHEN t1.quantity = 1
AND (SELECT count(*)
FROM elbat t2
WHERE t2.order_id = t1.order_id) > 1 THEN
'flag'
ELSE
'no flag'
END flag
FROM elbat t1;





share|improve this answer





















  • Hey, thanks for the quick reply. I think this is nearly there however, there is just 1 table so no need for a join? Sorry for the lack of info on this. Im a newb
    – Andrew Poole
    Nov 18 at 18:45








  • 1




    @sticky bit You explained a correct way to solve it via corelated subquery. I tried it a while ago but only was close t2.order_id=t1.order_id did the trick. But a simple suggestion would it be better if group bywas used for count even I am a little unsure as group by would again aggregate but we want row by row. Any point on that ?
    – Himanshu Ahuja
    Nov 18 at 20:22

















up vote
2
down vote













If I understand this right, you could use a subquery to get the count of records for an order and flag a record, if this count is larger then 1 and the quantity is equal to 1.



SELECT t1.order_id,
t1.product,
t1.quantity,
CASE
WHEN t1.quantity = 1
AND (SELECT count(*)
FROM elbat t2
WHERE t2.order_id = t1.order_id) > 1 THEN
'flag'
ELSE
'no flag'
END flag
FROM elbat t1;





share|improve this answer





















  • Hey, thanks for the quick reply. I think this is nearly there however, there is just 1 table so no need for a join? Sorry for the lack of info on this. Im a newb
    – Andrew Poole
    Nov 18 at 18:45








  • 1




    @sticky bit You explained a correct way to solve it via corelated subquery. I tried it a while ago but only was close t2.order_id=t1.order_id did the trick. But a simple suggestion would it be better if group bywas used for count even I am a little unsure as group by would again aggregate but we want row by row. Any point on that ?
    – Himanshu Ahuja
    Nov 18 at 20:22















up vote
2
down vote










up vote
2
down vote









If I understand this right, you could use a subquery to get the count of records for an order and flag a record, if this count is larger then 1 and the quantity is equal to 1.



SELECT t1.order_id,
t1.product,
t1.quantity,
CASE
WHEN t1.quantity = 1
AND (SELECT count(*)
FROM elbat t2
WHERE t2.order_id = t1.order_id) > 1 THEN
'flag'
ELSE
'no flag'
END flag
FROM elbat t1;





share|improve this answer












If I understand this right, you could use a subquery to get the count of records for an order and flag a record, if this count is larger then 1 and the quantity is equal to 1.



SELECT t1.order_id,
t1.product,
t1.quantity,
CASE
WHEN t1.quantity = 1
AND (SELECT count(*)
FROM elbat t2
WHERE t2.order_id = t1.order_id) > 1 THEN
'flag'
ELSE
'no flag'
END flag
FROM elbat t1;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 18:40









sticky bit

10.9k51629




10.9k51629












  • Hey, thanks for the quick reply. I think this is nearly there however, there is just 1 table so no need for a join? Sorry for the lack of info on this. Im a newb
    – Andrew Poole
    Nov 18 at 18:45








  • 1




    @sticky bit You explained a correct way to solve it via corelated subquery. I tried it a while ago but only was close t2.order_id=t1.order_id did the trick. But a simple suggestion would it be better if group bywas used for count even I am a little unsure as group by would again aggregate but we want row by row. Any point on that ?
    – Himanshu Ahuja
    Nov 18 at 20:22




















  • Hey, thanks for the quick reply. I think this is nearly there however, there is just 1 table so no need for a join? Sorry for the lack of info on this. Im a newb
    – Andrew Poole
    Nov 18 at 18:45








  • 1




    @sticky bit You explained a correct way to solve it via corelated subquery. I tried it a while ago but only was close t2.order_id=t1.order_id did the trick. But a simple suggestion would it be better if group bywas used for count even I am a little unsure as group by would again aggregate but we want row by row. Any point on that ?
    – Himanshu Ahuja
    Nov 18 at 20:22


















Hey, thanks for the quick reply. I think this is nearly there however, there is just 1 table so no need for a join? Sorry for the lack of info on this. Im a newb
– Andrew Poole
Nov 18 at 18:45






Hey, thanks for the quick reply. I think this is nearly there however, there is just 1 table so no need for a join? Sorry for the lack of info on this. Im a newb
– Andrew Poole
Nov 18 at 18:45






1




1




@sticky bit You explained a correct way to solve it via corelated subquery. I tried it a while ago but only was close t2.order_id=t1.order_id did the trick. But a simple suggestion would it be better if group bywas used for count even I am a little unsure as group by would again aggregate but we want row by row. Any point on that ?
– Himanshu Ahuja
Nov 18 at 20:22






@sticky bit You explained a correct way to solve it via corelated subquery. I tried it a while ago but only was close t2.order_id=t1.order_id did the trick. But a simple suggestion would it be better if group bywas used for count even I am a little unsure as group by would again aggregate but we want row by row. Any point on that ?
– Himanshu Ahuja
Nov 18 at 20:22




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363990%2fsql-create-flag-in-query-to-highlight-order-which-contain-quantity-1%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

Costa Masnaga

Fotorealismo

Sidney Franklin