﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Development  / Multi Query Group BY / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 03:48:00 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Multi Query Group BY</title><link>http://www.sqlservercentral.com/Forums/Topic819884-145-1.aspx</link><description>Good Luck!!</description><pubDate>Tue, 17 Nov 2009 04:32:26 GMT</pubDate><dc:creator>sshalloe</dc:creator></item><item><title>RE: Multi Query Group BY</title><link>http://www.sqlservercentral.com/Forums/Topic819884-145-1.aspx</link><description>* goes out to buy a Lotto ticket * :-D</description><pubDate>Tue, 17 Nov 2009 03:47:46 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Multi Query Group BY</title><link>http://www.sqlservercentral.com/Forums/Topic819884-145-1.aspx</link><description>Hi PaulThat's fantastic! .. worked a treat :)Many ThanksSean</description><pubDate>Tue, 17 Nov 2009 03:18:41 GMT</pubDate><dc:creator>sshalloe</dc:creator></item><item><title>RE: Multi Query Group BY</title><link>http://www.sqlservercentral.com/Forums/Topic819884-145-1.aspx</link><description>Sean,I had to make some guesses, but try this:[code]SELECT  supplier = ITM.poit_supplier,        local_orders = SUM(CASE WHEN ORD.pord_pondcode = 'Local' THEN ITM.poit_valord ELSE 0 END),        global_orders = SUM(CASE WHEN ORD.pord_pondcode &amp;lt;&amp;gt; 'Local' THEN ITM.poit_valord ELSE 0 END)FROM    poitem ITMJOIN    porder ORD        ON  ORD.pord_order = ITM.poit_order        AND ORD.pord_supplier = ITM.poit_supplierJOIN    plsupp SUP        ON  SUP.plsup_ndcode = ORD.pord_supndcodeWHERE   ITM.poit_qtydel &amp;lt; ITM.poit_qtyordGROUP   BY         ITM.poit_supplier,        SUP.plsup_minord;[/code]Paul</description><pubDate>Tue, 17 Nov 2009 03:09:53 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Multi Query Group BY</title><link>http://www.sqlservercentral.com/Forums/Topic819884-145-1.aspx</link><description>Thanks for that I think I understand. However, the resulting data needs to populate a 3 column table:Col 1 - poit_supplier (Supplier Code - to be output only once for each supplier in the result)Col 2 - sum(poit_valord) (Sum of ALL Orders for a supplier placed locally)Col 3 - sum(poit_valord) (Sum of ALL Orders for a supplier placed non-locally)I hope this clarifies what I'm trying to achieve? Any ideas greatly appreciatedThanksSean</description><pubDate>Tue, 17 Nov 2009 02:20:53 GMT</pubDate><dc:creator>sshalloe</dc:creator></item><item><title>RE: Multi Query Group BY</title><link>http://www.sqlservercentral.com/Forums/Topic819884-145-1.aspx</link><description>As the queries are the same barring the test for pord_pondcode the below would eb more efficient than two queries.Also you were grouping by plsup_minord but not selecting it out.[code="sql"]select case when pord_pondcode = 'Local' then 'Local' else 'Non-Local' end,sum(poit_valord),poit_supplierfrom poitem,porder,plsuppwhere poit_order = pord_orderAND poit_qtydel &amp;lt; poit_qtyordAND pord_supplier = poit_supplierAND plsup_ndcode = pord_supndcodeGROUP BY poit_supplier,plsup_minord,case when pord_pondcode = 'Local' then 'Local' else 'Non-Local' end[/code]</description><pubDate>Tue, 17 Nov 2009 02:07:37 GMT</pubDate><dc:creator>Dave Ballantyne</dc:creator></item><item><title>Multi Query Group BY</title><link>http://www.sqlservercentral.com/Forums/Topic819884-145-1.aspx</link><description>HiI'm fairly new to MS SQL and require some assistance if possible.I have two similar query statements one looking for sum of local orders and the other looking for sum of global orders. However, I need to combine the two queries into one the problem is each of the constructed queries require the Group By clause in order to out put the required detail. (see below)-- Query One select sum(poit_valord),poit_supplierfrom poitem,porder,plsuppwhere poit_order = pord_orderAND poit_qtydel &amp;lt; poit_qtyordAND pord_pondcode = 'Local'AND pord_supplier = poit_supplierAND plsup_ndcode = pord_supndcodeGROUP BY poit_supplier,plsup_minord -- Query Twoselect sum(poit_valord),poit_supplierfrom poitem,porder,plsuppwhere poit_order = pord_orderAND poit_qtydel &amp;lt; poit_qtyordAND pord_pondcode &amp;lt;&amp;gt; 'Local'AND pord_supplier = poit_supplierAND plsup_ndcode = pord_supndcodeGROUP BY poit_supplier,plsup_minordAny help would be very much appreciated.ThanksSean</description><pubDate>Tue, 17 Nov 2009 01:58:10 GMT</pubDate><dc:creator>sshalloe</dc:creator></item></channel></rss>