|
Question : Only select results with multiple columns that are > 0
|
|
The query combines 20 Product codes for All customers. After it is all done I want to make sure I only have the customers with somthing greater than zero
I was trying to count(*) as cnt but it sums into 1 line. Should I add up all the columns as total then check for > 0 ? otherwise I get almost 14,000 records...
Thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
|
Select cCustMarketingCodesALL.cmcust, cCustMarketingCodesALL.cmname, cCustSalesMan.smno, cCustSalesMan.smname, ;
NVL(c13603.Park_Ave_2009_Total_Sales, 00000000.00) as Park_Ave_2009_Total_Sales, NVL(c13604.Bellagio_Unback_2009_Total_Sales, 00000000.00) as Bellagio_Unback_2009_Total_Sales, ;
NVL(c13605.Palisades_2009_Total_Sales, 00000000.00) as Palisades_2009_Total_Sales, NVL(c13607.Bellagio_Backed_2009_Total_Sales, 00000000.00) as Bellagio_Backed_2009_Total_Sale,;
NVL(c22653.Bamboo_3_1_2_HS_2009_Total_Sales, 00000000.00) as Bamboo_3_1_2_HS_2009_Total_Sales, NVL(c22659.Johnson_Lumb_G1_2009_Total_Sales, 00000000.00) as Johnson_Lumb_G1_2009_Total_Sales, ;
NVL(c22660.Johnson_Lumb_G2_2009_Total_Sales, 000000000.00) as Johnson_Lumb_G2_2009_Total_Sales , NVL(c22661.Johnson_Lumb_G3_2009_Total_Sales, 00000000.00) as Johnson_Lumb_G3_2009_Total_Sales, ;
NVL(c22680.Grand_Mesa_2009_Total_Sales, 00000000.00) as Grand_Mesa_2009_Total_Sales, NVL(c82113.TAS_LVT_Comml_2009_Total_Sales, 00000000.00) as TAS_LVT_Comml_2009_Total_Sales, ;
NVL(c82119.TAS_LVT_Premium_2009_Total_Sales, 00000000.00) as TAS_LVT_Premium_2009_Total_Sales, NVL(c82121.TAS_LVT_ProFloor_2009_Total_Sales, 00000000.00) as LVT_ProFloor_2009_Total_Sales, ;
NVL(c82140.Titan_Grand_Plank_2009_Total_Sales, 00000000.00) as Titan_Grand_Plank_2009_Total_Sales, NVL(c83041.Congress_Oak_3_1_4_2009_Total_Sales, 00000000.00) as Congress_Oak_3_1_4_2009_Total_Sales, ;
NVL(c33500.Paratimber_2009_Total_Sales, 00000000.00) as Paratimber_2009_Total_Sales, NVL(c22654.Bamboo_5_HS_2009_Total_Sales, 00000000.00) as Bamboo_5_HS_2009_Total_Sales, ;
NVL(c13602.Manhattan_2009_Total_Sales, 00000000.00) as Manhattan_2009_Total_Sales, NVL(c2490.Prime_Supply_Oak_2009_Total_Sales, 00000000.00) as Prime_Supply_Oak_2009_Total_Sales, ;
NVL(c22652.Strand_Bamboo_2009_Total_Sales, 00000000.00) as Strand_Bamboo_2009_Total_Sales ;
from (((((((((((((((((((cCustSalesMan ;
left Join c13602 On c13602.cpcust = cCustSalesMan.cmcust) ;
left Join c13603 On c13603.cpcust = cCustSalesMan.cmcust) ;
left Join c13604 On c13604.cpcust = cCustSalesMan.cmcust) ;
left Join c13605 On c13605.cpcust = cCustSalesMan.cmcust) ;
left Join c13607 On c13607.cpcust = cCustSalesMan.cmcust) ;
left Join c22652 On c22652.cpcust = cCustSalesMan.cmcust) ;
left Join c22653 On c22653.cpcust = cCustSalesMan.cmcust) ;
left Join c22654 On c22654.cpcust = cCustSalesMan.cmcust) ;
left Join c22659 On c22659.cpcust = cCustSalesMan.cmcust) ;
left Join c22660 On c22660.cpcust = cCustSalesMan.cmcust) ;
left Join c22661 On c22661.cpcust = cCustSalesMan.cmcust) ;
left Join c22680 On c22680.cpcust = cCustSalesMan.cmcust) ;
left Join c2490 On c2490.cpcust = cCustSalesMan.cmcust) ;
left Join c33500 On c33500.cpcust = cCustSalesMan.cmcust) ;
left Join c82113 On c82113.cpcust = cCustSalesMan.cmcust) ;
left Join c82119 On c82119.cpcust = cCustSalesMan.cmcust) ;
left Join c82121 On c82121.cpcust = cCustSalesMan.cmcust) ;
left Join c82140 On c82140.cpcust = cCustSalesMan.cmcust) ;
left Join c83041 On c83041.cpcust = cCustSalesMan.cmcust) ;
inner Join cCustMarketingCodesALL On cCustSalesMan.cmcust=cCustMarketingCodesALL.cmcust Into Cursor cALL_customers Readwrite
select *, ;
COUNT(*) as cnt from cALL_customers into cursor cALL_customers2 readwrite
BROWSE
|
|
|
Answer : Only select results with multiple columns that are > 0
|
|
To calculate the sum of all columns seems to be the only solution when you need the reduced number of rows in query result:
WHERE NVL(c13603.Park_Ave_2009_Total_Sales, 0) + NVL(c13604.Bellagio_Unback_2009_Total_Sales, 0) + ... > 0
Another option could be to create a list of "non-zero" customers:
SELECT cpcust FROM c13602 ; UNION ; SELECT cpcust FROM c13603 ; UNION ; SELECT cpcust FROM c13604 ; UNION ; ...... etc. INTO CURSOR cNonZeroCust
and then you may add this cursor to your query (hope it will work):
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
|
Select cCustMarketingCodesALL.cmcust, cCustMarketingCodesALL.cmname, cCustSalesMan.smno, cCustSalesMan.smname, ;
NVL(c13603.Park_Ave_2009_Total_Sales, 00000000.00) as Park_Ave_2009_Total_Sales, NVL(c13604.Bellagio_Unback_2009_Total_Sales, 00000000.00) as Bellagio_Unback_2009_Total_Sales, ;
NVL(c13605.Palisades_2009_Total_Sales, 00000000.00) as Palisades_2009_Total_Sales, NVL(c13607.Bellagio_Backed_2009_Total_Sales, 00000000.00) as Bellagio_Backed_2009_Total_Sale,;
NVL(c22653.Bamboo_3_1_2_HS_2009_Total_Sales, 00000000.00) as Bamboo_3_1_2_HS_2009_Total_Sales, NVL(c22659.Johnson_Lumb_G1_2009_Total_Sales, 00000000.00) as Johnson_Lumb_G1_2009_Total_Sales, ;
NVL(c22660.Johnson_Lumb_G2_2009_Total_Sales, 000000000.00) as Johnson_Lumb_G2_2009_Total_Sales , NVL(c22661.Johnson_Lumb_G3_2009_Total_Sales, 00000000.00) as Johnson_Lumb_G3_2009_Total_Sales, ;
NVL(c22680.Grand_Mesa_2009_Total_Sales, 00000000.00) as Grand_Mesa_2009_Total_Sales, NVL(c82113.TAS_LVT_Comml_2009_Total_Sales, 00000000.00) as TAS_LVT_Comml_2009_Total_Sales, ;
NVL(c82119.TAS_LVT_Premium_2009_Total_Sales, 00000000.00) as TAS_LVT_Premium_2009_Total_Sales, NVL(c82121.TAS_LVT_ProFloor_2009_Total_Sales, 00000000.00) as LVT_ProFloor_2009_Total_Sales, ;
NVL(c82140.Titan_Grand_Plank_2009_Total_Sales, 00000000.00) as Titan_Grand_Plank_2009_Total_Sales, NVL(c83041.Congress_Oak_3_1_4_2009_Total_Sales, 00000000.00) as Congress_Oak_3_1_4_2009_Total_Sales, ;
NVL(c33500.Paratimber_2009_Total_Sales, 00000000.00) as Paratimber_2009_Total_Sales, NVL(c22654.Bamboo_5_HS_2009_Total_Sales, 00000000.00) as Bamboo_5_HS_2009_Total_Sales, ;
NVL(c13602.Manhattan_2009_Total_Sales, 00000000.00) as Manhattan_2009_Total_Sales, NVL(c2490.Prime_Supply_Oak_2009_Total_Sales, 00000000.00) as Prime_Supply_Oak_2009_Total_Sales, ;
NVL(c22652.Strand_Bamboo_2009_Total_Sales, 00000000.00) as Strand_Bamboo_2009_Total_Sales ;
from (((((((((((((((((((cCustSalesMan ;
left Join c13602 On c13602.cpcust = cCustSalesMan.cmcust) ;
left Join c13603 On c13603.cpcust = cCustSalesMan.cmcust) ;
left Join c13604 On c13604.cpcust = cCustSalesMan.cmcust) ;
left Join c13605 On c13605.cpcust = cCustSalesMan.cmcust) ;
left Join c13607 On c13607.cpcust = cCustSalesMan.cmcust) ;
left Join c22652 On c22652.cpcust = cCustSalesMan.cmcust) ;
left Join c22653 On c22653.cpcust = cCustSalesMan.cmcust) ;
left Join c22654 On c22654.cpcust = cCustSalesMan.cmcust) ;
left Join c22659 On c22659.cpcust = cCustSalesMan.cmcust) ;
left Join c22660 On c22660.cpcust = cCustSalesMan.cmcust) ;
left Join c22661 On c22661.cpcust = cCustSalesMan.cmcust) ;
left Join c22680 On c22680.cpcust = cCustSalesMan.cmcust) ;
left Join c2490 On c2490.cpcust = cCustSalesMan.cmcust) ;
left Join c33500 On c33500.cpcust = cCustSalesMan.cmcust) ;
left Join c82113 On c82113.cpcust = cCustSalesMan.cmcust) ;
left Join c82119 On c82119.cpcust = cCustSalesMan.cmcust) ;
left Join c82121 On c82121.cpcust = cCustSalesMan.cmcust) ;
left Join c82140 On c82140.cpcust = cCustSalesMan.cmcust) ;
left Join c83041 On c83041.cpcust = cCustSalesMan.cmcust) ;
inner Join cCustMarketingCodesALL On cCustSalesMan.cmcust=cCustMarketingCodesALL.cmcust ;
inner Join cNonZeroCust ON cNonZeroCust.cpcust = cCustSalesMan.cmcust ;
Into Cursor cALL_customers Readwrite
|
|
|
|
|