How to aggregate this in SQL [closed] - sql

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 5 years ago.
Improve this question
Based on the table structure below, I want to count a value only once per column value.
Person ID Email Template Email Event
1 A DELIVER
1 A OPEN
1 A OPEN
1 B CLICK
2 A DELIVER
2 A OPEN
3 A DELIVER
3 C DELIVER
What I would like to is to get number of email delivered, clicked and opened for each person. I am looking to get this number for each person not necessarily for templates. All email templates are delivered just once to a person but they can open and click that template multiple times. I would like to not include the repeat opens and click in this aggregation.

Something like this? I'm not sure what your table is called
select t.person_id,
sum(case when t.email_event_type = 'DELIVER' then 1 else 0) deliver_count,
sum(case when t.email_event_type = 'OPEN' then 1 else 0) open_count,
sum(case when t.email_event_type = 'CLICK' then 1 else 0) click_count
from <your_table_name> t
group by t.person_id

Related

How to get column values as a table column in sql [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 3 years ago.
Improve this question
This is my table defined in the database:
pi_value | Status
------------+-------------------
500.000 | Bank Submitted
500.0000 | Bank Submitted
1000.0000 | Maturity Received
4000.0000 | Bank Submitted
50.0000 | Maturity Received
I want the output to look like this:
Maturity Received | Bank Submitted |
------------------+----------------+
1050.0000 | 5000.0000 |
------------------+----------------+
use conditional aggregation
select sum(case when status='Bank Submitted' then pi_value else 0 end),
sum(case when status='Maturity Received' then pi_value else 0 end) from table
As you say in a comment that the status is not before-known, just select the mere data and have your app or website do the layout:
select status, sum(pi_value) as total
from mytable
group by status
order by status;
Whatever programming language you are using, it will be easy to simply loop through the result set and fill some grid with it.
Alternative:
If you want to get the pivoted data right away, you'll have to use dynamic SQL instead. This means you select the data needed to build the query first, then you build the query from it and run it. Here is how to get the statuses:
select distinct status from mytable order by status;
Then loop through these and construct a query like shown in Zaynul Abadin Tuhin's answer. Then run that query.

SQL QUERY SQL SSRS [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 4 years ago.
Improve this question
Hi can anyone help me with sum up first two rows in table and then rest would be same. example is
ID SUM
12 60
0 20
1 30
2 50
3 60
I am expecting
ID SUM
0 80
1 30
2 50
3 60
I am doing this from memory - so if this doesnt work let me know and we can do it another way looking at the row number;
Assuming you have a unique ID to sort it by as you suggested, you could do something like this;
you may want to change the order to be desc if that's how you classify your 'top 2'
SELECT TOP 2 ID,
SUM(VALUE)
FROM [Table]
GROUP BY ID
ORDER BY ID
UNION
SELECT ID,
VALUE
FROM [Table]
WHERE ID NOT IN (SELECT TOP 2 ID
FROM [Table] ORDER BY ID)

How to use Sum() function with condition? [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 5 years ago.
Improve this question
I have an Access database. There is a table named cost with bottom value:
reson Cost Type
------------ ------ ------
A1 2500 1
A1 6500 1
A2 95000 2
A3 2500 1
A1 6500 1
A4 50000 2
Now I want a query that calculate sum of all cost filed where type = 2 and sum of cost filed where type = 1 and substract the first value from the second value.
For example, the above pic calculate final result:
Sum of Type 2 = 145000
Sum of Type 1 = 18000
-------------------------
Final Result = 127000
My Sql Code
select iif(type = 2, sum(cost), -sum(cost)) As col1 from cost group by type
First off, I'm sorry you have to deal with such obnoxious hostility when asking your question here. You asked your question perfectly fine, laying out your table structure, and your desired result. It's understandable that you are new to queries and need help creating them. Not every answer requires code, and not every person knows where to start.
Here is your answer:
Step 1
Make sure you have your table created with the data you provided
Step 2
Create a new query named qySumType1. Build it like this, so it sums everything of type=1. make sure to click the totals button.
Step 3
Create another query, name this one qySumType2. This query should sum everything of type=2.
Step 4
Now create another query called "Final". Add both of your previous queries to it. Now create an expression in the last column to calculate the difference between the 2 numbers. Just like this.
And there you have it. Now just run the Final query anytime you want to get the difference.
Hope this helps! I can't tell you how many times I've started learning something new and relied on a community to help me get started. Always just try your best and wait for a decent answer to your question. Good luck!
Change T1 to the name of your table.
SELECT Sum(T.Type1) AS Type1, Sum(T.Type2) AS Type2, Sum(T.Type2) - Sum(T.Type1) AS DIFF
FROM
(
SELECT Sum(T1.Cost) AS Type1, 0 AS Type2
FROM T1
WHERE (((T1.Type)=1))
UNION
SELECT 0 AS Type1, Sum(T1.Cost) AS Type2
FROM T1
WHERE (((T1.Type)=2))
) AS T;
Type1 | Type2 | DIFF
18000 | 145000 | 127000

How to get results in a custom order? [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 9 years ago.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Instead of ASC or DESC, I want my query results to be in a specific, custom order.
For example, instead of A, B, C, D..., what if I wanted my results in, P, A, L, H...?
I have tried using case but not successfully
SELECT * FROM Customers
ORDER BY case country
when 'P' then 1 …
E.g., here, I'm trying to create a custom order on the Country column:
SELECT * FROM Customers
ORDER BY case when country = 'P' then 1
when country = 'A' then 2
when country = 'L' then 3
when country = 'H' then 4
else 5
end asc

SQL display in groups [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 table of projects which are all tagged with a lookup for the associated client and want to show the data like this:
Client 1
Project 1
Project 2
Project 3
Client 2
Project 1
Project 2
Client 3
Project 1
Project 2
Project 3
That way I get the projects back from my table and have them "grouped" by client.
What do you call this action? Is it nested groups or something like that?
SQL queries cannot supply a nested result as you have laid out1. Instead, your result set has to be flat, repeating the Client multiple times, once for each Project. Really in your SQL query, it is just a matter of ordering to sort the like Client values together: ORDER BY client
Client 1 Project 1
Client 1 Project 2
Client 1 Project 3
Client 2 Project 1
Client 2 Project 2
Client 2 Project 3
In your application display logic then, you only print the new Client when the value changes from the previous one.
Pseudocode:
# Start with empty last_client
last_client = ""
loop_over_rows
if last_client is not equal to current_row->client
print new row->client
# Print all projects
print row->project
# Store the current client to compare on next loop
last_client = row->client
endloop
1 It can be done in SQL with a lot of crazy string manipulation and UNION, but this is a matter of presentation and really belongs in the application presentation logic rather than the SQL.
If your RDBMS, which is not specified in your tags, support recursive SQL you can approximate the layout you are trying to accomplish.
The following SQL works against Teradata's data dictionary tables. The Hierarchy column is offset based on depth of the database in the ownership chain. From there you should be able to take the concept and apply it to your situation.
WITH RECURSIVE cte (DatabaseName, Path, Parent, Level) AS
(
SELECT TRIM(DatabaseName)
,DatabaseName(VARCHAR(600))
,TRIM(DatabaseName)
,0 (BYTEINT)
FROM DBC.Databases d
WHERE DatabaseName = 'DBC'
UNION ALL
SELECT TRIM(d.DatabaseName)
,cte.Path || '.' || TRIM(d.DatabaseName)
,cte.Path
,Level + 1
FROM DBC.Databases d
,cte
WHERE d.OwnerName = cte.DatabaseName
AND d.DatabaseName <> d.OwnerName
AND Level < 20
)
SELECT Level
, SUBSTRING(CAST('' AS CHAR(60)) FROM 1 FOR Level * 2) || DatabaseName AS Hierarchy
, Path
, Parent
FROM cte
ORDER BY Path;