How can I add blank columns to the results of a SQL query? - sql

I'm working on a query that pulls demographic information for people who have visited a location. However, the fields required for this report aren't all available in the DB, and some will need to be added manually from a separate Excel file after I've exported the results of my query. The person who will be responsible for merging the two files asked if it would be possible to create blank columns so they can more easily see where the missing data needs to go, and I wasn't sure how to go about that. Obviously those blank columns could just be created in the exported spreadsheet, but I wondered if there was a way to add them in the SQL query itself.
My SELECT statement currently looks something like this—I've just added comments to mark the missing fields so I can keep track of what order all the fields for this report need to be in.
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
--StaffFName,
--StaffLName,
FacilityPhone,
FacilityAddress,
...and so on
Since those two staff name fields don't exist in my DB, I obviously can't actually include them in the SELECT list. But is there a way to still include those two fields as blank columns in my query results? Something along the lines of "select [nothing] as StaffFName"?

Just add literal nulls to the select clause:
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
null as StaffFName,
null as StaffLName,
FacilityPhone,
FacilityAddress,
...
Or, if you prefer, you can use empty strings instead:
...
'' as StaffFName,
'' as StaffLName,
...
But null is the canonical way to represent the absence of data.

Related

How do I combine two tables that have only a few similar columns?

I'm attempting to combine two tables, both of which aren't related in any way except for a few columns (ID, Created Date, Country, etc.). In essence, I simply want to append one table to another. However, I would like to combine the columns that are similar and add on the columns that are not similar. I've attempted a Union, but my tables don't have the same number of columns. Currently, I'm working with this:
SELECT * FROM `leads`, `opportunity`
where `leads`.`Id` = `opportunity`.`Id`
which doesn't really work when I want to use this new query as a subquery elsewhere. Additionally, the fields in each table can change at any time, so I’m never sure which columns are matching or non-matching. I simply want to append the rows from one table onto the other while automatically combining columns with identical names. I feel like I'm missing something obvious...
NOTE: I am doing this within DOMO, so I have a few more limitations than I normally would.
You can use joins
SELECT * FROM `leads` JOIN `opportunity`
on `leads`.`Id` = `opportunity`.`Id`
and to get only selected columns
SELECT leads.column_name, opportunity.column_name FROM `leads` JOIN `opportunity`
on `leads`.`Id` = `opportunity`.`Id`

SQL Select Query - Removing Duplicates/Misspelled Data

Pulling data from a cmdb into another repository. Problem is the cmdb data has misspelled/duplicate records (e.g., some assets have a Department Name as Marketing, or Markting, or Marketing&amp -- when they are all just in Marketing). Want to run a select query that displays all incorrectly named department records as the single, correct name. Any help on how to approach this?
You can use CASE in to display "marketing" for its wrong entries. But query can be complicated depending on variations.
Better + easier way is a global search and replace in column. Following article describes it:
http://www.codecandle.com/articles/sql/update/483-sql-update-with-global-search-and-replace.html
Cleaning duplicate rows, following article may help:
http://www.codecandle.com/articles/sql/windowing/503-deleting-duplicate-rows-using-windowing.html
I'm sure this is passed but http://openrefine.org/ would probably help you clean the messy data.
you can use the SELECT DISTINCT statement is used to return only distinct (different) values.
you should use distinct keyword before coloumn names in select statement.
e.g: select distinct name (Coloumn name)
from table name;

SQL Select If Then

The table contains numerous rows for the same person and all columns contain the same data. For Instance:
FullName Gender DOB
---------------------------------
Mary Jones Female 2012-05-01
I would like to select one row for each individual to appear in a report. I thought the easiest way might be to us an if statment to check if the next row is the same as the first unique row.
As others have said, since all the rows are the same you can use the distinct keyword:
SELECT DISTINCT <columns>
FROM table
However, you're likely to find that the reason for these duplicate records is that at least one column somewhere in the table is different. In that case, you may need to use GROUP BY instead:
SELECT <columns>
FROM table
GROUP BY <columns>
If you need to show data that is not part of the grouped columns, you only list the columns that match in the GROUP BY clause and then you have to use an aggregate function or sub query to bring data from the unique columns into the select list. GROUP BY queries can get really complex and make your head hurt.
If the columns really are consistently the same, something in your schema or application design isn't right. It's rare that you should ever have truly duplicate records in a table, and it's more likely to mean something is wrong.
Finally, I need to comment about your IF/THEN request. This idea is way off. SQL has a SET-based or declarative programming style. Thinking about problems using procedural tools like IF/THEN will only lead to trouble. Even if you really do need a conditional expression, the way to do it within an SQL statement is via a CASE expression.
If the data is the same in all columns, then you can use the DISTINCT keyword:
SELECT DISTINCT FullName, Gender, DOB
FROM yourtable
This will check that the data in all fields are unique removing any duplicates.
See SQL Fiddle with Demo
Another way to write this is using a GROUP BY on all fields:
SELECT FullName, Gender, DOB
FROM yourtable
GROUP BY FullName, Gender, DOB
You should use the distinct clause instead.. if all the columns are in fact completely same.
select distinct FullName, Gender, DOB
from <your_table_name>;
Having duplicate rows is usually a sign of something wrong (may be the data is being loaded multiple times). You might have to investigate to see the actual reason.

Select names of columns that have no data in Access?

