Apply join on two result set - Postgres - postgresql

I have query that gives me the documentid
select documentid from tbldocumentbyclient
where tbldocumentbyclient.isactive = true
and applicationid = '000116'
result:
Another query that give me the following result
SELECT documentcategory,documentcategoryid, string_agg(documentid::text, ',') as documentid
FROM tbldocumentmaster
where accounttype = 1 and usertype= 'INDIVIDUAL'
group by documentcategory,documentcategoryid
order by documentcategoryid;
result :
[![enter image description here][2]][2]
Now,Can someone please suggest me how to get CategoryName that is not associated with any documentid
For Above case my result should be following.
DP Proof - 134 not available in first result
Address Proof Permanent - row 4.. not a single id available in documenid result
Here this are the documentcategory that is not associate with any document id

I think a simple join between your two tables should do the trick. The logic here is if a document category from tbldocumentmaster really has no document IDs which match to anything in the tbldocumentbyclient table then the join should filter it off, leaving behind those categories which do match.
SELECT t1.documentcategory
FROM tbldocumentmaster t1
LEFT JOIN tbldocumentbyclient t2
ON t1.documentid = t2.documentid
WHERE t2.isactive = true AND
t2.applicationid = '000116' AND
t1.accounttype = 1 AND
t1.usertype = 'INDIVIDUAL' AND
t2.documentid IS NULL

try this:
SELECT distinct documentcategory,documentcategoryid
FROM tbldocumentmaster m
LEFT OUTER JOIN tbldocumentbyclient d on d.documentid = m.documentid
where accounttype = 1 and usertype= 'INDIVIDUAL'
and m.documentid is null
and applicationid = '000116'
group by documentcategory,documentcategoryid
order by documentcategoryid;

I have not tested but you can try this query:
SELECT documentcategory,documentcategoryid, string_agg(documentid::text, ',') as documentid
FROM tbldocumentmaster
where accounttype = 1 and usertype= 'INDIVIDUAL' and documentid not in (select documentid from tbldocumentbyclient where tbldocumentbyclient.isactive = true and applicationid = '000116')
group by documentcategory,documentcategoryid
order by documentcategoryid;

Related

Postgresql, how to SELECT json object without duplicated rows

