Query in multiple codes to the same Product - postgresql-9.5

I have the following table:
cod_1 Cod_2 Prod
10 33 A
2 78 B
5 105 A
7 34 D
3 Null D
What I need to query:
How many cod_1 and Cod_2 the produt A has:
result:
A has cod_1 = 10 and 5 ; A has code_2 = 33 and 105
D has cod_1 7 and 3;
So, the query needs to show when a Product has more than one cod_1 and cod_2
I am not exp'ed in postgresql so I have no clue where to start.
Thanks

According to your table structure and examples, following query can achieve your requirement,
SELECT
PROD,
STRING_AGG(COD_1, ', ') AS COD_1,
STRING_AGG(COD_2, ', ') AS COD_2
FROM CUSTOMER
WHERE PROD = 'A'
GROUP BY PROD
Output
PROD COD_1 COD_2
A 10, 5 33, 105
Note: All my column names are block capital, you might need to change that.

Related

To select a common record when adding other field

A | B
1-1 10
1-1 10
1-1 12
1-1 22
1-4 20
1-4 20
1-4 10
Output Table:
A | B
1 104
I have given the table and the output table. I am not able to generate that output. The output I get is given below and the code I used as well.
A | B
1 54
1 50
The code I used is : select left(A,2) as A, SUm(KSL05) as B from LKP_FAGLFLEXT where condition group by A
you can use group by as below:
select left(a,2), sum(b) from yourtable
group by left(a,2)

Extracting sub data from a table

I would be happy for your help.
I have a table like this :
[MS_CODE] [MS_SML]
1 43
1 AA
2 51
3 24
3 21
4 11
4 43
5 AA
6 11
I want to write a query that will serach for the [MS_SML] which shows up in group (1 or 2 or 3) And (4 or 5 or 6) in [MS_Code].
For example:
43,AA because 43 is in a row where ms_code is 1 and 4 and same for 'AA'. I would like to create output like this:
[MS_Code] [MS_SML]
1 43
4 43
1 AA
5 AA
Thank you very much for your help!
One method is to use exists and apply your criteria:
select t.*
from t
where exists (select 1
from t t2
where t2.ms_sml = t1.ms_sml and t2.ms_code in (1, 2, 3)
) and
exists (select 1
from t t2
where t2.ms_sml = t1.ms_sml and t2.ms_code in (4, 5, 6)
);
Here is one way to do it.
select ms_code, ms_sml
from msc
where ms_sml in
(
select ms_sml
from msc
where ms_code in (1,2,3)
intersect
select ms_sml
from msc
where ms_code in (4,5,6)
)
order by ms_sml, ms_code
Note: If there is more than one ms_code for a given ms_sml in the same group, this will return all of them.
Suppose AA is mapped to 1, 3 and 5, this will return
1 AA
3 AA
5 AA
If that is an issue, We may need additional logic to deal with that: for example pick the minimum value of ms_code within the group.

SQL, find an ID which contains 2 values in 2 lines

My BDD looks like that :
id_feature | id_product | id_feature_value
1 1 20
2 2 21
3 3 20
4 2 20
I need to get the product which have id_Feature_Value 20 AND 21.
I can't find the rigth syntax the have my result...
Thanks
This should work:
select
a.id_product
from
yourtable a,
yourtable b
where
a.id_product=b.id_product and
a.id_feature_value=20 and
b.id_feature_value=21
I have found how to do.
My request check all the product which have the value 20 OR 21. Si if they have the two values, the id_product will be display twice because there is no group by.
I just had to add a where clause with HAVE(id_product) > 1

SQL Server Concatenate Rows Using Many-To-Many Related Tables

I've already searched for my problem, but most of the examples (if not all), have to deal with only one or two tables with a one-to-many relationship between them.
So, here goes my scenario:
My first table is:
OrderID Quantity Price
----------------------------------
18 1000.00 160.00
19 1000.00 40.00
22 1000.00 40.00
23 100.00 500.00
24 10.00 50.00
My second table is:
ExtrasID Name
-------------------
1 Value 1
2 Value 2
3 Value 3
4 Value 4
5 Value 5
I have a many-to-many relationship established between the tables above, so there is a third (joint) table which is as follows:
OrderExtrassID OrderExtras_OrderID OrderExtras_ExtrasID
----------------------------------------------------------------
20 19 2
22 22 3
23 23 2
24 23 5
Now, all I want to achieve is to get a result such as the following:
OrderID Extras
----------------------------
18 NULL
19 Value 2
22 Value 3
23 Value 2, Value 5
24 NULL
There are many examples using XML PATH, PIVOT, CTE etc., but none of these seems to help me with my scenario, or at least I've not managed to study them in depth, in order to get my work done...
Thanks in advance,
I would suggest using For XML to return multiple rows into a single row and then using STUFF to removing the extra comma. Something like this:
SELECT O.OrderId
,STUFF(
(
SELECT ',' + E.Name AS [text()]
FROM OrderExtras OE
Inner Join Extras E
ON E.ExtrasId = OE.OrderExtras_ExtrasID
WHERE O.OrderId = OE.OrderExtras_OrderID
ORDER BY E.Name
FOR XML PATH('')
), 1, 1, '') AS ColList
FROM Orders O
And here is the SQL Fiddle.
Good luck.

Rows to comma separated list in SQL

I have a requirement wherein the table structure I have looks like
ID Owner Id NAME
1 20 Name 1
1 21 Name 2
1 34 Name 3
2 10 Name 4
2 12 Name 5
3 100 Name 6
I need a query that would give me the results as
ID Owner ID Name
1 20 Name 1, Name2, Name 3
2 10 Name4, Name5
3 100 Name 6
Currently we do this on the codebehind, but I would ideally like to do this through SQL and see if that amounts to any performance improvement.
You did not mention your DBMS so I'm assuming PostgreSQL:
SELECT id,
min(owner_id) as lowest_id,
string_agg(name, ', ') as name_list
FROM the_table
GROUP BY id