Possibility of Dynamic SQL Query

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
I have data that looks like this:


Excel 2012
ABCDEFG
1PayorNCSCTNKYMANH
2ABC Co.000111
3BCD Co.000001
4CDE Co.111000
5DEF Co.111111
Sheet1


I would like to have a way to create a query that returns only fields with values above 0 for instance....

If I queried ABC Co. I would only want to see this as a result:

Excel 2012
ABCD
11PayorKYMANH
12ABC Co.111
Sheet1


If I queried BCD Co. I would only want to see this as a result:


Excel 2012
AB
14PayorNH
15BCD Co.1
Sheet1



If I queried DEF Co. I would only want to see this as a result:


Excel 2012
ABCDEFG
17PayorNCSCTNKYMANH
18DEF Co.111111
Sheet1



Is this possible? My issue is the data I have has columns for all 50 states...So I would not want to display the ones with 0's...\
If there is a better format I would be open to it! I am just at a loss for this situation. Any help would be appreciated.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
No, SQL doesn't work like that, it expects your data to be normalised, which yours isn't.

Is that really how the data is stored? It looks like the result of a Pivot/Cross tab query
 
Upvote 0
It really is just like that.....

I believe a third party company just imported a spreadsheet into access. The data is vital to the project I'm working on. I can probably write some code to reorganize it.

What is a suggested data format? Like This?


Excel 2012
ABC
26PayorSTQTY
27ABC Co.KY1
28ABC Co.MA1
29ABC Co.NH1
30BCD Co.NH1
31CDE Co.NC1
32CDE Co.SC1
33CDE Co.TN1
34DEF Co.NC1
35DEF Co.SC1
36DEF Co.TN1
37DEF Co.KY1
38DEF Co.MA1
39DEF Co.NH1
Sheet1


If I can normalize the data like that is it possible to create a query that would return:

Excel 2012
ABC
26PayorSTQTY
27ABC Co.KY1
28ABC Co.MA1
29ABC Co.NH1
30Total3
Sheet1



Excel 2012
ABC
32PayorSTQTY
33BCD Co.NH1
34Total1
Sheet1
 
Upvote 0
Generally, yes that's better format, you'd probably have 3 tables though, a customer table, a state table and a customer-state join table.

Based on your example, you'd want:
<font face="Courier New" size="2">
<font color = "blue">SELECT</font> <font color = "maroon">payor</font><font color = "silver">,</font>
<br/>       <font color = "maroon">st</font><font color = "silver">,</font>
<br/>       <font color = "fuchsia"><i>Sum</i></font><font color = "maroon">(</font><font color = "maroon">qty</font><font color = "maroon">)</font>
<br/><font color = "blue">FROM</font>   <font color = "maroon">mytable</font>
<br/><font color = "blue">WHERE</font>  <font color = "maroon">payor</font> <font color = "silver">=</font> <font color = "red">'ABC Co.'</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">qty</font> <font color = "silver">></font> <font color = "black">0</font>
<br/><font color = "blue">GROUP</font>  <font color = "blue">BY</font> <font color = "maroon">payor</font><font color = "silver">,</font>
<br/>          <font color = "maroon">st</font> 
</font>

If the Qty is always one though, you could probably dispense with the Qty field altogether assuming that there will only be a record in the join table should there be a qty
 
Upvote 0
Kyle123,

