humbly I ask for help with the next problem.
I have this one very simple. Don’t know how to describe it by words so I’ll do with an image.
table services
PERSONID Service_id chargeamount
1 3 20.50
2 4 80.50
3 5 78.80
table charges
service_id payed extracharges
3 true 20.50
4 true 80.50
3 false 78.80
if there is any unpayed extracharge it must show true or false , and if the service id do not match in the second table it must be shown in the query with null values or other calculated value
Something like this
PERSONID Service_id chargeamount payed
1 3 20.50 false
2 4 80.50 true
3 5 78.80 null
but extra rows of the same row are not allowed like the ones created by joins.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SERVICES](
[personid] [int] NULL,
[service_id] [int] NULL,
[chargeamount] [float] NULL
) ON [PRIMARY]
GO
insert into services (personid,service_id,chargeamount) values ( 1,3 ,20);
insert into services (personid,service_id,chargeamount) values ( 2,4 ,20);
insert into services (personid,service_id,chargeamount) values ( 3,5 ,20);
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[charges](
[payed] [bit] NULL,
[service_id] [int] NULL,
[extracharges] [float] NULL
) ON [PRIMARY]
GO
insert into services (service_id,payed,extracharges) values ( 3,true,20.50);
insert into services (service_id,payed,extracharges) values ( 3,false,78.80);
insert into services (service_id,payed,extracharges) values ( 4,false,80.50);
This is a simple left join on
service_id. When there is at least one row withpayed=false,falseis shown. Otherwisetrueornullis shown, respectively.SQLFiddle