Create a single query that repeats the rows of one table and multiplies fields according to another

sheetspread

Well-known Member
Joined
Sep 19, 2005
Messages
5,161
Right now I can only do this by creating numerous queries, but one neat simple format like below would help tremendously. Here's the first table:

Excel Workbook
ABCDEFGHIJKLMN
1NameTypeJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2BenningtonOrange2984875886275389671241
3CarneyGreen67571012774773217046330
4Council HillYellow1547170637794715022139
5FairmontOrange5398959317083763396076
6FoyilBlue7255451120027545642992
7HallettWhite15269184580963566631472
8HeadrickYellow2995918221708036904788
9IndiahomaYellow1753798971648365582239
10LambertWhite38825342086614791668034
11Le FloreGreen493558546982577964858
12MaysvilleWhite72959235616036983063564
13MulhallBlue7824471927287315487040
14MuskogeeBlue8463951615532939494452
15Oak GroveYellow36309975195859447127629
16OlusteeOrange9690428110769575111016
17QuapawBlue672297481566992813856910
18StidhamYellow548681427539486293269927
19TerltonOrange5991742287829459627971
20WanetteGreen193375833849614247490
DataTable


The allocation is in this table:

Excel Workbook
ABCDEF
1TypeNorthSouthEastWestCentral
2Orange0.410.210.3800
3Yellow0.0600.210.270.46
4Green0.140.360.220.150.13
5Blue0.160.330.050.40.06
6White0.310.130.1600.4
PercentsTable


And this is exactly how I'd like the query to appear:

Excel Workbook
ABCDEFGHIJKLMN
1NameSubTypeJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
2BenningtonNorth0.8240.1819.6830.7536.0825.4230.7515.5839.3629.119.840.41
3BenningtonSouth0.4220.5810.0815.7518.4813.0215.757.9820.1614.915.040.21
4BenningtonEast0.7637.2418.2428.533.4423.5628.514.4436.4826.989.120.38
5CarneyNorth9.387.981.41.6810.786.5810.222.949.86.444.620
6CarneySouth24.1220.523.64.3227.7216.9226.287.5625.216.5611.880
7CarneyEast14.7412.542.22.6416.9410.3416.064.6215.410.127.260
8CarneyWest10.058.551.51.811.557.0510.953.1510.56.94.950
9CarneyCentral8.717.411.31.5610.016.119.492.739.15.984.290
10Council HillNorth0.063.244.264.23.784.625.644.2631.320.062.34
11Council HillEast0.2111.3414.9114.713.2316.1719.7414.9110.54.620.218.19
12Council HillWest0.2714.5819.1718.917.0120.7925.3819.1713.55.940.2710.53
13Council HillCentral0.4624.8432.6632.228.9835.4243.2432.662310.120.4617.94
14FairmontNorth21.7340.1838.953.6912.7128.734.0331.161.2315.9924.631.16
15FairmontSouth11.1320.5819.951.896.5114.717.4315.960.638.1912.615.96
16FairmontEast20.1437.2436.13.4211.7826.631.5428.881.1414.8222.828.88
17FoyilNorth11.528.87.21.763.200.32127.210.244.6414.72
18FoyilSouth23.7618.1514.853.636.600.6624.7514.8521.129.5730.36
19FoyilEast3.62.752.250.55100.13.752.253.21.454.6
20FoyilWest28.822184.4800.8301825.611.636.8
21FoyilCentral4.323.32.70.661.200.124.52.73.841.745.52
22HallettNorth4.658.062.795.5813.9524.829.7610.8520.4619.534.3422.32
23HallettSouth1.953.381.172.345.8510.412.484.558.588.191.829.36
24HallettEast2.44.161.442.887.212.815.365.610.5610.082.2411.52
25HallettCentral610.43.67.2183238.41426.425.25.628.8
26HeadrickNorth0.125.943.540.064.921.264.24.82.165.42.825.28
27HeadrickEast0.4220.7912.390.2117.224.4114.716.87.5618.99.8718.48
28HeadrickWest0.5426.7315.930.2722.145.6718.921.69.7224.312.6923.76
29HeadrickCentral0.9245.5427.140.4637.729.6632.236.816.5641.421.6240.48
30IndiahomaNorth0.064.50.184.745.344.263.844.983.93.481.322.34
31IndiahomaEast0.2115.750.6316.5918.6914.9113.4417.4313.6512.184.628.19
32IndiahomaWest0.2720.250.8121.3324.0319.1717.2822.4117.5515.665.9410.53
33IndiahomaCentral0.4634.51.3836.3440.9432.6629.4438.1829.926.6810.1217.94
34LambertNorth11.7825.4216.431.246.226.6618.9114.5728.2120.4624.810.54
35LambertSouth4.9410.666.890.522.611.187.936.1111.838.5810.44.42
SingleQuery


cont'd