I'm working on a C# application that imports data from Access into SQL Server. They select an Access file, then a table in the file.
I then perform checks on the data to see if it's valid to import. I want to display a list of columns from the table that have no data in them, so the user has to confirm they want to import regardless of certain empty columns.
Is there an approach to this in Access besides looping through SELECT ... WHERE (field) IS NULL queries?
Is there an approach to this in Access besides looping through SELECT ... WHERE (field) IS NULL queries?
I don't know of an alternative to looping, but I will suggest a different strategy for the queries you run in the loop. Seems to me you would want to know whether any rows include non-Null values for the given field.
SELECT Count(*) AS row_count
FROM Table_Name
WHERE field_name Is Not Null;
No looping necessary
Use Count(*) to get the number of total records
Use Count with WHERE (field) IS NULL and compare the counts
If the counts are equal all of the rows are null for that column.

How not to display columns which are NULL in a view

I've set up a view which combines all the data across several tables. Is there a way to write this so that only columns which contain non-null data are displayed, and those columns which contain all NULL values are not included?
ADDED:
Sorry, still studying and working on my first big project so every day seems to be a new experience at the minute. I haven't been very clear, and that's partly because I'm not sure I'm going about things the right way! The client is an academic library, and the database records details of specific collections. The view I mentioned is to display all the data held about an item, so it is bringing together tables on publication, copy, author, publisher, language and so on. A small number of items in the collection are papers, so have additional details over and above the standard bibliographic details. What I didn't want was a user to get all the empty fields relating to papers if what was returned only consisted of books, therefore the paper table fields were all null. So I thought perhaps there would be a way to not show these. Someone has commented that this is the job of the client application rather than the database itself, so I can leave this until I get to that phase of the project.
There is no way to do this in sql.
CREATE VIEW dbo.YourView
AS
SELECT (list of fields)
FROM dbo.Table1 t1
INNER JOIN dbo.Table2 t2 ON t1.ID = t2.FK_ID
WHERE t1.SomeColumn IS NOT NULL
AND t2.SomeOtherColumn IS NOT NULL
In your view definition, you can include WHERE conditions which can exclude rows that have certain columns that are NULL.
Update: you cannot really filter out columns - you define the list of columns that are part of your view in your view definition, and this list is fixed and cannot be dynamically changed......
What you might be able to do is us a ISNULL(column, '') construct to replace those NULLs with an empty string. Or then you need to handle excluding those columns in your display front end - not in the SQL view definition...
The only thing I see you could do is make sure to select only those columns from the view that you know aren't NULL:
SELECT (list of non-null fields) FROM dbo.YourView
WHERE (column1 IS NOT NULL)
and so forth - but there's no simple or magic way to select all columns that aren't NULL in one SELECT statement...
You cannot do this in a view, but you can do it fairly easily using dynamic SQL in a stored procedure.
Of course, having a schema which shifts is not necessarily good for clients who consume the data, but it can be efficient if you have very sparse data AND the consuming client understands the varying schema.
If you have to have a view, you can put a "header" row in your view which you can inspect client-side on the first row in your loop to see if you want to not bother with the column in your grid or whatever, you can do something like this:
SELECT * FROM (
-- This is the view code
SELECT 'data' as typ
,int_col
,varchar_col
FROM TABLE
UNION ALL
SELECT 'hdr' as typ
-- note that different types have to be handled differently
,CASE WHEN COUNT(int_col) = 0 THEN NULL ELSE 0 END
,CASE WHEN COUNT(varchar_col) = 0 THEN NULL ELSE '' END
FROM TABLE
) AS X
-- have to get header row first
ORDER BY typ DESC -- add other sort criteria here
If we're reading your question right, there won't be a way to do this in SQL. The output of a view must be a relation - in (over-)simplified terms, it must be rectangular. That is, each row must have the same number of columns.
If you can tell us more about your data and give us some idea of what you want to do with the output, we can perhaps offer more positive suggestions.
In general, add a WHERE clause to your query, e.g.
WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL
Here, a b c are your column names.
If you are joining tables together on potentially NULL columns, then use an INNER JOIN, and NULL values will not be included.
EDIT: I may have misunderstood - the above filters out rows, but you may be asking to filter out columns, e.g. you have several columns and you only want to display columns that contain at least one null value across all the rows you are returning. Using dynamic SQL offers a solution, since the set columns varies depending upon your data.
Here's a SQL query that builds another SQL query containing the appropriate columns. You could run this query, and then submit it's result as another query. It assumes 'pk' is some column that is always non-null, e.g. a primary key - this means we can prefix additional row names with a comma.
SELECT CONCAT("SELECT pk"
CASE (count(columnA)) WHEN 0 THEN '' ELSE ',columnA' END,
CASE (count(columnB)) WHEN 0 THEN '' ELSE ',columnB' END,
// etc..
' FROM (YourQuery) base')
FROM
(YourQuery) As base
The query works using Count(column) - the aggregate function ignores NULL values, and so returns 0 for a column consisting entirely of NULLs. The query builder assumes that YourQuery uses aliases to ensure there no duplicate column names.
While you cant put this into a view, you could wrap it up as a stored procedure that copies the data to another table - the result table. You may also set up a trigger so that the result table is updated whenever the base tables change.
I suspect what's going on is that an end user is running CrystalReports and complaining about all the empty columns that have to be removed manually.
It would actually be possible to create a stored procedure that would create a view on the fly, leaving out dataless columns. But then you would have to run this proc before using the view.
Is that acceptable?