How do I put the different data into one cell [duplicate] - sql

This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 2 years ago.
This is the sample data that I've got
id
program
Community ID
sdg
246
#NoWasteGiving2020
97
11
246
#NoWasteGiving2020
97
17
246
#NoWasteGiving2020
97
10
With the data on the table above, how do I get the data to just be shown in one row and the sdg shown in one row. so at the end of the day the data will look like this:
246 #NoWasteGiving2020 97 10,11,17
This is the query that I've wrote
select Distinct a.id, a.title as program, b.community_id, c.goal_id as sdg
from programs_program as a
join associates_programcommunity as b on a.id = b.program_id
join associates_communitygoal as c on b.community_id = c.community_id

You can use string_agg as follows:
select a.id, a.title as program, b.community_id, string_agg(c.goal_id, ',') as sdg
from programs_program as a
join associates_programcommunity as b on a.id = b.program_id
join associates_communitygoal as c on b.community_id = c.community_id
group by a.id, a.title, b.community_id

Related

Horizontal To Vertical Sql Server

I'm stuck with a SQL query (SQL Server) that involves converting horizontal rows to vertical rows
Below is my Query that I am trying
SELECT P AS Amount_Rs
FROM (
Select (F1.D32-F1.D20) As Profit_For_The_Period ,F3.D2 as Current_Libilities,F5.D20 As Capital_Acount,
--M1.Name As Name,
F2.D20 AS Loan_Liabilities,F4.d1 As Opening_Diff --F2.D68 As Loan,
from Folio1 As F1
--inner Join Master1 As m1 on m1.Code like '101' or m1.Code Like '102' or m1.Code Like '106' or m1.Code Like '109' or m1.Code lIke '103'
--And m1.Code=102 And m1.Code=101)
inner Join Folio1 As F2 On (F2.MasterCode=F2.MasterCode)
inner Join Folio1 As F3 On (F3.MasterCode=F3.MasterCode)
inner Join Folio1 As F4 On (F4.MasterCode=F4.MasterCode)
inner Join Folio1 As F5 On (F5.MasterCode=F5.MasterCode)
Where F1.MasterCode=109
and F2.MasterCode =106
and F3.MasterCode=103
and F4.MasterCode=102
And F5.MasterCode=101
) p UNPIVOT
( p FOR value IN
( Profit_For_The_Period,Capital_Acount, Current_Libilities, Loan_Liabilities, Opening_Diff )
) AS unvpt
Current Output:
1 12392
2 0
3 0
4 4000
5 -200
Desired Output:
1 Capital Account 12392
2 Current Assets 0
3 Current Liabilities 0
4 Loans (Liability) 4000
5 Revenue Accounts -200
Thanks !!!
I think you are looking for a pivot. Use the CASE statement with a SUM or any aggregate function in the SELECT part and a group by in the where clause, that's how I use to put rows into columns in a query when I have to in MySQL. I don't know SQL Server but I think you can do quite the same.
your conditions below
F1.MasterCode=109
and F2.MasterCode =106
and F3.MasterCode=103
and F4.MasterCode=102
And F5.MasterCode=101
shouldn't be in the the where clause but with the case in the select part
example :
select whatever,
case when F2.MasterCode =106 then sum(column_name)
end case as column_alias, (other columns) from ...
hope this could help

Update a table by using a join [duplicate]

This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 8 years ago.
I have two tables ta, tb. ta columns - cId, c1, c2. c1 and c2 contain nulls and need to be filled with data. tb columns - cId, c3, c4. The data for c1 and c2 will come from c3 and c4 respectively.
So, I tried to do a simple inner join first. Both tables were aliased as al_ta and al_tb respectively. Then, I put an update statement -
UPDATE ta SET
al_ta.c1 = al_tb.c3,
al_ta.c2 = al_tb.c4
FROM ta AS al_ta
INNER JOIN tb AS al_tb
ON al_tb.cId = al_tb.cId
This does not work and I get an error - The multi-part identifier al_ta.c1 could not be bound. How do I make this work ?
Sample tables -
ta
cId c1 c2
1 NULL NULL
2 NULL NULL
3 NULL NULL
tb
cId c3 c4
1 11 111
2 22 222
3 33 333
4 44 444
When referencing the columns, you need to use the alias, not the base table name, if you've abstracted the table names away in the JOIN. Guessing at what your join might look like, you probably meant to write it this way:
UPDATE ta SET
ta.c1 = tb.c3,
ta.c2 = tb.c4
FROM dbo.some_long_table_name_a AS ta
INNER JOIN dbo.some_long_table_name_b AS tb
ON ta.cId = tb.cId
WHERE ta.c1 IS NULL OR ta.c2 IS NULL;
I don't understand the purposes of saying:
FROM ta AS al_ta
Why would you bother using an alias here that is actually harder to write than the original table name?
Please Try it
update ta set
ta.c1 = b.c3,
ta.c2 = b.c4
from ta a join tb b on a.cid = b.cid

GROUP_CONCAT in SQLite

