Friday, January 29, 2016

Access Query: Concatenation with Nulls

I've been trying to figure this out for a while now.  A part of a query that I often need to run is to add up several different things that may happen to a patient.  And a patient may have one, or none, or a couple or (heaven forbid!) 40 things happen to them over the course of a stay.

The standard way to concatenate in Access is by using the "&".  A query that I use multiple times a day starts with "Name: [last_name] & ", " & [First_name]" which results in "Hartshorn, Earle".  This is OK if both first and last name contain something (almost always).  But if you want to include middle name (almost always empty for my purposes) you would use Name: [last_name] & "," & " " & [First_name] &" "&[Middle_name].  this would result in "Hartshorn, Earle   "; if there is no middle name then there would be an extra space.  Not a big deal in this case, but what if you needed to include many fields that are hopefully blank?  Lots of blank spaces are what you would get, and it looks ugly and is confusing, and when you add commas between terms, you get this:

term, , , term, , , , , , term, , , ,  Urghh.

I decided to research the problem this morning and found this post.  When concatenating 40 or more fields it takes some time to write the query string, but it works very nicely.

Cross posted from http://www.spearsofttech.com/articles/microsoft-access-query-tip-concatenation-with-nulls/

Microsoft Access Query Tip – Concatenation With Nulls

I wonder, should I even assume that you know what that “concatsomething” word means. Since this is the “beginning” tip I’ll assume you don’t. But if you already know what concatenation is, then skip the next two paragraph of this section.
Concatenation, to get straight to the point, is joining two values in a query (or in programming code) to produce one value. Let’s use two fields, FirstName and LastName, as an example. Many beginners would, in fact, be tempted to create only one field (FullName) in a database because they don’t know how to concatenate fields in queries. Concatenation in queries allows you to “create” the FullName field in the query at the moment that the query is run.
The way this is done is by using the concatenation operator, which is the ampersand (&), in the query. After dropping in a table, in the QBE grid (that is the grid in the lower half of the query design window) click in the next blank column of the grid (into the Field cell) and type the following:
FullName: [FirstName] & " " & [LastName]. 
When you run the query you would see a FullName field with FirstName and LastName concatenated together.
Now for our trick. Many beginners are not aware that the plus(+) operator can also be used for string (text) concatenation. Concatenating with a plus(+) works slightly differently than concatenating with the ampersand when concatenating with Null values. (Note: A Null is an empty or blank value which is not the same as a “zero-length string”). Let me just state the difference and then I’ll explain. KEY CONCEPT: Concatenating a string with a Null using an ampersand(&) returns a string. (Null & String = String) Concatenating a string with a Null using a plus (+) returns a Null. (Null + String = Null) This is an important distinction that you can use to your advantage when creating queries. Let me explain. Let’s use the name example as above except let’s put the LastName in first and use a comma. Using the concatenation operator we would create a field like this:
FullName: [LastName] & ", " & [FirstName]
The problem with this method is that if the LastName or FirstName field is Null in some of the rows then it will return ugly results like the following:
Spearman,
, Brad
Johnson,

This problem can be handled by using the plus(+) operator in a special way. Using the plu(+) operator we would create a field like this:
FullName: ([LastName]+", ") & [FirstName]
This method fixes our problem and would return the following nice, neat rows:
Spearman
Brad
Johnson
Now for a final explanation of why the second method returned better results as compared to the first. In the Spearman example (where there is a Null FirstName field), using the ampersand, the value is created as Null & “, ” & Spearman and returns “,Spearman”. But in the second example, using the plus, the value is created as Null + “, ” which returns a Null which is then concatenated to Spearman thereby returning “Spearman” as the value. In other words, a Null is concatenated to Spearman.
Give it a try and see if that works for you.
Seth Spearman

The very first time that I used this method to concatenate fields it worked beautifully.  The second time I tried to use it I kept having an error:  The field aliases would be, for example, "Rem0" and "Rem1" and the Query String would be "All: ([Rem0]+", ") & ([Rem1]+", ")" but every time that I tried to run the query it would ask me to input the value of "Rem0" and "Rem1".  If I just hit "OK" on each of those the answers would come thru OK, but I couldn't figure out why it was doing that.  After staring at it and comparing the working query to the new query I suddenly realised that the "Total" row in the new query was set to "Where"!  I deleted "Where" and the query now works!

Until you add a numerical Value to the report, that is.  :)

Friday, November 6, 2015

SQL Notes

I'm starting a table of conversions.  Since I'm now working in 3 SQL dialects I need to keep them straight!

DateDiff

