I'm not sure if this is even possible, but here goes. Let's say I have a dataset containing some products and their prices, and I want to display a table that shows a product, its price and a "tax" column. The tax is simply some fraction multiplied by the price. Now, let's say I want to show the same table for several regions with different taxes. The region and tax information isn't available in a SQL table and must be hard-coded in the report. I could create a separate table for each region, of course, but ideally I'd like to use a group that simply repeats the same table several times, iterating over my hard-coded set. Is this possible?

有帮助吗?

解决方案

No loops. Put the region and tax info in a table variable in your dataset. Then do a cross join with your real data.

declare @RegionTax table (
    Region nvarchar(10),
    Tax float
)

insert @RegionTax values ('Region 1', .05)
insert @RegionTax values ('Region 2', .06)
insert @RegionTax values ('Region 3', .07)

select
    *
from
    Products p, @RegionTax t

其他提示

If the regions are in the dataset, you can add a computed column to the dataset. The computation would use some form of 'case' (can't remember the syntax - sorry!) to generate the right multiplier for the Region column and multiply the price by that.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top