SQL isset and not showing blank 'cells' - sql

I'm using one of my MySQL database tables as an actual table, with times of the day as each column, and one column called day. You guessed it, in day it says the day of the week, and in the rest of the cells it says what is happening at that time.
What I want to do is only show the cells that have value in it. In my case, I'm always going to have all the rows and 2 columns full. The 2 columns are 'day' and '19:00', however in the future I might add values for '18:00' etc.
So, how can I only SELECT the columns and rows which have data in them? Some type of 'WHERE: there is data'?
Thanks!
EDIT: Picture

Having time or day as columns means that you have data in your field names. Data belongs inside the table, so you should normalise the database:
table Calendar
--------------
Day
TimeOfDay
Appointment
This way you don't get a lot of empty fields in the table, and you don't have to change the database design to add another time of day.
Now you can easily fetch only the times that exist:
select Day, TimeOfDay, Appointment from Calendar

From what I gathere you are looking something along the lines of
WHERE col1 IS NOT NULL
But it would be helpful if you could elaborate more on your schema, especially if you could draw a sample table.

Related

How to efficiently query one table to obtain a value I need to then change two dates on two rows in another table?

I get the feeling this is easy for SQL, but I'm new to it. I want to query table CUST_TRAN and find out the ID for value $1 which is in column INVOICE.
I then want to use the newly discovered ID to update table TRANSACTS to change the DATE and DATE_TIME (two values in two columns) to value $2 using the column IDFROMTRAN to match the ID against. There are very often two rows with the same IDFROMTRAN that need to be updated, because the customer signs for the goods and that creates its own row.
Does any of this make sense?
Basically, to explain what I'm doing I'm changing the date on transactions to put them into last month, so that they'll be invoiced correctly and reflect when the customer was actually in store.
TLDR: How do I find out an alternative ID for a transaction then use that to change the date in two rows in another table?

SQL Server query to get the difference between two dates in two different columns

I would like to write a SQL Server query which should get the difference between two dates (i.e. dates are available in two columns named "Start date" and "End date"). I would like to find the difference between two dates in these two different columns and update in another column as "Expired" if the difference is -1 or below. Most importantly: The time should start from the specified start date and it should check periodically.
To find the differential between two date values you use the DATEDIFF() function.
Also, depending on your requirements, you can set this up in your table as a Computed Column. That way any changes to the component columns in the computed column's definition will automatically update the computed column to reflect the new values.
With the PERSISTED keyword you can also allow the computed column to be indexed on.
Check the documentation here for more details: https://technet.microsoft.com/en-us/library/ms191250%28v=sql.105%29.aspx
This is based on what I think you are trying to do, it's not exactly clear.
I don't think you want to add a column to your table to identify what is expired because that column would be dependant on the "End Date" column as well as the primary key which would violate the 3rd normal form. It really shouldn't be needed because you can query out which ones are expired at any time. I can't really think of a scenario where you would need to have a column that indicates expiry. You can create a query like others mentioned to display (not create) another column that marks the expired rows, or you can simply display only the ones expired, or it might make more sense to move them to a different table.
SET IDENTITY_INSERT ExpiredTableName ON
INSERT INTO ExpiredTableName (Column1, Column2, StartDate, EndDate)
SELECT *
FROM TableName
WHERE DATEDIFF(day, InvoiceDate, '2015-04-20') >0
Identity Insert is for auto-generated keys only.
You can run your queries at regular time intervals like was already mentioned.
You can use DATEDIFF and case when like this:
update table set expColumn = case when DATEDIFF(day,start_date,end_date) > 0
then 'Expired' end FROM table
Hope this helps.

See the number of new rows added to PostGRES tables each day

How can I see the number of new rows added to each of my database's tables in the past day?
Example result:
table_name new_rows
---------- -----------
users 32
questions 150
answers 98
...
I'm not seeing any table that stores this information in PostGRES statistics collector: http://www.postgresql.org/docs/9.1/static/monitoring-stats.html
The only solution I can think of, is create a database table that stores the row_count of each table at midnight each day.
Edit: I need this to work with any table, regardless of whether it has a "created_at" or other timestamp column. Many of the tables I would like to see the growth rate in, do not have timestamps columns & can't have one added.
The easiest way is to add a column in your table that keep a track of the insert/updated date.
Then to retrieve the rows, you can do a simple select for the last day.
From my knowledge, and I've also done a couple research to make sure, there is no intern functionality that allow you to do that without creating a field.

What is the proper way to store an array into a database table?

I have an array of 50+ elements that dictates how many hours were worked for a given week.
What is the proper way to store this information into a database table?
My initial idea was to use a delimiter, but the text is too large (280 characters) to fit.
Additionally, there seems something "wrong" with creating a table column for each element.
Ideas?
Array using delimiter (comma):
37.5,37.5,37.5,37.5,37.5,37.5,37.5,37.5,37.5,37.5, ...
The "proper" way is to store the array's contents as multiple rows in a whole other table, each with a foreign key referencing the record they belong to back in the first table. There may be other things that work for you, though.
[EDIT]: From the details you added I'm guessing your array elements consist of a number of hours worked each week and you have 50+ of them because a year has 52-ish weeks. So what I think you're looking for, is I guess that your current (main) table is called something like "employees," is that each row there should have some unique identifier for each employee record. So your new table might be called "work_weeks" and consist of something like employee_id (which matches the employee id in the current table), week_number, and hours_worked.
Seems like a 1 to many relationship. For this example, tableA is the 1 and tableBlammo is the many.
tableA => column blammoId
tableBlammo => column blammoId, column data
One row in tableA joins to multiple rows in tableBlammo via the blammoId column.
Each row in tableBlammo has one element of the array in the data column.

Make a fake schedule if it doesn't exist in db

I want to pull all the rows from a database for a month and if there are any rows in the beginning or in the end that are missing then I want to create fake rows, so every day there is a schedule that one has to follow. How can I detect missing rows or how can I solve this issue?
From how to fill up the missing dates ??
fill a calendar table use that as your
driving table (i.e. first one in your
select) and do a LEFT JOIN on it.
Create and fill a calendar table