Using Exclude/Intersect in the same Table - sql

i have this table:
Line Group
1 A
2 A1
3 A2
4 A2
5 ALA
i wanna make a query that selects only the lines that contain a,a1,a2 but not the one named ALA
i tried a query like this:
select linea from table where group like'%A%'
Except
select linea from table where group ='ALA'
But didnt worked what can i do to get the data i need?
Ty in advance

Related

Postgresql : Count columns whose value starting with the value of another column

How do I count rows where a column value starts with a another column value ?
For example, I have table products shown below
---------------------------
id code abbreviation
---------------------------
1 AA01 AA
2 AB02 AB
3 AA03 AA
4 AA04 AB
---------------------------
I want to get the count of products whose code starts with abbreviation. A query like this
select count(*) from products where code ilike abbreviation+'%'
I am using postgresql 9.5.3
The string concatenation operator in postgresql is: ||
select count(*) from products where code like abbreviation || '%';
You can try:
select count(*) from products where code like '%'+abbreviation+'%'
But i am not sure why do you need this type of query.

SQL Getting multiple rows from a single row

I need to accomplish the following :
I have a table with multiple column (c1, c2, c3, c4 ... cn).
I want a query that would return multiple rows in the following fashion (r1 r2 .. rx are the rows in the original table) :
r1c1 r1c2 r1c3
r1c4 r1c5 r1c6
...
r1cn-2 r1cn-1 r1cn
r2c1 r2c2 r2c3
r2c4 r2c5 r2c6
...
r2cn-2 r2cn-1 r2cn
...
rxc1 rxc2 rxc3
rxc4 rxc5 rxc6
...
rxcn-2 rxn-1 rxcn
I know I can use unions and repeat basically the same query n times, but I need to use that query in a web based reporting system that I have no control over and the query is to big for the maximum number of characters allowed in queries.
Any suggestions ?
Thank you !
EDIT : FYI I'm building a report in a report tool I can't change using a database I can't change. So using custom functions/procedures is not a solution. It has to be a PL-SQL query.
To be more specific, i need to have multiple rows from the original row, lets say row 1 is
a b c d e f h i j
and row 2 is
1 2 3 4 5 6 7 8 9
then I would get the following table with 3 columns :
a b c
d e f
h i j
1 2 3
4 5 6
7 8 9
So number 1, if you have a set number of columns in the original table that is divisible by 3, you can just do that many UNION ALL.
Your only other options are pivot or a pointer, neither of which is going to be any better.
The simple case when n is divisible by 3 just use rownum and union:
WITH T1 AS
(
SELECT rownum as rn,1 as tNum, c1 as s1,c2 as s2,c3 as s3 FROM T
UNION ALL
SELECT rownum as rn,2 as tNum, c4 as s1,c5 as s2,c6 as s3 FROM T
UNION ALL
SELECT rownum as rn,3 as tNum, c7 as s1,c8 as s2,c9 as s3 FROM T
)
SELECT s1,s2,s3 FROM T1
ORDER BY rn,tNum
SQLFiddle demo

postgresql full join migrate the first two columns

I have two datasets like,
A B
1 hello
2 hi
3 bye
And:
A C
2 yo
3 gutentag
4 seeya
I'm using FULL JOIN on column A to have the both dataset in one table... But I got this:
A A B C
1 hello
2 2 hi yo
3 3 bye gutentag
4 seeya
Instead of this I would like to get the two A column in one, liket this:
A B C
1 hello
2 hi yo
3 bye gutentag
4 seeya
I know this must be a basic question... But still, I can't solve it. :-)
My code is:
SELECT dataset1.A, dataset2.A, dataset1.B, dataset2.B FROM dataset1
JOIN dataset2
ON (dataset1.A = dataset2.A);
select a, d1.b, d2.c
from
d1
full join
d2 using (a);
Check the using clause: http://www.postgresql.org/docs/current/static/sql-select.html#SQL-FROM
USING implies that only one of each pair of equivalent columns will be included in the join output