Excel Workbook
ABCDEFGHIJKLMN
36LambertEast6.0813.128.480.643.213.769.767.5214.5610.5612.85.44
37LambertCentral15.232.821.21.6834.424.418.836.426.43213.6
38Le FloreNorth6.860.427.711.96.4413.723.510.7813.440.5611.91.12
39Le FloreSouth17.641.0819.830.616.5635.28927.7234.561.4430.62.88
40Le FloreEast10.780.6612.118.710.1221.565.516.9421.120.8818.71.76
41Le FloreWest7.350.458.2512.756.914.73.7511.5514.40.612.751.2
42Le FloreCentral6.370.397.1511.055.9812.743.2510.0112.480.5211.051.04
43MaysvilleNorth22.3229.4528.5210.8518.9118.611.1630.389.319.5317.361.24
44MaysvilleSouth9.3612.3511.964.557.937.84.6812.743.98.197.280.52
45MaysvilleEast11.5215.214.725.69.769.65.7615.684.810.088.960.64
46MaysvilleCentral28.83836.81424.42414.439.21225.222.41.6
47MulhallNorth12.483.840.6411.3614.7211.521.2811.682.47.6811.26.4
48MulhallSouth25.747.921.3223.4330.3623.762.6424.094.9515.8423.113.2
49MulhallEast3.91.20.23.554.63.60.43.650.752.43.52
50MulhallWest31.29.61.628.436.828.83.229.2619.22816
51MulhallCentral4.681.440.244.265.524.320.484.380.92.884.22.4
52MuskogeeNorth13.4410.0815.22.562.40.85.1214.8815.041.447.048.32
53MuskogeeSouth27.7220.7931.355.284.951.6510.5630.6931.022.9714.5217.16
54MuskogeeEast4.23.154.750.80.750.251.64.654.70.452.22.6
55MuskogeeWest33.625.2386.46212.837.237.63.617.620.8
56MuskogeeCentral5.043.785.70.960.90.31.925.585.640.542.643.12
57Oak GroveNorth2.161.80.545.823.065.75.15.642.820.724.561.74
58Oak GroveEast7.566.31.8920.3710.7119.9517.8519.749.872.5215.966.09
59Oak GroveWest9.728.12.4326.1913.7725.6522.9525.3812.693.2420.527.83
60Oak GroveCentral16.5613.84.1444.6223.4643.739.143.2421.625.5234.9613.34
61OlusteeNorth39.3636.917.223.284.51031.1638.9530.754.514.16.56
62OlusteeSouth20.1618.98.821.682.31015.9619.9515.752.312.13.36
63OlusteeEast36.4834.215.963.044.18028.8836.128.54.183.86.08
64QuapawNorth10.723.5215.527.682.410.5615.844.482.0813.611.041.6
65QuapawSouth22.117.2632.0115.844.9521.7832.679.244.2928.0522.773.3
66QuapawEast3.351.14.852.40.753.34.951.40.654.253.450.5
67QuapawWest26.88.838.819.2626.439.611.25.23427.64
68QuapawCentral4.021.325.822.880.93.965.941.680.785.14.140.6
69StidhamNorth3.245.164.862.524.52.342.883.725.581.565.941.62
70StidhamEast11.3418.0617.018.8215.758.1910.0813.0219.535.4620.795.67
SingleQuery


cont'd

Excel Workbook
ABCDEFGHIJKLMN
71StidhamWest14.5823.2221.8711.3420.2510.5312.9616.7425.117.0226.737.29
72StidhamCentral24.8439.5637.2619.3234.517.9422.0828.5242.7811.9645.5412.42
73TerltonNorth2.0540.596.9717.220.8235.6733.6238.5424.1925.4232.3929.11
74TerltonSouth1.0520.793.578.820.4218.2717.2219.7412.3913.0216.5914.91
75TerltonEast1.937.626.4615.960.7633.0631.1635.7222.4223.5630.0226.98
76WanetteNorth2.664.6210.51.124.6211.7613.441.963.360.980.5612.6
77WanetteSouth6.8411.88272.8811.8830.2434.565.048.642.521.4432.4
78WanetteEast4.187.2616.51.767.2618.4821.123.085.281.540.8819.8
79WanetteWest2.854.9511.251.24.9512.614.42.13.61.050.613.5
80WanetteCentral2.474.299.751.044.2910.9212.481.823.120.910.5211.7
SingleQuery



So each record in the original table is repeated as many times as its nonzero corresponding subtypes in the PercentsTable, with the monthly numbers multiplied by that percentage.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,

The data is poorly set up. This greatly complicates any work. The data should be set up differently.

The base tables are set up like reports - cross tab reports. Data should instead be stored in tables in normalised form. Then working with the data is easier.

So, the first table instead of "Name", "Type", "January", "February", etc

Could be "Name", "Type", "Month", "Value"

And the Percents table instead of "Type", "North", "South", etc

Could be "Type", "Location", "Percent"

Then the query could be something like
Code:
TRANSFORM SUM(D.Value * P.Percent)
SELECT DISTINCT D.Name, P.Location
FROM DataTable D, PercentsTable P
WHERE D.Type = P.Type AND P.Percent>0
GROUP BY D.Name, P.Location
PIVOT D.Month IN ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August','September', 'October', 'November', 'December')

