Display a column only if it is not null - mysql

I need to display a particular column in my SQL result only if it is not null. If it is null, I don't want that column to appear at all in my result.
Is there a way to express this condition in SQL?

This wouldn't make sense because a query may return multiple rows. One row may have a value for the column in question and the next may not have a value. Then a conditional column would create a structural inconsistency between returned rows.

No, it is not...

It's not possible, and really unnecessary. You'll need to have a fixed number of columns, there's just no other way. But that isn't really your problem, you don't want that at all!
Queries are just to retrieve the data, not for the representation of the data. You should just retrieve it and hide the column if all the values are null.

SQL doesn't generally let you reason about properties of entire columns. Conditions are on properties of rows. So there's no way to say "if all the values in this set of this column are null...". However, you can trivially restrict yourself to rows that lack the property.
If you want to show a column only when it is not null for every row, you could do a COUNT(*) WHERE ... your general condition ... AND that_column IS NULL and then redo the query, including the column if the first result was 0 and excluding it otherwise. But I'm not sure why you'd want to do such a thing.

Related

Working with SSRS dynamic fields

SSRS matrix table is a great way to generate dynamic fields as long as values exist.
However, is there a way to "always" show these dynamic fields even if a value doesn't exist for them? The report field locations varies based on data availability and users have to add missing columns in Excel manually.
Dynamic fields go from 3 to up to 30 (at least for now based on run by values). Adding these values manually would make the report hard to maintain.
The way I have handled for this is in the SQL. I build a table of all the values I will always want, I cross join that table to my final output table and update/insert values where they need to exist. That way I guarantee the rows, and eventually columns in the matrix, exists even if they end up being null.
Does that make sense?
Jesse's solution is a good one, but if for whatever reason you can't or prefer not to change the SQL you can do it in SSRS by forcing a blank value in the cell with a expression like this:
=iif(IsNothing(Fields!.xxx.Value)," ",Fields!.xxx.Value)

Best practice for handling positions of rows

What is the best practice for moving rows. So that you might want to change order of items. Now if you make a new column called order_id or something, wouldn't that fail if I delete or select rows.
Another method I guess is to just switch values completely with an primary ID, so just values except the ID are changed. however I do not know what people usually use. There are so many websites that give you the ability to change order of things.how so they do that?
Every SQL statement that returns a visible result set should include an ORDER BY clause so that the results are consistent. The Standard does not guarantee that the order of rows in a particular table will remain constant or consistent, even if obvious changes aren't made to the table.
What you use for your ORDER BY clause depends on the use case. A date value is the usual choice for a comment thread or blog entry ordering. However, if you want the user to be able to customize the order that a result set shows in, then you have to provide a column that represents the position of the row, and adjust the value of that column when the user makes changes to the order they see.
For example, if you decide that the column will contain a sequential number, starting with 1 for the first row, 2 for the second, etc. then you will be ok to delete rows when they need to be deleted without having to do updates. However, if you insert a row, you will need to give the row you insert the sequential number appropriate for it's position, and update all rows below that with their new position. Same goes for if you move a row from somewhere else to a new location; the rows between the new and old locations need to be updated with new postion indexes.

Trying to retrieve matching columns

I have a table, i'm interested in 2 columns. table.ACTUAL_IDENTIFIER and MODEL_IDENTIFIER.
I need to ensure that ACTUAL_IDENTIFIER is set to the VALUE OF MODEL_IDENTIFIER.
My way of thinking is going to the table and taking a note of the row count. Then running sql to return all rows which ACTUAL_IDENTIFIER = MODEL_IDENTIFIER and ensuring both row counts add up, if not it's clear ACTUAL_IDENTIFIER != MODEL_IDENTIFIER for all rows and there are errors somewhere.
Can someone help me out with the sql i will need to use to accomplish this?

add variable number of empty columns to a tablix

I have a tablix with a column group so that it will create a column for that field provided any row has data for that column.
I need to create a version of this report that contains some empty columns on the end.
I want the number of columns to be added to be based on some factor of the non empty columns with some min/max constraints also. (my question is not how to get the number of columns required)
so far i've tried.
1 - adding individual empty columns to the tablix and setting the visibility condition on each column.
a bit long winded and a bit of a faff.
2 - creating another column group and grouping on the same field, this creates
cant vary the number of columns returned.
am i missing a simple way of adding x empty rows or columns to a tablix? where x can be calculated somehow from the values in the dataset.
You will have to fib this scenario in your data. The column-wise grouping works with known data including those falling in the column group value with null values. There is no way to grow your groups without data unless you add some column group footer logic that would be pretty weird.
I would look into producing phantom NULL value records that will push out your columns.

Mysql sum as last row

Is it possible to have the SUM of all numaric fields in the last of a set of rows?
As of now, I'm using a very simple query such as:
SELECT
*,
SUM((UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start))/3600)
FROM
times
in SQL you cant have a column that appears in only one row, likewise, you also cant have a row that doenst contain all the columns from the other rows.. So having a row that contains something unique is not possible. You can, however, add the calculated column to all rows in the dataset or do the calculation in the calling code after the data is returned.
I think what you are looking for is GROUP BY WITH ROLLUP you will find details on that in the MySQL manual.