Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Inserting data into temporary table
Tinwolf
<p>Hello there, probably not the right place to put this question but I didn`t know were else to put it.</p>
<p> </p>
<p>I`m trying to insert data into a temporary table from more than one select statement.</p>
<p>So I have 4 fields which can hold a month as a 3 character entry and another 4 fields which hold values for that month.</p>
<p>I want to get all the months together to view the data for a month, so the select statements might return this data:</p>
<p> </p>
<p>Area month1 val1 month2 val2 month3 val3 month4 val4</p>
<p> a JUN 10 JUL 5 AUG 15 SEP 11</p>
<p> b MAY 6 JUN 10 JUL 10 AUG 15</p>
<p> </p>
<p>I want to find out the data for the areas for the month of JUN and I was thinking of loading the data into a temporary table but how do I insert data from more than one select?</p>
<p>The first area goes in fine with insert # but how would I insert other data?</p>
Find more posts tagged with
Comments
gharley
<p>You could cartesian a list of areas with a list of hard-coded months and then use CASE statement to put the values from YOURTABLE in the right val# column and run a distinct over it.</p>
<p> </p>
<p>SELECT <strong>DISTINCT</strong></p>
<p>areas.AREA,</p>
<p>months.THEMONTH,</p>
<p>CASE WHEN (data.area = areas.area and data.month1 = months.THEMONTH) then (data.VAL1) END AS MONTH_VAL1</p>
<p>CASE WHEN (data.area = areas.area and data.month2 = months.THEMONTH) then (data.VAL2) END AS MONTH_VAL2</p>
<p>CASE WHEN (data.area = areas.area and data.month3 = months.THEMONTH) then (data.VAL3) END AS MONTH_VAL3</p>
<p>CASE WHEN (data.area = areas.area and data.month4 = months.THEMONTH) then (data.VAL4) END AS MONTH_VAL4</p>
<p>FROM</p>
<p>(SELECT DISTINCT AREA FROM yourtable) areas</p>
<p>JOIN (SELECT JAN AS THEMONTH FROM DUAL UNION SELECT FEB AS THEMONTH FROM DUAL...etc...) months ON (1=1) </p>
<p>JOIN (YOURTABLE) data ON (1=1)</p>
<p> </p>
<p>Not sure of the grain of the data or the DBMS to figure out the exact syntax but this might get you close.</p>
micajblock
<p>Can you provide sample data and sample output?</p>
Tinwolf
<p>Thanks for the responses guys.</p>
<p>This is the basic, raw data set I`ve written to get the data from a view I created in sql:</p>
<p>select<br>
bsp_quotemonths.id,<br>
bsp_quotemonths.quotename,<br>
bsp_quotemonths.traderid,<br>
bsp_quotemonths.arearep,<br>
bsp_quotemonths.sumnett,<br>
bsp_quotemonths.month1,<br>
bsp_quotemonths.pcnt1,<br>
bsp_quotemonths.month2,<br>
bsp_quotemonths.pcnt2,<br>
bsp_quotemonths.month3,<br>
bsp_quotemonths.pcnt3,<br>
bsp_quotemonths.month4,<br>
bsp_quotemonths.pcnt4<br><br>
from<br>
bsp_quotemonths<br><br>
where bsp_quotemonths.arearep = '699'<br>
and bsp_quotemonths.month1 = 'JUN'<br>
or bsp_quotemonths.month2 = 'JUN'<br>
or bsp_quotemonths.month3 = 'JUN'<br>
or bsp_quotemonths.month4 = 'JUN'</p>
<p> </p>
<p>The scenario is, our external reps (arearep) have quotes (id) for our company, they need to be able to enter a month when a part of the quote will be converted into sales orders along with a percentage of the sumnett expected in that month. I have provided 4 month fields in a quote where they can select a month from a combo box and enter a percentage value (pcnt*). I need to be able to create a report that will display expected values by area by month.</p>
<p>Sample results attached.</p>
sam992
<p>This thread was the answer to the question I had!</p>
<p>Thanks guys! :wub:<br><br><br><br>
</p>
micajblock
<blockquote class="ipsBlockquote" data-author="Tinwolf" data-cid="147673" data-time="1495094478">
<div>
<p>This is the basic, raw data set I`ve written to get the data from a view I created in sql:</p>
</div>
</blockquote>
<p>Can you provide a CSV with the raw data that matches the image?</p>
Tinwolf
<p>This is how the raw data comes out</p>
micajblock
<p>and what do you want the output to be?</p>