Postgresql query (on a fixed value of a column of a table) - postgresql-9.5

I am a beginner in postgresql and I would like to ask a question.
I have a table (mytable) which has three columns (Customer ID(text), time(varchar), Consumption(integer))
one row, for example, is:
customer_id time consumption
C11 monday 290
I want to find the average consumption on sunday (only on this specified day).
I tried
SELECT AVG(Consumption)
FROM mytable
WHERE time='sunday';
but it doesn't work. I think the problem exists in the WHERE statement.

you can use like operator
EX:
select ...
where
time like '%sunday'
but check if exists some empty space or something like this

Related

SQL for append rows based on max date

This is more of a logic question as I am having a hard time wrapping my head around it.
Say I have table 1 that is truncated and populated everyday, and a time stamp column is added onto it. Everyday new records would be added to the table.
That table 1 is copied to table 2 initially, however on consequent runs I only want to add the new records from table 1 into table 2.
I know this will be a mixture of matching the columns and only importing the MAX DATES, however confused as to the actual logic of the query.
So in short I want to append only the latest rows from table 1 to table 2 based on the max date.
If you want to sync the tables daily, you may just look for timestamp_column > current_Date.
If you want to get the max dates, you can write something like this:
INSERT INTO table2 (x,y,z, timestamp_column)
SELECT x,y,z, current_timestamp() FROM table1
WHERE timestamp_column >
(SELECT IFNULL(MAX(timestamp_column), '0001-01-01' ) FROM table2);
On the other hand, I think Snowflake streams are a very good fit for this task:
https://docs.snowflake.com/en/user-guide/streams-intro.html
You can create an "Append-only" stream on table1, and use it as a source when synchronizing to table2.

Optimal SQL query for querying 31 tables (containing datestamp in tablename)

Fairly new to SQL and I was stumped on this question I received in an interview recently.
The question was along the lines of how would you count the total occurrences of 'True' for Column B in July.
Problem was; there was no date or timestamp column in the table. Instead the table naming convention was defined as "ProductX_YYYYMMDD". The assumption being that a new table is created for each day's data dump.
Is there an efficient query I can write to obtain the True COUNTs of Column B for each table (which doesn't involve ~30 JOIN or UNION statements to get the answer)?
Use STRING_SPLIT(myColumn, '_')
Then
SELECT RIGHT (SELECT LEFT(tempColumn, -4)), -2)
Now you have a temp table filled with only month |MM| and you can use
COUNT() FROM dailyTable WHERE dailyName like '07'
Add the count of every daily Table to a variable

Counting the number of times same record exist in a given period of time

I am trying to write a query to find out whether a record exist more than one or not in a given period of time. And even if it exist, how many times the same record has been repeated.
Now to solve this issue, I have sorted the records.
select * from table_name where date = ? and date > ? order by email
And trying to count the number of times the same record exist.But I am not able to figure out a way to count the number of times the same record exists.
Here is a problem.The image below holds the basic data structure.
Here is the expected output for a year
The table above holds Xyz name and xyz#email.com data three times. And the name Abc and email abc#email.com two times and the third record name Def and email def#email.com two times. Now what I am trying to figure out a way to find out the number of times each records are being repeated in a given period of time using a single query. I am thinking to make use of recursion on a record and count till it didn't find a different record after sorting it. But using recursion on every records seems expensive.
Is there a better solution to solve this problem ?
Regards
Group and count.
SELECT column_to_compare1, column_to_compare2, COUNT(*)
FROM table_name
WHERE [date] BETWEEN #date1 AND #date2
GROUP BY column_to_compare1, column_to_compare2
HAVING COUNT(*) > 1 -- IF YOU WANT TO ONLY INCLUDE RECORDS WITH DUPLICATES
Between is inclusive, so you can adjust your dates with DATEADD if you really want between.
You can use the COUNT function to do this.
To do this using your own query:
SELECT Name, Email, COUNT(*) AS count
FROM table_name
WHERE date BETWEEN '01/01/2005' AND '31/12/2005'
GROUP BY Name, Email
However your example query is poor so I cannot give you a better solution. Here is an example of this working: SQL Fiddle
EDIT: Updated my solution to match you expected output.

How is it possible to see a column name from a different table within a subquery from different table?

I was practicing a subqueries in sql and all of a sudden i jumped into an unsual query which i never thought of could happen.
The question of my query is....
Write a query to display the average rate of Australian dollar,where the currency rate date is July 1 2005??
And the query was...
USE AdventureWorks2012
SELECT AverageRate FROM Sales.CurrencyRate
WHERE ToCurrencyCode='AUD' AND CurrencyRateDate IN
(SELECT CurrencyRateDate FROM Sales.Currency
WHERE CurrencyRateDate='2005-07-01')
So,my question is how is it possible to get the column name "CurrencyRateDate" in the sub query when it is actually from the table "CurrencyRate"??
I know my query is not in the correct format as it should be.
I'm extremely sorry if my title doesn't make sense.If you guys can give any better please change it..
Thanks
AND CurrencyRateDate IN
(SELECT CurrencyRateDate FROM Sales.Currency
WHERE CurrencyRateDate='2005-07-01')
All the CurrencyRateDate references here point to the column from the outer query.
So for each row in the outer query, you are getting a list consisting of only that row's CurrencyRateDate, repeated once for every row in the Sales.Currency table (if the CurrencyRateDate of that row is 2005-07-01, otherwise the list is empty).
Then you check whether the outer CurrencyRateDate value is in that list. Which it is, if and only if it's equal to 2005-07-01 (assuming there is at least one row in Sales.Currency).
So your query is equivalent to:
SELECT * FROM Sales.CurrencyRate
WHERE ToCurrencyCode='AUD' AND CurrencyRateDate='2005-07-01'

How to find rows which differ by a given amount in SQL?

So I have a data table which looks like
Where each row has a timestamp column in Unix time. I need to find all the places where two entries with the same resource_id are x(day month, year etc) amount of time apart, so I need a query that will go through and look at the differences between one row and the next and spit back the ones which differ by more than a specified amount.
Anybody have any ideas on how to do this? Thanks in advance
You may use a cross join to compare every row in the table with every other row in the table then compare the field. For example the following will return where the two rows are 2 months apart.
SELECT t.resource_id, s.resource_id
FROM table t CROSS JOIN table s
WHERE TIMESTAMPDIFF(MONTH,t.timestamp,s.timestamp) = 2
Note that this could be extremely slow if the table is large. Or according to the MySQL docs just saying JOIN without specifying the condition will result in a cartesian product which is equivalent to a cross join.