Thanks so much for the help. Looks like I have to figure out how to reorganize all of this data :( before I can use it ahahah

I was curious what it would look like in the optimal format you propose.


Excel 2012
ABC
7IDStateFull Name
81NCNorth Carolina
92GAGeorgia
Sheet1



Excel 2012
ABCDEFG
1IDCoAddress 1Address 2StCityZip
21ABC Co.123 Main StSte 1NCGreensboro23756
32DEF Co.123 1st StSte 2GAAtlanta35698
Sheet1




How would I link the Quantities into this set up (There are not all 1's there are many different values)??
 
Upvote 0
You'd have something like this:
<table width="640" border="0" align="center" cellpadding="2" cellspacing="0" style="border-collapse:collapse;background-color:#9d2600;border:5px solid #9d2600;color:black;font-size:100%;font-family:arial,helvetica,sans-serif;"><tbody><tr><td colspan="4" style="padding:2px;color:white;font-size:150%;border:2px solid #9d2600;text-align:center;">CustomerStateQty</td></tr><tr><td valign="top" width="25%" style="text-align:center;background-color:white;color:Black;font-size:100%;border:2px solid #9d2600;">ID/Key</td><td valign="top" width="25%" style="text-align:center;background-color:white;border:2px solid #9d2600;">CustomerId</td><td valign="top" width="25%" style="text-align:center;background-color:white;border:2px solid #9d2600;">StateId</td><td valign="top" width="25%" style="text-align:center;background-color:white;border:2px solid #9d2600;">Qty</td></tr><tr><td valign="top" style="background-color:white;color:Black;font-size:100%;border:2px solid #9d2600;">1</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">1</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">2</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">10</td></tr><tr><td valign="top" style="background-color:white;color:Black;font-size:100%;border:2px solid #9d2600;">2</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">1</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">3</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">5</td></tr><tr><td valign="top" style="background-color:white;color:Black;font-size:100%;border:2px solid #9d2600;">3</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">1</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">1</td><td valign="top" style="background-color:white;border:2px solid #9d2600;">7</td></tr></tbody></table>

Then:

<font face="Courier New" size="2">
<font color = "blue">SELECT</font> <font color = "maroon">customers</font><font color = "silver">.</font><font color = "maroon">co</font><font color = "silver">,</font>
<br/>       <font color = "maroon">states</font><font color = "silver">.</font><font color = "maroon">[full name]</font><font color = "silver">,</font>
<br/>       <font color = "fuchsia"><i>Sum</i></font><font color = "maroon">(</font><font color = "maroon">customerstateqty</font><font color = "silver">.</font><font color = "maroon">qty</font><font color = "maroon">)</font>
<br/><font color = "blue">FROM</font>   <font color = "maroon">customerstateqty</font>
<br/>       <font color = "blue">INNER</font> <font color = "blue">JOIN</font> <font color = "maroon">customers</font>
<br/>               <font color = "blue">ON</font> <font color = "maroon">customerid</font> <font color = "silver">=</font> <font color = "maroon">customers</font><font color = "silver">.</font><font color = "maroon">id</font>
<br/>       <font color = "blue">INNER</font> <font color = "blue">JOIN</font> <font color = "maroon">states</font>
<br/>               <font color = "blue">ON</font> <font color = "maroon">stateid</font> <font color = "silver">=</font> <font color = "maroon">states</font><font color = "silver">.</font><font color = "maroon">id</font>
<br/><font color = "blue">WHERE</font>  <font color = "maroon">customer</font><font color = "silver">.</font><font color = "maroon">co</font> <font color = "silver">=</font> <font color = "red">'ABC co.'</font>
<br/><font color = "blue">GROUP</font>  <font color = "blue">BY</font> <font color = "maroon">co</font><font color = "silver">,</font>
<br/>          <font color = "maroon">[full name]</font> 
</font>


Does that make sense?
 
Upvote 0
I think I got it. Thanks! I appreciate the help. I'll let you know if I have further inquiries.
 
Upvote 0
this is a query that will make it look like the first table in your post #3

Code:
select 
  Payor, 
  'NC' as State, 
  NC  as Qty
from 
  table1
where 
  NC > 0 
  and 
  Payor = [param_Payor]


union 


select 
  Payor, 
  'SC' as State, 
  SC  as Qty
from 
  table1
where 
  SC > 0 
  and 
  Payor = [param_Payor]


union 


select 
  Payor, 
  'TN' as State, 
  TN  as Qty
from 
  table1
where 
  TN > 0 
  and 
  Payor = [param_Payor]


union 


select 
  Payor, 
  'KY' as State, 
  KY  as Qty
from 
  table1
where 
  KY > 0 
  and 
  Payor = [param_Payor]


union 


select 
  Payor, 
  'MA' as State, 
  MA  as Qty
from 
  table1
where 
  MA > 0 
  and 
  Payor = [param_Payor]


UNION select 
  Payor, 
  'NH' as State, 
  NH  as Qty
from 
  table1
where 
  NH > 0 
  and 
  Payor = [param_Payor];
 
Upvote 0
Thanks for all of the input guys. I'm reorganizing the data today. I'll let you know if I run into any issues.
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,020
Members
449,203
Latest member
tungnmqn90

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