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

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

Related

Oracle SQL inner join not returning records with blank values

I am joining two tables together, but the result set does not include blank rows. The query below does not return columns with null values. Why is that?
SELECT
table1.FE_KEY
,table2.CV_VALUE
,table2.CV_UOM AS EST_ISENTROPIC_POWERUoM
,CVDATA1.CV_VALUE
,CVDATA1.CV_UOM AS VAPOUR_OR_GAS_HANDLEUoM
,CVDATA2.CV_VALUE
,CVDATA2.CV_UOM AS NORMAL_FLOW_RATEUoM
FROM
((table1 FULL LEFT JOIN table2 ON table1.ID = table2.FE_ID)
INNER JOIN table2 CVDATA1 ON table1.ID = CVDATA1.FE_ID)
INNER JOIN table2 CVDATA2 ON table1.ID = CVDATA2.FE_ID
WHERE ((table1.FE_KEY) Like '6-K-%')
AND ((table2.CV_CODE)='EST_ISENTROPIC_POWER')
AND ((CVDATA1.CV_CODE)='VAPOUR_OR_GAS_HANDLE')
AND ((CVDATA2.CV_CODE)='NORMAL_FLOW_RATE')
If you want to include rows where FE_KEY or CV_CODE are null, then you need to explicitly include those. You also seem to have some extra parentheses that aren't needed. Try this reworked version:
SELECT
TABLE1.FE_KEY
,TABLE2.CV_VALUE
,TABLE2.CV_UOM AS EST_ISENTROPIC_POWERUOM
,CVDATA1.CV_VALUE
,CVDATA1.CV_UOM AS VAPOUR_OR_GAS_HANDLEUOM
,CVDATA2.CV_VALUE
,CVDATA2.CV_UOM AS NORMAL_FLOW_RATEUOM
FROM
(
(TABLE1 FULL LEFT JOIN TABLE2 ON TABLE1.ID = TABLE2.FE_ID)
INNER JOIN TABLE2 CVDATA1 ON TABLE1.ID = CVDATA1.FE_ID
)
INNER JOIN TABLE2 CVDATA2 ON TABLE1.ID = CVDATA2.FE_ID
WHERE (TABLE1.FE_KEY IS NULL OR TABLE1.FE_KEY LIKE '6-K-%')
AND (TABLE2.CV_CODE IS NULL OR TABLE2.CV_CODE = 'EST_ISENTROPIC_POWER')
AND (CVDATA1.CV_CODE IS NULL OR CVDATA1.CV_CODE = 'VAPOUR_OR_GAS_HANDLE')
AND (CVDATA2.CV_CODE IS NULL OR CVDATA2.CV_CODE = 'NORMAL_FLOW_RATE')

Select count in parent table

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

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;

Convert NOT IN query to LEFT JOIN

to adapt to Netezza DB I need to convert fallowing query(as NOT IN(SUBQUERY) is not supported by Netezza):
UPDATE table1 t1 SET t1.deal_type=t2.deal_type
FROM table2 t2
WHERE t1.id_col=t2.id_col
AND t1.price=t2.price
AND t1.id_col2=t2.id_col2
AND t2.price NOT IN (
SELECT st1.price
FROM table1 st1, table2 st2
WHERE st1.id_col=st2.id_col
AND st1.price=st2.price
AND st1.id_col2=st2.id_col2
AND st1.id_col=t1.id_col
AND t2.deal_type=st2.deal_type
GROUP BY st1.id_col, st1.price, st1.id_col2, st2.deal_type
HAVING COUNT (*)>1);
I tried with LEFT JOIN but not all records returned:
UPDATE table1 t1 SET t1.deal_type = t2.deal_type
FROM table2 t2
LEFT JOIN
(SELECT st1.price, st1.id_col, st2.deal_type
FROM table1 st1, table2 st2
WHERE st1.id_col=st2.id_col
AND st1.price=st2.price
AND st1.id_col2=st2.id_col2
GROUP BY st1.id_col, st1.price, st1.id_col2, st2.deal_type
HAVING COUNT (*)>1) subq ON (subq.id_col=t1.id_col
AND t2.deal_type=subq.deal_type)
WHERE
t1.id_col=t2.id_col
AND t1.price=t2.price
AND t1.id_col2=t2.id_col2
subq.price is null
Any suggestions where I was wrong. or any other way to work arround NOT IN witch is not supported by NETEZZA
I think you forgot to add the price to the Left Join condition.
if there are duplicates for this id&type but with different price,
the NOT-IN condition will pass, but the Left-Join (IS NULL) condition will fail
just change
ON (subq.id_col=t1.id_col
AND t2.deal_type=subq.deal_type)
to
ON (subq.id_col=t1.id_col
AND t2.deal_type=subq.deal_type)
AND subq.price=t2.price)
Can you please try this if netezza support EXISTS and your first query is logically right
UPDATE t1 SET t1.deal_type=t2.deal_type
FROM table1 t1
INNER JOIN table2 t2 ON t1.id_col=t2.id_col
AND t1.price=t2.price
AND t1.id_col2=t2.id_col2
LEFT JOIN ( SELECT st1.id_col
FROM table1 st1
INNER JOIN table2 st2 ON st1.id_col=st2.id_col
AND st1.price=st2.price
AND st1.id_col2=st2.id_col2
AND t2.deal_type=st2.deal_type
GROUP BY st1.id_col, st1.price, st1.id_col2, st2.deal_type
HAVING COUNT (*)>1) i ON i.id_col=t1.id_col
WHERE i.id_col IS NULL

SQL left join with where over three tables

I need Help with a SQL statement :-(
Lets say have three Tables:
Table1
id / vorname / user
Table2
id / groupename / user / level
Table 3
id /groupename / description
At the moment my SQL looks like this:
select table1.vorname, table2.groupename, table2.user, table2.level from table2 left join table1 on table2.user = table1.user;
So i get the all Data (users) auf Table 2 with the right uservorname of Table 1.
And now i stuck ...
I have the id from Table 3 and need to limit the recordset to only those groupes.
The key in this case is the groupename so i have to get the groupename from Table3 where the id ist the same that i have and compare the groupename with the groupename from Table2 to limit the records only to this groupe ...
Table2.groupename = Table3.groupename
But i dont know how to solve it.
THANK YOU !
You could use something old-school like
select table1.vorname, table2.groupename, table2.user, table2.level
from table1, table2, table3
where table2.user = table1.user
and table2.groupename = table3.groupename
and table3.id = '[my id]'
Using Joins is possibly more efficient, and possibly makes the intent clearer.
select table1.vorname, table2.groupename, table2.user, table2.level
from table1
inner join table2 on table2.user = table1.user
inner join table3 on table2.groupename = table3.groupename
where table3.id = '[my id]'
select table1.vorname,
table2.groupename,
table2.user,
table2.level
from table2
inner join table3 on table2.groupname = table3.groupname
left join table1 on table2.user = table1.user