How to get column values as a table column in sql [closed] - sql

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.

Related

Get intermediate time periods in SQL Server [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 4 years ago.
Improve this question
I want to generate a table in SQL of intermediate joined states. E.g. I have the following table
status_1 status_2 start_date_V1 end_date_v1 start_date_2 end_date_v2
--------------------------------------------------------------------------------
A B 01Jan2018 31Jul2018 31Dec2017 31Jan2018
A C 01Jan2018 31Jul2018 01Feb2018 30Dec2018
In this table there are start and end dates of the different states "status_1" and "status_2". I wan to have the information about the changes of the two joined states. The desired table would be:
status_1 status_2 start_date end_date
-----------------------------------------------
A B 01Jan2018 31Jan2018
A C 01Feb2018 31Jul2018
The following image might help to understand the problem:
Can anyone help?
Seems like you need the intersecting time period(?), that'd be solved with a simple 'CASE-WHEN-ELSE'-statement for each date in the query result.
SELECT
[status1],
[status2],
[start_date] = CASE WHEN [start_date_V1] < [start_date_2] THEN [start_date_V1] ELSE [start_date_2] END,
[end_date] = CASE WHEN [end_date_v1] < [end_date_v2] THEN [end_date_v1] ELSE [end_date_v2] END
FROM Table
If you've got many date columns (known amount), it'd be cleaner to type it as below. However, beware that sub queries like this can slow down your queries tremendously, if you don't know what you're doing.
SELECT
Status1,
Status2,
-- New Name Name of custom group of values Column1 Column2 Name of custom group of values
-- | | | | |
[start_date] = (SELECT MAX(StartDate) FROM (VALUES (start_date_1), (start_date_2)) AS value(StartDate)),
[end_date] = (SELECT MIN(EndDate) FROM (VALUES (end_date_1), (end_date_2)) AS value(EndDate))
FROM Table

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 aggregate this in SQL [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 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

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

Counting frequency of customer [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 this table on sql sever
cstomer |No_Nota
CUS000 | 98342
CUS000 | 98343
CUS000 | 98343
CUS001 | 98355
CUS001 | 98355
I would like to count the frequency of each customer. For similar number of no_nota the value is 1.
I'd like a result like this:
cstomer |Frequent
CUS000 | 2
CUS001 | 1
You want the distinct count of the column no_nota, so that's what you should select...
select customer, count(distinct no_nota) as frequent
from my_table
group by customer
You want to count the result of your select query:
SELECT COUNT(expression)
FROM tables
WHERE predicates;
Example:
SELECT COUNT(No_Nota) FROM your_table WHERE No_Nota > 0;
Sounds like you just want a group by statement to get a count of all individual customers. Something like:
SELECT cstomer, SUM(1) as Frequent FROM table GROUP BY cstomer, No_Nota