How to Merge 2 Rows into one by comma separate? - sql

I need to merge this individual rows to one column, I now how to merge column by comma separated,
+---------------+-------+-------+
|CID |Flag |Value |
+---------------+-------+-------+
|1 |F |10 |
|1 |N |20 |
|2 |F |12 |
|2 |N |23 |
|2 |F |14 |
|3 |N |21 |
|3 |N |22 |
+---------------+-------+-------+
Desired Result can be anything,
+-----------+----------------------------+ +--------------------------+
|Part Number| Value | | Value |
+-----------+----------------------------+ +--------------------------+
| 1 | 1|F|10 ; 1|N|20 | Or | 1|F|10 ; 1|N|20 |
| 2 | 2|F|12 ; 2|N|23 ; 2|F|14 | | 2|F|12 ; 2|N|23 ; 2|F|14 |
| 3 | 3|N|21 ; 3|N|22 | | 3|N|21 ; 3|N|22 |
+-----------+----------------------------+ +--------------------------+
Note:
Any hint in right direction with small example is more than enough
EDIT :
I have massive data in tables like thousands of records where parent's and child relationship is present. I have to dump this into text files by comma separated values In single line as record. Think as primary record has relationship with so many other table then all this record has to be printed as a big line.
And I am trying to achieve by creating query so load can be distributed on database and only thing i have to worry about in business is just dumping logic into text files or whatever form we need in future.

You can try to use LISTAGG and your query will look like this:
select a.cid, a.cid || listagg(a.flag || '|' || a.value, ',')
from foo.dat a
group by a.cid
You can use different separators and of course play with how the result will be formatted.

Related

How to join two queries with count?

I have 3 tables in database:
Table 1: violation
| violation_id | violation_name |
|:-------------:|:--------------:|
|1 | No Parking |
|2 | Speed Contest |
|3 | No Helmet |
Table 2: violators
| violator_id | violation_id |
|:-------------:|:--------------:|
|1 |1 |
|2 |1 |
|3 |3 |
Table 2: previous_violator
| prev_violator_id| violation_id |
|:---------------:|:--------------:|
|1 |1 |
|2 |2 |
|3 |2 |
This view that I want:
| violation_name | Total |
|:-------------:|:--------------:|
|No Parking | 3 |
|Speed Contest | 2 |
|No Helmet | 1 |
I perform this code that joins the violator table and violation:
SELECT *,count(violators.violation_id) as vid
FROM violators
LEFT JOIN violation ON violation.violation_id = violators.violation_id
LEFT JOIN previous_violator ON previous_violator.violator_id = violators.violator_id
WHERE date_apphrehend BETWEEN '$from' AND '$to'
GROUP BY violators.violation_id
My problem is, I want to join the previous violator table that count to the total based on the violation_name.
You can first union all to get them into a single result and then count(*) it. Finally join with Violation to get names. ie:
select violation_name, count(*) as cnt
from (select violation_id from Violators
union all
select violation_id from previous_Violators) tmp
inner join Violation on tmp.violation_id = Violation.violation_id
group by Violation.violation_id, violation_name;
Sample DBFiddle demo.
PS: Sample is in postgreSQL but it would be the same for most backends. You didn't tag your backend.

In PostgreSQL, conditionally count rows

