Difference between revisions of "Values and Subtotals"
| (3 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
<p>Get values and Subtotals in one shot.</p> | <p>Get values and Subtotals in one shot.</p> | ||
| − | <p>In this example you are shown how to obtain subtotals in your query, allowing you to easily see <code>SUM</code> results for different | + | <p>In this example you are shown how to obtain subtotals in your query, allowing you to easily see <code>SUM</code> results for different elements.</p> |
<p>Table 1 shows the result without the subtotals and Table 2 shows the result with subtotals.</p> | <p>Table 1 shows the result without the subtotals and Table 2 shows the result with subtotals.</p> | ||
<div class="ref_section"> | <div class="ref_section"> | ||
| Line 46: | Line 46: | ||
the price a <code>COALESCE</code> function is also used.</p> | the price a <code>COALESCE</code> function is also used.</p> | ||
</div> | </div> | ||
| − | <source lang='sql' class='def'>SELECT item, serialnumber, price FROM( | + | <source lang='sql' class='def'> |
| − | SELECT item, serialnumber, price FROM serial | + | SELECT item, serialnumber, price |
| − | UNION | + | FROM(SELECT item, serialnumber, price |
| − | SELECT item, NULL, SUM(price) | + | FROM serial |
| − | FROM serial | + | UNION |
| − | GROUP BY item ) t | + | SELECT item, NULL, SUM(price) |
| − | ORDER BY item, COALESCE(serialnumber,1E9)</source> | + | FROM serial |
| + | GROUP BY item ) t | ||
| + | ORDER BY item, | ||
| + | COALESCE(serialnumber,1E9)</source> | ||
<div class="ecomm e-mysql" style="display: none">Grand total can also be obtained through <code>ROLLUPS</code>: | <div class="ecomm e-mysql" style="display: none">Grand total can also be obtained through <code>ROLLUPS</code>: | ||
| − | SELECT item, serialnumber, SUM(price) | + | <pre>SELECT item, serialnumber, SUM(price) |
| − | FROM serial | + | FROM serial |
| − | GROUP BY item, serialnumber WITH ROLLUP</div> | + | GROUP BY item, serialnumber WITH ROLLUP</pre></div> |
<div class="ecomm e-sqlserver" style="display: none">Grand total can also be obtained through <code>ROLLUPS</code>: | <div class="ecomm e-sqlserver" style="display: none">Grand total can also be obtained through <code>ROLLUPS</code>: | ||
| − | SELECT item, serialnumber, SUM(price) | + | <pre>SELECT item, serialnumber, SUM(price) |
| − | FROM serial | + | FROM serial |
| − | GROUP BY item, serialnumber WITH ROLLUP</div> | + | GROUP BY item, serialnumber WITH ROLLUP</pre></div> |
<div class="ecomm e-oracle" style="display: none">Grand total can be obtained through <code>GROUPING SETS</code>: | <div class="ecomm e-oracle" style="display: none">Grand total can be obtained through <code>GROUPING SETS</code>: | ||
| − | SELECT item, serialnumber, SUM(price) | + | <pre>SELECT item, serialnumber, SUM(price) |
| − | FROM serial | + | FROM serial |
| − | GROUP BY GROUPING SETS ((item,serialnumber),(item),())</div> | + | GROUP BY GROUPING SETS ((item,serialnumber),(item),())</pre></div> |
<div class="ecomm e-db2" style="display: none">Both oracle and MySQL methods work for DB2</div> | <div class="ecomm e-db2" style="display: none">Both oracle and MySQL methods work for DB2</div> | ||
</div> | </div> | ||
{{Hacks Ref}} | {{Hacks Ref}} | ||
Latest revision as of 15:10, 2 August 2012
Get values and Subtotals in one shot.
In this example you are shown how to obtain subtotals in your query, allowing you to easily see SUM results for different elements.
Table 1 shows the result without the subtotals and Table 2 shows the result with subtotals.
| item | serialnumber | price |
|---|---|---|
| Awl | 1 | 10 |
| Awl | 3 | 10 |
| Bowl | 2 | 10 |
| Bowl | 5 | 10 |
| Bowl | 6 | 10 |
| Cowl | 4 | 10 |
| item | serialnumber | price |
|---|---|---|
| Awl | 1 | 10 |
| Awl | 3 | 10 |
| Awl | 20 | |
| Bowl | 2 | 10 |
| Bowl | 5 | 10 |
| Bowl | 6 | 10 |
| Bowl | 30 | |
| Cowl | 4 | 10 |
| Cowl | 10 |
DROP TABLE serial
CREATE TABLE serial( item VARCHAR(20), serialnumber INTEGER, price INTEGER ); INSERT INTO serial VALUES ('Awl',1,10); INSERT INTO serial VALUES ('Awl',3,10); INSERT INTO serial VALUES ('Bowl',2,10); INSERT INTO serial VALUES ('Bowl',5,10); INSERT INTO serial VALUES ('Bowl',6,10); INSERT INTO serial VALUES ('Cowl',4,10);
In this example a UNION is used to make the query show the subtotal
results along with the price results and to ensure the subtotals come after
the price a COALESCE function is also used.
SELECT item, serialnumber, price FROM(SELECT item, serialnumber, price FROM serial UNION SELECT item, NULL, SUM(price) FROM serial GROUP BY item ) t ORDER BY item, COALESCE(serialnumber,1E9)
Hack 10 Converting subqueries into joins
Hack 11 Converting aggregate subqueries into joins
Hack 16 Search for a String across columns
Hack 24 Multiply Across a Result Set
Hack 25.5 Splitting and combining columns
Hack 26 Include the rows your JOIN forgot
Hack 30 Calculate the maximum/minimum of two fields
Hack 33 Get values and subtotals in one shot
Hack 50 Combine tables containing different data
Hack 51/52 Display rows as columns
Hack 55 Import Someone Else's Data
Hack 62 Issue Queries Without Using a Table
Hack 63 Generate rows without tables
Hack 72 Extract a subset of the results
Hack 78 Break it down by Range
Hack 88 Test two values from a subquery