I'm trying to find out how to get JSON object results of selected rows, and not to show duplicated rows.
My current query:
SELECT DISTINCT ON (vp.id) jsonb_agg(jsonb_build_object('affiliate',a.*)) as affiliates, jsonb_agg(jsonb_build_object('vendor',vp.*)) as vendors FROM
affiliates a
INNER JOIN related_affiliates ra ON a.id = ra.affiliate_id
INNER JOIN related_vendors rv ON ra.product_id = rv.product_id
INNER JOIN vendor_partners vp ON rv.vendor_partner_id = vp.id
WHERE ra.product_id = 79 AND a.is_active = true
GROUP BY vp.id
The results that I receive from this is:
[
affiliates: {
affiliate: affiliate1
affiliate: affiliate2
},
vendors: {
vendor: vendor1,
vendor: vendor1,
}
As you can see in the second record, vendor is still vendor1 because there are no more results, so I'd like to also know if there's a way to remove duplicates.
Thanks.
First point : the result you display here above doesn't conform the json type : the keys are not double-quoted, the string values are not double-quoted, having dupplicated keys in the same json object ('{"affiliate": "affiliate1", "affiliate": "affiliate2"}' :: json) is not be accepted with the jsonb type (but it is with the json type).
Second point : you can try to add the DISTINCT key word directly in the jsonb_agg function :
jsonb_agg(DISTINCT jsonb_build_object('vendor',vp.*))
and remove the DISTINCT ON (vp.id) clause.
You can also add an ORDER BY clause directly in any aggregate function. For more information, see the manual.
You could aggregate first, then join on the results of the aggregates:
SELECT a.affiliates, v.vendors
FROM (
select af.id, jsonb_agg(jsonb_build_object('affiliate',af.*)) as affiliates
from affiliates af
group by af.id
) a
JOIN related_affiliates ra ON a.id = ra.affiliate_id
JOIN related_vendors rv ON ra.product_id = rv.product_id
JOIN (
select vp.id, jsonb_agg(jsonb_build_object('vendor',vp.*)) as vendors
from vendor_partners vp
group by vp.id
) v ON rv.vendor_partner_id = v.id
WHERE ra.product_id = 79
AND a.is_active = true

Flutter sqflite, retreiving data from a rawQuery

I have the following rawQuery
final maps = await db.rawQuery("""
select r.id, r.name, r.description,r.created_by,
a.id,a.name,a.description from Resources r
left join Resource_Attribute ra on ra.resource_id = r.id
left join AttributeItems a on ra.attribute_id=a.id
""");
My question is how do I retrieve the data from all the columns in my query? Doing the following
maps.forEach((dbItem){
String resourceId = dbItem["r.id"];
doesn't seem to work. dbItems is a map but contains only the four columns in the Resources table. dbItem["r.id"] returns a null.
dbItems does contain a "row" field which is an array with all the field. Is there way to access that?
If you look at dbItem.keys, you will see that it is ['id','name',...] not ['r.id','r.name'].
You can access your resource id using:
var resourceId = dbItem['id'] as String;
You can name a column explicitely using AS. For example:
var resultSet = await db.rawQuery("SELECT r.id AS r_id FROM test r");
// Get first result
var dbItem = resultSet.first;
// Access its id
var resourceId = dbItem['r_id'] as String;
As alextk suggested , I changed my query to
select r.id as resource_id, r.name as resource_name, r.description as resource_descr,r.created_by as resource_created_by,
a.id as attribute_id,a.name as attribute_name,a.description as attribute_descr from Resources r
left join Resource_Attribute ra on ra.resource_id = r.id
left join AttributeItems a on ra.attribute_id=a.id
and that did the trick.

Why is this field not showing up in the results?

When I run the selects below, I do not get Field3 in the result set, why?
Select
a.Field1,
a.Field2,
a.Field3,
sum(IsNull(a.Field4, 0)) AS SomeAlias1,
a.SomeField5,
a.SomeField6,
a.SomeField7
From SomeTable a
INNER JOIN SomeView1 v on v.au = a.au
inner join (select Username, House from Users userBuildings where UserName = #UserName) as userHouses on userHouses.au = a.au
WHERE
(((where claus logic here....
Group BY a.Field1,
a.Field2,
a.SomeAlias1,
a.Field3,
a.Field4,
a.Field5,
a.Field6,
a.Fielf7
)
Select
transBudget.Field1,
transBudget.Field2,
transDiscount.Field4,
... some other fields...
IsNull(transDiscount.Actual, 0) - IsNull(transBudget.Actual, 0) AS Variance
from (Select * from Transactdions Where TransDesc = 'Budget') AS transBudget
FULL OUTER JOIN
(Select * from Transactions Where TransDesc = 'Discount') AS transDiscount
ON transBudget.Market = transDiscount.Market AND transBudget.SubMarket = transDiscount.SubMarket
I see every field except Field3 for some reason and it's beyond me how the heck this can happen.
In the second part of your query, you are missing field 3.
Select
transBudget.Field1,
transBudget.Field2,
transDiscount.Field4,
... some other fields...
IsNull(transDiscount.Actual, 0)
You appear to have two separate SQL queries there. The first one contains Field3, but the second one does not.

How to join the tables in linq to sql?

Table1 :
userid name address
1 venkat srinagr
2 venkatesh sainagar
Table2:
id userid lat lon
1 1 14.000 15.000
2 2 14.3526 15.3698
by passing "venkat" as parameter then need to pull all matching records and his userid,name,lat,lon.
in above table1 "venkat" contains in both rows then need to pull 2 records.how to get userid,name,lat,lon for all matching rows..
for sigle record i am able to get.but there are multiple rows how to get please tell me....
var result = from p in cxt.Table2
where p.Table1.Name.Contains(name)
select new
{
p.Users.User_Id,p.Users.Name,p.Latitude,p.Longitude
};
Im sure someone will say this is not the most effective way but this is how i would do it.
string InputString = "venkat";
var tab =(from a in db.tablea
from b in db.tableb
where a.userid == b.userid && a.name == InputString
select new
{
UserID = a.userid,
Username = a.name,
Latitude = b.lat,
Longditude = b.lon
}).FirstOrDefault();
FirstOrDefault() is only if you want to force only one output or null,
if you want a collection of some sort, then just remove it.

Linq: the linked objects are null, why?

I have several linked tables (entities). I'm trying to get the entities using the following linq:
ObjectQuery<Location> locations = context.Location;
ObjectQuery<ProductPrice> productPrice = context.ProductPrice;
ObjectQuery<Product> products = context.Product;
IQueryable<ProductPrice> res1 = from pp in productPrice
join loc in locations
on pp.Location equals loc
join prod in products
on pp.Product equals prod
where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
select pp;
This query returns 2 records, ProductPrice objects that have linked object Location and Product but they are null and I cannot understand why. If I try to fill them in the linq as below:
res =
from pp in productPrice
join loc in locations
on pp.Location equals loc
join prod in products
on pp.Product equals prod
where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
select new ProductPrice
{
ProductPriceId = pp.ProductPriceId,
Product = prod
};
I have the exception "The entity or complex type 'PBExplorerData.ProductPrice' cannot be constructed in a LINQ to Entities query"
Could someone please explain me what happens and what I need to do?
Thanks
The answer to your first question the Product and Location are null because you need to add an Include("") to your query.
IQueryable<ProductPrice> res1 = from pp in
productPrice.Include("Location").Include("Product")
join loc in locations
on pp.Location equals loc
join prod in products
on pp.Product equals prod
where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
select pp;
The second issue is EF is trying to push down your query and ProductPrice (is not an entity) so it can not. If you want to do this convert it to an anonymous type so just do
select new
{
ProductPriceId = pp.ProductPriceId,
Product = prod
};
And then do
res.ToList().ConvertAll(x=new ProductPrice () {
ProductPriceId = x.ProductPriceId ,
Product = x.Product
});
Or you could do it other ways, by selecting the entity you want, and just populating manual.