Background
I'm a novice Postgres user running a local server on a Windows 10 machine. I've got a dataset g that looks like this:
+--+---------+----------------+
|id|treatment|outcome_category|
+--+---------+----------------+
|a |1 |cardiovascular |
|a |0 |cardiovascular |
|b |0 |metabolic |
|b |0 |sensory |
|c |1 |NULL |
|c |0 |cardiovascular |
|c |1 |sensory |
|d |1 |NULL |
|d |0 |cns |
+--+---------+----------------+
The Problem
I'd like to get a count of outcome_category by outcome_category for those id who are "ever treated" -- defined as "id's who have any row where treatment=1".
Here's the desired result:
+----------------+---------+
|outcome_category| count |
+----------------+---------+
|cardiovascular | 3 |
|sensory | 1 |
|cns | 1 |
+----------------+---------+
It would be fine if the result had to contain metabolic, like so:
+----------------+---------+
|outcome_category|treatment|
+----------------+---------+
|cardiovascular | 3 |
|metabolic | 0 |
|sensory | 1 |
|cns | 1 |
+----------------+---------+
Obviously I don't need the rows to be in any particular order, though descending would be nice.
What I've tried
Here's a query I've written:
select treatment, outcome_category, sum(outcome_ct)
from (select max(treatment) as treatment,
outcome_category,
count(outcome_category) as outcome_ct
from g
group by outcome_category) as sub
group by outcome_category, sub.treatment;
But it's a mishmash result:
+---------+----------------+---+
|treatment|outcome_category|sum|
+---------+----------------+---+
|1 |cardiovascular |3 |
|1 |sensory |2 |
|0 |metabolic |1 |
|1 |NULL |0 |
|0 |cns |1 |
+---------+----------------+---+
I'm trying to identify the "ever exposed" id's using that first line in the subquery: select max(treatment) as treatment. But I'm not quite getting at the rest of it.
EDIT
I realized that the toy dataset g I originally gave you above doesn't correspond to the idiosyncrasies of my real dataset. I've updated g to reflect that many id's who are "ever treated" won't have a non-null outcome_category next to a row with treatment=1.
Interesting little problem. You can do:
select
outcome_category,
count(x.id) as count
from g
left join (
select distinct id from g where treatment = 1
) x on x.id = g.id
where outcome_category is not null
group by outcome_category
order by count desc
Result:
outcome_category count
----------------- -----
cardiovascular 3
sensory 1
cns 1
metabolic 0
See running example at db<>fiddle.
This would appear to be just a simple aggregation,
select outcome_category, Count(*) count
from t
where treatment=1
group by outcome_category
order by Count(*) desc
Demo fiddle

Noob needs advise on three tables in postgresql DB

I've very minor experience at working with databases( I only know the absolute basics). With that stated I guess my problem is rather easy to solve for more experienced minds.
My question is:
I need a way to be able to search for example "all types of Aluminium and all its sub_materials"?
I can do simple queries like
SELECT *
FROM sub_materials
WHERE category_id = 2;
So what I'm asking for is basically to be able to see the whole branch of Aluminium. I've looked at ltreeand Closure Tables But I'm to much of a Noob to figure it out by my self.
I believe I have to connect the tables somehow as "grandparent, parent, child" or something similar, but I've no idea if there is some other way or?
I'm not even sure if I'm doing this the right way.
Can someone advise me on this?
I've three tables in a database.(below are samples from the tables)
materials_category Holds all the categories for every material in the DB.
materials Holds the Category name and id, material_names and ids and the values for each material.
Some materials has sub_categories. not all but some.
3. sub_materials Holds the category info, material name and id, the sub_material name and id and the sub_material values.
1. `materials_category`
|category_id| category_name|
------------------------------
| 1 | Aggregate |
| 2 | Aluminium |
| 3 | Asphalt |
2. `materials`
|category_id |category_name |material_id |material_name|EE_1|EE_2|EC_1|EC_2
---------------------------------------------------------------------------
| 1 |Aggregate | 1 | General |3 |0 | 52 | 8
| 2 |Aluminium | 2 | General |55 |7 | 9 | 24
| 2 |Aluminium | 3 | Cast Pr |34 |30 | 22 | 28
| 2 |Aluminium | 4 | Extrud. |65 |16 | 8 | 74
| 2 |Aluminium | 5 | Rolled |15 | 0 | 9 | 61
| 3 |Asphalt | 6 |Asphalt, 4% |2 |22 | 6 | 54
| 3 |Asphalt | 7 |Asphalt, 5% |3 |91 | 1 | 4
3. `sub_materials`
|category_id|category_name|material_id|material_name|sub_mar_id|sub_mar_name|EE_1|EE_2|EC_1|EC_2|
|2 |Aluminium |2 |General |1 |Virgin |21 |5 |9 |60 |
|2 |Aluminium |2 |General |2 |Recycled |29 |3 |8 |9 |
Your tables are denormalised. This is usually not a good thing, unless this is a reporting only database.
Anyway, here's a start for what you're after. why don't you run it and give some feedback on whether it's what you want or not.
select *
from materials_category c
left outer join materials m
on c.category_id = m.category_id
left outer join sub_materials s
on s.material_id = m.material_id
where c.category_name = 'Aluminium'
Looking at your data, I'm guessing that 'recycled' could apply to any number of material categories, not just Aluminium? Would you have a whole bunch of 'Recycled' records in your sub_materials to allow for each material_category?

