| Author |
Topic |
|
php95saj
Starting Member
43 Posts |
Posted - 2002-05-24 : 10:59:12
|
| OK I have this table:tbl_MinimumChargesserviceLevelID tinyint, currencyID tinyint, customerID int, minCharge decimal(9,2)the values I have areserviceLevelID currencyID customerID minCharge 3 1 0 49.902 1 0 17.951 1 0 1.001 1 12 0.992 1 12 16.50The values where custmerID is 0 are the default values. In this scenario I want to select charges for three service levels for a given currency. I want to select charges for a customer. If there is no charge for a service level for that customer, I want to use the default value. Is it possible in one query?what I have done is to create a tempTable and drop values into it and then select values from the tempTable. |
|
|
php95saj
Starting Member
43 Posts |
Posted - 2002-05-24 : 11:20:46
|
| OK 9 people have read this post and no reply. May be it is not clear enough.Here is my tableserviceLevelID currencyID customerID minCharge 3 1 0 49.902 1 0 17.951 1 0 1.001 1 12 0.992 1 12 16.50Here is my query that works fine. I just want to do it in a slightly neater way:DECLARE @step intSET @step = 3 DECLARE @customerID intSET @customerID = 12 drop table #tempMinChargecreate table #tempMinCharge(serviceLevelID tinyint, currencyID tinyint, customerID int, minCharge decimal(9,2))WHILE @step >= 1 BEGIN IF EXISTS(SELECT minCharge FROM tbl_Costs_MinimumCharges WHERE customerID = @customerID AND serviceLevelID = @step) BEGININSERT INTO #tempMinCharge SELECT serviceLevelID, currencyID, customerID, minCharge FROM tbl_Costs_MinimumCharges WHERE customerID = @customerID AND serviceLevelID = @step AND currencyID = 1 ORDER BY customerIDEND ELSE BEGININSERT INTO #tempMinCharge SELECT serviceLevelID, currencyID, customerID, minCharge FROM tbl_Costs_MinimumCharges WHERE customerID = 0 AND serviceLevelID = @step AND currencyID = 1 ORDER BY customerID ENDSET @step = @step - 1 ENDSELECT serviceLevelID, currencyID, customerID, minCharge FROM #tempMinCharge order by serviceLevelID DESCPlease SQL gurus save me on Friday afternoon.TIASharjeel |
 |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2002-05-24 : 11:35:20
|
| saved ...select *from #temp twhere exists ( select 1 from #temp where servicelevelID = t.servicelevelID having max(customerID) = t.customerID )order by serviceLevelID desc where #temp is tbl_costs_minimumchargessetBasedIsTheTruepath<O> |
 |
|
|
php95saj
Starting Member
43 Posts |
Posted - 2002-05-24 : 11:43:41
|
| Not quite with you at this point. Can you explain a bit what is happenning?select *from #temp twhere exists ( select 1 from #temp where servicelevelID = t.servicelevelID having max(customerID) = t.customerID )order by serviceLevelID desc where #temp is tbl_costs_minimumchargessetBasedIsTheTruepath<O>[/quote] |
 |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2002-05-24 : 11:48:08
|
well, at least tell me you think it will work for you or not ... setBasedIsTheTruepath<O> |
 |
|
|
php95saj
Starting Member
43 Posts |
Posted - 2002-05-24 : 11:51:00
|
How can I tell you if I cann't figure out what is happennign out there. well, at least tell me you think it will work for you or not ... setBasedIsTheTruepath<O>[/quote] |
 |
|
|
php95saj
Starting Member
43 Posts |
Posted - 2002-05-24 : 12:04:36
|
Ok I have figured out what your point was but it is not the one that I want: This is what your query will returnserviceLevelID currencyID customerID minCharge -------------- ---------- ----------- -------------------- 3 1 12 49.902 1 12 17.951 1 10 5.00while I want something (for a customerID=12)serviceLevelID currencyID customerID minCharge -------------- ---------- ----------- -------------------- 3 1 12 49.902 1 12 17.951 1 0 1.00while my original table is:serviceLevelID currencyID customerID minCharge -------------- ---------- ----------- -------------------- 1 1 0 1.001 2 0 .691 3 0 1.142 1 0 19.952 2 0 13.802 3 0 22.693 1 0 49.953 2 0 34.553 3 0 56.823 1 5 49.003 2 5 34.003 3 5 56.001 1 10 5.001 2 10 3.001 3 10 6.002 1 10 15.002 2 10 10.002 3 10 16.003 1 10 49.953 2 10 34.563 3 10 56.822 1 12 17.952 2 12 12.802 3 12 20.693 1 12 49.903 2 12 34.653 3 12 56.56quote: How can I tell you if I cann't figure out what is happennign out there. well, at least tell me you think it will work for you or not ... setBasedIsTheTruepath<O>
[/quote] |
 |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2002-05-24 : 13:10:02
|
| In that case you can modify my query slightly to add a "customerID = @customerID" and it should be what you want.setBasedIsTheTruepath<O> |
 |
|
|
drsloat
Starting Member
45 Posts |
Posted - 2002-05-24 : 13:39:53
|
| I hard coded in the customerID=12 and currencyID=1 but you can pass these as parameters Select t1.ServiceLevelID, t1.currencyID, IsNull(tm.CustomerID,0) as 'customerID', IsNull(tm.minCharge,t1.defaultMinCharge) as 'minCharge' From ( Select ServiceLevelID, 12 as 'CustomerID', minCharge as 'defaultMinCharge', currencyID From tbl_Costs_MinimumCharges Where customerID = 0 and currencyID=1 ) as t1 Left Outer Join tbl_Costs_MinimumCharges tm on (t1.ServiceLevelID = tm.ServiceLevelID and t1.CustomerID = tm.CustomerID and t1.currencyID = tm.currencyID) |
 |
|
|
|