How to do 'Summarizing' in Pig Latin?

Im trying to do a summarize operation with pig.
For example, I have a table called t3:
product price country
A 5 Italy
B 4 USA
C 12 France
A 5 Italy
B 7 Russia
I need to do a summarize operation, using 2 keys: product and country.
I do concatenate operation, using product and country
I have to calculate the price, summarizing the price values just where CONCAT result repeats
Where CONCAT result does not repeat, price remains the same as in t3 table.
The expected output could be:
CONCAT Price_1
AItaly 10
BUSA 4
CFrance 12
BRussia 7
In pig i write following script (the code is wrong, but just to show an idea):
t3 = LOAD '/home/Desktop/3_table/3_table.data' AS (product:chararray, price:int, country:chararray);
c1 = FOREACH t3 GENERATE CONCAT(product, country);
c2 = FOREACH t3 GENERATE *, c1;
product_1 = GROUP c2 BY c1;
price_1 = FOREACH product_1 GENERATE group, SUM(product_1.price);
STORE price_1 INTO 'summarise_by_2_ID' USING PigStorage('\t');
Maybe someone can explain how to reach the expected result?
Thanks a lot in advance!
If you want to calculate the sum per product and country you do not need to use the concat function. Just group by those two fields.
A = LOAD 's.txt' USING PigStorage('\t') AS (product:chararray, price:int, country:chararray);
B = GROUP A BY (product, country);
C = FOREACH B GENERATE CONCAT(group.product,group.country), SUM(A.price);
Actually, the concat is not necessary here, it is only to format the output as expected.
DUMP C
(AItaly,10)
(BUSA,4)
(BRussia,7)
(CFrance,12)

Why does this query return "incorrect" results?

I have 3 tables:
'CouponType' table:
AutoID Code Name
1 CouT001 SunCoupon
2 CouT002 GdFriCoupon
3 CouT003 1for1Coupon
'CouponIssued' table:
AutoID CouponNo CouponType_AutoID
1 Co001 1
2 Co002 1
3 Co003 1
4 Co004 2
5 Co005 2
6 Co006 2
'CouponUsed' table:
AutoID Coupon_AutoID
1 2
2 3
3 5
I am trying to join 3 tables together using this query below but apparently I am not getting right values for CouponIssued column:
select CouponType.AutoID, Code, Name, Count(CouponIssued.CouponType_AutoID), count(CouponUsed.Coupon_AutoID)
from (CouponType left join CouponIssued
on (CouponType.AutoID = CouponIssued.CouponType_AutoID))
left join CouponUsed
on (couponUsed.Coupon_AutoID = CouponIssued.AutoID)
group by CouponType.AutoID, code, name
order by code
The expected result should be like:
**Auto ID Code Name Issued used**
1 CouT001 SunCoupon 3 2
2 CouT002 GdFriCoupon 3 1
3 CouT003 1for1Coupon 0 0
Thanks!
SELECT t.AutoID
,t.Code
,t.Name
,count(i.CouponType_AutoID) AS issued
,count(u.Coupon_AutoID) AS used
FROM CouponType t
LEFT JOIN CouponIssued i ON i.CouponType_AutoID = t.AutoID
LEFT JOIN CouponUsed u ON u.Coupon_AutoID = i.AutoID
GROUP BY 1,2,3;
You might consider using less confusing names for your table columns. I have made very good experiences with using the same name for the same data across tables (as far as sensible).
In your example, AutoID is used for three different columns, two of which appear a second time in another table under a different name. This would still make sense if Coupon_AutoID was named CouponIssued_AutoID instead.
change count(Coupon.CouponType_AutoID) to count(CouponIssued.CouponType_AutoID) and count(Coupon.Coupon_AutoID) to count(CouponUsed.Coupon_AutoID)