Why does union join (uj) sometimes trigger "cast" error? - union

I am getting error 'cast when I try to union join (uj) two very simple, five row tables in KDB+.
What is the cause of the error in the below code?
q)t1b
Symbol | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
q)t2b
Symbol| RIC
------| ------
1301 | 1301.T
1332 | 1332.T
1333 | 1333.T
1334 | 1334.T
1352 | 1352.T
q)meta t1b
c | t f a
------| -----
Symbol| s
RIC | s
q)meta t2b
c | t f a
------| -----
Symbol| s
RIC | s
q)keys t1b
,`Symbol
q)keys t2b
,`Symbol
q)t1b uj t2b
k){+.[x;(!+y;i);:;.+y z i:&z<#y]}
'cast
q))
If I strip the primary key, join, then re-apply primary key, it works. But why? Surely, I am missing something fundamental about KDB+ here.
q)tuj: `Symbol xkey (() xkey t1b) uj (() xkey t2b)
q)tuj
Symbol | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
1301 | 1301.T
1332 | 1332.T
1333 | 1333.T
1334 | 1334.T
1352 | 1352.T
q)meta tuj
c | t f a
------| -----
Symbol| s
RIC | s
q)keys tuj
,`Symbol

Most likely because some of your symbols are enumerated and some aren't

Can you please review your sample code above:
You have declared two tables t1b and t2b, but you then get the cast error when trying to join t1 and t2? What are the values of these tables?
t1b and t2b will join:
q)t1b:([Symbol:`000001.SZ`000001.ZK`000002.SZ`000002.ZK`000004.SZ]RIC:`000001.SZ`000001.ZK`000002.SZ`000002.ZK`000004.SZ)
q)t2b:([Symbol:`1301`1332`1333`1334`1352]RIC:`1301.T`1332.T`1333.T`1334.T`1352.T)
q)meta t1b
c | t f a
------| -----
Symbol| s
RIC | s
q)meta t2b
c | t f a
------| -----
Symbol| s
RIC | s
q)keys t1b
,`Symbol
q)keys t2b
,`Symbol
q)t1b uj t2b
Symbol | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
1301 | 1301.T
1332 | 1332.T
1333 | 1333.T
1334 | 1334.T
1352 | 1352.T
q)t1b
Symbol | RIC
---------| ---------
000001.SZ| 000001.SZ
000001.ZK| 000001.ZK
000002.SZ| 000002.SZ
000002.ZK| 000002.ZK
000004.SZ| 000004.SZ
q)t2b
Symbol| RIC
------| ------
1301 | 1301.T
1332 | 1332.T
1333 | 1333.T
1334 | 1334.T
1352 | 1352.T

Looks like you are trying to union join tables t1 and t2 rather than t1b and t2b. Joining the example tables as shown should work as expected.

Related

Avoid multiple left joins in MSSQL