This might be a little rough. It is just how I wrote it (in Excel, btw).

The only reason for the last line listing all the months is to get the order. I think setting things up better in the source tables would avoid that. Normally the line would just be "PIVOT D.Month"

Same sort of idea with your custom sort on the Location field. Define it in a source table and everything will be simpler.

If one were to absolutely insist on maintaining the poor structure, then the normalisation of the data could be included in the single query. This would be a really bad way to work. Change the "FROM table" to whatever you need. Like a UNION query on each current field.

SELECT Name, Type, 'January' AS [Month], January AS [Value]
FROM datatable
UNION
SELECT Name, Type, 'February' AS [Month], February AS [Value]
FROM datatable
etc etc to December

Same sort of thing for the other table. To be clear, this would never be a recommended approach.

HTH
 
Upvote 0
Ah Fazza, thanks for replying. Yes, ideally there shouldn't be month fields to start with but the input data are reports themselves, in that format. So is this how the SQL should look?


Code:
SELECT Name, Type, 'January' AS [Month], January AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'February' AS [Month], February AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'March' AS [Month], March AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'April' AS [Month], April AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'May' AS [Month], May AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'June' AS [Month], June AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'July' AS [Month], July AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'August' AS [Month], August AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'September' AS [Month], September AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'October' AS [Month], October AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'November' AS [Month], November AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'December' AS [Month], December AS [Value]

SELECT Name, Type, 'North' AS [SubType], North AS [Value]
FROM PercentsTable
UNION
SELECT Name, Type, 'South' AS [SubType], South AS [Value]
FROM PercentsTable
UNION
SELECT Name, Type, 'East' AS [SubType], East AS [Value]
FROM PercentsTable
UNION
SELECT Name, Type, 'West' AS [SubType], West AS [Value]
FROM PercentsTable
UNION
SELECT Name, Type, 'Central' AS [SubType], Central AS [Value]
FROM PercentsTable
UNION

This gives me a syntax error, incomplete query clause. Do I need one last bit or is the Transform.....Pivot you have written above (or some variation) needed as well?
 
Upvote 0
for one query, the whole query would be like I wrote above except the line

FROM DataTable D, PercentsTable P

would be replaced by

FROM (SELECT etc etc) D, (SELECT etc etc) P

The new bits need to be complete within themselves so you can test them separately to get them correct.

Syntax as I wrote previously. That is,

SELECT first one
FROM table

UNION
SELECT second one
FROM table

UNION
SELECT third one
FROM table

etc etc

cheers
 
Upvote 0
I hope this is closer, but there's a "syntax error in from clause" pointing to (in bold):


Rich (BB code):
TRANSFORM Sum(D.Value*P.Fraction) AS Expr1
SELECT D.Name, P.SubType AS Expr2
FROM  SELECT Name, Type, 'January' AS [Month], January AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'February' AS [Month], February AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'March' AS [Month], March AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'April' AS [Month], April AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'May' AS [Month], May AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'June' AS [Month], June AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'July' AS [Month], July AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'August' AS [Month], August AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'September' AS [Month], September AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'October' AS [Month], October AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'November' AS [Month], November AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'December' AS [Month], December AS [Value]
FROM DataTable]. AS D, [SELECT Type, 'North' AS [SubType], North AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'South' AS [SubType], South AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'East' AS [SubType], East AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'West' AS [SubType], West AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'Central' AS [SubType], Central AS [Fraction]
FROM PercentsTable]. AS P
WHERE (((D.Type)=[P].[Type]) AND (([P].[Fraction])>0))
GROUP BY D.Name, P.SubType
PIVOT D.Month In ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August','September', 'October', 'November', 'December');
 
Upvote 0
After further tinkering the syntax error is further down (a good sign?), in the join operation:

Rich (BB code):
TRANSFORM Sum(D.Value*P.Fraction) AS Expr1
SELECT D.Name, P.Subtype AS Expr2
FROM (SELECT Name, Type, 'January' AS [Month], January AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'February' AS [Month], February AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'March' AS [Month], March AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'April' AS [Month], April AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'May' AS [Month], May AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'June' AS [Month], June AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'July' AS [Month], July AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'August' AS [Month], August AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'September' AS [Month], September AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'October' AS [Month], October AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'November' AS [Month], November AS [Value]
FROM DataTable
UNION
SELECT Name, Type, 'December' AS [Month], December AS [Value]
FROM DataTable]) AS D, ([SELECT Type, 'North' AS [SubType], North AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'South' AS [SubType], South AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'East' AS [SubType], East AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'West' AS [SubType], West AS [Fraction]
FROM PercentsTable
UNION
SELECT Type, 'Central' AS [SubType], Central AS [Fraction]
FROM PercentsTable]) AS P
WHERE (((D.Type)=[P].[Type]) AND (([P].[Fraction])>0))
GROUP BY D.Name, P.SubType
PIVOT D.Month In ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August','September', 'October', 'November', 'December');


I did test the two segments like you said, and they seem to be ok apart.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top