Using same database table with different alias to retrieve data - sql

Here what I am trying to do is to get the values for P1,P2,P3 that should comes from A3,
this works well for two tables but not for three...
SELECT x.A1,x.A3,x.A4,A5,A6, x.A2 as P1,y.A2 as P2,z.A2 as P3
FROM Contact x,Contact y,Contact z
WHERE (x.id = y.id) AND (y.id = z.id) AND
(x.A3 ='pre-sale') AND (y.A3= pos-sale') AND(z.A3='current-sale')
ORDER by x.A4 DESC
For example
the CONTACT table will look like this with some expected results for P1,P2, P3
A1 A2 A3 A4 A5 A6 P1 P2 P3
----------------------------------------------------
1 22 pre-sale 9 kk 8 22 31 2
2 31 pos-sale 4 yy 6 44 61 11
3 2 current-sale 1 hh 2 null null null
4 44 pre-sale 2 kk 8
5 61 pos-sale 1 yy 6
6 11 current-sale 1 hh 2
For P1, P2 using twice same table works well, adding the third table the values
for P1, P2 are the same and for P3 all null

I suspect that what you are trying to do might best be done with conditional aggregation. It is a bit hard to tell what you are really trying to accomplish, because not all the columns are aliased. Here is an example
SELECT c.id,
max(case when c.A3 = 'pre-sale' then A4 end) as PreSale_A4,
max(case when c.A3 = 'pos-sale' then A2 end) as PosSale_A2,
max(case when c.A3 = 'current-sale' then A4 end) as CurrentSale_A2
FROM Contact c
group by c.id
order by PreSale_A4 desc;
Your original query has some fundamental problems. For instance, you are using implicit joins via the where clause, which are always inner joins. The solution to your problem might be using outer joins instead. Conditional aggregation probably solves the problem, though.

Related

How to calculate rank with group by in sql?

Suppose if I have table1 as following
Category Brand Value
A A1 4
B B1 7
C C1 8
A A2 3
B B2 4
C C2 6
A A3 9
B B3 10
C C3 1
A A4 5
Now if I want to calculate rank for each brand but grouped by category how do I go about it?
Something like
Select rank() (over value)
from table
group by category
Expected output is this:
Category Brand Value Rank
A A3 9 1
A A4 5 2
A A1 4 3
A A2 3 4
B B3 10 1
B B1 7 2
B B2 4 3
C C1 8 1
C C2 6 2
C C3 1 3
Maybe you are looking for something like this.
See this official documentation on DENSE_RANK for more details
select brand, category, dense_rank() over(partition by category order by value desc) as dr
from table
You may add a PARTITION BY clause to your RANK() call, specifying the category as the partition.
SELECT RANK() OVER (PARTITION BY category ORDER BY value) rnk
FROM yourTable
ORDER BY category, rnk;

Teradata SQL count on Top n subgroups

I am using TD v15. I have a table as below - each row is a single record, I want to perform Count in the following way:
In Question Column: I have 4 'A', 5 'B', 3 'C' and 2 'D'. Select top 2 from them, which are A & B. Group the rest Questions as 'OtherQ' - Put them in Result Question Column.
In Change Column, I have 2 'AA', 3 'AB', 2'AC', 2 'AD', 4 'AE' and 2 'AG', select top 2, which are AE & AB, group the rest Change as 'Other' - Put them in Result Change Column.
Then, count according...
Question Result Change
A Pass AG
A Pass AE
A Pass AA
A Pass AB
B Pass AC
B Pass AG
B Pass AB
B Pass AE
B Pass AD
B Pass AA
C Pass AB
C Pass AC
C Pass AD
D Pass AE
D Pass AE
A Fail Null
A Fail Null
C Fail Null
E Fail Null
B Fail Null
This is the desired result, it counts on top 2 questions (A&B) and OtherQ with Top 2 changes (AE&AB) and other Changes, also, it counts Pass&Fail for A&B and OtherQ.
The sum of Count is 20, this should match the 20 individual row in the table above.
Question Result Change Count
A Pass AE 1
A Pass AB 1
A Pass Other 2
B Pass AE 1
B Pass AB 1
B Pass Other 4
OtherQ Pass AE 2
OtherQ Pass AB 1
OtherQ Pass Other 2
A Fail Null 2
B Fail Null 1
OtherQ Fail Null 2
Could you please kindly help? It's very large data table, needs the code to be efficient. Many thank for your time and help in advance.
I would suggest using aggregations and subqueries:
select coalesce(tq.question, 'Other') as question
(case when t.change is null then null
else coalesce(tch.change, 'Other')
end) as change,
count(*)
from t left join
(select question, count(*) as cnt,
row_number() over (order by count(*) desc) as seqnum
from t
group by question
) tq
on tq.question = t.question and tq.seqnum <= 2 left join
(select change, count(*) as cnt,
row_number() over (order by count(*) desc) as seqnum
from t
group by change
) tch
on tch.change = t.change and tch.seqnum <= 2
group by coalesce(tq.question, 'Other'),
coalesce(tch.change, 'Other');

sql combining 2 columns sql

I'm a bit rusty on my SQL and Access so just wanted some help.
I have a set of data like the below in Access
C1 C2 C3
1 A C
2 B D
3 B E
From this data I wanted run a SQL query that will combine columns 2 and 3 but also retain the information in column 1 alongside it. I've illustrated this below
C1 C2
1 A
2 B
3 B
1 C
2 D
3 E
I've run a sql query using a union syntax to combine columns 1 and 2 already but can't figure out how to include the column 1 data as well.
Any help on this would be greatly appreciated.
Thanks,
Shan
Don't know access but if it supports union something like:
select c1, c2 as c2 from T
union all
select c1, c3 as c2 from T
should work. If c2 and c3 always differ, or if you are not interested in duplicates union can be used instead of union all

ORACLE Special JOIN

Let me try to explain the scenario. I have two tables A (Columns - A1, A2, A3) & B (Columns - B1, B2, B3). I need to join table A with table B on A1.B2. For every join, table B has one or two records with different values for B3(X or Y). I wanna write one query where the JOIN query needs to pick the row with B3=X(if there's no other row with B3=Y); If two rows exists (B3=X & B3=Y), then the query needs to pick only the row with B3=Y (ignoring the row with B3=X).
Let me try to give some values to the tables & explain a little bit more.
Table A
********
A1 A2 A3
1 11 111
2 22 222
3 33 333
4 44 444
Table B
********
B1 B2 B3
6 1 X
7 1 Y
8 2 X
9 3 X
10 3 Y
11 4 X
Again.. JOIN is on A1.B2. The result should be as following,
JOIN Results
*************
A1 A2 A3 B1 B2 B3
1 11 111 7 1 Y
2 22 222 8 2 X
3 33 333 10 3 Y
4 44 444 11 4 X
Let me know if you guys have any clarification about my question.
Thanks in advance.
Yogi
You can pick the rows from table B with the ROW_NUMBER function if you partition by the join column and order by your "picking order" column:
SELECT b1, b2, b3,
ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY b3 DESC) as rn
FROM b;
1 Y 1
1 X 2
2 X 1
3 Y 1
3 X 2
4 X 1
Then you can filter the first row, the one with rn=1:
SELECT b1, b2, b3
FROM (SELECT b1, b2, b3,
ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY b3 DESC) as rn
FROM b)
WHERE rn=1;
7 1 Y
8 2 X
10 3 Y
11 4 X
The filtered rows can then be joined to table a:
SELECT *
FROM a
JOIN (
SELECT b1, b2, b3
FROM (SELECT b1, b2, b3,
ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY b3 DESC) as rn
FROM b
)
WHERE rn=1
) bfilter ON a.a1 = bfilter.b2;
1 11 111 7 1 Y
2 22 222 8 2 X
3 33 333 10 3 Y
4 44 444 11 4 X
If 'X' and 'Y' are not actual values, you can extend the ORDER clause with a CASE statement to allow for general values:
ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY
CASE b3 WHEN 'Y' THEN 1
WHEN 'X' THEN 2
...
END ASC)
Edit:
SELECT a1, a2, a3, b1, b2, b3
FROM (
SELECT a1, a2, a3, b1, b2, b3,
ROWNUMBER() OVER (PARTITION BY a1 ORDER BY
CASE WHEN a2=... AND b3=... THEN 1
WHEN a2=... AND b3=... THEN 2
...
END ASC)
FROM a JOIN b ON a.a1 = b.b2
)
WHERE rn = 1;
You can use left outer joins as follows
select A.A1, A.A2, A.A3,
nvl(BT1.B1, BT2.B1),
nvl(BT1.B2, BT2.B2),
nvl(BT1.B3, BT2.B3) from A
left outer join B BT1 on A.A1 = BT1.B2 and BT1.B3 = 'Y'
left outer join B BT2 on A.A1 = BT2.B2 and BT2.B3 = 'X'
A good explanation of the various joins is at http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Here is, how I would do it:
Make the join
group by B2
take the max(B3)
That way you ensure that X is only picked, when there is no alphabetically higher value (Y) available
With UNION
select a.*,b.* from a,b
where a.a1=b.b2
and b.b3='Y'
union
select a.*,b.* from a,b
where a.a1=b.b2
and not exists (select bb.br from b bb where bb.b2=a.a1 and bb.b3='Y')
Without UNION
select a.*,b.* from a,b
where a.a1=b.b2
and (b.b3='Y'
or not exists (select bb.b3 from b bb where bb.b2=a.a1 and bb.b3='Y'))
The constraint here is that B has exactly 1 or 2 rows for each A's row

