I'm trying to eliminate duplicate rows from an Access dataset made up of two tables table1 and table2. I want to retrieve the distinct values from one column and fields from two tables, but also retrieve the values of other columns of duplicates and unique values.
Sample data (not actual data):
Table 1:
ID BOM_PART_NAME PART FIND NO POS LCN POS_CT
1 E 0001 1 P0 1
2 A 0002 1 P1 1
3 C 0003 1 P2 1
4 D 0004 1 P3 1
5 F 0005 1 P4 1
Table 2:
ID PART-ATA-NO PART-SN PART-NAME
1 001 A
2 002 B
3 003 C
4 004 1100 D
5 005 1101 E
Table 2:
ID PART-ATA-NO PART-SN PART-NAME
1 001 A
2 002 B
3 003 C
4 004 1100 D
5 005 1101 E
What I'm getting (just showing major columns in result set to keep brief):
ID ... PART FIND NO POS PART-ATA-NO PART-SN
1 001 1 001 1369
2 002 1 002 1444
3 003 1 003 1100
3 003 1 003 1101
3 003 1 003 1102
4 003 2 003 1101
4 003 2 003 1102
5 004 1 004 1101
5 004 1 004 1102
Desired Result:
ID PART FIND NO POS PART-ATA-NO PART-SN
1 001 1 001 1369
2 002 1 002 1444
3 003 1 003 1100
4 003 2 003 1101
5 003 3 003 1102
6 003 4 003 1103
7 003 5 003 1104
8 004 1 004 1105
9 004 2 004 1106
ID PART FIND NO POS PART-ATA-NO PART-SN
1 001 1 001 1369
2 002 1 002 1444
3 003 1 003 1100
3 003 1 003 1101
3 003 1 003 1102
4 003 2 003 1101
4 003 2 003 1102
5 004 1 004 1101
5 004 1 004 1102
Desired Result:
ID PART FIND NO POS PART-ATA-NO PART-SN
1 001 1 001 1369
2 002 1 002 1444
3 003 1 003 1100
4 003 2 003 1101
5 003 3 003 1102
6 003 4 003 1103
7 003 5 003 1104
8 004 1 004 1105
9 004 2 004 1106
With matching PART FIND NO's (and PART-ATA-NO's), POS should be incremented by 1. The SN's should all be unique.
The query I used to join the two tables:
SELECT Table1.*, Table2.[PART-ATA-NO], Table2.[PART-SN]
FROM Table1 INNER JOIN Table2 ON Table1.[PART FIND NO] = Table2.[PART-ATA-NO];
The field I want unique values from is [PART-SN] from table2. I want to select all other fields from table1 and [PART-SN] from table 2, of which all rows should be returned for all distinct rows of [PART-SN]. [PART FIND NO] and [PART-ATA-NO] have equivalent values. Is there a way to join the two tables to get this result?
Related
how do I join on the same column multiple times, one of the tables is a configuration table with values and other is the data table.
T1:
ID Seq Code1 Code2 Code3
1 001 101 203 305
1 002 107
2 001 103
3 005 213
4 009 320
5 001 101 314
T2 (Config table):
Value Description
101 Strawberry
103 Raspberry
107 Blueberry
111 Banana
203 Cashews
213 Almonds
305 Bellpepper
320 Tomatoes
314 Potatoes
I need to display the code and its relevant description for each ID.
Expected Output:
ID Code1 Description Code2 Description Code3 Description
1 101 Strawberry 203 Cashews 305 Bellpepper
1 107 Blueberry
2 103 Raspberry
3 213 Almonds
4 320 Tomatoes
5 101 Strawberry 314 Potatoes
This is what I have tried so far, however, it is not giving me the desired output:
select distinct ID,code1, T2.description,
code2, T2.description, code3, T2.description
from T1,T2
where (T1.Code1=T2.Value OR T1.Code2=T2.Value or T1.Code3=T2.Value)
How can I achieve this? Sorry if my question is confusing, I can provide more details if needed.
That's multiple outer join with the t2 table:
select a.id, a.seq,
a.code1, d1.description,
a.code2, d2.description,
a.code3, d3.description
from t1 a left join t2 d1 on a.code1 = d1.value
left join t2 d2 on a.code2 = d2.value
left join t2 d3 on a.code3 = d3.value
order by a.id, a.seq;
ID SEQ CODE1 DESCRIPTIO CODE2 DESCRIPTIO CODE3 DESCRIPTIO
----- --- ---------- ---------- ---------- ---------- ---------- ----------
1 001 101 Strawberry 203 Cashews 305 Bellpepper
1 002 107 Blueberry
2 001 103 Raspberry
3 005 213 Almonds
4 009 320 Tomatoes
5 001 101 Strawberry 314 Potatoes
I have table and have a data as follows
Ac1 ac2 name ind
123 223 john y
123 000 john y
123 001 john N
123 002 roy N
223 000 roy N
223 001 roy Y
223 002 james y
234 001 james y
234 000 james y
I have this table I want to show ac2 is other than 000 any other value i need to show the ind1 new column as the ind of 000 value
Ac1 ac2 name ind ind1
123 223 john y Y
123 000 john y Y
123 001 john N Y
123 002 roy N N
223 000 roy N N
223 001 roy Y N
223 002 james y Y
234 001 james y Y
234 000 james y Y
My table always has account with 000 and now if i want to view it by name then I am not getting expected result
select ac1, ac2, ind,case when ac2 = 0 then ind end as ind1 from table;
select a.ac1, a.ac2, a.name, a.ind, b.ind ind1
from table a join (select distinct name, ind from table where ac2='000')b on a.name=b.name
I want to add a new column on my SQL query that will count the number of times a value on the row appears in a different table.
For example, let's say I have these two separate tables.
Table Name: Table_A
Id | Coach | Team_Color | Team_Number
------------------------------------------
001 | Jane | Orange | 121
002 | Frank | Purple | 232
003 | Tim | Red | 343
Table Name: Table_B
Id | Team_Number | Player_Name
----------------------------------
901 | 121 | Jimmy
902 | 121 | Wesley
903 | 121 | Samantha
904 | 121 | Wendy
905 | 232 | Tim
906 | 232 | Sean
907 | 343 | Andrew
908 | 343 | Erik
909 | 343 | Sarah
910 | 343 | Allison
911 | 343 | Desmond
912 | 343 | Kathryn
I want to end up with something like this:
Id | Coach | Team_Color | Team_Number | Player Count
--------------------------------------------------------
001 | Jane | Orange | 121 | 4
002 | Frank | Purple | 232 | 2
003 | Tim | Red | 343 | 6
The new column called "Player Count" is referencing the Team_Number value in Table_A, and counting the number of instances found on Table_B. How would I compose this into one query?
Solution to your Problem:
SELECT A.Id, A.Coach, A.Team_Color,A.Team_Number,Count(B.Id) AS Player_Count
FROM Table_A AS A
INNER JOIN Table_B B
ON A.Team_Number = B.Team_Number
GROUP BY A.Id, A.Coach, A.Team_Color,A.Team_Number;
OUTPUT:
Id Coach Team_Color Team_Number Player_Count
1 Jane Orange 121 4
2 Frank Purple 232 2
3 Tim Red 343 6
Follow the link to the demo:
http://sqlfiddle.com/#!9/9c6da4/1
EXPLAINATION:
In your problem, you have to use JOIN to join the two tables on a common Column i.e.Team_Number. Now after Joining you will get a result like this:
Id Coach Team_Color Team_Number Id Team_Number Player_Name
1 Jane Orange 121 901 121 Jimmy
1 Jane Orange 121 902 121 Wesley
1 Jane Orange 121 903 121 Samantha
1 Jane Orange 121 904 121 Wendy
2 Frank Purple 232 905 232 Tim
2 Frank Purple 232 906 232 Sean
3 Tim Red 343 907 343 Andrew
3 Tim Red 343 908 343 Erik
3 Tim Red 343 909 343 Sarah
3 Tim Red 343 910 343 Allison
3 Tim Red 343 911 343 Desmond
3 Tim Red 343 912 343 Kathryn
Now Use aggregate function COUNT on above Result to get the final result.
I have try to UNION two tables and left join but the result no I want.Any Idea ?Please Help.
Table a
Deduction_No GROUP_ID EMP_ID Service_code AMT
PRD533 PRG158 NULL 2351 35
PRD533 PRG158 NULL 4854 160
PRD533 PRG158 NULL 4857 44
PRD533 PRG158 NULL 6611 3
Table b
Deduction_No GROUP_ID EMP_ID Is_Selected Deduct_Amt
PRD533 PRG158 001 Y 82
PRD533 PRG158 001 N 0
PRD533 PRG158 002 Y 195
PRD533 PRG158 002 N 0
PRD533 PRG158 003 Y 79
PRD533 PRG158 003 N 0
PRD533 PRG158 004 Y 107
PRD533 PRG158 004 N 0
Output,but I don't want to like this (UNION)
Deduction_No GROUP_ID EMP_ID Service_code Deduct_Atm
PRD533 PRG158 001 -
PRD533 PRG158 002 -
PRD533 PRG158 003 -
PRD533 PRG158 004 -
PRD533 PRG158 - 2351 35
PRD533 PRG158 - 4854 160
PRD533 PRG158 - 4857 44
PRD533 PRG158 - 6611 3
Output using left join
Deduction_No GROUP_ID EMP_ID Service_code Deduct_Atm
PRD533 PRG158 001 2351 35
PRD533 PRG158 001 4854 160
PRD533 PRG158 001 4857 44
PRD533 PRG158 001 6611 3
PRD533 PRG158 002 2351 35
PRD533 PRG158 002 4854 160
PRD533 PRG158 002 4857 44
PRD533 PRG158 002 6611 3
Desired output
Deduction_No GROUP_ID EMP_ID Service_code AMT
PRD533 PRG158 001 2351 35
PRD533 PRG158 002 2351 35
PRD533 PRG158 003 2351 35
PRD533 PRG158 001 4854 160
PRD533 PRG158 002 4857 44
PRD533 PRG158 004 4857 44
PRD533 PRG158 003 6611 3
PRD533 PRG158 004 6611 3
Thank you
This query:
SELECT a.DEDUCTION_NO, a.GROUP_ID, b.EMP_ID, a.SERVICE_CODE, a.AMT
FROM #a a
LEFT JOIN #b b ON a.DEDUCTION_NO = b.DEDUCTION_NO
WHERE b.IS_SELECTED ='Y'
ORDER BY Service_code
Will give you following output:
DEDUCTION_NO GROUP_ID EMP_ID SERVICE_CODE AMT
PRD533 PRG158 1 2351 35
PRD533 PRG158 2 2351 35
PRD533 PRG158 3 2351 35
PRD533 PRG158 4 2351 35
PRD533 PRG158 1 4854 160
PRD533 PRG158 2 4854 160
PRD533 PRG158 3 4854 160
PRD533 PRG158 4 4854 160
PRD533 PRG158 1 4857 44
PRD533 PRG158 2 4857 44
PRD533 PRG158 3 4857 44
PRD533 PRG158 4 4857 44
PRD533 PRG158 1 6611 3
PRD533 PRG158 2 6611 3
PRD533 PRG158 3 6611 3
PRD533 PRG158 4 6611 3
Since you don't give me more information I can't help for you, you should provide how data should be selected, on whose conditions and etc.
I have a dimensions table
#dimensions
Dateid Rep Mkt Prodgroup
201111 002 S 001
201111 002 R 001
201111 002 S 002
201111 002 R 002
201111 002 S 003
201111 002 R 003
201111 002 S 004
201111 002 R 004
...
I want to join it with this table to include the percentage.
#percentageincrease
Prodgroup Mkt Percent
ALL S 1.05
ALL R 1.06
002 S 1.07
002 R 1.08
003 S 1.09
003 R 1.10
I want to join them on Mkt and Prodgroup where #dimensions.Prodgroup IN #percentageincrease.Prodgroup and if not in then to join on the ALL. So the output table would be
Dateid Rep Mkt Prodgroup Percent
201111 002 S 001 1.05 // joined on ALL
201111 002 R 001 1.06 // joined on ALL
201111 002 S 002 1.07 // joined on 002
201111 002 R 002 1.08 // joined on 002
201111 002 S 003 1.09 // joined on 003
201111 002 R 003 1.10 // joined on 003
201111 002 S 004 1.05 // joined on ALL
201111 002 R 004 1.06 // joined on ALL
...
As my join condition I have tried
on case p.Prodgroup
when N'ALL'
then d.prodgrpid
else p.Prodgrpid = d.prodgrpid
AND p.Mkt = d.Mkt
but it gave me
Dateid Rep Mkt Prodgroup Percent
201111 002 S 001 1.05
201111 002 R 001 1.06
201111 002 S 002 1.07
201111 002 R 002 1.08
201111 002 S 002 1.05 //Joined on ALL
201111 002 R 002 1.06 //Joined on ALL
201111 002 S 003 1.09
201111 002 R 003 1.10
201111 002 S 003 1.05 //Joined on ALL
201111 002 R 003 1.06 //Joined on ALL
201111 002 S 004 1.05
201111 002 R 004 1.06
...
where the tuples which were in the #percentageincrease table were also joined on the ALL condition.
This is being run on SQL Server 2008 as a stored procedure as part of a larger query.
Assuming the table structures
declare #dimensions table
(Dateid varchar(10), Rep varchar(10), Mkt varchar(10),Prodgroup varchar(10))
declare #percentageincrease table
(Prodgroup varchar(10), Mkt varchar(10), [Percent] decimal(9,5))
then
select
d.*,
isnull(pinc.[Percent], pincall.[Percent])
from #dimensions d
left join #percentageincrease pinc
on d.Mkt = pinc.Mkt
and d.Prodgroup = pinc.Prodgroup
left join #percentageincrease pincall
on d.Mkt = pincall.Mkt
and pincall.Prodgroup ='all'
try this:
select * from
(
select d.*,p.[Percent] from
dimensions d inner join percentageincrease p
on d.Mkt = p.Mkt and d.Prodgroup = p.Prodgroup
union
select d.*,p.[Percent] from
dimensions d join percentageincrease p
on d.Mkt = p.Mkt and d.Prodgroup not in
(select distinct d.Prodgroup from dimensions d inner join percentageincrease p
on d.Mkt = p.Mkt and d.Prodgroup = p.Prodgroup)
where p.Prodgroup='ALL'
) a
order by a.Prodgroup