Group and Combine Rows In SQL - sql

I have a table that I unfortunately can't alter, but I can use to create a view. The issue is that the data in one column is spread across multiple rows. Here's a sample of what that looks like:
Customer
Activity
Note
Sequence
1
Note
The custo
1
1
Note
mer calle
2
1
Note
d and lef
3
1
Note
t a messa
4
1
Note
ge.
5
1
Charge
$39.95
6
2
Charge
$14.47
7
I need the data to look like this:
Customer
Activity
Note
1
Note
The customer called and left a message.
1
Charge
$39.95
2
Charge
$14.47
Any ideas on how to do it?

select customer
,Activity
,string_agg(note, '') within group (order by Sequence) as note
from t
group by customer, Activity
customer
Activity
note
1
Charge
$39.95
2
Charge
$14.47
1
Note
The customer called and left a message
Fiddle

try this:
STRING_AGG
Search for the syntax dialect according to the software you are using.

Related

Select query to fetch required data from SQL table

I have some data like this as shown below:
Acc_Id || Row_No
1 1
2 1
2 2
2 3
3 1
3 2
3 3
3 4
and I need a query to get the results as shown below:
Acc_Id || Row_No
1 1
2 3
3 4
Please consider that I'm a beginner in SQL.
I assume you want the Count of the row
SELECT Acc_Id, COUNT(*)
FROM Table
GROUP BY Acc_Id
Try this:
select Acc_Id, MAX(Row_No)
from table
group by Acc_Id
As a beginner then this is your first exposure to aggregation and grouping. You may want to look at the documentation on group by now that this problem has motivated your interest in a solutions. Grouping operates by looking at rows with common column values, that you specify, and collapsing them into a single row which represents the group. In your case values in Acc_Id are the names for your groups.
The other answers are both correct in the the final two columns are going to be equivalent with your data.
select Acc_Id, count(*), max(Row_No)
from T
group by Acc_Id;
If you have gaps in the numbering then they won't be the same. You'll have to decide whether you're actually looking for a count of rows of a maximum of a value within a column. At this point you can also consider a number of other aggregate functions that will be useful to you in the future. (Note that the actual values here are pretty much meaningless in this context.)
select Acc_Id, min(Row_No), sum(Row_No), avg(Row_No)
from T
group by Acc_Id;

SELECT query for selecting all field and counting the contents of the fields

Hello can anyone help me with a query where you select all columns and then count the content of the columns. Not sure how this could be achieved can someone shed some light on that.
For example I have a table that returns number of tickets but those tickets can be more than one so I would like to select all the tickets depending on a variable and then count the contents of it like this:
Booking ID | Tickets | variable
1 2
2 1
3 5
How would I go about to count the contents of for example booking 1 and 3 so that the result will be 7
Thank you.
SELECT SUM(Tickets) FROM your_table WHERE Booking_ID IN (1,3)

T-SQL 2008 INSERT dummy row WHEN condition is met

**Schema & Dataset**
id version payment name
1 1 10 Rich
2 1 0 David
3 1 10 Marc
4 1 10 Jess
5 1 0 Steff
1 2 10 Rich
2 2 0 David
3 2 10 Marc
4 2 10 Jess
5 2 0 Steff
2 3 0 David
3 3 10 Marc
4 3 10 Jess
http://sqlfiddle.com/#!3/1c457/18 - Contains my schema and the dataset I'm working with.
Background
The data above is the final set after a stored proc has done it's processing so everything above is in one table and unfortunately I can't change it.
I need to identify in the dataset where a person has been deleted with a payment total greater than 0 in previous versions and insert a dummy row with a payment of 0. So in the above example, Rich has been deleted in version 3 with a payment total of 10 on previous versions. I need to first identify where this has happened in all instances and insert a dummy row with a 0 payment for that version. Steff has also been deleted on version 3 but she hasn't had a payment over 0 on previous versions so a dummy row is not needed for her.
Tried so far -
So I looked at pinal dave's example here and I can look back to the previous row which is great so it's a step in the right direction. I'm not sure however of how to go about achieving the above requirement. I've been toying with the idea of a case statement but I'm not certain that would be the best way to go about it. I'm really struggling with this one and would appreciate some advice on how to tackle it.
You can do this by generating all possible combinations of names and versions. Then filter out the ones you don't want according to the pay conditions:
select n.name, v.version, coalesce(de.payment, 0) as payment
from (select name, max(payment) as maxpay from dataextract group by name) n cross join
(select distinct version from dataextract) v left outer join
dataextract de
on de.name = n.name and de.version = v.version
where de.name is not null or n.maxpay > 0;
Here is a SQL Fiddle.

Group by and sorting before a where clause?

I have a database that looks like this:
ID parent ticket category _record_status _log_user _log_timestamp _log_type
------------------------------------------------------------------------------------------------
1 1 1 1 active 1 2012-01-29 15:49:21 create
2 1 1 1 deleted 1 2012-01-29 15:52:14 destroy
3 3 1 2 active 1 2012-01-29 15:58:43 create
I want to be able to select all records that are not deleted for a specified ticket. However, this is difficult because of the revision system.
Is there a way to do this via SQL that is efficient, or would it be better to just do this in PHP, considering each ticket will only have a few records on average. I doubt any ticket would ever get more than 100 records in it's lifetime.
select *
from YourTable
where _record_status <> 'deleted'
and ticket = #yourTicketId
Is that what you're looking for??
Sql!
SELECT .....
FROM .....
WHERE _record_status!="deleted" AND ticket=X

Progressive count using a query?

I use this query to
SELECT userId, submDate, COUNT(submId) AS nSubms
FROM submissions
GROUP BY userId, submDate
ORDER BY userId, submDate
obtain the total number of submissions per user per date.
However I need to have the progressive count for every user so I can see how their submissions accumulate over time.
Is this possible to implement in a query ?
EDIT: The obtained table looks like this :
userId submDate nSubms
1 2-Feb 1
1 4-Feb 7
2 1-Jan 4
2 2-Jan 2
2 18-Jan 1
I want to produce this :
userId submDate nSubms progressive
1 2-Feb 1 1
1 4-Feb 7 8
2 1-Jan 4 4
2 2-Jan 2 6
2 18-Jan 1 7
EDIT 2 : Sorry for not mentioning it earlier, I am not allowed to use :
Stored procedure calls
Update/Delete/Insert/Create queries
Unions
DISTINCT keyword
as I am using a tool that doesn't allow those.
You can use a self-join to grab all the rows of the same table with a date before the current row:
SELECT s0.userId, s0.submDate, COUNT(s0.submId) AS nSubms, COUNT (s1.submId) AS progressive
FROM submissions AS s0
JOIN submissions AS s1 ON s1.userId=s0.userId AND s1.submDate<=s0.submDate
GROUP BY s0.userId, s0.submDate
ORDER BY s0.userId, s0.submDate
This is going to force the database to do a load of pointless work counting all the same rows again and again though. It would be better to just add up the nSubms as you go down in whatever script is calling the query, or in an SQL variable, if that's available in your environment.
The Best solution for this is to do it at the client.
It's the right tool for the job. Databases are not suited for this kind of task
Select S.userId, S.submDate, Count(*) As nSubms
, (Select Count(*)
From submissions As S1
Where S1.userid = S.userId
And S1.submDate <= S.submDate) As TotalSubms
From submissions As S
Group By S.userid, S.submDate
Order By S.userid, S.submDate