I am trying to make this request. I added the group by because I had duplicates. I am getting the error:
Invalid column name for the columns in the group by.
I have read a lot of posts regarding this type of error but I am still stuck. Also I would like to know if I should use sum(). Help please.
INSERT INTO tabA
([load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd])
select 1 [load_number]
,getdate() [load_date]
,'' [no_command]
,'' [no_document]
,coalesce(c.id_numb,-1) [id_transc]
,coalesce(b.elem_cd,-1) [division_cd]
,coalesce(d.budget_cd,-1) [activity_cd]
,'' [project_cd]
from tabB b
left join tabC c on c.credit = b.account
left join tabD d on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010'
group by [load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd]
If you are concerned about duplicates, you can just use select distinct:
select distinct 1 [load_number],
getdate() [load_date],
'' [no_command],
'' [no_document],
coalesce(c.id_numb,-1) [id_transc],
coalesce(b.elem_cd,-1) [division_cd],
coalesce(d.budget_cd,-1) [activity_cd],
'' [project_cd]
from tabB b left join
tabC c
on c.credit = b.account left join
tabD d
on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010';
You can't use column aliases defined in the select in the group by in SQL Server.
Related
I have the following extraction query
select A.documentid.Docid, A.documentId.Appid, A.timestamp, A.EventStatus, D.Sequence, D.EventAppName, E.Value as Federation
from `dbo.events` A left join `dam.eventsroot` B on A.documentid.docid = B.docid left join `dbo.documentroot` C on B.rootdocid=C.rootdocid
inner join `dbo.reference_status` D
on A.DocumentID.AppID=D.EventAppID and A.EventStatus = D.EventStatus left join unnest(C.metadata) E on E.Key='Federation'
where A.timestamp > TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(),DAY)
The result is that in column FEDERATION i get different values and values of '' (nothing) and i want to change them to Null.
How can i do this?
Thank you very much
I could use this in simple way like
select A.documentid.Docid, A.documentId.Appid, A.timestamp, A.EventStatus,
D.Sequence, D.EventAppName,
Case
When LTrim(RTrim(E.Value)) = '' Then Null
Else E.Value
End AS Federation
from dbo.events A
left join dam.eventsroot B on A.documentid.docid = B.docid
left join dbo.documentroot C on B.rootdocid=C.rootdocid
inner join dbo.reference_status D on A.DocumentID.AppID=D.EventAppID and
A.EventStatus = D.EventStatus
left join unnest(C.metadata) E on E.Key='Federation'
where A.timestamp > TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(),DAY)
there are functions like NULLIF(column , '') and REPLACE(column,'',NULL) which you can them in such this cases. u can use LTRIM(RTRIM(column)) so any extra spaces will be removed.
so like:
NULLIF(LTRIM(RTRIM(federation)), '')
REPLACE(LTRIM(RTRIM(federation)),'',NULL)
and so many other ways...
You can use nullif() in any database:
select nullif(federation, '')
You might want to remove white space in general, and do:
select nullif(trim(federation), '')
Or, perhaps:
select (case when trim(federation) <> '' then federation end)
I'm trying to write a SQL query to find duplicates. What I can't manage to do is to make my query only select duplicates where one of the columns value must differ. So, I want to find all the duplicates where all the columns are the same, but one of the values must differ.
What I've got at the moment:
SELECT
a.1, underlag.1, f.1, f.2, f.3, f.4, f.5, f.6, f.7, f.8,
COUNT(*) TotalCount
FROM
f
JOIN
a ON a.Id = f.Id
JOIN
underlag ON underlag.Id = f.Id
GROUP BY
a.1, underlag.1, f.1, f.2, f.3, f.4, f.5, f.6, f.7, f.8
HAVING
COUNT(*) > 1
ORDER BY
underlag.1
The column that I want to differ is f.9 but I've no clue on how to do this. Any help or pointers in the right direction would be great!
SELECT *
FROM (
SELECT
a1 = a.[1]
, underlag1 = underlag.[1]
, f.[1], f.[2], f.[3], f.[4], f.[5], f.[6], f.[7], f.[8], f.[9]
, val = SUM(1) OVER (PARTITION BY CHECKSUM(f.[1], f.[2], f.[3], f.[4], f.[5], f.[6], f.[7], f.[8]))
FROM f
JOIN a on a.Id = f.Id
JOIN underlag on underlag.Id = f.Id
) t
WHERE t.val > 1
ORDER BY underlag1
Im trying to get the price from a supplier list into a join with the table.
But I am not able to give the Subquery a Name/shorthandel (A in this case)
I get the following message
Meldung 8155, Ebene 16, Status 2, Zeile 16
No columnname 1 was given to the column L'
This is my query
SELECT [kItem]
,[ItemID] as ebayID
,[SKU]
,A.[cArtNr]
,[StartPrice]
,[Title]
,I.[kArtikel]
,[Status]
,A.fVKNetto
,A.fVKBrutto
,L.fEKBrutto
,L.fEKNetto
FROM [dbname].[dbo].[ebay_item] I
JOIN [dbname].[dbo].[tartikel] A ON A.[kArtikel] = I.[kArtikel]
left JOIN (Select min(fEKNetto),[tArtikel_kArtikel]
FROM [dbname].[dbo].[tliefartikel] L Group by fEKNetto, [tArtikel_kArtikel])
AS L ON L.[tArtikel_kArtikel] = I.[kArtikel]
The problem isn't the alias on the subquery. It is the column min(fEKNetto). Give that an alias, which based on the rest of the code should be fEKNetto. I think this is the subquery you want:
FROM [dbname].[dbo].[ebay_item] I JOIN
[dbname].[dbo].[tartikel] A
ON A.[kArtikel] = I.[kArtikel] left JOIN
(Select min(fEKNetto) as fEKNetto, [tArtikel_kArtikel]
FROM [dbname].[dbo].[tliefartikel] L
Group by [tArtikel_kArtikel])
) L
ON L.[tArtikel_kArtikel] = I.[kArtikel]
SELECT [kItem]
,[ItemID] as ebayID
,[SKU]
,A.[cArtNr]
,[StartPrice]
,[Title]
,I.[kArtikel]
,[Status]
,A.fVKNetto
,A.fVKBrutto
,L.fEKBrutto
,L.fEKNetto
FROM [dbname].[dbo].[ebay_item] I
JOIN [dbname].[dbo].[tartikel] A ON A.[kArtikel] = I.[kArtikel]
left JOIN (Select min(fEKNetto) AS fEKNetto --<-- This Alias was missing
,[tArtikel_kArtikel]
FROM [dbname].[dbo].[tliefartikel]
Group by [tArtikel_kArtikel] --<-- only group by tArtikel_kArtikel
)AS L
ON L.[tArtikel_kArtikel] = I.[kArtikel]
Ok here's my View (vw_LiftEquip)
SELECT dbo.tbl_equip_swl_unit.unit_id,
dbo.tbl_equip_swl_unit.unit_name,
dbo.tbl_equip_swl_unit.archived,
dbo.tbl_categories.category_id,
dbo.tbl_categories.categoryName,
dbo.tbl_categories.parentCategory,
dbo.tbl_categories.sub_category,
dbo.tbl_categories.desc_category,
dbo.tbl_categories.description,
dbo.tbl_categories.miscellaneous,
dbo.tbl_categories.category_archived,
dbo.tbl_equip_swl_unit.unit_name AS Expr1,
dbo.tbl_categories.categoryName AS Expr2,
dbo.tbl_categories.description AS Expr3,
dbo.tbl_equip_depts.dept_name,
dbo.tbl_equip_man.man_name,
dbo.tbl_Lifting_Gear.e_defects AS Expr7,
dbo.tbl_Lifting_Gear.e_defects_desc AS Expr8,
dbo.tbl_Lifting_Gear.e_defects_date AS Expr9,
dbo.tbl_equipment.equipment_id,
dbo.tbl_equipment.e_contract_no,
dbo.tbl_equipment.slID,
dbo.tbl_equipment.e_entered_by,
dbo.tbl_equipment.e_serial,
dbo.tbl_equipment.e_model,
dbo.tbl_equipment.e_description,
dbo.tbl_equipment.e_location_id,
dbo.tbl_equipment.e_owner_id,
dbo.tbl_equipment.e_department_id,
dbo.tbl_equipment.e_manafacture_id,
dbo.tbl_equipment.e_manDate1,
dbo.tbl_equipment.e_manDate2,
dbo.tbl_equipment.e_manDate3,
dbo.tbl_equipment.e_dimensions,
dbo.tbl_equipment.e_test_no,
dbo.tbl_equipment.e_firstDate1,
dbo.tbl_equipment.e_firstDate2,
dbo.tbl_equipment.e_firstDate3,
dbo.tbl_equipment.e_prevDate1,
dbo.tbl_equipment.e_prevDate2,
dbo.tbl_equipment.e_prevDate3,
dbo.tbl_equipment.e_insp_frequency,
dbo.tbl_equipment.e_swl,
dbo.tbl_equipment.e_swl_unit_id,
dbo.tbl_equipment.e_swl_notes,
dbo.tbl_equipment.e_cat_id,
dbo.tbl_equipment.e_sub_id,
dbo.tbl_equipment.e_parent_id,
dbo.tbl_equipment.e_last_inspector,
dbo.tbl_equipment.e_last_company,
dbo.tbl_equipment.e_deleted AS Expr11,
dbo.tbl_equipment.e_deleted_desc AS Expr12,
dbo.tbl_equipment.e_deleted_date AS Expr13,
dbo.tbl_equipment.e_deleted_insp AS Expr14,
dbo.tbl_Lifting_Gear.e_defects_action AS Expr15,
dbo.tbl_equipment.e_rig_location,
dbo.tbl_Lifting_Gear.e_add_type AS Expr17,
dbo.tbl_Lifting_Gear.con_id,
dbo.tbl_Lifting_Gear.lifting_date,
dbo.tbl_Lifting_Gear.lifting_ref_no,
dbo.tbl_Lifting_Gear.e_id,
dbo.tbl_Lifting_Gear.inspector_id,
dbo.tbl_Lifting_Gear.lift_testCert,
dbo.tbl_Lifting_Gear.lift_rig_location,
dbo.tbl_Lifting_Gear.inspected,
dbo.tbl_Lifting_Gear.lifting_through,
dbo.tbl_Lifting_Gear.liftingNDT,
dbo.tbl_Lifting_Gear.liftingTest,
dbo.tbl_Lifting_Gear.e_defects,
dbo.tbl_Lifting_Gear.e_defects_desc,
dbo.tbl_Lifting_Gear.e_defects_date,
dbo.tbl_Lifting_Gear.e_defects_action,
dbo.tbl_Lifting_Gear.lift_department_id,
dbo.tbl_Lifting_Gear.lifting_loc
FROM dbo.tbl_equipment
INNER JOIN dbo.tbl_equip_swl_unit
ON dbo.tbl_equipment.e_swl_unit_id = dbo.tbl_equip_swl_unit.unit_id
INNER JOIN dbo.tbl_categories
ON dbo.tbl_equipment.e_cat_id = dbo.tbl_categories.category_id
INNER JOIN dbo.tbl_equip_depts
ON dbo.tbl_equipment.e_department_id = dbo.tbl_equip_depts.dept_id
INNER JOIN dbo.tbl_equip_man
ON dbo.tbl_equipment.e_manafacture_id = dbo.tbl_equip_man.man_id
INNER JOIN dbo.vwSubCategory
ON dbo.tbl_equipment.e_sub_id = dbo.vwSubCategory.category_id
INNER JOIN dbo.vwDescCategory
ON dbo.tbl_equipment.e_cat_id = dbo.vwDescCategory.category_id
INNER JOIN dbo.tbl_Lifting_Gear
ON dbo.tbl_equipment.equipment_id = dbo.tbl_Lifting_Gear.e_id
And here's the select statement with subquery that I am using:
SELECT *
FROM vw_LiftEquip
WHERE lifting_loc = ? AND
con_id = ? AND
EXPR11 =
'N'(
SELECT MAX(lifting_date) AS maxLift
FROM vw_LiftEquip
WHERE e_id = equipment_id
)
ORDER BY lifting_ref_no,
category_id,
e_swl,
e_serial
I get the error :
Column "vw_LiftEquip.category_id" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
Can't see why its returning that error, this is admittedly the first time I've ran a subquery on such a complex view, and I am a bit lost, thanks in advance for any help. I have looked through the similar posts and can find no answers to this one, sorry if I am just being dumb.
You are missing AND between EXPR11 = 'N' and (SELECT MAX(...
Otherwise, it looks OK. MAX without GROUP BY is allowed if you have no other columns in the SELECT
Update: #hvd also noted that you have nothing to compare to MAX(lifting_date). See comment
Update 2,
SELECT *
FROM vw_LiftEquip v1
CROSS JOIN
(
SELECT MAX(lifting_date) AS maxLift
FROM vw_LiftEquip
WHERE e_id = equipment_id
) v2
WHERE v1.lifting_loc = ? AND
v1.con_id = ? AND
v1.EXPR11 = 'N'
ORDER BY v1.lifting_ref_no,
v1.category_id,
v1.e_swl,
v1.e_serial
SELECT pv.PropertyID, COUNT(pv.VisitID) AS InitialVisit
FROM tblPAppointments pa INNER JOIN tblPropertyVisit pv ON pv.AppID = pa.AppID
WHERE pv.Status = 0
GROUP BY pv.PropertyID
UNION ALL
SELECT jv.PropertyID, COUNT(jv.JobVistID) AS JobVisit
FROM tblPAppointments pa INNER JOIN tblJobVisits jv ON jv.AppID = pa.AppID
WHERE jv.VisitStatus = 1
GROUP BY jv.PropertyID
I need to get InitialVisit count and JobVisit count in two separate columns.above query returns just two columns (PropertyID,InitialVisit).
Use a NULL as a placeholder for the column that there won't be any output for:
SELECT pv.PropertyID,
COUNT(pv.VisitID) AS InitialVisit,
NULL AS jobvisit
FROM tblPAppointments pa
JOIN tblPropertyVisit pv ON pv.AppID = pa.AppID
WHERE pv.Status = 0
GROUP BY pv.PropertyID
UNION ALL
SELECT jv.PropertyID,
NULL AS initialvisit,
COUNT(jv.JobVistID) AS JobVisit
FROM tblPAppointments pa
JOIN tblJobVisits jv ON jv.AppID = pa.AppID
WHERE jv.VisitStatus = 1
GROUP BY jv.PropertyID
This will return three columns. The column alias is necessary in the first query, but not in the second -- I aliased both to make it clear what is happening.
Be aware that using NULL like this in SQL Server will require you to use CAST/CONVERT on the NULL for data types other than INT because SQL Server defaults the NULL to an INT data type (as odd as that is).
An alternate query that doesn't use UNION:
SELECT x.propertyid,
COUNT(y.visitid) AS initialvisit,
COUNT(z.jobvisitid) AS jobvisit
FROM (SELECT pv.propertyid
FROM TBLPROPERTYVISIT pv
WHERE EXISTS (SELECT NULL
FROM TBLAPPOINTMENTS a
WHERE a.appid = pv.appid)
UNION
SELECT jv.propertyid
FROM TBLJOBVISIT jv
WHERE EXISTS (SELECT NULL
FROM TBLAPPOINTMENTS a
WHERE a.appid = jv.appid)) x
LEFT JOIN TBLPROPERTYVISIT y ON y.propertyid = x.propertyid
LEFT JOIN TBLJOBVISIT z ON z.propertyid = x.propertyid
GROUP BY x.propertyid
No need for a UNION at all. And you don't use tblPAppointments either
Edited to allow for no rows in one of the tables. Still one row output though
SELECT
ISNULL(pv2.PropertyID, jv2.PropertyID),
ISNULL(pv2.InitialVisit, 0),
ISNULL(jv2.JobVisit, 0)
FROM
(
SELECT pv.PropertyID, COUNT(pv.VisitID) AS InitialVisit
FROM tblPropertyVisit pv
WHERE pv.Status = 0
GROUP BY pv.PropertyID
) pv2
FULL OUTER JOIN
(
SELECT jv.PropertyID, COUNT(jv.JobVistID) AS JobVisit
FROM tblJobVisits jv
WHERE jv.VisitStatus = 1
GROUP BY jv.PropertyID
) jv2 ON pv2.PropertyID = jv2.PropertyID