Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 23rd, 2006, 07:44 PM
Authorized User
 
Join Date: Sep 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default using union all syntax with sum function



I have the following fields in Table A running in MS SQL Server 2000:

MAJOR| MINOR| PRODMGR|CST_USD |REV_USD|LCTRYNUM |AMT_TYPE
================================================== ==========
538 | 1616 | LN |0 |250 |834 | I
538 | 1641 | OT |0 |300 |834 | I
548 | 1616 | LN |100 |0 |834 | I
548 | 1641 | OT |120 |0 |834 | I
400 | 0100 | LV |50 |0 |888 | D
402 | 0200 | LO |80 |0 |888 | D
404 | 0110 | LJ |30 |0 |333 | J

I would like to query these fields so that i can have the below result:

              |616 | 641 |
============================
Gross Profit |150 | 180

PRODMGR |616 | 641
===========================
LN |150 |--> (250-100)
OT | 0 | 180--> (300-120}

My query is as follows:

SELECT
SUM(REV_USD)-SUM(CST_USD) AS [616]
FROM Table A
WHERE LCTRYNUM='834' AND MAJOR IN ('538','548') AND
STR(MINOR,2,3)='616' AND AMT_TYPE='I'
UNION ALL
SELECT
SUM(REV_USD)-SUM(CST_USD) AS [641]
FROM Table A
WHERE LCTRYNUM='834' AND MAJOR IN ('538','548') AND
STR(MINOR,2,3)='641' AND AMT_TYPE='I'
UNION ALL
SELECT DISTINCT PRODMGR,
(SELECT SUM(REV_USD)-SUM(CST_USD)
 FROM Table A
 WHERE LCTRYNUM='834' AND MAJOR IN ('538','548') AND
 STR(MINOR,2,3)='616' AND AMT_TYPE='I'AND PRODMGR=MAIN.PRODMGR) AS [616],
(SELECT SUM(REV_USD)-SUM(CST_USD)
 FROM Table A
 WHERE LCTRYNUM='834' AND MAJOR IN ('538','548') AND
 STR(MINOR,2,3)='641' AND AMT_TYPE='I'AND PRODMGR=MAIN.PRODMGR) AS [641]
FROM Table A AS MAIN

I can't seem to get the result i want, can anyone help?









 
Old July 24th, 2006, 03:42 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT 'Gross profit' 'ProdMgr',
            SUM(CASE WHEN RIGHT(MINOR, 3) = '616' THEN REV_USD - CST_USD ELSE 0 END) '616',
            SUM(CASE WHEN RIGHT(MINOR, 3) = '641' THEN REV_USD - CST_USD ELSE 0 END) '641'
FROM [table A]
WHERE LCTRYNUM = '834'
            AND MAJOR IN ('538', '548')
            AND AMT_TYPE = 'I'
UNION ALL

SELECT PRODMGR,
        SUM(CASE WHEN RIGHT(MINOR, 3) = '616' THEN REV_USD - CST_USD ELSE 0 END) '616',
        SUM(CASE WHEN RIGHT(MINOR, 3) = '641' THEN REV_USD - CST_USD ELSE 0 END) '641'
FROM [table A]
WHERE LCTRYNUM = '834'
        AND MAJOR IN ('538', '548')
        AND AMT_TYPE = 'I'
GROUP BY PRODMGR
ORDER BY PRODMGR
 
Old July 24th, 2006, 06:40 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

See also

http://www.sqlservercentral.com/foru...ssageid=296604
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=69501



 
Old July 27th, 2006, 02:31 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

for complex queries like this you may want to select the desired data into a temp table then sum the temp tables data.






Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax of sum in SQL heghtera SQL Language 1 March 13th, 2006 09:23 AM
syntax of sum in SQL heghtera Access VBA 1 March 10th, 2006 03:17 AM
SELECT SUM syntax... Anubis Access VBA 8 December 15th, 2003 08:04 PM
SUM Function jmss66 Classic ASP Basics 17 July 29th, 2003 08:00 AM
Need Help with the Sum Function athanatos XSLT 1 July 22nd, 2003 10:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.