SQL,Trying to get the sum with a datetime column [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have 2 columns, one is showing the email and the other one shows the date the email was created.
What I'm trying to do is something like this:
registered customer 4 Date 01-01-2018
registered customer 2 Date 01-02-2018
registered customer 9 Date 01-03-2018
Any help would be appreciated.
Thanks

If table stucture consist of 2 columns(email, created_at) you should groupping your data by created_at field.
Example:
select date, count(email) as count
from your_table
group by date

You appears to want :
select date, count(email) as emailcount
from table t
group by date;
EDIT : Use count() instead of sum() & if you want to count day wise email as registered customer then use the date in GROUP BY clause instead of doing aggregation.
SELECT SITEID, THEDATE, COUNT(EMAIL)
FROM [database].[dbo].[table]
WHERE LOGIN NOT LIKE '' AND SITEID = 'someSiteId' AND
THEDATE >= '2017-01-01'
GROUP BY SITEID, THEDATE
ORDER BY THEDATE ASC;

Depending on how many entries you have, you could also do:
Select Num, date
FROM table
ORDER BY date
Then make Num be an identity column in the table that matches the email with a customer number. Maybe not as ideal depending on your data...but if you are referring to the customer by their number you may potentially want that info in the table.

Related

Difference between Group by queries with and without Having [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 15 days ago.
Improve this question
I'm new to SQL queries so I'm trying to understand the difference between a select query with and without having. In the task, I need to report the first login date for each player.
How the table is arranged:
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| player_id | int |
| device_id | int |
| event_date | date |
| games_played | int |
+--------------+---------+
The first attempt, in which I wrote having, was unsuccessful.
SELECT player_id, event_date as first_login FROM Activity
GROUP BY player_id
HAVING MIN(event_date)
Failed Test
{"headers":{"Activity":["player_id","device_id","event_date","games_played"]},"rows":{"Activity":[[1,2,"2016-03-01",5],[1,2,"2016-05-02",6],[1,3,"2015-06-25",1],[3,1,"2016-03-02",0],[3,4,"2016-02-03",5]]}}
This option passed all tests:
SELECT player_id, MIN(event_date) as first_login FROM Activity
GROUP BY player_id
Why is it impossible to write HAVING in the first option, what is the difference between queries?
HAVING as well as WHERE are both filtering clauses. You use WHERE to specify filtering condition before grouping takes place. You can use HAVING to further filter grouped results.
Below you can see some example of viewing all the customers from specific state - WHERE who spend more than 70$. Here you need HAVING since you need to filter from grouped results. In order to see who spent more than 70$ you need to calculate how much they spent. I hope this clarifies your question.
SELECT oi.order_id, SUM(quantity * unit_price) total_spent, CONCAT(c.first_name, ' ', c.last_name) AS fullname, c.state
FROM order_items oi
JOIN orders o USING(order_id)
JOIN customers c USING(customer_id)
WHERE c.state = 'VA'
GROUP BY order_id
HAVING total_spent > 70
ORDER BY order_id;
The HAVING clause is used to place conditions on groups created by the GROUP BY clause, similar to how the WHERE clause places conditions on columns.
You don't need a HAVING clause to solve this problem (as you noted). You might use a HAVING clause if you needed to report the first login date for each player before or after a specific date.
SELECT player_id, MIN(event_date) as first_login
FROM Activity
GROUP BY player_id
HAVING MIN(event_date) > '2020-12-31'

How to count mention in one day [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am try to understant how to make a table that tells me in which specific day were written the most mentions about the persons in my table.
SELECT
person,
COUNT(1) AS count_mentions,
COUNT(DISTINCT current_date) AS mention_per_date,
FROM
`aesthetic-honor-311413.big_data_alon_peled_2021.israel_media_person`
GROUP BY
person
ORDER BY
current_date asc
LIMIT
10;
EXPECTED RESULT:
person mention_per_date
Tomer 24
Shalev 18
Yosef 15
Eran 15
Gal 11
(Fictive names and numbers)
I am try to understant how to make a table that tells me in which specific day were written the most mentions about the persons in my table.
For this question, I would expect a query like this:
SELECT person, date, COUNT(*)
FROM `aesthetic-honor-311413.big_data_alon_peled_2021.israel_media_person`
GROUP BY person, date
QUALIFY ROW_NUMBER() OVER (PARTITION BY person ORDER BY COUNT(*) DESC) = 1;
This returns the date that most frequently occurs for each person. You don't mention the name of the date column, so this just uses date.

Select users with conditional counts in a date range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am having a problem to write a select to a following table:
user_id
email
user_name
task_id
status
date
123
123#email.ru
user_user
456
approve (or decline)
2021-03-3121:11:10.367485
I need to make a select for dates 1.03 to 31.03 for each user (name, email and id) to show how many tasks he did in total, how may of them were approved and how many were declined.
1.You should show the table structure first by
\d table_name。I need know the type of date
2.I guess the type date is timestamp.
3.total_count he done:
select count(1) from table_name where user_id=1231 and date between '2021-03-01' and '2021-04-01';
4.group by status
select count(1),status from table_name where user_id=123 and date between '2021-03-01' and '2021-04-01' group by status

on the basis of one condition try to pick data from another field and then pull the data on the basis of second value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a table where date, transaction id, amount is available. "transaction id"s are numeric and alpha-numeric. First I need to check the first case where "transaction id" starts with a particular letter and then I need to pull the "date" for that particular "transaction id". Then according to that particular "date" I need to pull transaction data. Please help me how to do this.
You can use an EXISTS clause to check for the date in a subquery like so.
This will return all transactions that occurred on a date that had a transaction_id starting with the letter N
SELECT *
FROM some_table st
WHERE EXISTS
( SELECT 1
FROM some_table st1
WHERE st1.date = st.date
AND st1.transaction_id LIKE 'N%'
)
FIDDLE DEMO
these can also use an index on transaction_id which can speed things up
You can find date(s) for transactionId, that starts with; lets say 'A':
SELECT date FROM your_table WHERE transactionId LIKE 'A%';
Now you may have multiple records as you are searching with 'LIKE', then according to the date(s) you can fetch data from your table as:
SELECT * FROM your_table WHERE date = '2014-12-25 10:40:00';
But if you know your transactionId accurately, then following query will be sufficient.
SELECT * FROM your_table WHERE transactionId = 'A1001';

SELECT DISTINCT on one column, with multiple columns returned [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a database with 20 columns but on my select I only want the following 3 columns:
Task
UniqueID
MessageID (unique identifier)
How can I get all the distinct UniqueID while returning the above 3 columns.
SELECT DISTINCT(UniqueID), Task, MessageID
doesn't seem to work? :/
DISTINCT is not a function :
SELECT DISTINCT UniqueID
, Task
, MessageID
FROM table
But this will return all unique rows not unique UniqueID. If you want to retrieve only rows with different UniqueID you should use the GROUP BY clause and aggregate functions with fields Task and MessageID to manipulate grouped data.
Try something like:-
SELECT task,
UniqueID,
MessageID,
FROM (SELECT task,
UniqueID,
MessageID,
Row_number() OVER(PARTITION BY UniqueID ORDER BY MessageID) rn
FROM TableName) t
WHERE rn = 1