How can I select multiple columns and group them by 2 or 3 of them in SQL? - sql

I got stuck on a simple task and it sounds like this: I need to write a select query, so it returns me 5-6 columns grouped by 2-3 of them.
So, for example it would look like this:
Select SSN,ClientTypeId,Id,Count(SSN)
From Contact
Where ClientTypeId = 'GUID for active clients'
Group By SSN,ClientTypeId
All I want to do is to get a list of customers (identified by SSN) who has several active contacts.
I have found examples where multiple columns were grouped by one columns, but couldn't find something like my case. Thank you!

Your GROUP BY needs to go after your WHERE clause and all non-aggregated columns need to be in your GROUP BY e.g.
Select SSN,ClientTypeId,Id,Count(SSN)
from Contact
where ClientTypeId = 'GUID for active clients'
Group By SSN,ClientTypeId,Id

Related

Validate that only one value exists

I have a table with two relevant columns. I'll call them EID and MID. They are not unique.
In theory, if the data is set up correctly, there will be many records for each EID and every one of those records should have the same MID.
There are situations where someone may manually update data incorrectly and I need to be able to quickly identify if there is a second MID for any EID.
Ideally, I'd have a query that returns how many MIDs for each EID, but only showing results where there is more than 1 MID. Below is what I'd like the results to look like.
EID Count of Distinct MID values
200345 2
304334 3
I've tried several different forms of queries, but I can't seem to figure out how to reach this result. We're on SQL Server.
You can use the following using COUNT with DISTINCT and HAVING:
SELECT EID, COUNT(DISTINCT MID)
FROM table_name
GROUP BY EID
HAVING COUNT(DISTINCT MID) > 1
demo on dbfiddle.uk

Repeat a query over the parameter list

I would like to iterate the same query while using different parameter values from a predefined list.
Say, I have a table with two columns. The first columns contains customer name. The second column contains customer spending.
###CUSTOMER; SPENDING###
customer1; 1000
customer2; 111
customer3; 100
customer1; 323
...
I know the complete list of customers: customerlist = {customer1, customer2, customer3}.
I would like to do something like:
Select sum(spending)
from mytable
where customer = #customerlist
The query should compute the sum of spending for each customer defined in the customer list. I have found some examples of sql procedures with stored parameters but not the case with one parameter of multiple values.
Thank you
P.S. This is just a hypothetical example to illustrate my question (I know it would be much more effective to use here a simple group by).
You can use nested query like this
SELECT CustomerList.CustomerName Cust, isnull((SELECT SUM(Spending) CustSpending
FROM Customer
WHERE Customer.CustomerName = CustomerList.CustomerName),0)
FROM CustomerList
This would normally be done using GROUP BY:
Select customer, sum(spending)
from mytable
group by customer;
GROUP BY is a very fundamental part of SQL. You should review your knowledge of SQL so you understand how to use it.

De-Dupe Query in MS Access

I'm using Access to create a report. I have several duplicate records in my Avail_Amt field. I used the Query Wizard to de-dup records from a table, like this.
In (SELECT [DEALLOCALBAL] FROM [TBL_DATA] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
What I really want to do, though, is de-dupe the records in my Query, not in my Table. I think it should look like the script below, but it's not working.
In (SELECT [DEALLOCALBAL] FROM [dbo_LIMIT_HIST.Avail_Amt] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
dbo_LIMIT_HIST is the Table in SQL Server and Avail_Amt is the Field that I am trying to de-dupe. Any idea what could be wrong here?
My data set looks like this:
Basically, for each Contact ID I could have multiple DEALLOCALBAL amounts. I want to capture each unique amount for each Contact ID, but I don't want dupes.
Thanks!!

Creating a BIRT using query params

I'm to a query multiple times from a single query in BIRT. For example, my DB2 query could be SELECT * FROM GROUPS and my dataset would look like
id | name
1 | group 1
2 | group 2
From that dataset I'd like to run another query for each row. So maybe something like SELECT * FROM ORDERS WHERE group_id = params['id'] where id is id of the current GROUP record.
The actual report would look something like:
Order for Group 1
01/01/2015 Order #321
01/15/2015 Order #948
Orders for Group 2
01/02/2015 Order #123
01/23/2015 Order #456
I'm fairly new to BIRT and have seen examples of using scripts on certain events (beforeOpen, etc), but I wanted to make sure that was the proper way to go for something this rudimentary.
Grouping on Groups in my example and OP's question, read carefully.
From what I understand of your requirements probably the easiest way to get what you want it to group on the table.
Put your fields in the data set and on the table then 'group by' elements of the table.
The report below is grouped by date and UPMC_Assign, then I count the number of INCIDENT_ID (ticket owner is criteria that is not displayed)
Create your 'Data Set', drop the Data Set on the Layout, a table is auto created.
Add a 'Group' (red circle lower part of second image), in my case I grouped by Date then by Group, in your case you would group by your 'Group'
I added an aggregation from the Palette to get counts. You can delete anything from the table you don't want. In my case i started out with a line for every ticket, but I deleted the entire row, and just show the groups and counts.
See my answer here for suggestions on versioning reports during development.

access select distinct on certain column

I have a table with some search results. The search results maybe repeated because each result may be found using a different metric. I want to then query this table select only the distinct results using the ID column. So to summarize I have a table with an ID column but the IDs may be repeated and I want to select only one of each ID with MS Access SQL, how should I go about doing this?
Ok I have some more info after trying a couple of the suggestions. The Mins, and Maxes won't work because the column they are operating on cannot be shown. I get an error like You tried to execute a query that does not include the specified expression... I now have all my data sorted, here is what it looks like
ID|Description|searchScore
97 test 1
97 test .95
120 ball .94
97 test .8
120 ball .7
so the problem is that since the rows were put into the table using different search criteria I have duplicated rows with different scores. What I want to do is select only one of each ID sorted by the searchScore descending. Any ideas?
SELECT DISTINCT ID
FROM Search_Table;
Based on the last update to your question, the following query seems appropriate.
SELECT ID, [Description], Max(searchScore)
FROM Search_Table
GROUP BY ID, [Description];
However that's nearly the same as Gordon's suggestion from yesterday, so I'm unsure whether this is what you want.
Here is a way where you can get one of the search criteria:
select id, min(search_criteria)
from t
group by id
This will always return the first one alphabetically. You can also easily get the last one using max().
You could also use:
select id, first(search_criteria)
from t
group by id