I am having data like this
1 A
1 B
1 C
1 D
2 E
2 F
3 G
3 H
3 I
3 J
3 K
by using this query
select ABSTRACTS_ITEM._id,Name
from ABSTRACTS_ITEM , ABSTRACT_AUTHOR , AUTHORS_ABSTRACT
where
ABSTRACTS_ITEM._id = AUTHORS_ABSTRACT.ABSTRACTSITEM_ID
and
ABSTRACT_AUTHOR._id = AUTHORS_ABSTRACT.ABSTRACTAUTHOR_ID
Now, I want to show data like this
1 A,B,C,D
2 EF
and so on..I also know it can achieve by GROUP_CONCAT function. So, I tried with this
SELECT ABSTRACTS_ITEM._id,
GROUP_CONCAT(ABSTRACT_AUTHOR.NAME) FROM
(select ABSTRACTS_ITEM._id,
Name
from
ABSTRACTS_ITEM , ABSTRACT_AUTHOR , AUTHORS_ABSTRACT
where
ABSTRACTS_ITEM._id = AUTHORS_ABSTRACT.ABSTRACTSITEM_ID
and
ABSTRACT_AUTHOR._id = AUTHORS_ABSTRACT.ABSTRACTAUTHOR_ID)
But, It shows me error. So, what I am doing wrong here. What are the right procedure to achieve that?
You need to add GROUP BY clause when you are using aggregate function. Also use JOIN to join tables.
So try this:
SELECT AI._id, GROUP_CONCAT(Name) AS GroupedName
FROM ABSTRACTS_ITEM AI
JOIN AUTHORS_ABSTRACT AAB ON AI.ID = AAB.ABSTRACTSITEM_ID
JOIN ABSTRACT_AUTHOR AAU ON AAU._id = AAB.ABSTRACTAUTHOR_ID
GROUP BY tbl._id;
See this sample SQLFiddle
What you were trying was almost correct. You just needed to add GROUP BY clause at the end. But the first one is better.
SELECT ID,
GROUP_CONCAT(NAME)
FROM
(select ABSTRACTS_ITEM._id AS ID,
Name
from
ABSTRACTS_ITEM , ABSTRACT_AUTHOR , AUTHORS_ABSTRACT
where
ABSTRACTS_ITEM._id = AUTHORS_ABSTRACT.ABSTRACTSITEM_ID
and
ABSTRACT_AUTHOR._id = AUTHORS_ABSTRACT.ABSTRACTAUTHOR_ID)
GROUP BY ID;
Here my answer to this closed question (it has a good showcase in it). SQL group results by row
You must add an Aggregate Function to your selected field.
The one you want is: GROUP_CONCAT
with a custom SEPARATOR ', '
See related MySQL Documentation:
https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html
Your Select Query would look like this:
SELECT id_place, placename, GROUP_CONCAT(plant_name SEPARATOR ', ') AS 'plantnames'
FROM BLOOM
JOIN PLACE ON id_place = fk_place
JOIN PLANT ON id_plant = fk_plant
GROUP BY id_place, placename;
Then you get your desired output:
id_place
placename
plantnames
1
New York
orchid, tulip
2
London
rosebush, orchid, tulip
3
Paris
rosebush, lily, violet
Here is a DB Fiddle to check this out (MySQL 8.0):
https://www.db-fiddle.com/f/sSU7G4kKEaxsLMNweAtLSA/2

SQL using same codes table for two different columns

I have a table (Foo) that has two columns that store a code value from a codes table:
id - code1 - code2
1 - CC - DD
The Codes table:
Name - Code - Grouping
Call Center - CC - 22
County - DD - 54
I need a SQL that will pull 'Call Center' and 'County' based on the first table. It is assumed that I know Foo.code1 necessarily uses Codes.Grouping=22 and Foo.code2 uses Codes.Grouping=54.
I'm trying to write one SQL that will return both values.
Try this query:
select name from codes c inner join foo f on c.code = f.code1 or c.code = f.code2
Here is sqlfiddle
I am not really sure if this is the answer to your question, because I don't know exactly what you mean. I guess however, that you want to get both codes and groupings for an id value of your Foo table. For this I would
SELECT Foo.id,
C1.Name AS code1_name, C1.Code AS code1_code,
C1.Grouping AS code1_grouping,
C2.Name AS code2_name, C2.Code AS code2_code,
C2.Grouping AS code2_grouping
FROM Foo
INNER JOIN Codes AS C1 ON C1.Code = Foo.code1
INNER JOIN Codes AS C2 ON C2.Code = Foo.code2
WHERE Foo.id = 1;
expanding tuffkid sqlfiddlesqlfiddle

In sql server how to seperate the column values with comma [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
How to return multiple values in one column (T-SQL)?
(6 answers)
How to separate column values with comma
(1 answer)
Closed 10 years ago.
My query is:
select
gp.GroupName+ '' As GroupName,
us.UnitsName+'' As Unitname,
sp.Sensorname+',' As sensorName
from Grouping as gp
inner join Units as us
on gp.UnitsID = us.UnitsID
inner join Sensors as sp
on gp.SensorID = sp.SensorID
The output is
GroupName Unitname sensorName
G1 Electricity S1,
G1 Electricity S3,
G2 Gas test,
But the output should look like
G1 Electricity S1,S3
G2 Gas test
How to get that?
WITH CTE
AS
(
select
gp.GroupName As GroupName,
us.UnitsName As Unitname,
sp.Sensorname As sensorName
from Grouping as gp
inner join Units as us on gp.UnitsID = us.UnitsID
inner join Sensors as sp on gp.SensorID = sp.SensorID
)
SELECT
c1.Groupname,
c1.Unitname,
STUFF((
SELECT ', ' + c2.sensorName
FROM CTE c2
WHERE c2.GroupName = c1.GroupName
AND c2.Unitname = c1.Unitname
FOR XML PATH (''))
,1,2,'') AS Sensors
FROM CTE c1
GROUP BY c1.GroupName,
c1.Unitname;