Skip to content

Tag: sql

How to store historical records in a history table in SQL Server

I have 2 tables, Table-A and Table-A-History. Table-A contains current data rows. Table-A-History contains historical data I would like to have the most current row of my data in Table-A, and Table-A-History containing historical rows. I can think of 2 ways to accomplish this: whenever a new data row is avail…

Problems with Postgresql CASE syntax

Here is my SQL query: When I execute the above query, I get this error: ERROR: CASE types record and integer cannot be matched From the error message I understand that PostgreSQL take the second select, respectively elapsed_time_from_first_login as a row, even if it will always be a single value (because of t…

SQL Column definition : default value and not null redundant?

I’ve seen many times the following syntax which defines a column in a create/alter DDL statement: The question is: since a default value is specified, is it necessary to also specify that the column should not accept NULLs ? In other words, doesn’t DEFAULT render NOT NULL redundant ? Answer DEFAUL…

Combine multiple SELECT statements

I’ve used Excel to generate numerous SELECT statements from a list of the schema names from a database with a large number of identical schemas: select result from foo.table limit 1; select result …

Inner Joining three tables

I have three tables I wish to inner join by a common column between them. Say my tables are; TableA TableB TableC I wish to join A-B, but then also B-C all by this common field I will call common. …

GROUP BY and COUNT in PostgreSQL

The query: Returns n records in Postgresql: I just want to retrieve the number of records returned: 6. I used a subquery to achieve what I want, but this doesn’t seem optimum: How would I get the number of records in this context right in PostgreSQL? Answer I think you just need COUNT(DISTINCT post_id) …

Insert multiple rows into single column

I’m new to SQL, (using SQL 2008 R2) and I am having trouble inserting multiple rows into a single column. I have a table named Data and this is what I am trying That code was taken from this question, but it, like many other examples I have found on the web uses 2 columns, I just want to use