datepart
Analytics
MS-SQL
J-SQL
year
yy, yyyy
yy, yyyy
yyyy
quarter
qq, q
qq, q
q
month
mm, m
mm, m
m
dayofyear
dy, y
dy, y
y
day
dd, d
dd, d
d
Weekday
dw, w
w
week
wk, ww
wk, ww
ww
hour
hh
hh
h
minute
mi
mi, n
n
second
ss, s
ss, s
s
millisecond
ms
ms
microsecond
mcs
mcs
nanosecond

ns


Wildcards:
Description
MS-SQL
Jet SQL
Any String of zero or more characters
%
*
Match one character
_(underscore)
?

Thursday, September 25, 2014

Excel - Making a Chart behave - Trendlines, copy format, stacked chart.

These are some personal notes, but if you find them useful then go for it!

In Excel, you cannot take a stacked chart and add a trendline to it.  But I wanted to.  I had resorted to using Paint in the past, but this particular chart was going to be rather complex, and I had several of them to build, so I wanted to figure a "better way (TM)".  Here's what I did in Excel 2010.

Select your data and create the stacked chart.  That is the easy part.  For a stacked chart, you would normally have groups with quantities such as this:
Month  Widget 1   Widget 2   Widget 3         Total
March 26 6 13 45
April 21 9 11 41
May 25 7 12 44

Create the chart without using the totals column.


Right click on the chart and "Select Data".  In the "Legend Entries (Series)" pane click on "Add".  Select "Total" for the Name, and add the "Total" data to the Series Values.  Now, the "Totals" are stacked on top of the other data and you should have "Total" in your Series pane.  Click "OK".



Right click on the "Totals" data and select "Format Data Series".  On the "Series Options" tab click the "Secondary Axis" radio button under "Plot Series On".  The "Totals" data will now be hiding the stacked data.

Right click again on the "Totals" data and "Change Series Type" to a regular column.  Click the "Layouts" tab on the Excel Ribbon, click the "Totals" column, and now "Trendline" should be an option.  Select the trendline you want to use, in my case a linear trendline.

Now you have the trendline, but your stacked data is hidden.  Right click again on the "Totals" series and click on "Format Data Series".  Change the "Fill" to "No Fill", change the "Border Color" to "No line", click "Close.

There you go!



Now to copy this to multiple charts without having to walk thru all of the steps.  Begin by creating your stacked chart, but this time include the totals column.  Right click on the first chart and "copy".  Click on the new chart, and, on the Ribbon, under the "Home" Tab, click on the little arrow under "Paste" and select "Paste Special.  On the dialog box, select the radio button for "Formats".  All done!!


Credit to mrexcel.com and techrepublic.com for the tips that I put together to create this.


Thursday, August 7, 2014

What do I do?

I often get asked what I do at St. Joe's and find it difficult to explain.  I work as a System Support Admin, which basically means that I boss a computer around :).  I'm also a sort of Systems Analyst, which means that I look at numbers a lot.

I'm part of a team working for the CHI Franciscan Heart Center.  The team follows patients that have heart procedures at any of our Franciscan facilities, and we collect data.  What sort of data?  Well, just about anything that might have some sort of impact on their disease, on their care, on their health, on their outcomes.  How much anesthesia, how much they weigh.  And it is all used for research, to help us provide better care for them and for future patients.  If something doesn't work, we tell the doctors and surgeons about it so that they don't do that again.  If something works better, we help make certain that the technique gets used more often.  We help keep an eye on things to make sure that patients are getting appropriate medications and treatments at appropriate times and places.  We try to encourage the doctors to give patients all the tests they need for proper diagnoses, and to not order tests that are not helpful.

My role is to aggregate and report on the data that the team enters into our database.  At any time, an administrator or physician may want to know about a specific data point; I get them the answer.  How many patients had multiple bypass last month?  I can answer that question.  How many cardiac patients were prescribed statin drugs last month?  I can answer that question.  How many patients last week did NOT get all of their meds on time?  I can answer that question.   How many patients two days ago did not get appropriate ventilator weaning?  I can answer that question.  What are the chances for successful outcome of a specific surgery on a specific patient?  With the right data I can give you an answer.

Besides using our data internally for our own research, I also submit data to the National Registries such as the Society of Thoracic Surgeons and the American College of Cardiology.  These are databases that collect all of the data on all heart procedures done across the U.S. for use by researchers. There is also a Washington State database that I report to.

The data from these Registries are all held to a high standard.  Every few years we are audited for accuracy and completeness by outside firms.  We also participate in user groups that audit each other, in an attempt to make sure that we are all collecting the same data in the same way, so that we are comparing apples to apples across the nation.