I have the following database structure:
Users
----------------------
| User_ID | Username |
|--------------------|
| 14590 | Sam |
| 14591 | Michael |
| 14592 | Albert |
----------------------
Addresses
----------------------------------------------
| Adr_ID | City | Street |
|--------------------------------------------|
| 62 | New York | Perfect Street 1 |
| 63 | New York | Another Street 12 |
| 64 | Prague | Zlata Ulicka 52 |
| 65 | Berlin | Alexanderplatz 36 |
| 66 | Berlin | Am Bahnhof 49 |
| 67 | Warsaw | Poniatowskiego 74 |
| 68 | Paris | Rue Des Barres 33 |
| 69 | Paris | Rue De L’abreuvoir 63 |
| 70 | Lisbon | Rua Augusta |
----------------------------------------------
Addresses_Link
------------------------------------------------------------
| Link_ID | Adr_ID | User_ID | Main_Address | Address_Type |
|----------------------------------------------------------|
| 570 | 62 | 14590 | 1 | 1 |
| 571 | 63 | 14590 | 1 | 2 |
| 572 | 64 | 14590 | 0 | 3 |
| 573 | 65 | 14591 | 1 | 1 |
| 574 | 66 | 14591 | 1 | 2 |
| 575 | 67 | 14591 | 0 | 3 |
| 576 | 68 | 14592 | 1 | 1 |
| 577 | 69 | 14592 | 1 | 2 |
| 578 | 70 | 14592 | 0 | 3 |
------------------------------------------------------------
The result I want to get:
-----------------------------------------------------------------------------------------------------
| User_ID | Username | Adr_Private_City | Adr_Private_Street | Adr_Job_City | Adr_Job_Street |
|---------------------------------------------------------------------------------------------------|
| 14590 | Sam | New York | Perfect Street 1 | New York | Another Street 12 |
| 14591 | Michael | Berlin | Alexanderplatz 36 | Berlin | Am Bahnhof 49 |
| 14592 | Albert | Paris | Rue Des Barres 33 | Paris | Rue De L’abreuvoir 63 |
-----------------------------------------------------------------------------------------------------
Columns:
Adr_Private_City / Adr_Private_Street - when Main_Address = 1 and Address_Type = 1
Adr_Job_City / Adr_Job_Street - when Main_Address = 1 and Address_Type = 2
I created an SQL query like this:
SELECT
u.User_ID,
u.Username,
a1.City AS Adr_Private_City,
a1.Street AS Adr_Private_Street,
a2.City AS Adr_Job_City,
a2.Street AS Adr_Job_Street
FROM Users u
LEFT JOIN Addresses_Link al1 ON al1.User_ID = u.User_ID
LEFT JOIN Addresses_Link al2 ON al2.User_ID = u.User_ID
LEFT JOIN Addresses a1 ON a1.Adr_ID = al1.Adr_ID
LEFT JOIN Addresses a2 ON a2.Adr_ID = al2.Adr_ID
WHERE
al1.Main_Address = 1 AND al1.Address_Type = 1 AND
al2.Main_Address = 1 AND al2.Address_Type = 2
Is it possible to avoid multiple left joins and make the query not too slow?
You can achieve what you want with below
Your original query
FROM Users u
LEFT JOIN Addresses_Link al1 ON al1.User_ID = u.User_ID
....
WHERE al1.Main_Address = 1
effectively is an INNER JOIN, when you have the condition al1.Main_Address = 1 in the WHERE clause
Since you used LEFT JOIN, I have turn it into a true LEFT JOIN query. For Addresses_Link and Addresses, since you join it on Adr_ID, I use INNER JOIN
SELECT *
FROM Users u
LEFT JOIN
(
SELECT al.User_ID,
MAX(CASE WHEN al.Address_Type = 1 THEN a.City END) AS Adr_Private_City,
MAX(CASE WHEN al.Address_Type = 1 THEN a.Street END) AS Adr_Private_Street,
MAX(CASE WHEN al.Address_Type = 2 THEN a.City END) AS Adr_Job_City,
MAX(CASE WHEN al.Address_Type = 2 THEN a.Street END) AS Adr_Job_Street,
FROM Addresses_Link al
INNER JOIN Addresses a ON a.Adr_ID = al.Adr_ID
WHERE al.Main_Address = 1
AND al.Address_Type IN (1, 2)
GROUP BY al.User_ID
) a ON a.User_ID = u.User_ID

How To Merge Two Table into one output data

