How to do a 'SUM' of a COUNT(*) in your select to be grouped by a particular column in SQL SERVERSQL join...

Calculating list of areas between the curves in an intersection region

How do I add a strong "onion flavor" to the biryani (in restaurant style)?

How can guns be countered by melee combat without raw-ability or exceptional explanations?

Taking an academic pseudonym?

Including proofs of known theorems in master's thesis

Question Tag error

In the Lost in Space intro why was Dr. Smith actor listed as a special guest star?

What really causes series inductance of capacitors?

How many diagrams is too much in a research article?

What does "south of due west" mean?

What could cause an entire planet of humans to become aphasic?

Expression for "unconsciously using words (or accents) used by a person you often talk with or listen to"?

Did ancient Germans take pride in leaving the land untouched?

Is Screenshot Time-tracking Common?

What is an efficient way to digitize a family photo collection?

Have any astronauts or cosmonauts died in space?

Was Opportunity's last message to Earth "My battery is low and it's getting dark"?

Why aren't passengers instructed how to lift aisle armrests?

Tikz: Perpendicular FROM a line

How bad is a Computer Science course that doesn't teach Design Patterns?

How to regain lost focus?

Why does a single AND gate need 60 transistors?

Missing a connection and don't have money to book next flight

Why is Shelob considered evil?



How to do a 'SUM' of a COUNT(*) in your select to be grouped by a particular column in SQL SERVER


SQL join query to show rows with non-existent rows in one tablemysql select latest per group using 2 different tables along with self joinWindow function sorting performanceCreate a view showing total minutes per customer per month and change from previous monthCan I make this multiple join query faster?Check for existing matches to find the field their grouped-byMost efficient way to join two tables with MAX valueIssue with SUM OVER PARTITION BY in conjunction with GROUP BYMySQL: SELECT with SUM() and COUNT() as new columnsMS SQL rolling grouped sum for every new data entry













0















What I am looking for is to be able to SUM the open_lines and group them by that so instead of 'cntnr_id' having open_lines showing as '1' it should show '2' since it is the same cntnr_id and order_num.



I am currently using SQLSERVER 2012 but I will be using this in a Crystal Report so I dont believe I can use windowing functions... If I need to clarify on something please advise! Thank you!



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, COUNT(PH.ORDER_NUM) AS [OPEN_LINES]
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE

HAVING COUNT(*) < 5

ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM


The results of this are



ZONE_NUM   CNTNR_ID   CREATION_DATE   CREATION_TIME   ORDER_NUM   ORDER_STATUS   ORDER_TYPE   OPEN_LINES

17 W531 20150701 091749 385747-*-* 31 41 1
16 W532 20150626 095603 389291-*-* 31 41 1
17 W532 20150626 095603 389291-*-* 31 41 1









share|improve this question
















bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Try to GROUP BY just with PL.CNTNR_ID.

    – oNare
    Jul 2 '15 at 15:58






  • 2





    Can't Crystal Reports call a stored procedure? Why should Crystal Reports limit what T-SQL you can use?

    – Aaron Bertrand
    Jul 2 '15 at 16:01













  • Thanks for the input oNare and Aaron Bertrand. I am using the command feature of Crystal and unfortunately being rather new, I feel a small tweak to the query would resolve this issue on the database side, rather then utilizing the presentation layer.

    – Hector
    Jul 2 '15 at 16:46













  • It wouldn't be just a 'small tweak' though.

    – Mark Sinkinson
    Jul 2 '15 at 17:00











  • No worries, I am open to any feedback as this is a learning experience for me. Lets keep valuable comments and ideas coming along so we can help people like myself and others that may find this useful.

    – Hector
    Jul 2 '15 at 18:23
















0















What I am looking for is to be able to SUM the open_lines and group them by that so instead of 'cntnr_id' having open_lines showing as '1' it should show '2' since it is the same cntnr_id and order_num.



I am currently using SQLSERVER 2012 but I will be using this in a Crystal Report so I dont believe I can use windowing functions... If I need to clarify on something please advise! Thank you!



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, COUNT(PH.ORDER_NUM) AS [OPEN_LINES]
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE

HAVING COUNT(*) < 5

ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM


The results of this are



ZONE_NUM   CNTNR_ID   CREATION_DATE   CREATION_TIME   ORDER_NUM   ORDER_STATUS   ORDER_TYPE   OPEN_LINES

