Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

I need help on SQL query

$
0
0

I have requirement to get the rule count from the rule table for interface errors

Interface has the logic to log an error, if more than 1 rule ID found for same combination of product, location and carrier. 

This is my Input:

  1. Error Table:

ID

Source

Product

Location

Carrier

Date

Error Msg

1

VMI

EX_0

EX_A

EX_ABC

Jan-15

More than 1 Active Rule found for this combination

2

VMI

EX_1

EX_B

EX_XYZ

Jan-15

More than 1 Active Role found for this combination

Expecting Output like this, invalid Rule id to the error list.

ID

Source

Product

Location

Carrier

Date

Error Msg

Invalid  Rule

1

VMI

EX_0

EX_A

EX_ABC

Jan-15

More than 1 Active Role found for this combination

1

2

VMI

EX_1

EX_B

EX_XYZ

Jan-15

More than 1 Active Role found for this combination

3

I need to look 5 different tables to get invalid rule ID for the same product, location and carrier combination.

  1. Cross –Reference table (this table has the corresponding internal values for the external value from the error table) source is the common field in error table and the cross reference table.  

ID

Source

External Value

Internal Value

1

VMI

EX_0

IN_Pen

2

VMI

EX_1

IN_Pencil

3

VMI

EX_A

IN_NJ

4

VMI

EX_B

IN_CA

5

VMI

EX_ABC

IN_PILOT

6

VMI

EX_XYZ

IN_LITER

  1. Once I get the internal value from cross reference table, I need to look 3 different following tables to get Id’s to find out the rule.

Product

Location

Carrier

ID

Name

ID

Name

ID

Name

P1

IN_Pen

L1

IN_NJ

C1

IN_PILOT

P2

IN_Pencil

L2

IN_CA

C2

IN_LITER

3. After getting all the required value to find Rule, I am executing below query to find out count.

SELECT count(ID)

FROM dbo.Rule r

WHERE r.Status='Active'

AND r.productID= P1

AND r.LocationID= L1

AND r.CarrierID= C1

Result:

ID

2

Rule Table

ID

Product ID

Location ID

Carrier ID

Status

From Date

To Date

1

P1

L1

C1

Active

Jan 14

Dec 14

2

P1

L1

C1

Active

Jan 15

Dec 15

3

P2

L2

C2

Active

Jan 14

Dec 14

4

P2

L2

C2

Active

Jan 15

Dec 15



Viewing all articles
Browse latest Browse all 7129

Trending Articles