I have two tables with similar information:
1st Table For Estimation Data :
EstChargeCode | EstAmount
------------- | -------------
CNFS0001 | 43,250,000.00
CNIH0001 | 0.00
CNIH0001 | 2,625,000.00
CNIP0001 | 4,500,000.00
CNIP0005 | 2,250,000.00
CNOH0001 | 20,484,690.00
CNOP0001 | 0.00
2nd Table for Actual Data :
ActChargeCode | ActAmount
------------- | -------------
CNFS0001 | 39,950,000.00
CNIH0001 | 1,300,000.00
CNIH0001 | 950,000.00
CNIH0001 | -950,000.00
CNIH0001 | 950,000.00
CNIP0001 | 4,500,000.00
CNIP0005 | 2,250,005.00
CNOH0001 | 20,484,690.00
CNOP0001 | 3,300,000.00
if using Union All to merge the table from above then the result like this
ChargeCode | EstAmount | ActAmount
---------- | ------------- | -------------
CNFS0001 | 43,250,000.00 | -------------
CNIH0001 | 0.00 | -------------
CNIH0001 | 2,625,000.00 | -------------
CNIP0001 | 4,500,000.00 | -------------
CNIP0005 | 2,250,000.00 | -------------
CNOH0001 | 20,484,690.00 | -------------
CNOP0001 | 0.00 | -------------
CNFS0001 | ------------- | 39,950,000.00
CNIH0001 | ------------- | 1,300,000.00
CNIH0001 | ------------- | 950,000.00
CNIH0001 | ------------- | -950,000.00
CNIH0001 | ------------- | 950,000.00
CNIP0001 | ------------- | 4,500,000.00
CNIP0005 | ------------- | 2,250,005.00
CNOH0001 | ------------- | 20,484,690.00
CNOP0001 | ------------- | 3,300,000.00
I need to group both data into single result data like this
ChargeCode | EstAmount | ActAmount
---------- | ------------- | -------------
CNFS0001 | 43,250,000.00 | 39,950,000.00
CNIH0001 | 0.00 | 1,300,000.00
CNIH0001 | 0.00 | 950,000.00
CNIH0001 | 0.00 | -950,000.00
CNIH0001 | 0.00 | 950,000.00
CNIH0001 | 2,625,000.00 | 0.00
CNIP0001 | 4,500,000.00 | 4,500,000.00
CNIP0005 | 2,250,000.00 | 2,250,005.00
CNOH0001 | 20,484,690.00 | 20,484,690.00
CNOP0001 | 0.00 | 3,300,000.00
I don't know how handle this out. Any help would be greatly appreciated!
you can use code like below
select isnull(EstChargeCode ,ActChargeCode ) as ChargeCode ,isnull(EstAmount,0) as
EstAmount , isnull(ActAmount,0) as ActAmount from Estimation full join Actual
on Estimation.EstChargeCode =Actual.ActChargeCode
select a.EstChargeCode as ChargeCode ,
a.EstAmount,
b.ActAmount
from Estimation_Data as a
LEFT JOIN Actual_Data as b
ON a.ActChargeCode =b.ActChargeCode
You use INNER JOIN like:
SELECT T1.EstChargeCode AS EstChargeCode,
T1.EstAmount AS EstAmount1,
T2.EstAmount AS EstAmount2
FROM Table1 T1 INNER JOIN Table2 T2 ON T1.EstChargeCode = T2.ActChargeCode;
You can use JOIN to achieve the result:
SELECT t2.ActAmount ChargeCode ,nvl(t1.EstAmount,0.0) EstAmount, nvl(t2.ActAmount,0.0) ActAmount
FROM <Estimation Data table> t1
RIGHT OUTER JOIN <Actual Data> t2 ON t1.EstChargeCode = t2.ActChargeCode ;
For more clarification, there is an explanation here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?

Join two tables with different field value

