| Author |
Topic |
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-17 : 08:26:44
|
Hello friends,I have the following diagram as attached belowI need to create the following result based on the client IDClientID-Date-BillNum-AmountLBP(billdetail)-AmountUSD(billdetail)-AmountLBP(ClientPayments)-AmountUSD(ClientPayments) Thanks in advance for your help |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-11-17 : 09:35:46
|
| What have you tried so far? This is in no way complex. It is the most basic of the basics...http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
daniel.nospam
Starting Member
11 Posts |
Posted - 2011-11-17 : 09:40:54
|
| How does ClientPauments relates to Bills? Is it via ClientID and BillNumber? |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-17 : 11:27:14
|
| It's just via the ClientID |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-17 : 11:29:38
|
| have a look at concept of joins and you can very easily get this on your own------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-17 : 11:44:12
|
| here is what i wrote but the columns results in some cases should be zero because the rows are not the same for the same client (clientpayments and billdetails)Select BI.BillNumber, BI.BillFile,BI.ClientID, CL.ClientName,BD.AmountLL, BD.AmountUSD, CP.ValueUSD,CP.ValueLBPfrom Bills AS BIJOIN Clients CL on BI.ClientID=CL.ClientIDJOIN BillDetails BD on BI.BillID=BD.BillIDJOIN ClientPayments CP on BI.ClientID=CP.ClientIDwhere BI.Hidden=0 and BI.ClientID=20 and CP.ClientID=20Result12 1 20 ???????? ??????? 500000 100 5000.00 20000.0012 1 20 ???????? ??????? 500000 100 200.00 10000.0012 1 20 ???????? ??????? 200000 100 5000.00 20000.0012 1 20 ???????? ??????? 200000 100 200.00 10000.00 |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-11-17 : 11:50:34
|
| Without sample data, we have no way of knowing why you get the results that you do. Follow the HOW TO ASK link, and it will provide you with instructions on how to provide DDL and DML.http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-17 : 12:17:53
|
DB Diagram: Bills Data Sample:11-7/13/2011-12-1-20-dfgsd-4-1-1-5-444.00000-444.00000-45434534-7/13/2011-543453453-0-0-NULL-NULL-FalseBillDetails Data Sample: 22-11-1-500000-100-NULL-NULL-NULL-323-11-1-200000-100-NULL-NULL-NULL-1ClientsPayments Data Sample:1-20-12-NULL-21-Aug-11-test-5000.0000-20000.00002-20-12-NULL-21-Aug-11-sdfsdfsdf-200.0000-10000.0000Expected Result:BillNumber-BillFile-ClientID-ClientName-AmountLL-AmountUSD-ValueUSD-ValueLBP12-1-20-ClientName-500000.00-100.00-NULL-NULL12-1-20-ClientName-200000.00-100.00-NULL-NULL12-1-20-ClientName-NULL-NULL-5000.00-20000.0012-1-20-ClientName-NULL-NULL-200.00-10000.00ClientID=20 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-17 : 12:45:04
|
| [code]SELECT *FROM(SELECT b.BillNumber,b.BillFile,b.ClientID,c.ClientName,bd.AmountLL,bd.AmuntUSD,CAST(NULL AS Numeric(15,2)) AS ValueUSD,CAST(NULL AS Numeric(15,2)) AS ValueLBP,1 AS OrdFROM Bills bINNER JOIN BillDetails bdOn bd.BillID = b.BillIDINNER JOIN Clients cOn c.ClientID = b.ClientIDUNION ALLSELECT b.BillNumber,b.BillFile,b.ClientID,c.ClientName,NULL,NULL,cp.ValueUSD,cp.ValueLBP,2FROM Bills bINNER JOIN ClientsPayments cpON cp.ClientID = b.ClientIDINNER JOIN Clients cOn c.ClientID = b.ClientID)tORDER BY BillNumber,BillFile,ClientID,ClientName,Ord[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-11-17 : 12:47:06
|
try thisSELECT cp.billnumber, b.billfile, cp.clientid , c.clientname, bd.amountLL, bd.amountUSD , cp.valueUSD, cp.valueLBPFROM clientpayments cpINNER JOIN clients c ON cp.Clientid = c.ClientidINNER JOIN bills b ON cp.Billnumber = b.billnumberAND cp.clientID = b.clientIDLEFT JOIN billdetails bdON b.billID = bd.billID-- to get your clientWHERE c.clientID = 20AND b.hidden = 0 EDIT: of course visahk posts correctly before i do |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-18 : 11:48:01
|
| Hi DonAtWork, the query didn't return anythingBills Table:29 11/18/2011 1 1 45 5 12 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL False30 11/18/2011 1 2 45 8 20 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL False31 11/18/2011 1 3 35 5 34 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL False32 11/18/2011 1 4 45 5 10 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL False33 11/18/2011 1 5 45 5 12 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL False34 11/18/2011 2 6 45 5 7 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL False35 11/18/2011 3 7 45 5 35 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL False36 11/18/2011 4 8 45 5 16 NULL NULL NULL NULL NULL NULL 0 0 NULL NULL FalseBillDetails:27 36 1 1 1 NULL NULL NULL 1ClientPayments:1 20 12 NULL 21-Aug-11 5000.0000 20000.00002 20 12 NULL 21-Aug-11 200.0000 10000.00003 20 45 NULL 11/17/2011 0.0000 500000.00004 27 NULL 11/17/2011 0.0000 523000.00005 25 NULL 11/17/2011 0.0000 20.00006 53 NULL 11/18/2011 1.0000 20.00007 45 NULL 11/18/2011 5.0000 5.0000expected results:ClientID ClientName AmountLL AmountUSD ValueLBP ValueUSD45 Name 1 1 NULL NULL45 name NULL NULL 5000 5000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-18 : 11:52:04
|
| you mean you dont have records in BillDetails for all the bills?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-18 : 11:56:31
|
| Yes i have just 1 record and it's related to clientID(as billnumber can be optional) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-18 : 12:06:06
|
quote: Originally posted by mike1973 Yes i have just 1 record and it's related to clientID(as billnumber can be optional)
ok ... can you explain how you will get above output with given data?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-18 : 12:11:16
|
| I have a selected ClientID which exists in both tables (ClientPayments and Bills >> BillDetails)the requirement is to select a client and to display (what we paid for him=bill>>>billdetails) and (what he paid for us=clientpayments), let's ignore the billnumber. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-18 : 12:53:05
|
do you mean this then?;WITH CTE ()AS(SELECT b.BillNumber,b.BillFile,b.ClientID,c.ClientName,bd.AmountLL,bd.AmuntUSD,CAST(NULL AS Numeric(15,2)) AS ValueUSD,CAST(NULL AS Numeric(15,2)) AS ValueLBP,1 AS OrdFROM Bills bINNER JOIN BillDetails bdOn bd.BillID = b.BillIDINNER JOIN Clients cOn c.ClientID = b.ClientIDUNION ALLSELECT b.BillNumber,b.BillFile,b.ClientID,c.ClientName,NULL,NULL,cp.ValueUSD,cp.ValueLBP,2FROM Bills bINNER JOIN ClientsPayments cpON cp.ClientID = b.ClientIDINNER JOIN Clients cOn c.ClientID = b.ClientID)SELECT c.*FROM CTE cINNER JOIN (SELECT ClientID,COUNT(DISTINCT Ord) AS DistOrd FROM CTE GROUP BY ClientID) c1ON c1.ClientID = c.ClientIDAND c1.DistOrd =2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-18 : 13:21:50
|
| Here is the result i received, i even tried to remove the billnumber and billfile and it's not giving me the result45 ClientName 1 1 NULL NULL 145 ClientName NULL NULL 5.0000 5.0000 245 ClientName NULL NULL 5.0000 5.0000 245 ClientName NULL NULL 5.0000 5.0000 245 ClientName NULL NULL 5.0000 5.0000 245 ClientName NULL NULL 5.0000 5.0000 245 ClientName NULL NULL 5.0000 5.0000 245 ClientName NULL NULL 5.0000 5.0000 2it should be like this45 ClientName 1 1 NULL NULL 45 ClientName NULL NULL 5.0000 5.0000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-19 : 01:24:25
|
| [code];WITH CTE ()AS(SELECT b.ClientID,c.ClientName,bd.AmountLL,bd.AmuntUSD,CAST(NULL AS Numeric(15,2)) AS ValueUSD,CAST(NULL AS Numeric(15,2)) AS ValueLBP,1 AS OrdFROM Bills bINNER JOIN BillDetails bdOn bd.BillID = b.BillIDINNER JOIN Clients cOn c.ClientID = b.ClientIDUNION SELECT b.ClientID,c.ClientName,NULL,NULL,cp.ValueUSD,cp.ValueLBP,2FROM Bills bINNER JOIN ClientsPayments cpON cp.ClientID = b.ClientIDINNER JOIN Clients cOn c.ClientID = b.ClientID)SELECT c.*FROM CTE cINNER JOIN (SELECT ClientID,COUNT(DISTINCT Ord) AS DistOrd FROM CTE GROUP BY ClientID) c1ON c1.ClientID = c.ClientIDAND c1.DistOrd =2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-20 : 03:39:31
|
| Thanks a lot Visakh, it's working for me i appreciate your help now the query is like thisSELECT b.ClientID,c.ClientName,b.billnumber,ISNULL(bd.AmountLL, 0) As AmountLL ,ISNULL(bd.AmountUSD, 0) As AmountUSD,CAST(NULL AS Numeric(15,2)) AS ValueUSD,CAST(NULL AS Numeric(15,2)) AS ValueLBP,CONVERT(VARCHAR(10),bd.ModifiedDate,110) AS ModifiedDate,bd.Notes,1 AS OrdFROM Bills bINNER JOIN BillDetails bd On bd.BillID = b.BillIDINNER JOIN Clients c On c.ClientID = b.ClientIDWhere b.ClientID=20 and b.Hidden=0UNION SELECT cp.ClientID,c.ClientName,cp.BillNumber,NULL,NULL,cp.ValueUSD,cp.ValueLBP,cp.PaymentDate,cp.PaymentDescription,2FROM ClientPayments cpINNER JOIN Clients c On c.ClientID = cp.ClientIDWhere cp.ClientID=20if i want to add 2 columns to the result BalanceLBP = (AmountLL - ValueLBP) and another one BalanceUSD = (AmountUSD - ValueUSD)how i do so please?thanks for your help again |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-20 : 03:43:36
|
do you mean this?;WITH CTE ()AS(SELECT b.ClientID,c.ClientName,bd.AmountLL,bd.AmuntUSD,CAST(NULL AS Numeric(15,2)) AS ValueUSD,CAST(NULL AS Numeric(15,2)) AS ValueLBP,1 AS OrdFROM Bills bINNER JOIN BillDetails bdOn bd.BillID = b.BillIDINNER JOIN Clients cOn c.ClientID = b.ClientIDWhere b.ClientID=20 and b.Hidden=0UNION SELECT b.ClientID,c.ClientName,NULL,NULL,cp.ValueUSD,cp.ValueLBP,2FROM Bills bINNER JOIN ClientsPayments cpON cp.ClientID = b.ClientIDINNER JOIN Clients cOn c.ClientID = b.ClientIDWhere cp.ClientID=20)SELECT c.*,BalanceLBP,BalanceUSDFROM CTE cINNER JOIN (SELECT ClientID,COUNT(DISTINCT Ord) AS DistOrd,SUM(AmountLL) - SUM(ValueLBP) AS BalanceLBP, SUM(AmountUSD)-SUM(ValueUSD) AS BalanceUSD FROM CTE GROUP BY ClientID) c1ON c1.ClientID = c.ClientIDAND c1.DistOrd =2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-11-20 : 05:54:12
|
| Dear Visakhlooks good but it is showing me the same numbers when substracting like this17037.4824 345372.643417037.4824 345372.643417037.4824 345372.643417037.4824 345372.643417037.4824 345372.6434because it is SUM(AmountLL) - SUM(ValueLBP) AS BalanceLBP, SUM(AmountUSD)-SUM(ValueUSD) AS BalanceUSDwhile i need it like this but not working (AmountLL - ValueLBP) AS BalanceLBP, (AmountUSD-ValueUSD) AS BalanceUSDThanks |
 |
|
|
Next Page
|