17 W531 20150701 091749 385747-*-* 31 41 1
16 W532 20150626 095603 389291-*-* 31 41 1
17 W532 20150626 095603 389291-*-* 31 41 1









share|improve this question
















bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Try to GROUP BY just with PL.CNTNR_ID.

    – oNare
    Jul 2 '15 at 15:58






  • 2





    Can't Crystal Reports call a stored procedure? Why should Crystal Reports limit what T-SQL you can use?

    – Aaron Bertrand
    Jul 2 '15 at 16:01













  • Thanks for the input oNare and Aaron Bertrand. I am using the command feature of Crystal and unfortunately being rather new, I feel a small tweak to the query would resolve this issue on the database side, rather then utilizing the presentation layer.

    – Hector
    Jul 2 '15 at 16:46













  • It wouldn't be just a 'small tweak' though.

    – Mark Sinkinson
    Jul 2 '15 at 17:00











  • No worries, I am open to any feedback as this is a learning experience for me. Lets keep valuable comments and ideas coming along so we can help people like myself and others that may find this useful.

    – Hector
    Jul 2 '15 at 18:23














0












0








0








What I am looking for is to be able to SUM the open_lines and group them by that so instead of 'cntnr_id' having open_lines showing as '1' it should show '2' since it is the same cntnr_id and order_num.



I am currently using SQLSERVER 2012 but I will be using this in a Crystal Report so I dont believe I can use windowing functions... If I need to clarify on something please advise! Thank you!



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, COUNT(PH.ORDER_NUM) AS [OPEN_LINES]
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE

HAVING COUNT(*) < 5

ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM


The results of this are



ZONE_NUM   CNTNR_ID   CREATION_DATE   CREATION_TIME   ORDER_NUM   ORDER_STATUS   ORDER_TYPE   OPEN_LINES

17 W531 20150701 091749 385747-*-* 31 41 1
16 W532 20150626 095603 389291-*-* 31 41 1
17 W532 20150626 095603 389291-*-* 31 41 1









share|improve this question
















What I am looking for is to be able to SUM the open_lines and group them by that so instead of 'cntnr_id' having open_lines showing as '1' it should show '2' since it is the same cntnr_id and order_num.



I am currently using SQLSERVER 2012 but I will be using this in a Crystal Report so I dont believe I can use windowing functions... If I need to clarify on something please advise! Thank you!



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, COUNT(PH.ORDER_NUM) AS [OPEN_LINES]
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE

HAVING COUNT(*) < 5

ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM


The results of this are



ZONE_NUM   CNTNR_ID   CREATION_DATE   CREATION_TIME   ORDER_NUM   ORDER_STATUS   ORDER_TYPE   OPEN_LINES

17 W531 20150701 091749 385747-*-* 31 41 1
16 W532 20150626 095603 389291-*-* 31 41 1
17 W532 20150626 095603 389291-*-* 31 41 1






sql-server group-by count sum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 2 '15 at 15:40









Mark Sinkinson

7,76432947




7,76432947










asked Jul 2 '15 at 15:38









HectorHector

8741620




8741620





bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Try to GROUP BY just with PL.CNTNR_ID.

    – oNare
    Jul 2 '15 at 15:58






  • 2





    Can't Crystal Reports call a stored procedure? Why should Crystal Reports limit what T-SQL you can use?

    – Aaron Bertrand
    Jul 2 '15 at 16:01













  • Thanks for the input oNare and Aaron Bertrand. I am using the command feature of Crystal and unfortunately being rather new, I feel a small tweak to the query would resolve this issue on the database side, rather then utilizing the presentation layer.

    – Hector
    Jul 2 '15 at 16:46













  • It wouldn't be just a 'small tweak' though.

    – Mark Sinkinson
    Jul 2 '15 at 17:00











  • No worries, I am open to any feedback as this is a learning experience for me. Lets keep valuable comments and ideas coming along so we can help people like myself and others that may find this useful.

    – Hector
    Jul 2 '15 at 18:23



















  • Try to GROUP BY just with PL.CNTNR_ID.

    – oNare
    Jul 2 '15 at 15:58






  • 2





    Can't Crystal Reports call a stored procedure? Why should Crystal Reports limit what T-SQL you can use?

    – Aaron Bertrand
    Jul 2 '15 at 16:01













  • Thanks for the input oNare and Aaron Bertrand. I am using the command feature of Crystal and unfortunately being rather new, I feel a small tweak to the query would resolve this issue on the database side, rather then utilizing the presentation layer.

    – Hector
    Jul 2 '15 at 16:46













  • It wouldn't be just a 'small tweak' though.

    – Mark Sinkinson
    Jul 2 '15 at 17:00











  • No worries, I am open to any feedback as this is a learning experience for me. Lets keep valuable comments and ideas coming along so we can help people like myself and others that may find this useful.

    – Hector
    Jul 2 '15 at 18:23

