Would it be possible to join two tables with different field values? I am using MS SQL Server 2008.
Table A (original image, thanks to #EdwardRusu for translating images to text):
+ ----- + --------- + -------- + ----------- + ------------------ + ---------------- + ----------- +
| RecID | Member ID | LoanType | LoanSubType | Application Number | Application Date | Loan Amount |
+ ----- + --------- + -------- + ----------- + ------------------ + ---------------- + ----------- +
| 3 | 00005 | Regular | | 201604002 | 2016-02-28 | 39864.00 |
| 185 | 00005 | Special | Special ... | 201604183 | 2016-10-31 | 10000.00 |
| 318 | 00005 | Regular | | 201605063 | 2016-05-18 | 39864.00 |
| 427 | 00005 | Regular | | 201608021 | 2016-08-18 | 39872.00 |
| 486 | 00005 | Special | Special ... | 201609044 | 2016-09-07 | 10000.00 |
| 589 | 00005 | Regular | | 201611008 | 2016-11-04 | 39872.00 |
| 689 | 00005 | Regular | | 201702004 | 2017-02-02 | 39872.00 |
+ ----- + --------- + -------- + ----------- + ------------------ + ---------------- + ----------- +
Table B (original image):
+ --------------- + --------- + ------ + ----- + ---------- + -------- + -------- + -------- +
| ProjectAcctCode | Member ID | TMonth | TYear | TLastDate | TDebit | TCredit | TBalance |
+ --------------- + --------- + ------ + ----- + ---------- + -------- + -------- + -------- +
| 105350500 | 00005 | 1 | 2017 | 2017-01-31 | 0.00 | 2952.00 | -2952.00 |
| 105350500 | 00005 | 5 | 2016 | 2016-05-31 | 73084.00 | 33220.00 | 39864.00 |
| 105350500 | 00005 | 6 | 2016 | 2016-06-30 | 0.00 | 2951.42 | -2952.42 |
| 105350500 | 00005 | 7 | 2016 | 2016-07-31 | 0.00 | 3014.14 | -3014.14 |
| 105350500 | 00005 | 8 | 2016 | 2016-08-31 | 39872.00 | 33905.26 | 5973.55 |
| 105350500 | 00005 | 9 | 2016 | 2016-09-30 | 0.00 | 2952.00 | -2952.00 |
| 105350500 | 00005 | 10 | 2016 | 2016-10-31 | 0.00 | 3014.73 | -3014.73 |
| 105350500 | 00005 | 11 | 2016 | 2016-11-30 | 39872.00 | 33905.26 | 5966.74 |
| 105351000 | 00005 | 1 | 2017 | 2017-01-31 | 0.00 | 975.03 | -975.03 |
| 105351000 | 00005 | 5 | 2016 | 2016-05-31 | 5000.00 | 1000.00 | 4000.00 |
| 105351000 | 00005 | 6 | 2016 | 2016-06-30 | 0.00 | 1000.00 | -1000.00 |
| 105351000 | 00005 | 7 | 2016 | 2016-07-31 | 0.00 | 1000.00 | -1000.00 |
| 105351000 | 00005 | 8 | 2016 | 2016-08-31 | 0.00 | 1000.00 | -1000.00 |
| 105351000 | 00005 | 9 | 2016 | 2016-09-30 | 10000.00 | 1000.00 | 9000.00 |
| 105351000 | 00005 | 10 | 2016 | 2016-10-31 | 0.00 | 955.82 | -955.82 |
| 105351000 | 00005 | 11 | 2016 | 2016-11-30 | 0.00 | 965.38 | -965.38 |
+ --------------- + --------- + ------ + ----- + ---------- + -------- + -------- + -------- +
I want to get the total TBalance from TableB grouped by MemberID, LoanType, ApplicationNo, ApplicationDate. These two tables should be joined based on LoanType and ProjAcctCode AND ApplicationDate and TLastDate. LoanType and ProjAcctCode have different values. But, "REGULAR" Loantype is equal to "105350500" ProjAcctCode and "SPECIAL LoanType" is equivalent to "105351000" ProjAcctCode.
ApplicationDate should be less than or equal to TLastDate.
So if I will generate records for "REGULAR" loan types , I should have something like this (original image):
+ -------- + -------- + ------------- + --------------- + ---------- + ------------ + --------- + --------- +
| MemberID | LoanType | ApplicationNO | ApplicationDate | LoanAmount | ProjAcctCode | TLastDate | Balance |
+ -------- + -------- + ------------- + --------------- + ---------- + ------------ + --------- + --------- +
| 000005 | Regular | 201608021 | 8/18/2016 | 39,872.00 | 105350500 | 8/31/2016 | 39,871.44 |
+ -------- + -------- + ------------- + --------------- + ---------- + ------------ + --------- + --------- +
But with my query,
SELECT a.MemberID,
(SELECT TOP (1) ApplicationNo
FROM TABLE A
WHERE (MemberID = a.MemberID) AND (ApplicationDate <= b.TLastDate)
ORDER BY ApplicationNo DESC) AS ApplicationNo,
(SELECT TOP (1) LoanAmount
FROM TABLE A AS SAL_APPLICATION_HEADER_1
WHERE (MemberID = a.MemberID) AND (ApplicationDate <= b.TLastDate)
ORDER BY ApplicationNo DESC) AS LoanAmount,
(SELECT TOP (1) ApplicationDate
FROM TABLE A AS SAL_APPLICATION_HEADER_2
WHERE (MemberID = a.MemberID)
AND (ApplicationDate <= b.TLastDate)
ORDER BY ApplicationNo DESC) AS ApplicationDate,
vwSAL_Balance_SL_1.ProjAcctCode,
b.TDebit,
b.TCredit,
b.TBalance AS Balance,
b.TLastDate
FROM TABLE A AS a
INNER JOIN TABLE B AS b ON a.MemberID = b.SLCode
GROUP BY a.MemberID,
b.TDebit,
b.TCredit,
b.TBalance,
b.ProjAcctCode,
b.TLastDate
HAVING (a.MemberID = N'00005') AND (b.TLastDate = '8/31/2016')
I got this result (original image):
+ -------- + ------------- + ---------- + --------------- + ------------ + -------- + -------- + -------- + ---------- +
| MemberID | ApplicationNo | LoanAmount | ApplicationDate | ProjAcctCode | TDebit | TCredit | Balance | TLastDate |
+ -------- + ------------- + ---------- + --------------- + ------------ + -------- + -------- + -------- + ---------- +
| 00005 | 201608021 | 39872.00 | 2016-08-18 | 105351000 | 0.00 | 1000.00 | -1000.00 | 2016-08-31 |
| 00005 | 201608021 | 39872.00 | 2016-08-18 | 105350500 | 39872.00 | 33898.45 | 5973.55 | 2016-08-31 |
+ -------- + ------------- + ---------- + --------------- + ------------ + -------- + -------- + -------- + ---------- +
This might be too long but please help. Thank you.
tl;dr Yes, it is possible. The query block at the end will give you what you want.
Yes, it is possible to join two tables with different field values as long as you know some sort of correlation between the two. I will walk you through the construction of the join statement that gives you what you want. You need three things:
1) Match MemberID and SLCode
2) Match LoanType and ProjAcctCode
3) Match the dates
MemberID and SLCode
Based on your comment above, I'm going to assume that SLCode is the same thing as MemberID in B.
select *
from A
inner join B
on B.MemberID = A.MemberID -- or B.SLCode = A.MemberID
LoanType and ProjAcctCode
In this example, you want to use a case statement in the join, something like
inner join B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end
This says that if the LoanType is Regular, then only join records that have ProjAcctCode = 105350500; otherwise join records that have ProjAcctCode 105351000. (Here, LoanType only has two states, so using the else clause to capture Special loans is perfectly fine. If there are more than two states, then you must use additional conditions).
select *
from A
inner join B
on B.MemberID = A.MemberID
and B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end
Dates
Getting the date filter right is immediate.
select *
from A
inner join B
on B.MemberID = A.MemberID
and B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end
and A.ApplicationDate <= B.TLastDate
Group and Aggregate
Now that you have the tables joined appropriately, you just need to group and aggregate. You can group on anything from A since those fields don't change for the aggregates you want, and you just have to be a little clever in how you select things that came from B. In your question, it looks like you want ProjAcctCode (this doesn't change, so we can group on this too), TLastDate (this does change, so we need to aggregate on some criteria), and TBalance (from which we want the sum). I'm going to assume that you want the latest TLastDate, so your query will be something like
select A.MemberID,
A.LoanType,
A.ApplicationNumber,
A.ApplicationDate,
[anything else you want from A],
B.ProjAcctCode -- aggregate not needed because this doesn't change
max(B.TLastDate) as TLastDate,
sum(B.TBalance) as TBalance
from A
inner join B
on B.MemberID = A.MemberID
and B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end
and A.ApplicationDate <= B.TLastDate
group by A.MemberID,
A.LoanType,
A.ApplicationNumber,
A.ApplicationDate,
[anything else you selected from A],
B.ProjAcctCode
This produces the following table
+ -------- + -------- + ----------------- + --------------- + ------------ + ---------- + -------- +
| MemberID | LoanType | ApplicationNumber | ApplicationDate | ProjAcctCode | TLastDate | TBalance |
+ -------- + -------- + ----------------- + --------------- + ------------ + ---------- + -------- +
| 00005 | Regular | 201604002 | 2016-02-08 | 105350500 | 2017-01-31 | 36913.19 |
| 00005 | Special | 201604183 | 2016-10-31 | 105351000 | 2017-01-31 | -2896.23 |
| 00005 | Regular | 201605063 | 2016-05-18 | 105350500 | 2017-01-31 | 36913.19 |
| 00005 | Regular | 201608021 | 2016-08-18 | 105350500 | 2017-01-31 | 3014.75 |
| 00005 | Special | 201609044 | 2016-09-07 | 105351000 | 2017-01-31 | 6103.77 |
| 00005 | Regular | 201611008 | 2016-11-04 | 105350500 | 2017-01-31 | 3014.74 |
+ -------- + -------- + ----------------- + --------------- + ------------ + ---------- + -------- +
Note: The loan with ApplicationNumber 201702004 doesn't appear in the final result because it gets filtered out by date (i.e. its application date is greater than all TLastDate in B).
I just use UNION (per LoanType) in my query to get the desire result.

