Select count in parent table - sql

I have two tables (table one is parent and table two is children) in ms access that are related together with ID
Table one :
table two :
and I need a select sql code to run this query:
I could write the sql command without count of TeamName :
Select Table1.TackName,Sum(Table2.PaintingValue) as SumofPaintingValue
from (Table1 INNER JOIN
Table2
on (Table1.ID = Table2.fgk)
GROUP BY Table1.TackName

Try this
Select TrackName, Sum(PaintingValue) as SumOfPaintingValue, Dcount( "TrackName","Table1","TrackName='" & a.TrackName & "'") as CountOfTeamName from Table1 as a INNER JOIN Table2 as b ON a.ID = b.fgk
group by TrackName
OR
Create two saved query
qryA
Select TrackName, count(TrackName) as CountOfTeamName from Table1 as a group by TrackName
qryB
Select TrackName, Sum(PaintingValue) as SumOfPaintingValue from Table1 as a INNER JOIN Table2 as b ON a.ID = b.fgk group by TrackName
Then use below for result
Select qryA.TrackName, SumOfPaintingValue, CountOfTeamName from qryA INNER JOIN qryB on qryA.TrackName = qryB.TrackName

I can't test this at the moment, but I think a correlated query should do the trick:
Select
Table1.TackName,
Sum(Table2.PaintingValue) as SumofPaintingValue,
(select count(TeamName) from table1 t1 where t1.TrackName=table1.TrackName
group by TeamName) as CountOfTeams
from Table1 INNER JOIN Table2 on (Table1.ID = Table2.fgk)
GROUP BY Table1.TackName

Related

merge multiple tables in Hive SQL

I have two original tables: product table and component tables
Firstly, I need to do an inner merge between these two tables, then create a new table called T1:
select a.Product, a.Plant, b.component, b.position, b.valid_date from Product a
inner join component b on a.Product=b.Product
where a.Plant='A'
The result T1 looks like in the following way:
Furthermore, I need to create a new table named T2 based on T1
select T1.Product, T1.Plant,T1.position,max(T1.valid_date) as valid_date from T1
Where T1.Plant='A'
Group by T1.Product, T1.Plant,T1.position
The result T2 is:
Finally, I want to merge T1 and T2 based on Product, Plant, position, and valid_date for a final table:
select T2.Product, T2.Plant,T1.Component, T2.position, T2.valid_date from T1
INNER JOIN T2 on T1.Product=T2.Product and T1.Plant=T2.Plant and T1.position=T2.position and T1.valid_date=T2.valid_date
where T1.Plant='A'
The final table:
I know this whole process can be done in one hive SQL script. I am confused with multiple tables in one query. I appreciate someone can help me for that. Thank you
isn't it that you just want to do this?
select
a.Plant,b.Product,b.position,b.component,
max(valid_date) as valid_date
from Product a
inner join component b on a.Product = b.Product
where a.Plant = 'A'
group by a.Plant,b.Product,b.position,b.component
db<>fiddle here
the query above produces what you show in output , but based on your comment , I think this is what you are looking for :
select t.Plant,t.Product,t.position,t.component, max(t.valid_date) valid_date
(
select
b.Product, a.Plant,b.component,b.position,max(valid_date) over (partition by a.Plant,b.Product,b.position) as valid_date
from Product a
join component b on a.Product = b.Product
where a.Plant = 'A'
) t
group by t.Plant,t.Product,t.position,t.component;

How to convert the following set of queries' output to a view in SQL Server?

I have the below code in SQL Server and I want the same output but as a view. How do I write a view to give this output? I want the result from the first query basically along with the count of distinct people id for each occupancy id but I can't use group by in that query.
Thanks in advance!
SELECT DISTINCT
pp.PeopleID,
od.OccupancyID,
pp.Gender
INTO
t1
FROM
dimPeople AS pp
LEFT JOIN
OccupanciesPeople AS op ON op.PeopleID = pp.PeopleID
AND op.CompanyID = pp.CompanyID
LEFT JOIN
OccupancyDetail AS od ON op.OccupancyID = od.OccupancyID
AND op.CompanyID = od.CompanyID
WHERE
od.OccupancyEndDate IS NULL
AND op.DateLeftOccupancy IS NULL
AND pp.DateOfDeath IS NULL
SELECT
OccupancyID, COUNT(DISTINCT peopleID) AS PeopleCount
INTO
t2
FROM
t1
GROUP BY
OccupancyID
SELECT
t1.*,
t2.PeopleCount, t2.[HouseHold Person]
FROM
t1
JOIN
t2 ON t1.occupancyID = t2.OccupancyID
DROP TABLE t1
DROP TABLE t2
If those queries work the way you want (spoiler: they don't - you don't define t2.[HouseHold Person] when you create t2), you could replace the 2 "into" tables with Common Table Expressions and get your results.
CREATE VIEW dbo.MyView AS
WITH t1 AS (
SELECT DISTINCT
pp.PeopleID,
od.OccupancyID,
pp.Gender
FROM
dimPeople AS pp
LEFT JOIN
OccupanciesPeople AS op ON op.PeopleID = pp.PeopleID
AND op.CompanyID = pp.CompanyID
LEFT JOIN
OccupancyDetail AS od ON op.OccupancyID = od.OccupancyID
AND op.CompanyID = od.CompanyID
WHERE
od.OccupancyEndDate IS NULL
AND op.DateLeftOccupancy IS NULL
AND pp.DateOfDeath IS NULL
), t2 as (
SELECT
OccupancyID, COUNT(DISTINCT peopleID) AS PeopleCount
FROM
t1
GROUP BY
OccupancyID
)
SELECT
t1.PeopleID,
t1.OccupancyID,
t1.Gender,
t2.PeopleCount
FROM
t1
JOIN
t2 ON t1.occupancyID = t2.OccupancyID;

Postgresql SQL Select items from table1 based on a condition from table2

I am trying to select items from table1 which has a child table2 there is a third table3 involved.
Select j.ccmasterid,
(Select sum(i.ccmatpullqty) From table2 i
Where i.ccmasterid = j.ccmasterid) pulled
from table1 j
INNER JOIN table3 s on j.ccstatus = s.sysstatusid and s.ccopenjob=false
where j.ccmasterid LIKE 'W%' and pulled = 0
This generates an error:
ERROR: column "pulled" does not exist
LINE 6: where j.ccmasterid LIKE 'W%' and pulled = 0
If I take the "and pulled = 0" out the the query, it works as one would expect producing a list of records from table1 with the sum of the values in table2 as pulled.
ccmasterid pulled
W106063 0
W100553 9
W100685 1
WHAT I can't figure out is how to select based on pulled being 0.
Change this query into a subquery, and move WHERE condition to the outer query:
SELECT * FROM (
Select j.ccmasterid,
(Select sum(i.ccmatpullqty) From table2 i
Where i.ccmasterid = j.ccmasterid) pulled
from table1 j
INNER JOIN table3 s on j.ccstatus = s.sysstatusid and s.ccopenjob=false
where j.ccmasterid LIKE 'W%'
) x
WHERE pulled = 0
Avoid the correlated subquery which runs for every row in outer query and not once if joining to an aggregate query with GROUP BY clause:
SELECT j.ccmasterid
FROM table1 j
INNER JOIN table3 s
ON j.ccstatus = s.sysstatusid AND s.ccopenjob = false
INNER JOIN
(SELECT i.ccmasterid, SUM(i.ccmatpullqty) AS pulled
FROM table2 i
GROUP BY i.ccmasterid
) AS agg
ON agg.ccmasterid = j.ccmasterid
WHERE j.ccmasterid LIKE 'W%' AND agg.pulled = 0
Even use CTE
WITH agg AS
(SELECT i.ccmasterid, SUM(i.ccmatpullqty) AS pulled
FROM table2 i
GROUP BY i.ccmasterid)
SELECT j.ccmasterid
FROM table1 j
INNER JOIN table3 s
ON j.ccstatus = s.sysstatusid AND s.ccopenjob = false
INNER JOIN agg
ON agg.ccmasterid = j.ccmasterid
WHERE j.ccmasterid LIKE 'W%' AND agg.pulled = 0

selecting records from main table and count of each row in another table

I have 2 table in my database that tables are in relationship with foreign key
I want to select all records from main table and then select count of each row in another table than have same ID from main table I tried to create a select query but it is not work correctly
this query return all records from main table + count of all records from next table(not count of each row in relationship)
SELECT tblForumSubGroups_1.id, tblForumSubGroups_1.GroupID,
tblForumSubGroups_1.SubGroupTitle, tblForumSubGroups_1.SubGroupDesc,
(SELECT COUNT(dbo.tblForumPosts.id) AS Expr1
FROM dbo.tblForumSubGroups INNER JOIN dbo.tblForumPosts ON
dbo.tblForumSubGroups.id = dbo.tblForumPosts.SubGroupID) AS Expr1
FROM dbo.tblForumSubGroups AS tblForumSubGroups_1 INNER JOIN
dbo.tblForumPosts AS tblForumPosts_1 ON tblForumSubGroups_1.id
= tblForumPosts_1.SubGroupID
SELECT tblForumSubGroups_1.id, tblForumSubGroups_1.GroupID, tblForumSubGroups_1.SubGroupTitle, tblForumSubGroups_1.SubGroupDesc,
COUNT(tblForumPosts_1.id) AS Expr1
FROM dbo.tblForumSubGroups AS tblForumSubGroups_1
INNER JOIN dbo.tblForumPosts AS tblForumPosts_1 ON tblForumSubGroups_1.id = tblForumPosts_1.SubGroupID
GROUP BY tblForumSubGroups_1.id, tblForumSubGroups_1.GroupID, tblForumSubGroups_1.SubGroupTitle, tblForumSubGroups_1.SubGroupDesc
I would suggest cross apply as you can do a lot more things with it ...
SELECT t1.id,
t1.GroupID,
t1.SubGroupTitle,
t1.SubGroupDesc,
t2.val
FROM dbo.tblForumSubGroups AS t1
cross apply (SELECT COUNT(*)
FROM dbo.tblForumPosts as t2
WHERE t1.id = t2.SubGroupID) x(val)
Do not mix sub-query and join logic. Use only one of them. I prefer sub-select.
SELECT tblForumSubGroups_1.id,
tblForumSubGroups_1.GroupID,
tblForumSubGroups_1.SubGroupTitle,
tblForumSubGroups_1.SubGroupDesc,
(SELECT COUNT(*)
FROM dbo.tblForumPosts
WHERE dbo.tblForumSubGroups.id = dbo.tblForumPosts.SubGroupID) AS Expr1
FROM dbo.tblForumSubGroups AS tblForumSubGroups_1
Just to supply another answer though I believe the cross apply is likely the best option:
SELECT
A.id, A.GroupID, A.SubGroupTitle, A.SubGroupDesc,
B.IDCount AS Expr1
FROM dbo.tblForumSubGroups A
INNER JOIN (
Select SubGroupID, Count(ID) as IDCount
from dbo.tblForumPosts
Group By SubGroupID
) B On A.ID = B.SubGroupID

How to write Simple.Data query to join two tables?

I need to write Simple.data queries for following SQL queries can you help me ?
SELECT
Table1.UserID,
Table1.we, Table1.ba, Table1.re,
Table1.rtes, Table1.datae, Table1.void,
Table1.deletee
FROM
Table1
INNER JOIN
Table1 ON UserID.UserID = Table2.UserID
WHERE
Table2.clinicId = 11
I try it following way
db.Table1.FindAll()
.Where(db.Table1.UserID == db.Table2.FindAll(db.Table2.ClinicID = 11).Select(db.Table2.UserID));
but it does not work. I use mysql 4.0
db.Table2.FindAllByClinicId(11)
.Select(
db.Table2.Table1.UserId,
db.Table2.Table1.we,
db.Table2.Table1.ba,
db.Table2.Table2.re,
db.Table2.Table1.rtes,
db.Table2.Table1.datae,
db.Table2.Table1.void,
db.Table2.Table1.deletee);
That should end up sending this to the database:
SELECT Table1.UserId
, Table1.we
, Table1.ba
, Table1.re
, Table1.rtes
, Table1.datae
, Table1.void
, Table1.deletee
FROM Table1
INNER JOIN Table2 ON Table1.UserId = Table2.UserId
WHERE Table2.ClinicId = 11
you are joining with same table. so you need to join two different tables or join same table giving alias.
SELECT Table1.UserID, Table1.we, Table1.ba, Table1.re, Table1.rtes, Table1.datae,
Table1.void, Table1.deletee FROM Table1 INNER JOIN Table2 ON
UserID.UserID = Table2.UserID
where Table2.clinicId=11
OR
using alias for same table.
SELECT t1.UserID, t1.we, t1.ba, t1.re, t1.rtes, t1.datae, t1.void, t1.deletee FROM
Table1 as t1 INNER JOIN Table1 as t2 ON UserID.UserID = t2.UserID
where t2.clinicId=11
This should work, you should just say " table1 inner join tabloe2". && condition Table1.UserID = Table2.UserID
where Table2.clinicId=11
SELECT Table1.UserID, Table1.we, Table1.ba, Table1.re, Table1.rtes, Table1.datae,
Table1.void, Table1.deletee
FROM Table1
INNER JOIN Table2
ON Table1.UserID = Table2.UserID
where Table2.clinicId=11