Group by with similar ID - sql

I have a table that has multiple items with slightly different IDs that I want to group together under one common ID. My table looks like this:
ID | Total
alex_dog 1
ben_dog 2
charlie_dog 3
alex_cat 4
ben_cat 5
charlie_cat 6
And I want to be able to group them into one table to look like this:
ID | total
dog 6
cat 15
If i leave the _ before the ID that is fine. Is is possible to do a groupby query where you can groupby '_%ID%'?

This can be done with a bit of string manipulation to get everything after _ along with a group by on the same value
SELECT SUBSTRING(ID,CHARINDEX('_',ID)+1,LEN(ID)), SUM(Total) as Total
FROM Data
GROUP BY SUBSTRING(ID,CHARINDEX('_',ID)+1,LEN(ID))
Live example: http://rextester.com/BHNY98025

Related

Count distinct on field value

I have a specific problem to count and do an operation on fields on MySQL database. I cannot describe the situation well because there are some specific things I need to do. Let's take an example
name | quantity(varchar)
----------------------
Test 10
Bar 5
Foo 2
Test 5
Bar 5
Foo 10
Bar 5
Foo 5
What I want is to select the count for each name and show only one name per line with the total. I have to cast the text to integer. But I don't want to specify the WHERE name='xyz' (because I will have many many names). I tried to do a query that will select all the names and the total.
Name | Total
----------------------
Test 15
Foo 17
Bar 15
And so on if there are +100 distinct names.
you can use sum and group by for that:
select name, sum(quantity) from yourTable group by name;

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)

Same entity from different tables/procedures

I have 2 procedures (say A and B). They both return data with similar columns set (Id, Name, Count). To be more concrete, procedures results examples are listed below:
A:
Id Name Count
1 A 10
2 B 11
B:
Id Name Count
1 E 14
2 F 15
3 G 16
4 H 17
The IDs are generated as ROW_NUMBER() as I don't have own identifiers for these records because they are aggregated values.
In code I query over the both results using the same class NameAndCountView.
And finally my problem. When I look into results after executing both procedures sequentially I get the following:
A:
Id Name Count
1 A 10 ->|
2 B 11 ->|
|
B: |
Id Name Count |
1 A 10 <-|
2 B 11 <-|
3 G 16
4 H 17
As you can see results in the second set are replaced with results with the same IDs from the first. Of course the problem take place because I use the same class for retrieving data, right?
The question is how to make this work without creating additional NameAndCountView2-like class?
If possible, and if you don't really mind about the original Id values, maybe you can try having the first query return even Ids :
ROW_NUMBER() over (order by .... )*2
while the second returns odd Ids :
ROW_NUMBER() over (order by .... )*2+1
This would also allow you to know where the Ids come from.
I guess this would be repeatable with N queries by having the query number i selecting
ROW_NUMBER() over (order by .... )*n+i
Hope this will help

Counting the number of rows based on like values

I'm a little bit lost on this. I would like to list the number of names beginning with the same letter, and find the total amount of names containing that first same letter.
For instance:
name | total
-------|--------
A | 12
B | 10
C | 8
D | 7
E | 3
F | 2
...
Z | 1
12 names beginning with letter 'A', 10 with 'B' and so on.
This is what I have so far
SELECT
LEFT(customers.name,1) AS 'name'
FROM customers
WHERE
customers.name LIKE '[a-z]%'
GROUP BY name
However, I'm unsure how I would add up columns based on like values.
This should work for you:
SELECT
LEFT(customers.name,1) AS 'name',
COUNT(*) AS NumberOfCustomers
FROM customers
WHERE
customers.name LIKE '[a-z]%'
GROUP BY LEFT(customers.name,1)
EDIT: Forgot the explanation; as many have mentioned already, you need to group on the calculation itself and not the alias you give it, as the GROUP BY operation actually happens prior to the SELECT and therefore has no idea of the alias yet. The COUNT part you would have figured out easily. Hope that helps.
You don't want to count the names, but only the first letters. So you must not group by name, but group by the first letter
SELECT LEFT(name, 1) AS name, count(*)
FROM customers
GROUP BY LEFT(name, 1)
SQLFiddle

Conditioning on multiple rows in a column in Teradata

Suppose I have a table that looks like this:
id attribute
1 football
1 NFL
1 ball
2 football
2 autograph
2 nfl
2 blah
2 NFL
I would like to get a list of distinct ids where the attribute column contains the terms "football", "NFL", and "ball". So 1 would be included, but 2 would not. What's the most elegant/efficient way to do this in Terdata?
The number of attributes can vary for each id, and terms can repeat. For example, NFL appears twice for id 2.
You can use the following:
select id
from yourtable
where attribute in ('football', 'NFL', 'ball')
group by id
having count(distinct attribute) = 3
See SQL Fiddle with Demo (fiddle is showing MySQL, but this should work in TeraData)