For 20+ years this data has been mostly looked at by students and physicians, but the last couple of years some of this data has been coming to light in public places.  Because of the reliability of the data, Insurers are using this data to determine the quality of programs, and then recommending good programs to their clients.  And now, Consumer Reports is also using this data to report directly to the public (YOU!) so that you can choose the best programs for your needs.  Here are a couple of recent news items that demonstrate the work that our team puts out.




Be informed!!

Thursday, May 8, 2014

Dog Pack attacks Gator in Florida

Be sure to read to the end - it's surprising!

At times nature can be cruel, but there is also a raw beauty,
and even a certain justice manifested within that cruelty.

The alligator, one of the oldest and ultimate predators, normally considered the "apex predator",
can still fall victim to implemented 'team work' strategy, made possible due to the tight knit social structure and "survival of the pack mentality" bred into the canines.

See the remarkable photograph below courtesy of Nature Magazine.

Note that the Alpha dog has a muzzle hold on the gator
 preventing it from breathing, while another dog has a hold on the tail to keep it from thrashing.

The third dog attacks the soft underbelly of the gator.

Not for the squeamish...

 



GOTCHA!!!
Laughter is good for the soul...

 

Go get your Gators!!

Sunday, May 4, 2014

The Nine Billion Names of God

The Nine Billion Names of God is a short story written by Arthur C. Clarke back in 1953.  It tells a story of a group of Tibetan Monks who hire IBM to install a mainframe to print out the Nine Billion Names of God; they believe that the world was created to discover the Nine Billion Names and that when the purpose of the world was completed the world would end.  I’m not going to tell you the rest of the story, you will need to find it out yourself.

But I don’t want to talk about the god of Tibetan Monks.  I want to talk about the God of the Judeo-Christian Traditions.  “God” is a generic term for a deity, or a power mightier than man who interacts with man. 

There is a ton of speculation about the origin of the word “god”. http://en.wikipedia.org/wiki/God_(word) has quite a bit on the subject. 

Personally, while I agree that the idea that our English “god” comes from the Germanic “gott”, I think that there is a deeper undercurrent to the meaning.  In the article on “Gad” in the Jewish Encyclopedia  it is mentioned that Gad is the name of the Canaanite god of Fortune, pronounced “gawd”.  And today, we wish each other “Good Luck”, or we say “how fortunate” or “how lucky” a person is, which is similar to saying “Gad be with you”.  Of course, “luck” is Loki, the Norse god of fortune, “fortune” is the Roman goddess “Fortuna”. 

Where this all started for me was a couple of years ago in discussion with an acquaintance about “God”.  This acquaintance was arguing that since I worship “God”, and Muslims worship “God”, and members of the Church of Latter Day Saints worship “God”, and many others claim to worship a “God” or “gods” that we should all just get together and worship “god” together, or better yet, just live and let live and let each one worship his or her own “god”: the way they want to.  I mean, like, you know, its all the same anyways, right man?

Well, no, it isn’t all the same, at least not to me.  See, my “God” requires me to worship him a certain way, and not only that, my “God” claims to be the only true “god” and claims that all others are not really “gods” at all.  If only one “god” said that then it would not be a problem, but practically every “god” around makes similar claims.  So now what do I do?

I have settled to my own satisfaction that the “God” I worship is, for me, the true “God”.  I’m not going to go into the details here and now, but for a good place to start you can try “Mere Christianity” by C.S. Lewis.

In the Hebrew Old Testament, or Tanakh, what is translated usually as “God” is usually the Hebrew for “Elohiym”; which may be loosely translated as “Mighty One”.  In Genesis 1:1 it says that Elohiym created the heaven and the earth.  Some scholars think that Elohiym is the plural for Eloahh and that Elohiym should be translated as "Mighty Ones"; there is great debate on this matter.  As near as I can tell it is all speculation and no-one really knows for sure; much data has been lost about the Hebrew language.  Although, considering how little we know about other languages from 4-5,000 years ago, we know quite a lot about the Hebrew language.  Interesting, is is not?

In Genesis 2:4 is the first appearance of what is sometimes called the Tetragrammaton, or The Name, or YHWH, or Jehovah, or as I read it, Yahuah (ya HOO ah).  For arguments on the pronunciation of the Tetragrammaton see the article in Wikipedia or in the Jewish Encyclopedia.

So, as someone else might say that their God is Allah, I will say that my Elohiym is Yahuah, because I believe, worship, and trust the Hebrew deity.  Depending on my audience, I may say “God Bless you” or I may say “Elohiym bless you”.  I may say “Praise the Lord” or I may say “HalleluYAH”.  However, in my own personal worship and prayer, I normally use the Hebrew names.  And I will rarely tell a person "Good Luck" or say how lucky someone is, because to me, that is a referent to a different god.