Roll up multiple rows into one when joining in SQL Server - sql

I have a table, Foo
ID | Name
-----------
1 | ONE
2 | TWO
3 | THREE
And another, Bar:
ID | FooID | Value
------------------
1 | 1 | Alpha
2 | 1 | Alpha
3 | 1 | Alpha
4 | 2 | Beta
5 | 2 | Gamma
6 | 2 | Beta
7 | 3 | Delta
8 | 3 | Delta
9 | 3 | Delta
I would like a query that joins these tables, returning one row for each row in Foo, rolling up the 'value' column from Bar. I can get back the first Bar.Value for each FooID:
SELECT * FROM Foo f OUTER APPLY
(
SELECT TOP 1 Value FROM Bar WHERE FooId = f.ID
) AS b
Giving:
ID | Name | Value
---------------------
1 | ONE | Alpha
2 | TWO | Beta
3 | THREE | Delta
But that's not what I want, and I haven't been able to find a variant that will bring back a rolled up value, that is the single Bar.Value if it is the same for each corresponding Foo, or a static string something like '(multiple)' if not:
ID | Name | Value
---------------------
1 | ONE | Alpha
2 | TWO | (multiple)
3 | THREE | Delta
I have found some solutions that would bring back concatenated values (albeit not very elegant) 'Alpha' Alpha, Alpha', 'Beta, Gamma, Beta' &c, but that's not what I want either.

One method, using a a CASE expression and assuming that [Value] cannot have a value of NULL:
WITH Foo AS
(SELECT *
FROM (VALUES (1, 'ONE'),
(2, 'TWO'),
(3, 'THREE')) V (ID, [Name])),
Bar AS
(SELECT *
FROM (VALUES (1, 1, 'Alpha'),
(2, 1, 'Alpha'),
(3, 1, 'Alpha'),
(4, 2, 'Beta'),
(5, 2, 'Gamma'),
(6, 2, 'Beta'),
(7, 3, 'Delta'),
(8, 3, 'Delta'),
(9, 3, 'Delta')) V (ID, FooID, [Value]))
SELECT F.ID,
F.[Name],
CASE COUNT(DISTINCT B.[Value]) WHEN 1 THEN MAX(B.Value) ELSE '(Multiple)' END AS [Value]
FROM Foo F
JOIN Bar B ON F.ID = B.FooID
GROUP BY F.ID,
F.[Name];

You can also try below:
SELECT F.ID, F.Name, (case when B.Value like '%,%' then '(Multiple)' else B.Value end) as Value
FROM Foo F
outer apply
(
select SUBSTRING((
SELECT distinct ', '+ isnull(Value,',') FROM Bar WHERE FooId = F.ID
FOR XML PATH('')
), 2 , 9999) as Value
) as B

Related

SQL aggregates over 3 tables

Well, this is annoying the hell out of me. Any help would be much appreciated.
I'm trying to get a count of how many project Ids and Steps there are. The relationships are:
Projects (n-1) Pages
Pages (n-1) Status Steps
Sample Project Data
id name
1 est et
2 quia nihil
Sample Pages Data
id project_id workflow_step_id
1 1 1
2 1 1
3 1 2
4 1 1
5 2 3
6 2 3
7 2 4
Sample Steps Data
id name
1 a
2 b
3 c
4 d
Expected Output
project_id name count_steps
1 a 3
1 b 1
2 c 2
2 d 1
Thanks!
An approach to meet the expected result. See it also at SQL Fiddle
CREATE TABLE Pages
("id" int, "project_id" int, "workflow_step_id" int)
;
INSERT INTO Pages
("id", "project_id", "workflow_step_id")
VALUES
(1, 1, 1),
(2, 1, 1),
(3, 1, 2),
(4, 1, 1),
(5, 2, 3),
(6, 2, 3),
(7, 2, 4)
;
CREATE TABLE workflow_steps
("id" int, "name" varchar(1))
;
INSERT INTO workflow_steps
("id", "name")
VALUES
(1, 'a'),
(2, 'b'),
(3, 'c'),
(4, 'd')
;
CREATE TABLE Projects
("id" int, "name" varchar(10))
;
INSERT INTO Projects
("id", "name")
VALUES
(1, 'est et'),
(2, 'quia nihil')
;
Query 1:
select pg.project_id, s.name, pg.workflow_step_id, ws.count_steps
from (
select distinct project_id, workflow_step_id
from pages ) pg
inner join (
select workflow_step_id, count(*) count_steps
from pages
group by workflow_step_id
) ws on pg.workflow_step_id = ws.workflow_step_id
inner join workflow_steps s on pg.workflow_step_id = s.id
order by project_id, name, workflow_step_id
Results:
| project_id | name | workflow_step_id | count_steps |
|------------|------|------------------|-------------|
| 1 | a | 1 | 3 |
| 1 | b | 2 | 1 |
| 2 | c | 3 | 2 |
| 2 | d | 4 | 1 |

