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
Related
I have 3 columns like below:
ID
Status
Date
001
T
2022-09-27
001
T
2022-09-26
001
T
2022-09-25
001
T
2022-09-24
001
T
2022-09-23
001
T
2022-09-22
001
T
2022-09-21
001
R
2022-09-20
001
R
2022-09-19
001
R
2022-09-18
001
R
2022-09-17
001
R
2022-09-16
001
T
2022-09-15
001
T
2022-09-14
001
T
2022-09-13
001
T
2022-09-12
001
T
2022-09-11
001
T
2022-09-10
001
T
2022-09-09
001
T
2022-09-08
001
T
2022-09-07
001
T
2022-09-06
How can I generate an output like this in Snowflake?
ID
Status
Start_date
End_date
001
T
2022-09-21
2022-09-27
001
T
2022-09-06
2022-09-15
It is "gaps and islands" class problem. The easiest way is to use MATCH_RECOGNIZE clause:
SELECT *
FROM test
MATCH_RECOGNIZE (
PARTITION BY ID
ORDER BY Date
MEASURES
CLASSIFIER() AS Status
,MIN(Date) AS Start_date
,MAX(Date) AS End_date
PATTERN (T+)
DEFINE T AS Status = 'T'
) AS mr;
Output:
I need trial balance from following tables:
Table: Journal
ID
TransactionDate
AccountCodeLevel1VarChar
AccountCodeLevel2VarChar
AccountCodeLevel3VarChar
AccountCodeLevel4VarChar
AccountCodeLevel5VarChar
AccountCodeLevel6VarChar
DebitAmountDecimal
CreditAmountDecimal
DescriptionVarchar
1
2022-1-1
1
01
01
01
001
001
1
Received cash from issuing common stocks
2
2022-1-1
3
01
01
01
001
001
1
Received cash from issuing common stocks
3
2022-1-1
1
01
01
01
001
001
2
Received cash borrowed from bank
4
2022-1-1
2
01
01
01
001
001
2
Received cash borrowed from bank
5
2022-1-1
1
01
01
01
001
001
3
Received cash borrowed from bank
6
2022-1-1
2
01
01
01
001
001
3
Received cash borrowed from bank
7
2022-1-1
1
01
01
01
001
001
4
Received cash from selling products
8
2022-1-1
4
01
01
01
001
001
4
Received cash from selling products
9
2022-1-1
1
01
01
01
001
001
5
Collected cash from services rendered
10
2022-1-1
4
02
01
01
001
001
5
Collected cash from services rendered
11
2022-1-1
5
01
01
01
001
001
6
Paid employee salaries
12
2022-1-1
1
01
01
01
001
001
6
Paid employee salaries
Table: AccountCode
AccountCodeLevel1VarChar
AccountCodeLevel2VarChar
AccountCodeLevel3VarChar
AccountCodeLevel4VarChar
AccountCodeLevel5VarChar
AccountCodeLevel6VarChar
AccountNameVarChar
1
01
01
01
001
001
Cash
2
01
01
01
001
001
Loan from banks
3
01
01
01
001
001
Paid-up common shares
4
01
01
01
001
001
Service revenue
4
02
01
01
001
001
Sales revenue
5
01
01
01
001
001
Salary
How can I get the SQL to render the summary result as shown hereunder with DebitAmountDecimal minus CreditAmountDecimal?
AccountCodeLevel1VarChar
AccountCodeLevel2VarChar
AccountCodeLevel3VarChar
AccountCodeLevel4VarChar
AccountCodeLevel5VarChar
AccountCodeLevel6VarChar
AccountNameVarChar
TrialBalanceAmount
1
01
01
01
001
001
Cash
9
2
01
01
01
001
001
Loan from banks
-5
3
01
01
01
001
001
Paid-up common shares
-1
4
01
01
01
001
001
Service revenue
-4
4
02
01
01
001
001
Sales revenue
-5
5
01
01
01
001
001
Salary
6
I tried to do with this SQL statement but the result is wrong:
SELECT
"ACC"."AccountCodeLevel1VarChar",
"ACC"."AccountCodeLevel2VarChar",
"ACC"."AccountCodeLevel3VarChar",
"ACC"."AccountCodeLevel4VarChar",
"ACC"."AccountCodeLevel5VarChar",
"ACC"."AccountCodeLevel6VarChar",
"ACC"."AccountNameVarChar",
SUM( "JNL"."DebitAmountDecimal" - "JNL"."CreditAmountDecimal" ) "TrialBalanceAmount"
FROM
"AccountCode" "ACC", "Journal" "JNL"
WHERE
"ACC"."AccountCodeLevel1VarChar" = "JNL"."AccountCodeLevel1VarChar"
AND
"ACC"."AccountCodeLevel2VarChar" = "JNL"."AccountCodeLevel2VarChar"
AND
"ACC"."AccountCodeLevel3VarChar" = "JNL"."AccountCodeLevel3VarChar"
AND
"ACC"."AccountCodeLevel4VarChar" = "JNL"."AccountCodeLevel4VarChar"
AND
"ACC"."AccountCodeLevel5VarChar" = "JNL"."AccountCodeLevel5VarChar"
AND
"ACC"."AccountCodeLevel6VarChar" = "JNL"."AccountCodeLevel6VarChar"
GROUP BY
"ACC"."AccountCodeLevel1VarChar",
"ACC"."AccountCodeLevel2VarChar",
"ACC"."AccountCodeLevel3VarChar",
"ACC"."AccountCodeLevel4VarChar",
"ACC"."AccountCodeLevel5VarChar",
"ACC"."AccountCodeLevel6VarChar",
"ACC"."AccountNameVarChar"
ORDER BY
"ACC"."AccountCodeLevel1VarChar" ASC,
"ACC"."AccountCodeLevel2VarChar" ASC,
"ACC"."AccountCodeLevel3VarChar" ASC,
"ACC"."AccountCodeLevel4VarChar" ASC,
"ACC"."AccountCodeLevel5VarChar" ASC,
"ACC"."AccountCodeLevel6VarChar" ASC
Thank you so much #Mark Rotteveel for the comment that I was summing NULL.
SUM( COALESCE ( "JNL"."DebitAmountDecimal", 0 ) ) - SUM( COALESCE ( "JNL"."CreditAmountDecimal", 0 ) ) "TrialBalanceAmount"
Now the result is complete.
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'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?
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.