sql join on Nearest greater than Value

If i have the following database structure:
tbl1
|id | EYearMonth |
| 1 | 1617 |
| 2 | 1618 |
| 3 | 1619 |
| 4 | 1620 |
| 5 | 1621 |
| 6 | 1622 |
| 7 | 1623 |
| 8 | 1624 |
| 9 | 1625 |
| 10 | 1626 |
| 11 | 1627 |
| 12 | 1628 |
Tbl2
|id | Value | Serial#
| 1 | 1617 | 1068
| 2 | 1618 | 1104
| 3 | 1624 | 1215
What I really want, is the following:
Result
|id | EYearMonth | Serial#
| 1 | 1617 | 1068
| 2 | 1618 | 1104
| 3 | 1619 | 1104
| 4 | 1620 | 1104
| 5 | 1621 | 1104
| 6 | 1622 | 1104
| 7 | 1623 | 1104
| 8 | 1624 | 1215
| 9 | 1625 | 1215
| 10 | 1626 | 1215
| 11 | 1627 | 1215
| 12 | 1628 | 1215
How can I make this Result? Please Help Me
You can use CROSS APPLY and TOP for this:
SELECT *
FROM tbl1 t1
CROSS APPLY(
SELECT TOP 1 t2.[Serial#]
FROM tbl2 t2
WHERE t2.Value <= t1.EYearMonth
ORDER BY t2.Value DESC
)t2
ONLINE DEMO
The following query will work:
SELECT
T1.id,
T1.EYearMonth,
T2.Serial#
FROM tbl1 T1
INNER JOIN Tbl2 T2
ON tbl1.EYearMonth = Tbl2.Value

How to merge row which are not present in other table in sql?

I have table like
CL_Client
cl_id | cl_name |cl_system
1 | a |Dpo
2 | b | Dpo
3 | c |Dpo
4 | d
CLOI_ClientOrderItems
Cl_id|cl_name|orderid| date |status |masterid
1 | a | 123 | 27/5/0215 | 12 | 111
1 | a | 123 | 27/5/0215 | 15 | 111
2 | b | 213 | 27/5/0215 | 12 | 222
3 | c | 452 | 27/5/0215 | 16 | 333
4 | d | 458 | 27/5/0215 | 20 | 444
4 | d | 452 | 27/5/0215 | 22 | 333
Invoice table
orderid|rate|master id|invoice_date
123 |10 | 111 |27/5/2015
213 |10 | 222 |27/5/2015
458 |10 | 444 |27/5/2015
in invoice table there is no row of masterorderid 333 but in result I want to show that also.
I have tried this query but it's not working correctly:
SELECT distinct
C.cl_id,
C.cl_name,
[dbo].getOrderCountbyMasterorderID(CO.masterorderid) as No_Of_Orders,
CONVERT(VARCHAR(5),CO.cloi_order_date,108) as OrderTime,
I.in_total,
CO.MasterOrderId,
CO.cloi_current_status
from
dbo.CL_Clients C
INNER JOIN dbo.CLOI_ClientOrderItems CO
ON C.cl_id = CO.cl_id
LEFT OUTER JOIN dbo.IN_Invoices I
ON CO.MasterOrderId = I.MasterOrderId
where
CO.cloi_current_status in(7,8,160,163,167,170,250,251,162) and
C.cl_system='Dpo' and
datepart(yyyy,I.in_date_issued)=2015 and
datepart(mm,I.in_date_issued)=05 and
datepart(dd,I.in_date_issued)=27
group by
C.cl_id,
C.cl_name,
CO.masterorderid,
CO.cloi_order_date,
CO.cloi_current_status,
I.in_total,
CO.MasterOrderId
order by
OrderTime
expected result
cl_id | cl_name |No_Of_Orders| OrderTime|in_total|MasterOrderId|status
1 | a |2 | 09:45 | 65.33 |111 |12
2 | b |1 | 09:53 | 65.33 |222 |15
3 | c |1 | 09:54 | 43.21 |333 |16
4 | d |2 | 09:56 | 43.21 |444 |20
You have not shown data for CL_Clients table.
Try executing this query first
SELECT distinct
C.cl_id,
C.cl_name,
[dbo].getOrderCountbyMasterorderID(CO.masterorderid) as No_Of_Orders,
CONVERT(VARCHAR(5),CO.cloi_order_date,108) as OrderTime,
--I.in_total,
CO.MasterOrderId,
CO.cloi_current_status
from
dbo.CL_Clients C
INNER JOIN dbo.CLOI_ClientOrderItems CO
ON C.cl_id = CO.cl_id
--LEFT OUTER JOIN dbo.IN_Invoices I
--ON CO.MasterOrderId = I.MasterOrderId
where
CO.cloi_current_status in(7,8,160,163,167,170,250,251,162)
and C.cl_system='Dpo'
--and datepart(yyyy,I.in_date_issued)=2015
--and datepart(mm,I.in_date_issued)=05
--and datepart(dd,I.in_date_issued)=27
group by
C.cl_id,
C.cl_name,
CO.masterorderid,
CO.cloi_order_date,
CO.cloi_current_status,
--I.in_total,
CO.MasterOrderId
order by
OrderTime
If masterorderid = 333 is not even displayed here, then check your where clauses.

Resources