Stored procedure: Select by matching input parameter or return the null records

I have the following case:
There is a table like this:
Id | Param | Value
------ | ------ | -------------
1 | 1 | One 1
1 | NULL | Null-Value 1
1 | 2 | Two 1
1 | 3 | Three 3
2 | NULL | Nul-Value 2
2 | 2 | Two 2
3 | NULL | Null-Value 3
4 | 1 | One 4
5 | NULL | Null-Vaue 5
6 | NULL | Null-Value 6
I have to write a stored procedure as by given input nullable parameter for "Param" I have to generate a result which will contain a table with ID and Value and the result is based on the logic -
If the input parameter is null - return all rows with null values for Param
If the parameter is not null, then return the rows which match that parameter in the Param column and also all rows (for the other IDs), which have null value as Param.
There MUST be only ONE result per Id.
Consider that there is applied an unique composite index on the Id and Param columns.
Example:
Input parameter: 1
Output table:
Id | Value
-- | -------------
1 | One 1
2 | Nul-Value 2
3 | Null-Value 3
4 | One 4
5 | Null-Vaue 5
6 | Null-Value 6
Example 2 (let me include the Param column as well for better visibility):
Input parameter: 2
Output table:
Id | Param | Value
-- | ----- |-------------
1 | 2 |Two 1
2 | 2 |Two 2
3 | NULL |Null-Value 3
5 | NULL |Null-Value 5
6 | NULL |Null-Value 6
I suppose it will be a join of the table with itself, or even better with cross(or maybe outer) apply and some proper where clause...
This solves the problem with multiple results per id. You don't give any rules for which one you want per id so I can't do the next step.
SELECT *
FROM tableyoudidnotsaythenameof as x
WHERE (coalesce(x.value, #inparam) = #inparam) or
(#inparam is null and x.value is null)
if you don't care which one you return when there is more than one per id this will work:
SELECT *
FROM (
SELECT *, row_number() over (partition by id) as rn
FROM tableyoudidnotsaythenameof as x
WHERE (coalesce(x.value, #inparam) = #inparam) or
(#inparam is null and x.value is null)
) zed
WHERE zed.rn = 1
to sort nulls with bigger rn:
SELECT *, row_number() over (partition by id order by CASE WHEN x.value is null then 2 else 1 END) as rn
You could use this one without the use of joins. When #param is null it returns all rows.
DECLARE #t TABLE (id INT, [Param] INT, Val VARCHAR(50))
INSERT INTO #t VALUES
(1, 1,' One 1'),
(1, NULL,'Null-Value 1'),
(1, 2,'Two 1'),
(1, 3,'Three 3'),
(2,NULL,'Nul-Value 2'),
(2, 2,'One 2'),
(3, NULL,'Null-Value 3'),
(4, 1,'One 4'),
(5, NULL,'Null-Vaue 5'),
(6, NULL,'Null-Value 6')
DECLARE #Param INT
SET #Param = 1
SELECT *
FROM #t
WHERE #Param IS NULL OR (([Param] = #Param) OR ([Param] IS NULL AND Id <> #Param))
I figured it out. Here is the query:
declare #input int = 2
select t.Id, t.Value
from [dbo].[MyTable] t
outer apply (select * from MyTable t2
where t2.Param = #input
and t.Id = t2.Id) mt
where (t.Id = mt.Id and t.Param = #input)
or (t.Param is null and mt.Param is null)

Count Based on Columns in SQL Server

I have 3 tables:
SELECT id, letter
FROM As
+--------+--------+
| id | letter |
+--------+--------+
| 1 | A |
| 2 | B |
+--------+--------+
SELECT id, letter
FROM Xs
+--------+------------+
| id | letter |
+--------+------------+
| 1 | X |
| 2 | Y |
| 3 | Z |
+--------+------------+
SELECT id, As_id, Xs_id
FROM A_X
+--------+-------+-------+
| id | As_id | Xs_id |
+--------+-------+-------+
| 9 | 1 | 1 |
| 10 | 1 | 2 |
| 11 | 2 | 3 |
| 12 | 1 | 2 |
| 13 | 2 | 3 |
| 14 | 1 | 1 |
+--------+-------+-------+
I can count all As and Bs with group by. But I want to count As and Bs based on X,Y and Z. What I want to get is below:
+-------+
| X,Y,Z |
+-------+
| 2,2,0 |
| 0,0,2 |
+-------+
X,Y,Z
A 2,2,0
B 0,0,2
What is the best way to do this at MSSQL? Is it an efficent way to use foreach for example?
edit: It is not a duplicate because I just wanted to know the efficent way not any way.
For what you're trying to do without knowing what is inefficient with your current code (because none was provided), a Pivot is best. There are a million resources online and here in the stack overflow Q/A forums to find what you need. This is probably the simplest explanation of a Pivot which I frequently need to remind myself of the complicated syntax of a pivot.
To specifically answer your question, this is the code that shows how the link above applies to your question
First Tables needed to be created
DECLARE #AS AS TABLE (ID INT, LETTER VARCHAR(1))
DECLARE #XS AS TABLE (ID INT, LETTER VARCHAR(1))
DECLARE #XA AS TABLE (ID INT, AsID INT, XsID INT)
Values were added to the tables
INSERT INTO #AS (ID, Letter)
SELECT 1,'A'
UNION
SELECT 2,'B'
INSERT INTO #XS (ID, Letter)
SELECT 1,'X'
UNION
SELECT 2,'Y'
UNION
SELECT 3,'Z'
INSERT INTO #XA (ID, ASID, XSID)
SELECT 9,1,1
UNION
SELECT 10,1,2
UNION
SELECT 11,2,3
UNION
SELECT 12,1,2
UNION
SELECT 13,2,3
UNION
SELECT 14,1,1
Then the query which does the pivot is constructed:
SELECT LetterA, [X],[Y],[Z]
FROM (SELECT A.LETTER AS LetterA
,B.LETTER AS LetterX
,C.ID
FROM #XA C
JOIN #AS A
ON A.ID = C.ASID
JOIN #XS B
ON B.ID = C.XSID
) Src
PIVOT (COUNT(ID)
FOR LetterX IN ([X],[Y],[Z])
) AS PVT
When executed, your results are as follows:
Letter X Y Z
A 2 2 0
B 0 0 2
As i said in comment ... just join and do simple pivot
if object_id('tempdb..#AAs') is not null drop table #AAs
create table #AAs(id int, letter nvarchar(5))
if object_id('tempdb..#XXs') is not null drop table #XXs
create table #XXs(id int, letter nvarchar(5))
if object_id('tempdb..#A_X') is not null drop table #A_X
create table #A_X(id int, AAs int, XXs int)
insert into #AAs (id, letter) values (1, 'A'), (2, 'B')
insert into #XXs (id, letter) values (1, 'X'), (2, 'Y'), (3, 'Z')
insert into #A_X (id, AAs, XXs)
values (9, 1, 1),
(10, 1, 2),
(11, 2, 3),
(12, 1, 2),
(13, 2, 3),
(14, 1, 1)
select LetterA,
ISNULL([X], 0) [X],
ISNULL([Y], 0) [Y],
ISNULL([Z], 0) [Z]
from (
select distinct a.letter [LetterA], x.letter [LetterX],
count(*) over (partition by a.letter, x.letter order by a.letter) [Counted]
from #A_X ax
join #AAs A on ax.AAs = A.ID
join #XXs X on ax.XXs = X.ID
)src
PIVOT
(
MAX ([Counted]) for LetterX in ([X], [Y], [Z])
) piv
You get result as you asked for
LetterA X Y Z
A 2 2 0
B 0 0 2

How do I limit a query with distinct values in MariaDB

I have a table like this:
Id Num Some text
--------------------
1 1 ""
2 1 ""
3 2 ""
4 2 ""
5 2 ""
6 2 ""
7 3 ""
What I want is a query to select the first ten distinct nums, so if I want to get the two first nums I'll get the first six rows. I'm using MariaDB.
Try this approach:
select *
from Table1 as T1
join (
select distinct num
from Table1
order by num
limit 2 ) as T2
on T1.num = T2.num
;
In a fiddle here: http://sqlfiddle.com/#!9/1468ad/5
In MySQL you can use LIMIT. The caveat is that LIMIT is not directly supported in the IN sub-queries and therefore you must use a sub-query inside a IN sub-query:
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE Table1
(`Id` int, `Num` int, `Some text` varchar(2))
;
INSERT INTO Table1
(`Id`, `Num`, `Some text`)
VALUES
(1, 1, '""'),
(2, 1, '""'),
(3, 2, '""'),
(4, 2, '""'),
(5, 2, '""'),
(6, 2, '""'),
(7, 3, '""')
;
Query 1:
select * from Table1
where Num in (select Num FROM(select distinct Num from Table1 order by Num limit 2)a)
Results:
| Id | Num | Some text |
|----|-----|-----------|
| 1 | 1 | "" |
| 2 | 1 | "" |
| 3 | 2 | "" |
| 4 | 2 | "" |
| 5 | 2 | "" |
| 6 | 2 | "" |

Create view based on three tables with columns as rows in one of tables

I'm starting to learn SQL, learning about views.
I was wondering if is possible to join three tables in one view, where some columns will be based on rows from of these tables.
I have three tables:
Roles:
Id | Name | PermissionId
1 | Admin | 1
2 | Staff | 2
RolePermissions:
Id | RoleId | PermissionId
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 2 <- staff doesn't have permission 3
Permissions:
Id | Name
1 | Perm1
2 | Perm2
3 | Perm3
.
. (not fixed number of permissions)
I would like to create view like this:
Id (of role) | Name | Perm1 | Perm2 | Perm3 ... (not fixed number of columns)
1 | Admin | True | True | True
2 | Staff | True | True | False
Is it possible?
You cannot use a view if you don't know how many columns to output.
Code below should be you in the right direction. Use it in a Procedure with dynamic SQL if you need to dynamically build the list of columns
; With roles(Id, Name, PermissionID) as (
Select * From (values(1, 'Admin', '1'), (2, 'Staff', '2')) as r(Id, Name, PermissionId)
), RolePermissions(Id, RoleId, PermissionId) as (
Select * From (Values(1, 1, 1), (2, 1, 2), (3, 1, 3), (4, 2, 1), (5, 2, 2)) as p(Id, RoleId, PermissionId)
), Permissionss(Id, Name) as (
Select * From (Values(1, 'Perm1'), (2, 'Perm2'), (3, 'Perm3')) as p(Id, Name)
), data as(
Select r.Id, rp.PermissionId, p.Name From Roles as r
Inner Join RolePermissions as rp on rp.RoleId = r.Id
Inner Join Permissionss as p on rp.PermissionId = p.Id
)
Select piv.Id as [Id of Role], r.Name
, [Perm1] = case when [Perm1] is not null then 'true' else 'false' end
, [Perm2] = case when [Perm2] is not null then 'true' else 'false' end
, [Perm3] = case when [Perm3] is not null then 'true' else 'false' end
From data
Pivot( max(PermissionId)
For Name in ( [Perm1], [Perm2], [Perm3])
) as piv
Inner Join roles as r on r.Id = Piv.Id