I'm working on a SQL query where the user's maximum Audit_Date is not in a range, as in they haven't used the system for a long time. I tried it this way:
SELECT DISTINCT
UserID
,max(Audit_Date)
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON
RV_USERS.UserID=RV_AUDIT.UserID
group BY RV_USERS.UserID
AND --it doesn't like the "and" here
not exists(
select *
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON ON RV_USERS.UserID=RV_AUDIT.UserID
where
Audit_Date not between '2019-05-29 00:00:00' and '10/29/2019'
)
I tried to use not exists, like the example but it's not working in this case. I get incorrect syntax near the keyword 'AND', right before the not exists. I need to make a view out of this so I think variables and temp tables are out. It's going to be used in a crystal report and scheduled in Central Management Console.
**Update1: Tried this per answer:
SELECT DISTINCT
"RV_USERS"."UserID"
,"RV_AUDIT"."Audit_Date"
FROM "RV_USERS" "RV_USERS"
INNER JOIN "RV_AUDIT" "RV_AUDIT" ON "RV_USERS"."UserID"="RV_AUDIT"."UserID"
group BY "RV_USERS"."UserID", "RV_AUDIT"."Audit_Date"
HAVING
max("RV_AUDIT"."Audit_Date") < '2019-05-29 00:00:00'
and
"RV_USERS".UserID='me'
This returns me with dates in may, even though I have used the system since May. I checked that by removing the max date part and see my dates go to today.
**Update2: Tried this per other answer:
SELECT DISTINCT
"RV_USERS"."UserID"
,max("RV_AUDIT"."Audit_Date")
FROM "RV_USERS" "RV_USERS"
INNER JOIN "RV_AUDIT" "RV_AUDIT" ON "RV_USERS"."UserID"="RV_AUDIT"."UserID"
WHERE
not exists(
select *
FROM "RV_USERS" u2
INNER JOIN "RV_AUDIT" a2 ON u2."UserID"=a2."UserID"
where
a2."Audit_Date" not between '2019-05-27 00:00:00' and '10/31/2019'
)
group BY "RV_USERS"."UserID"
This is not returning anything, but we know there are managers that haven't used the system.
**Update 3 per answer:
SELECT DISTINCT
u."UserID"
,max(a."Audit_Date")
FROM "RV_USERS" u
INNER JOIN "RV_AUDIT" a ON u."UserID"=a."UserID"
WHERE
u.UserID not in(
select u2.UserID
FROM "RV_USERS" u2
INNER JOIN "RV_AUDIT" a2 ON u2."UserID"=a2."UserID"
where
a2."Audit_Date" between '2019-05-27 00:00:00' and '10/31/2019'
)
group BY u."UserID"
You have more than one mistakes, here is corrected code:
SELECT DISTINCT
RV_U.UserID
, max(RV_U.Audit_Date)
FROM RV_USERS RV_U
INNER JOIN RV_AUDIT RV_A ON RV_U.UserID=RV_A.UserID
WHERE
NOT EXISTS(
SELECT *
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON RV_USERS.UserID=RV_AUDIT.UserID
WHERE Audit_Date not between '2019-05-29 00:00:00' and '10/29/2019'
)
GROUP BY RV_U.UserID;
Your line NNER JOIN RV_AUDIT RV_AUDIT ON ON
RV_USERS.UserID=RV_AUDIT.UserID has ON two times.
GROUP BY should go on the end
AND should be replaced with WHERE
Try to implement this changes.
I would suggest a having clause:
SELECT UserID, max(Audit_Date)
FROM RV_USERS RV_USERS INNER JOIN
RV_AUDIT RV_AUDIT
ON RV_USERS.UserID = RV_AUDIT.UserID
GROUP BY RV_USERS.UserID
HAVING max(Audit_Date) < '2019-05-29';
Your query seems much more complicated than necessary.
Replace and with where clause and put the group by at the last to make the above query run atleast
Related
I have the following SQL select statement that I use to get a subset of products, or wines:
SELECT pv.SkProdVariantId AS id,
pa.Colour AS colour,
FROM Dim.ProductVariant AS pv
JOIN ProductAttributes_new AS pa
ON pv.SkProdVariantId = pa.SkProdVariantId
WHERE pv.ProdTypeName = 'Wines'
The length of this table generated is 3,905. I want to get all the transactional data for these products.
At the moment I'm using this select statement
SELECT c.CalDate AS timestamp,
f.SkProductVariantId AS sku_id,
f.Quantity AS quantity
FROM fact.FTransactions AS f
LEFT JOIN Dim.Calendar AS c
ON f.SkDateId = c.SkDateId
LEFT JOIN (
SELECT pv.SkProdVariantId AS id,
pa.Colour AS colour,
FROM Dim.ProductVariant AS pv
JOIN ProductAttributes_new AS pa
ON pv.SkProdVariantId = pa.SkProdVariantId
WHERE pv.ProdTypeName = 'Wines'
) AS s
ON s.id = f.SkProductVariantId
WHERE c.CalDate LIKE '%2019%'
The calendar dates are correct, but the number of unique products returned is 5,648, rather than the expected 3,905 from the select query.
Why does my LEFT JOIN on the first select query not work as I expect it to, please?
Thanks for any help!
If you want all the rows form your query, it needs to be the first reference in the LEFT JOIN. Then, I am guessing that you want transaction in 2019:
select . . .
from (SELECT pv.SkProdVariantId AS id, pa.Colour AS colour,
FROM Dim.ProductVariant pv JOIN
ProductAttributes_new pa
ON pv.SkProdVariantId = pa.SkProdVariantId
WHERE pv.ProdTypeName = 'Wines'
) s LEFT JOIN
(fact.FTransactions f JOIN
Dim.Calendar c
ON f.SkDateId = c.SkDateId AND
c.CalDate >= '2019-01-01' AND
c.CalDate < '2020-01-01'
)
ON s.id = f.SkProductVariantId;
Note that this assumes that CalDate is really a date and not a string. LIKE should only be used on strings.
You misunderstand somehow how outer joins work. See Gordon's answer and my request comment on that.
As to the task: It seems you want to select transactions of 2019, but you want to restrict your results to wine products. We typically restrict query results in the WHERE clause. You can use IN or EXISTS for that.
SELECT
c.CalDate AS timestamp,
f.SkProductVariantId AS sku_id,
f.Quantity AS quantity
FROM fact.FTransactions AS f
INNER JOIN Dim.Calendar AS c ON f.SkDateId = c.SkDateId
WHERE DATEPART(YEAR, c.CalDate) = 2019
AND f.SkProductVariantId IN
(
SELECT pv.SkProdVariantId
FROM Dim.ProductVariant AS pv
WHERE pv.ProdTypeName = 'Wines'
);
(I've removed the join to ProductAttributes_new, because it doesn't seem to play any part in this query.)
EDIT:
The result supposed to be like this:
desired result
I have this query:
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join
(
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join
(
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join
(
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
But I always get an error on line 21 Pos 145:
ORA-00904 WKURSE.KURS invalid identifier
The WKURSE table is supposed be joined already above, but why do I still get error?
How can I do join with all these tables?
I need to join all these tables:
Mitarbeiter, Vertragspos, vertrag_ek_vk_zuord, wkurse, fremdwaehrung, vertragskopf.
What is the right syntax? I'm using SQL Tool 1,8 b38
Thank you.
Because LEFT JOIN is executed on entire dataset, and not in row-by-row manner. So there's no wkurse.kurs available in the execution context of subquery. Since you join that tables, you can place the calculation in the top-most select statement.
EDIT:
After you edited the statement, it became clear where does vertragskopf.zahlintervall came from. But I don't know where are you going to use calculated vertragswert (now it is absent in the query), so I've put it in the result. As I'm not a SQL parser and have no idea of your tables, so I cannot check the code, but calculation now can be resolved (all the values are available in calculation context).
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2, s.amount / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join (
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join (
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join (
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge) as amount
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
Rewriting the code using WITH clause makes it much clearer than select from select.
Also get the rate on last day before today in oracle is as simple as
select wkurse.lfdnr
, max(wkurse.kurs) keep (dense_rank first order by wkurse.tag desc) as rate
from wkurse
where tag < sysdate
group by wkurse.lfdnr
One option is a lateral join:
left join lateral
(SELECT vertrag_ID, Sum(preis) as preis, Sum(menge) as menge,
Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s
ON vertragskopf.id = s.vertrag_id
SELECT DISTINCT 'DATA' AS DSN,
'MASTER' AS ORG_ID,e.emp_id,
e.emp_name AS EMPLOYEE_NUMBER,
e.emp_firstname AS EMPLOYEE_FIRST_NAME,
e.emp_lastname AS EMPLOYEE_LAST_NAME,
e.emp_val20 AS PAY_STATUS,
(NVL(TO_CHAR(PGT.PAYGRPTYP_NAME),'MASTER')) AS PAY_FREQUENCY,
E.EMP_VAL16 AS PAY_CLASS,
SP.SHFTPAT_NAME AS SHIFT_CODE,
WB.WBU_EMAIL AS EMAIL_ADDR,
(NVL(TO_CHAR( DE.DEPT_NAME),'MASTER')) AS DEPARTMENT,
NULL AS HELD_POSTN,
(NVL(TO_CHAR( J.JOB_NAME),'MASTER')) AS JOB_TITLE,
WB. WBU_NAME AS USER_NAME,
(NVL(TO_CHAR(ET.WBT_ID ),'MASTER')) AS TEAM_CODE,
WT.WBT_NAME AS TEAM_NAME,
WT.WBT_DESC AS TEAM_DESC,
(NVL(TO_CHAR(EB.EMPBDG_BADGE_NUMBER),'MASTER')) AS PERMANENT_BADGE_NO
FROM EMPLOYEE E
LEFT JOIN SHIFT_PATTERN SP
ON E.SHFTPAT_ID =SP.SHFTPAT_ID
LEFT JOIN PAY_GROUP PT
ON E.PAYGRP_ID =PT.PAYGRP_ID
LEFT JOIN PAY_GROUP_TYPE PGT
ON PT.PAYGRPTYP_ID =PGT.PAYGRPTYP_ID
LEFT JOIN EMPLOYEE_BADGE EB
ON E.EMP_ID=EB.EMP_ID
LEFT JOIN EMP_UDF_DATA ED
ON E.EMP_ID=ED.EMP_ID
LEFT JOIN WORKBRAIN_USER WB
ON E.EMP_ID=WB.EMP_ID
LEFT JOIN EMP_DEF_LAB D
ON E.EMP_ID=D.EMP_ID
LEFT JOIN DEPARTMENT DE
ON D.DEPT_ID=DE.DEPT_ID
LEFT JOIN EMPLOYEE_JOB EJ
ON E.EMP_ID=EJ.EMP_ID
LEFT JOIN JOB J
ON EJ.JOB_ID=J.JOB_ID
LEFT JOIN EMPLOYEE_TEAM ET
ON E.EMP_ID=ET.EMP_ID
LEFT JOIN WORKBRAIN_TEAM WT
ON ET.WBT_ID=WT.WBT_ID
LEFT JOIN CALC_GROUP CG
ON E.CALCGRP_ID =CG.CALCGRP_ID
WHERE E.EMP_ID NOT IN (SELECT EMP_ID FROM EMPLOYEE_READER_GROUP)
AND sysdate BETWEEN ET.empt_start_date AND ET.empt_end_date
AND ET.empt_home_team = 'Y'
AND sysdate BETWEEN EJ.EMPJOB_START_DATE AND EJ.EMPJOB_END_DATE
GROUP BY E.EMP_ID,
-- ER.rdrgrp_id,
e.emp_name,
e.emp_lastname,
e.emp_firstname,
e.emp_status,
e.emp_val20,
PGT.PAYGRPTYP_NAME,
E.EMP_VAL16,
e.EMP_VAL18,
EB.EMPBDG_BADGE_NUMBER,
WB.WBU_EMAIL,
DE.DEPT_NAME,
J.JOB_NAME,
SP.SHFTPAT_NAME,
WB. WBU_NAME ,
ET.WBT_ID ,
WT.WBT_NAME,
WT.WBT_DESC;
I want all data from employee table so i used left join but i am not getting all rows which is available in employee table some values are missing .Please help me with this . I have 43000 rows in employee but when i am using this i am getting only 17000 rows alone. I am not sure where i am lagging.I want the null column as Master for the right table values.
Think of the data you want in sets. When you LEFT JOIN, you're joining the whole tables when you really only need data from one of them. I've modified a few of your LEFT JOINS to factor for this. Also, doing a WHERE x NOT IN (SELECT x....) can be VERY slow on large datasets. I've changed it to allow the optimizer to work a little better. It now does a LEFT JOIN....WHERE x IS NULL. This should work a lot faster. What do you plan on MAXing? This query is pretty involved. You want to make sure you're getting the initial data that you want before you try to aggregate it.
SELECT 'DATA' AS DSN,
'MASTER' AS ORG_ID,e.emp_id,
e.emp_name AS EMPLOYEE_NUMBER,
e.emp_firstname AS EMPLOYEE_FIRST_NAME,
e.emp_lastname AS EMPLOYEE_LAST_NAME,
e.emp_val20 AS PAY_STATUS,
(NVL(TO_CHAR(ptpgt.PAYGRPTYP_NAME),'MASTER')) AS PAY_FREQUENCY, /* From subquery join */
E.EMP_VAL16 AS PAY_CLASS,
SP.SHFTPAT_NAME AS SHIFT_CODE,
WB.WBU_EMAIL AS EMAIL_ADDR,
(NVL(TO_CHAR( DE.DEPT_NAME),'MASTER')) AS DEPARTMENT,
NULL AS HELD_POSTN,
(NVL(TO_CHAR( eej.JOB_NAME),'MASTER')) AS JOB_TITLE, /* From subquery join */
WB. WBU_NAME AS USER_NAME,
(NVL(TO_CHAR(eet.WBT_ID ),'MASTER')) AS TEAM_CODE, /* From subquery join */
WT.WBT_NAME AS TEAM_NAME,
WT.WBT_DESC AS TEAM_DESC,
(NVL(TO_CHAR(EB.EMPBDG_BADGE_NUMBER),'MASTER')) AS PERMANENT_BADGE_NO
FROM EMPLOYEE E
/* This plus WHERE ERG.EMP_ID IS NULL is the same as the subquery filter. But faster. */
LEFT OUTER JOIN EMPLOYEE_READER_GROUP erg ON e.EMP_ID = ERG.EMP_ID
LEFT JOIN SHIFT_PATTERN SP ON E.SHFTPAT_ID =SP.SHFTPAT_ID
LEFT JOIN (
SELECT PT.EMP_ID,PGT.PAYGRPTYP_NAME
FROM PAY_GROUP PT
INNER JOIN PAY_GROUP_TYPE PGT ON PT.PAYGRPTYP_ID =PGT.PAYGRPTYP_ID
) ptpgt ON E.PAYGRP_ID =ptpgt.PAYGRP_ID
LEFT JOIN EMPLOYEE_BADGE EB ON E.EMP_ID=EB.EMP_ID
LEFT JOIN EMP_UDF_DATA ED ON E.EMP_ID=ED.EMP_ID
LEFT JOIN WORKBRAIN_USER WB ON E.EMP_ID=WB.EMP_ID
LEFT JOIN (
SELECT D.EMP_ID, DE.DEPT_NAME
FROM EMP_DEF_LAB D
INNER JOIN DEPARTMENT DE ON D.DEPT_ID=DE.DEPT_ID
) dde ON E.EMP_ID=dde.EMP_ID
LEFT JOIN (
SELECT EJ.EMP_ID, J.JOB_NAME
FROM EMPLOYEE_JOB EJ
INNER JOIN JOB J ON EJ.JOB_ID=J.JOB_ID
WHERE sysdate BETWEEN EJ.EMPJOB_START_DATE AND EJ.EMPJOB_END_DATE
/*
Doublecheck your date filters. Are they getting the edge dates you need?
BETWEEN makes it easy to miss a date, especially if your fields
are DATETIME datatypes.
USE <=/>= to be a bit clearer.
*/
) eej ON E.EMP_ID=eej.EMP_ID
LEFT JOIN (
SELECT ET.EMP_ID, WBT_ID
FROM EMPLOYEE_TEAM ET
INNER JOIN WORKBRAIN_TEAM WT ON ET.WBT_ID=WT.WBT_ID
WHERE
ET.empt_home_team = 'Y'
AND sysdate BETWEEN ET.empt_start_date AND ET.empt_end_date
/*
Doublecheck your date filters. Are they getting the edge dates you need?
BETWEEN makes it easy to miss a date, especially if your fields
are DATETIME datatypes.
USE <=/>= to be a bit clearer.
*/
) eet ON E.EMP_ID=eet.EMP_ID
LEFT JOIN CALC_GROUP CG ON E.CALCGRP_ID=CG.CALCGRP_ID
WHERE
ERG.EMP_ID IS NULL
You can LEFT join all day long, your where clause is still going to function as a filter. Try it with no where clause and see if you get all 43k. Plus you are grouping which could be collapsing rows.
These filters in the WHERE clause turn the OUTER JOIN into an INNER JOIN:
AND sysdate BETWEEN ET.empt_start_date AND ET.empt_end_date
AND ET.empt_home_team = 'Y'
AND sysdate BETWEEN EJ.EMPJOB_START_DATE AND EJ.EMPJOB_END_DATE
Because they exclude rows where the tested attributes are null (which is what they are in a non-matched outer-joined row).
So there are a couple of approaches. The first is to allow for nulls, e.g.
AND ( ET.empt_home_team is null or
( sysdate BETWEEN ET.empt_start_date AND ET.empt_end_date
AND ET.empt_home_team = 'Y' )
)
AND ( EJ.EMPJOB_START_DATE is null
or sysdate BETWEEN EJ.EMPJOB_START_DATE AND EJ.EMPJOB_END_DATE )
The second is to replace the tables with inline views e.g.
LEFT JOIN ( select * from EMPLOYEE_TEAM
where sysdate BETWEEN empt_start_date AND empt_end_date
AND empt_home_team = 'Y' ) ET
ON E.EMP_ID=ET.EMP_ID
LEFT JOIN ( select * from EMPLOYEE_JOB
where sysdate BETWEEN EMPJOB_START_DATE AND EMPJOB_END_DATE ) EJ
ON E.EMP_ID=EJ.EMP_ID
Which approach is the better one for you depends on the data. test them and see.
I am having an issue getting this SQL Script to join the two tables together, I can execute it and it will show the results of the first portion (AccountID, AuditDate ..etc) but it will not join to the inner select statement.
I have verified by spot checking that there is data that matches in the database.
What portion did i mess up?
Select AccountID, AuditDate, SourceVersion
From Audit
Left join (
Select A.PrinterAuditID, A.AuditID, A.SerialNr, A.PageCountTotal,a.PageCountColor, A.PageCountMono
from InvalidPrinterAudit A
where A.DeviceID = 90757
) InvalidPrinterAudit on InvalidPrinterAudit.AuditID = Audit.AuditID
There's a couple of things happening here.
One: In order to have fields in your result set, they must be included in the SELECT portion of your query:
Select AccountID, AuditDate, SourceVersion, InvalidPrinterAudit.PrinterAuditID, InvalidPrinterAudit.AuditID, InvalidPrinterAudit.SerialNr, InvalidPrinterAudit.PageCountTotal,InvalidPrinterAudit.PageCountColor, InvalidPrinterAudit.PageCountMono
From Audit
Left join (
Select A.PrinterAuditID, A.AuditID, A.SerialNr, A.PageCountTotal,a.PageCountColor, A.PageCountMono
from InvalidPrinterAudit A
where A.DeviceID = 90757
) InvalidPrinterAudit on InvalidPrinterAudit.AuditID = Audit.AuditID
Two: You don't need to have a subquery here. Subqueries are great if you need to aggregate the results from a seperate table or something, but here you can just go with a LEFT OUTER JOIN and be done with it.
Select AccountID, AuditDate, SourceVersion, InvalidPrinterAudit.PrinterAuditID, InvalidPrinterAudit.AuditID, InvalidPrinterAudit.SerialNr, InvalidPrinterAudit.PageCountTotal,InvalidPrinterAudit.PageCountColor, InvalidPrinterAudit.PageCountMono
From Audit
Left OUTER JOIN InvalidPrinterAudit
ON InvalidPrinterAudit.AuditID = Audit.AuditID
AND InvalidPrinterAudit.DeviceID = 90757
This will apply that DeviceID = 90757 filter to your InvalidPrinterAudit before the join is applied, so you'll still get all of your Audit records, and then only the InvalidPrinterAudit records for that DeviceID that matches.
You are selecting less columns in the outer query than in the inner query.
If you want them to be returned in the resultset, include them in the outer:
Select AccountID, AuditDate, SourceVersion, PrinterAuditID, SerialNr
From Audit
Left join (
Select A.PrinterAuditID, A.AuditID, A.SerialNr, A.PageCountTotal,a.PageCountColor, A.PageCountMono
from InvalidPrinterAudit A
where A.DeviceID = 90757
) InvalidPrinterAudit on InvalidPrinterAudit.AuditID = Audit.AuditID
I have design one query such as
Select Temp3.*
From (
(Select temp1.alletec_ce1name,temp1.employeeid, count (temp1.alletec_mifid) AS MifAssign,alletec_mifid
from (select MIF.alletec_ce1name,User1.employeeid,MIF.alletec_mifid from Filteredalletec_mif as MIF FULL OUTER JOIN FilteredSystemUser As User1 on MIF.alletec_ce1=User1.systemuserid
FULL OUTER JOIN FilteredBusinessUnit as BU ON User1.businessunitid=BU.businessunitid
where MIF.alletec_organisationname='Konica Minolta India' AND MIF.alletec_cityname ='Delhi' AND
MIF.alletec_regionname ='North' ) as temp1
Group by temp1.alletec_mifid,temp1.alletec_ce1name,temp1.employeeid ) as Temp2 Inner Join FilteredIncident As Incident On Incident.alletec_serialnomif=Temp2.alletec_mifid ) as temp3
Now the issue is it is showing the Syntax error near the first from. What can be the possible reason for that. Thanks in advance.
I have one more query, such as
with temp2 (
alletec_ce1name,
employeeid,
alletec_mifid,
alletec_cityname,
alletec_regionname
)
as(
Select temp1.alletec_ce1name,temp1.employeeid, temp1.alletec_mifid, temp1.alletec_cityname,temp1.alletec_regionname
from (select MIF.alletec_ce1name,User1.employeeid,MIF.alletec_mifid, MIF.alletec_regionname, MIF.alletec_cityname from Filteredalletec_mif as MIF FULL OUTER JOIN FilteredSystemUser As User1 on MIF.alletec_ce1=User1.systemuserid
FULL OUTER JOIN FilteredBusinessUnit as BU ON User1.businessunitid=BU.businessunitid
where MIF.alletec_organisationname='Konica Minolta India' AND (MIF.alletec_cityname ='Delhi') AND
(MIF.alletec_regionname ='North') ) as temp1
)
select temp2.alletec_ce1name,temp2.employeeid, temp2.alletec_regionname,temp2.alletec_cityname, count(alletec_mifid) as MIFASSIGN,
Incident.alletec_casecalltypename
from temp2 Left Outer join FilteredIncident As Incident On Incident.alletec_serialnomif=temp2.alletec_mifid
group by temp2.alletec_ce1name,temp2.employeeid,temp2.alletec_mifid,temp2.alletec_regionname,temp2.alletec_cityname,
Incident.alletec_casecalltypename
now as with i got one temporary table. i wish to have one more temporary table so that can incorporate my last executable table values from temp2. do have a look if u can help for either. Thanks
Count up your parentheses, I think they are mismatched.
Your query amounts to
select
*
from
(table) as temp3
This isn't valid, you can't put the parentheses there. The extra level of nesting is broken. You either need to do something like:
select
*
from
table
or something like
select
*
from (
select
*
from
table
) as temp3
Your first query can probably be simplified to:
Select
*
From (
select
MIF.alletec_ce1name,
User1.employeeid,
MIF.alletec_mifid,
count (MIF.alltec_mifid) as MifAssign
from
Filteredalletec_mif as MIF
full outer join
FilteredSystemUser As User1
on MIF.alletec_ce1 = User1.systemuserid
full outer join
FilteredBusinessUnit as BU
on User1.businessunitid=BU.businessunitid
where
MIF.alletec_organisationname = 'Konica Minolta India' AND
MIF.alletec_cityname ='Delhi' AND
MIF.alletec_regionname ='North'
group by
MIF.alletec_mifid,
MIF.alletec_ce1name,
User1.employeeid
) as temp1
Inner Join
FilteredIncident As Incident
On Incident.alletec_serialnomif = Temp1.alletec_mifid;