Show accounts with multiple contacts to as separate columns rather than rows? - sql-server

Suppose, I have a table of contacts.
That table has contact IDs and Account IDs
So if I do
Select ContactID,AccountID From table
I get this:
Instead, I want to see this:
Is this plausible?

With two and only two contacts par account, aggregation seems like a straight-forward option:
select account_id, min(contactId) contactId1, max(contactId) contactId2
from mytable
group by account_id
If some accounts have 2 contacts and others have just 1, then:
select
account_id,
min(contactId) contactId1,
case when count(*) > 1 then max(contactId) end contactId2
from mytable
group by account_id

Related

un-struct in big query

I have a table with some struct data types that I can access but would like it as columns.
select
consignment id,
user
from tbl
What I currently have
select
consignment id
, user.name
, user.email
, user.externalId
from tbl
What I want
I was able to get it by just calling every key of user but on a table with hundreds of columns that would be terrible
use below
select
consignment id,
user.*
from tbl

How To Get Single Column Result From SqlServer Query Using Group By

I have two classic tables (OK, not my real problem). Orders and OrderItems. I want to using a single statement to delete all Orders that have no OrderItems. I can get the list of Orders I want to delete with a query like this:
SELECT COUNT(*),OrderId
FROM OrderItems
GROUP BY OrderId
HAVING COUNT(*) > 0
and what I want to do is something like:
DELETE FROM Orders WHERE Id NOT IN (....)
Where "...." is my SELECT above. The select is giving me two columns and I really don't want the second column, just the first.
I feel like there is some kind of self join, or something like that I can use but I'm read only when it comes to that.
DELETE from Orders Where Id NOT IN (SELECT OrderId
FROM OrderItems
GROUP BY OrderId
HAVING COUNT(*) >0);

SELECTing data using a Postgres Array

I have a table that contains a column user_ids which is a Postgres Array.
I need to select all messages from another table where the column user_id is one of the ids in the given array.
In Psuedo-sql:
select users.*
from users
where id IN a_postgres_array
Any ideas?
You could use the ANY operator. From your sample:
select users.*
from users
where id =ANY(a_postgres_array)
When using two tables, it could be a JOIN, something like:
SELECT users.*
FROM users INNER JOIN table_with_array ON users.id =ANY(table_with_array.a_postgres_array)
select users.*
from users
where id IN (
select unnest(a_postgres_array)
from t
where columnX = some_value
)

T-SQL query to get # of something for a given category, via lookup table

I've got three tables, two are "data" tables, one is a join (or lookup) table.
Place Table
PlaceId
Name
etc...
Categories Table
CatId
Name
etc...
PlaceCats Table
PlaceId
CatId
(with appropriate relationships defined between each Id field)
What I want to do is pull the categories that contain less than 5 Places... for some reason I just can wrap my mind around the T-SQL to make that happen.
SELECT *
FROM Categories
WHERE CatId IN
(
SELECT CatId
FROM PlaceCats
GROUP BY CatId
HAVING COUNT(*) < 5
)
To get the raw data:
select CatID, count(*)
from PlaceCats
group by CatID
having count(*) < 5

SQL SERVER query, Mutual Rows in Given ID

I have a sql server query question. I have a table like below. Giving a parameter to stored procedure, I need to query mutual CustomFields in lists that I want. For example, if ListID is given 1 and 2 and 3, result table will have columns that gives me 'FullName' in that case, because only 'FullName' is in all three ID's. I did that which solved the issue somehow, but looking for a better and precise practice. Thanks
SELECT DISTINCT(CustomField)
FROM CustomFields a
WHERE EXISTS (
SELECT count(*)
FROM CustomFields b
WHERE a.CustomField = b.CustomField
HAVING count(*)>2
)
ORDER BY a.CustomField
CustomField ListID
PhoneNumber 1
Unvan 1
FullName 2
Surname 2
Regiob 2
FullName 3
BirthPlace 3
FullName 1
Here's a more common approach to find CustomFields that are in groups 1, 2 and 3:
SELECT CustomField
FROM CustomFields
WHERE ListID in (1,2,3)
GROUP BY
CustomField
HAVING COUNT(DISTINCT ListId) = 3
No subquery required, and the where clause filters out uninteresting groups before the group by.
I was thinking of something more along these lines, which is adapted from #Andomar and is a little more general.
CREATE TYPE ListIdType AS TABLE (
ListId int PRIMARY KEY
);
GO
CREATE PROCEDURE S
#ListIdTable ListIdType READONLY
AS BEGIN SET NOCOUNT ON;
SELECT CustomField
FROM CustomFields
WHERE ListID in (SELECT ListId FROM #ListIdTable)
GROUP BY
CustomField
HAVING COUNT(DISTINCT ListId) = (SELECT COUNT(ListId) FROM #ListIdTable)
END;

Resources