Try to GROUP BY just with PL.CNTNR_ID.

– oNare
Jul 2 '15 at 15:58





Try to GROUP BY just with PL.CNTNR_ID.

– oNare
Jul 2 '15 at 15:58




2




2





Can't Crystal Reports call a stored procedure? Why should Crystal Reports limit what T-SQL you can use?

– Aaron Bertrand
Jul 2 '15 at 16:01







Can't Crystal Reports call a stored procedure? Why should Crystal Reports limit what T-SQL you can use?

– Aaron Bertrand
Jul 2 '15 at 16:01















Thanks for the input oNare and Aaron Bertrand. I am using the command feature of Crystal and unfortunately being rather new, I feel a small tweak to the query would resolve this issue on the database side, rather then utilizing the presentation layer.

– Hector
Jul 2 '15 at 16:46







Thanks for the input oNare and Aaron Bertrand. I am using the command feature of Crystal and unfortunately being rather new, I feel a small tweak to the query would resolve this issue on the database side, rather then utilizing the presentation layer.

– Hector
Jul 2 '15 at 16:46















It wouldn't be just a 'small tweak' though.

– Mark Sinkinson
Jul 2 '15 at 17:00





It wouldn't be just a 'small tweak' though.

– Mark Sinkinson
Jul 2 '15 at 17:00













No worries, I am open to any feedback as this is a learning experience for me. Lets keep valuable comments and ideas coming along so we can help people like myself and others that may find this useful.

– Hector
Jul 2 '15 at 18:23





No worries, I am open to any feedback as this is a learning experience for me. Lets keep valuable comments and ideas coming along so we can help people like myself and others that may find this useful.

– Hector
Jul 2 '15 at 18:23










1 Answer
1






active

oldest

votes


















0














Is the following what you are looking for?



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, OLC.OpenLinesTotal
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
inner join (select ORDER_NUM, sum(OPEN_LINES) as OpenLinesTotal from PK_HEADER Group By ORDER_NUM) OLC on OLC.ORDER_NUM = PL.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
and OLC.OpenLinesTotal < 5
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE
ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM





share|improve this answer
























  • Unfortunately this did not work but it certainly is what I am looking for, I had that in my original script but instead of doing a subselect I just had it in the select statement to count [order_num] as sum of [open_lines]. Thanks for the input though User16421!

    – Hector
    Jul 7 '15 at 18:03













  • I ended up using Crystal Reports and did a GROUP record, then used a SUM function to calculate the SUM of records grouped on Order Number. Thank you all for your input.

    – Hector
    Jul 10 '15 at 20:01













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f105799%2fhow-to-do-a-sum-of-a-count-in-your-select-to-be-grouped-by-a-particular-col%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Is the following what you are looking for?



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, OLC.OpenLinesTotal
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
inner join (select ORDER_NUM, sum(OPEN_LINES) as OpenLinesTotal from PK_HEADER Group By ORDER_NUM) OLC on OLC.ORDER_NUM = PL.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
and OLC.OpenLinesTotal < 5
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE
ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM





share|improve this answer
























  • Unfortunately this did not work but it certainly is what I am looking for, I had that in my original script but instead of doing a subselect I just had it in the select statement to count [order_num] as sum of [open_lines]. Thanks for the input though User16421!

    – Hector
    Jul 7 '15 at 18:03













  • I ended up using Crystal Reports and did a GROUP record, then used a SUM function to calculate the SUM of records grouped on Order Number. Thank you all for your input.

    – Hector
    Jul 10 '15 at 20:01


















0














Is the following what you are looking for?



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, OLC.OpenLinesTotal
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
inner join (select ORDER_NUM, sum(OPEN_LINES) as OpenLinesTotal from PK_HEADER Group By ORDER_NUM) OLC on OLC.ORDER_NUM = PL.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
and OLC.OpenLinesTotal < 5
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE
ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM





share|improve this answer
























  • Unfortunately this did not work but it certainly is what I am looking for, I had that in my original script but instead of doing a subselect I just had it in the select statement to count [order_num] as sum of [open_lines]. Thanks for the input though User16421!

    – Hector
    Jul 7 '15 at 18:03













  • I ended up using Crystal Reports and did a GROUP record, then used a SUM function to calculate the SUM of records grouped on Order Number. Thank you all for your input.

    – Hector
    Jul 10 '15 at 20:01
















0












0








0







Is the following what you are looking for?



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, OLC.OpenLinesTotal
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
inner join (select ORDER_NUM, sum(OPEN_LINES) as OpenLinesTotal from PK_HEADER Group By ORDER_NUM) OLC on OLC.ORDER_NUM = PL.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
and OLC.OpenLinesTotal < 5
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE
ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM





share|improve this answer













Is the following what you are looking for?



SELECT PL.ZONE_NUM
, PL.CNTNR_ID
-- , PH.CREATION_DATE
-- , PH.CREATION_TIME
, PS.ORDER_NUM
-- , PH.ORDER_STATUS
-- , PH.ORDER_TYPE
, OLC.OpenLinesTotal
FROM PK_LINE PL
INNER JOIN PK_SHIPPING PS
ON PL.ORDER_NUM = PS.ORDER_NUM
LEFT JOIN CARRIER_CFG CC
ON PS.SERVICE_CODE = CC.SERVICE_CODE
INNER JOIN PK_HEADER PH
ON PL.ORDER_NUM = PH.ORDER_NUM
inner join (select ORDER_NUM, sum(OPEN_LINES) as OpenLinesTotal from PK_HEADER Group By ORDER_NUM) OLC on OLC.ORDER_NUM = PL.ORDER_NUM
WHERE PH.ORDER_TYPE = '41'
AND ORDER_STATUS < '40'
AND PL.CNTNR_ID NOT IN ('',' ')
and OLC.OpenLinesTotal < 5
GROUP BY PL.CNTNR_ID
, PL.ZONE_NUM
, PH.CREATION_DATE
, PH.CREATION_TIME
, PS.ORDER_NUM
, PH.ORDER_STATUS
, PH.ORDER_TYPE
ORDER BY Open_lines
, PL.CNTNR_ID
, PL.ZONE_NUM






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 2 '15 at 19:32









user16421user16421

16




16













  • Unfortunately this did not work but it certainly is what I am looking for, I had that in my original script but instead of doing a subselect I just had it in the select statement to count [order_num] as sum of [open_lines]. Thanks for the input though User16421!

    – Hector
    Jul 7 '15 at 18:03













  • I ended up using Crystal Reports and did a GROUP record, then used a SUM function to calculate the SUM of records grouped on Order Number. Thank you all for your input.

    – Hector
    Jul 10 '15 at 20:01





















  • Unfortunately this did not work but it certainly is what I am looking for, I had that in my original script but instead of doing a subselect I just had it in the select statement to count [order_num] as sum of [open_lines]. Thanks for the input though User16421!

    – Hector
    Jul 7 '15 at 18:03













  • I ended up using Crystal Reports and did a GROUP record, then used a SUM function to calculate the SUM of records grouped on Order Number. Thank you all for your input.

    – Hector
    Jul 10 '15 at 20:01



















Unfortunately this did not work but it certainly is what I am looking for, I had that in my original script but instead of doing a subselect I just had it in the select statement to count [order_num] as sum of [open_lines]. Thanks for the input though User16421!

– Hector
Jul 7 '15 at 18:03







Unfortunately this did not work but it certainly is what I am looking for, I had that in my original script but instead of doing a subselect I just had it in the select statement to count [order_num] as sum of [open_lines]. Thanks for the input though User16421!

– Hector
Jul 7 '15 at 18:03















I ended up using Crystal Reports and did a GROUP record, then used a SUM function to calculate the SUM of records grouped on Order Number. Thank you all for your input.

– Hector
Jul 10 '15 at 20:01







I ended up using Crystal Reports and did a GROUP record, then used a SUM function to calculate the SUM of records grouped on Order Number. Thank you all for your input.

– Hector
Jul 10 '15 at 20:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Database Administrators Stack Exchange!


  • 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%2fdba.stackexchange.com%2fquestions%2f105799%2fhow-to-do-a-sum-of-a-count-in-your-select-to-be-grouped-by-a-particular-col%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

Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...