Select rows with same id but different result in another column

sql: I have a table like this:
+------+------+
|ID |Result|
+------+------+
|1 |A |
+------+------+
|2 |A |
+------+------+
|3 |A |
+------+------+
|1 |B |
+------+------+
|2 |B |
+------+------+
The output should be something like:
Output:
+------+-------+-------+
|ID |Result1|Result2|
+------+-------+-------+
|1 |A |B |
+------+-------+-------+
|2 |A |B |
+------+-------+-------+
|3 |A | |
+------+-------+-------+
How can I do this?
SELECT
Id,
MAX((CASE result WHEN 'A' THEN 'A' ELSE NULL END)) result1,
MAX((CASE result WHEN 'B' THEN 'B' ELSE NULL END)) result2,
FROM
table1
GROUP BY Id
results
+------+-------+-------+
|Id |Result1|Result2|
+------+-------+-------+
|1 |A |B |
|2 |A |B |
|3 |A |NULL |
+------+-------+-------+
run live demo on SQL fiddle: (http://sqlfiddle.com/#!9/e1081/2)
there are a few ways to do it.
None of tehm a are straight forward.
in theory, a simple way would be to create 2 temporary tables, where you separte the data, all the "A" resultas in one table and "B" in another table.
Then get the results with simple query. using JOIN.
if you are allowed to use some scrpting on the process then it is simpler, other wise you need a more complex logic on your query. And for you query to alwasy work, you need to have some rules like, A table always contains more ids than B table.
If you post your real example, it is easier to get better answers.
for this reason:
ID Name filename
1001 swapan 4566.jpg
1002 swapan 678.jpg
1003 karim 7688.jpg
1004 tarek 7889.jpg
1005 karim fdhak.jpg
output:
ID Name filename
1001 swapan 4566.jpg 678.jpg
1003 karim 7688.jpg fdhak.jpg
1004 tarek 7889.jpg ...
.. ... ... ...

How to get middle character from a string using SQL Server?

I have a table like this.
----------------
|Id | Name |
----------------
|1 |Apple |
|2 |banana |
|3 |Orange |
|4 |Grapes |
|5 |Mango |
----------------
I need middle character from each string.My result is to be like this
--------------------
|Id | Name |MidChar|
------------------------
|1 |Apple | p |
|2 |banana | n(or)a|
|3 |Orange | a(or)n|
|4 |Grape | a |
|5 |Mango | n |
-------------------
Please give some solutions or ideas.
Use LEN and Substring function
Try this
declare #Name varchar(10) = 'Apple'
select substring(#Name,LEN(#Name)/2+1,1)
I would suggest you try the below query. It should suit all your specified requirements.
SELECT [Id],[Name],CASE WHEN LEN(Name)%2 != 0
THEN SUBSTRING(Name,LEN(Name)/2+1,1)
ELSE SUBSTRING(Name,LEN(Name)/2,1)+' (or) '+ SUBSTRING(Name,LEN(Name)/2 + 1,1)
END AS [MidChar]
FROM [yourTable]
Kindly share your feedback.
Try the below script,
SELECT Id,Name,SUBSTRING(Name,CEILING(LEN(Name)/2.0),1) AS MidChar
FROM MyTable