SELECT statement with multiple WHERE criteria (MS-Access)

Below is the sample data:
c1 c2 c3 c4 c5
1 a1 a 1 1
2 a2 a 2 1
3 a3 a 3 1
4 a4 a 4 1
5 b1 b 1 1
6 b2 b 2 1
7 b3 b 3 1
8 b4 b 4 1
9 a1 c 3 1
I want to get the the below details:
c1 c2 c3 c4 c5
1 a1 a 1 1
5 b1 b 1 1
9 a1 c 3 1
C1 is primary key, the criteria is for any given unique(c2) where c4 is the lowest, I want to return the contents(all the 5 columns) of the row.
Try this:
SELECT t1.*
FROM Table1 t1
INNER JOIN
(
SELECT c3, MIN(c4) c4
FROM Table1
GROUP BY c3
) t2 ON t1.c3 = t2.c3 ANd t1.c4 = t2.c4
SQL Fiddle Demo
Update:1 In SQL the returned results is a set set(unless you specify an ORDER BY clause, it is a cursor in this case), wherein the order is not guaranteed. This is a standard. You should use an ORDER BY clause if you want to guarantee a specific order. In your case , the results is not guaranteed to be ordered like 1 5 9. Add ORDER BY c1 instead.
The ORDER BY clause might be crucial in some cases, for example, if want to get the top three rows, or the maximum one, in this case you have to specify an ORDER BY clause.
So if you wants to persist a specific order the you have specify an ORDER BY.
1 As noted by #Fahim Parker, see the comments below.
select c1,c2,c3,c4,c5
from table
where c4= (select min(c4) from table as f where f.c4 = table.c4);
i hope that helps