GROUP BY syntax Error - sql

SELECT
SinifNo, OgrNo, Kalan, Ad, Soyad, MAX(VadeGunu)
FROM
(SELECT
o.SinifNo, a.OgrNo, o.Ad, o.Soyad, o.Sezon, o.SubeKod,
o.TCNo, o.AnneAd, o.AnneMobil, o.BabaAd, o.BabaMobil,
a.Tarih, a.Tutar, a.Odenen, a.Kalan,
DATEDIFF(DAY, a.Tarih, (getdate() + 1)) AS VadeGunu
FROM
TblTahPlan a
JOIN
TblOgrenci o ON a.OgrNo = o.OgrNo)
GROUP BY
SinifNo, OgrNo, Kalan, Ad, Soyad
It is my SQL query. But I'm getting this error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'group'.
What can I do to solve this?

If you format your query, it becomes more easy to read and more easy to spot the mistake:
select SinifNo
, OgrNo
, Kalan
, Ad
, Soyad
, MAX(VadeGunu)
FROM (
SELECT o.SinifNo,a.OgrNo,o.Ad,o.Soyad,o.Sezon,o.SubeKod,o.TCNo,o.AnneAd,o.AnneMobil,o.BabaAd,o.BabaMobil,a.Tarih,a.Tutar,a.Odenen,a.Kalan
, DATEDIFF(DAY,a.Tarih,(getdate()+1)) as VadeGunu
FROM TblTahPlan AS a
JOIN TblOgrenci AS o
ON a.OgrNo=o.OgrNo
) AS T1
GROUP by SinifNo,OgrNo, Kalan,Ad,Soyad
You were missing an alias for your subquery, and a space before the group by. Why are you selecting all those attributes in your subquery? Why do you use a subquery at all? You could just as easily do:
SELECT o.SinifNo
, a.OgrNo
, a.Kalan
, o.Ad
, o.Soyad
, MAX(DATEDIFF(DAY,a.Tarih,(getdate()+1))) as VadeGunu
FROM TblTahPlan a
JOIN TblOgrenci o
ON a.OgrNo=o.OgrNo
GROUP by o.SinifNo
, a.OgrNo
, a.Kalan
, o.Ad
, o.Soyad

Change to the code
join TblOgrenci o on a.OgrNo=o.OgrNo) as tbl group by SinifNo,OgrNo,

Dont write code for yourself. Have proper alignment and formatting in place.
Change your query as below. You have to give alias to your inner query.
select
SinifNo,
OgrNo,
Kalan,
Ad,
Soyad,
max(VadeGunu)
from (select
o.SinifNo,
a.OgrNo,
o.Ad,
o.Soyad,
o.Sezon,
o.SubeKod,
o.TCNo,
o.AnneAd,
o.AnneMobil,
o.BabaAd,
o.BabaMobil,
a.Tarih,
a.Tutar,
a.Odenen,
a.Kalan,
DATEDIFF(DAY,a.Tarih,(getdate()+1)) as VadeGunu
from TblTahPlan a
join TblOgrenci o on a.OgrNo=o.OgrNo) as tblA
group by SinifNo, OgrNo, Kalan, Ad, Soyad

you can use this way
SELECT SinifNo,OgrNo, Kalan,Ad,Soyad, max(VadeGunu)
FROM
(SELECT o.SinifNo As SinifNo
, a.OgrNo As OgrNo
, o.Ad As Ad
, o.Soyad As Soyad
, o.Sezon As Sezon
, o.SubeKod As SubeKod
, o.TCNo As TCNo
, o.AnneAd As AnneAd
, o.AnneMobil As AnneMobil
, o.BabaAd As BabaAd
, o.BabaMobil As BabaMobil
, a.Tarih As Tarih
, a.Tutar As Tutar
, a.Odenen As Odenen
, a.Kalan As Kalan
, DATEDIFF(DAY,a.Tarih,(getdate()+1)) AS VadeGunu
FROM TblTahPlan a
Inner Join
TblOgrenci o
On a.OgrNo = o.OgrNo ) c
GROUP BY c.SinifNo
, c.OgrNo
, c.Kalan
, c.Ad
, c.Soyad

Related

Join 2 oracle queries

someone, please help to join the below queries.
I have tried my best but not able to join with the condition.
PLN_ID is the common column on both the tables.
Query 1-
SELECT PLN_ID
, ASSORTMENT_GROUP
, STORE
, PLANOGRAM
, STATUS
FROM ACN_PLANOGRAMS
WHERE PLANOGRAM not like '%<Untitled>%'
;
​
Query 2
SELECT distinct(PLN_ID)
, count(*)
, (sum(WIDTH)) AS width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1
;
Please changes join what you want you. Try this query:
SELECT
DISTINCT(a.PLN_ID),
(SUM(a.WIDTH)) AS width,
b.PLN_ID,
b.ASSORTMENT_GROUP,
b.STORE,
b.PLANOGRAM,
b.STATUS
FROM
ACN_FIXEL a
INNER JOIN
ACN_PLANOGRAMS b ON a.PLN_ID = b.PLN_ID
WHERE
a.type = '0'
AND b.PLANOGRAM NOT LIKE '%<Untitled>%'
GROUP BY
a.PLN_ID,
b.PLN_ID,
b.ASSORTMENT_GROUP,
b.STORE,
b.PLANOGRAM,
b.STATUS
HAVING
COUNT(*) > 1
There are several ways to solve this. Without understanding your data model or business logic I offer the simplest solution, a derived table (inline view):
SELECT p.PLN_ID
, p.ASSORTMENT_GROUP
, p.STORE
, p.PLANOGRAM
, p.STATUS
, f.fixel_count
, f.fixel_width
FROM ACN_PLANOGRAMS p
inner join (SELECT PLN_ID
, count(*) as fixel_count
, (sum(WIDTH)) AS fixel_width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1 ) f
on f.pln_id = p.pln_id
WHERE p.PLANOGRAM not like '%<Untitled>%'
;
This solution only returns results for PLN_ID in both result sets. If you have a different logic you may need to use LEFT OUTER JOIN instead.
Make Query 2 a subquery:
SELECT ap.PLN_ID
, ap.ASSORTMENT_GROUP
, ap.STORE
, ap.PLANOGRAM
, ap.STATUS
, sq.cnt
, sq.width
FROM ACN_PLANOGRAMS ap
JOIN (
SELECT PLN_ID
, count(*) AS cnt
, sum(WIDTH) AS width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1
) sq
ON ( sq.PLN_ID = ap.PLN_ID )
WHERE ap.PLANOGRAM not like '%<Untitled>%'
;

Column Ambiguously Defined - No Line / Column Number

I've been trying to create a query that joins two sets of tables together using several columns. I have tried assigning aliases to each column but I am still receiving a (ORA-00918: column ambiguously defined) error with no line number / direction on which column oracle has an issue with. Can someone please help me? Been stuck on this issue for hours now. My code is below, thanks in advance!
SELECT *
FROM
(select v.value_id as i_value_id
, v.value_nb as i_value_nb
, v.utc_offset as i_utc_offset
, v.data_Date as i_data_date
, v.hr_utc as i_hr_utc
, v.utc_offset as i_utc_offset
, v.hr as i_hr
, v.hr_num as i_hr_num
, v.data_Code as i_data_code
, v.Code as i_code
, ff.form_Field_tx as i_form_field_tx
, sv.submission_value_id as i_submission_value_id
, s.submission_name_tx as i_submission_name_tx
, s.submission_id as i_submission_id
, fl.form_line_tx as i_form_line_tx
, fs.form_section_tx as i_form_section_tx
, sf.form_name_tx as i_form_name_tx
, sf.form_label_tx as i_form_label_tx
, sf.form_number_tx as i_form_number_tx
, sf.survey_id as i_survey_id
from value v
join sub_value sv on v.value_id = sv.value_id
join form_Field ff on sv.form_Field_id = ff.form_Field_id
join sub s on sv.submission_id = s.submission_id
LEFT OUTER JOIN form_line fl ON ff.form_line_id = fl.form_line_id
LEFT OUTER JOIN form_section fs ON fl.form_section_id = fs.form_section_id
LEFT OUTER JOIN survey_form sf ON fs.survey_form_id = sf.survey_form_id) subq1
JOIN
(select va.value_id as o_value_Id
, va.value_nb as o_value_nb
, va.utc_offset as o_utc_offset
, va.data_Date as o_data_date
, va.hr_utc as o_hr_utc
, va.hr as o_hr
, va.hr_num as o_hr_num
, va.data_Code as o_data_Code
, va.balancing_Authority_Code as o_balancing_authority_code
, ff2.form_Field_tx as o_form_field_tx
, sva.submission_value_id as o_submission_value_id
, s.submission_id as o_submission_id
, fl.form_line_tx as o_form_line_tx
, fs.form_section_tx as o_form_section_tx
, sf.form_name_tx as o_form_name_tx
, sf.form_label_tx as o_form_label_tx
, sf.form_number_tx as o_form_number_tx
, sf.survey_id as o_survey_id
from submission s
join submission_value_audit sva on s.submission_id = sva.submission_id
join value_audit va on sva.value_id = va.value_id
join form_Field ff2 on sva.form_Field_id = ff2.form_Field_id
LEFT OUTER JOIN form_line fl ON ff2.form_line_id = fl.form_line_id
LEFT OUTER JOIN form_section fs ON fl.form_section_id = fs.form_section_id
LEFT OUTER JOIN survey_form sf ON fs.survey_form_id = sf.survey_form_id) subq2
on subq1.sub_id = subq2.sub_id
on subq1.code = subq2.code;
looks like you have a duplicate definition of i_utc_offset (see code snippet below)
(select v.value_id as i_value_id
, v.value_nb as i_value_nb
, v.utc_offset as i_utc_offset
, v.data_Date as i_data_date
, v.hr_utc as i_hr_utc
, v.utc_offset as i_utc_offset
ORA-00918: column ambiguously defined: You tried to execute a SQL statement that joined two or more tables, where a column with the same name exists in both tables.
Reference: https://www.techonthenet.com/oracle/errors/ora00918.php
The duplicate column name is below.
SELECT *
FROM
(
select ...
, v.utc_offset as i_utc_offset
...
, v.utc_offset as i_utc_offset

How do I change this sql statement to only select the first from each ID?

I have the below code to select from a database, however, I only want the first record for each unique ID. Is there a way to change the SQL to achieve this?
SELECT
[CARL_Property].ID
,[PrDoorNum]
,[PrAddress1]
,[PrAddress2]
,[PrAddress3]
,[PrAddress4]
,[PrPostcode]
,[PrRent]
,[PrAgreedRent]
,[PrCommence]
,[PrEnd]
,[PrAvailable]
,[PrGrossIncome]
,[PrCouncilTax]
,[PrInventoryFee]
,[PrLetFee]
,[PrReletFee]
,[PrDateWithdrawn]
,[Rent Review]
,CARL_Owners.OwForenames
,CARL_Owners.OwSurname
,CARL_Property_List.[ID]
,CARL_Property_List.[PrId]
,CARL_Property_List.[PLBedrooms]
,CARL_Property_List.[PlRooms]
,CARL_Property_List.[PlBathrooms]
,CARL_Property_List.[PlReceptions]
,CARL_Property_List.[PlDeposit]
,CARL_Tenant_Contacts.[Tenant Name]
,CARL_New_Tenants.[TnLeaseperiod]
,CARL_Property_List.[PlAdvertising]
,[CARL_Property_Memos].[PrNotes]
,[CARL_Safety].[PrGasInsp]
from dbo.CARL_Property Join dbo.[CARL_Property_Memos] on CARL_Property.ID=CARL_Property_Memos.PrID Join dbo.CARL_Owners on CARL_Owners.ID=CARL_Property.OwID Join dbo.CARL_PROPERTY_LIST ON dbo.CARL_PROPERTY.ID=dbo.CARL_PROPERTY_LIST.PrId Join dbo.[CARL_New_Tenants] ON CARL_New_Tenants.PrId=CARL_Property.ID JOIN CARL_Tenant_Contacts ON CARL_New_Tenants.ID = CARL_Tenant_Contacts.TnID Join [dbo].[CARL_Safety] On dbo.CARL_Property.ID=dbo.CARL_Safety.PrID
The result is as seen below.
Something along these lines I think is what you are looking for. Also, notice that I used aliases in your main query. It makes this a lot simpler to work with and reduces the amount of typing by a LOT.
with SortedResults as
(
SELECT
cp.ID
, ROW_NUMBER() over(partition by cp.ID order by pl.ID) as RowNum --order by whatever column defines "first"
, [PrDoorNum]
, [PrAddress1]
, [PrAddress2]
, [PrAddress3]
, [PrAddress4]
, [PrPostcode]
, [PrRent]
, [PrAgreedRent]
, [PrCommence]
, [PrEnd]
, [PrAvailable]
, [PrGrossIncome]
, [PrCouncilTax]
, [PrInventoryFee]
, [PrLetFee]
, [PrReletFee]
, [PrDateWithdrawn]
, [Rent Review]
, o.OwForenames
, o.OwSurname
, pl.[ID] as PL_ID
, pl.[PrId]
, pl.[PLBedrooms]
, pl.[PlRooms]
, pl.[PlBathrooms]
, pl.[PlReceptions]
, pl.[PlDeposit]
, tc.[Tenant Name]
, nt.[TnLeaseperiod]
, pl.[PlAdvertising]
, pm.[PrNotes]
, cs.[PrGasInsp]
from dbo.CARL_Property p
Join dbo.[CARL_Property_Memos] pm on p.ID = pm.PrID
Join dbo.CARL_Owners o on o.ID = p.OwID
Join dbo.CARL_PROPERTY_LIST pl ON p.ID = pl.PrId
Join dbo.[CARL_New_Tenants] nt ON nt.PrId = p.ID
JOIN CARL_Tenant_Contacts tc ON nt.ID = tc.TnID
Join [dbo].[CARL_Safety] cs On p.ID = cs.PrID
)
select *
from SortedResults
where RowNum = 1
order by ID

My Select Statement won't give me column from Table

what is wrong with this query? I do have column called percet with tinyint.
please help
SELECT LC.ID , LC.DESCRIPTION , LC.CHANGEDBYID , LP.PERCENT , LP.ADDEDBYID , LP.DATEADDED FROM LIKELIHOODTYPECODE LC JOIN LIKELIHOODPERCENT LP ON LC.ID = LP.ID;
If I do Select * from likelihoodpercent then it will show all my column with percent, but when I do select percent from likelihoodpercent then it give me error.
Thanks
PERCENT is a SQL Server keyword. To use it in a query, put square brackets around it:
SELECT LC.ID , LC.DESCRIPTION , LC.CHANGEDBYID , LP.[PERCENT] , LP.ADDEDBYID , LP.DATEADDED FROM LIKELIHOODTYPECODE LC JOIN LIKELIHOODPERCENT LP ON LC.ID = LP.ID;

SQL Query to get only 1 instance of a record where one to many relationship exists

Ive got an SQL Query trying to get 1 record back when a 1 to many relationship exists.
SELECT dbo.BlogEntries.ID AS blog_entries_id, dbo.BlogEntries.BlogTitle, dbo.BlogEntries.BlogEntry, dbo.BlogEntries.BlogName,
dbo.BlogEntries.DateCreated AS blog_entries_datecreated, dbo.BlogEntries.inActive AS blog_entries_in_active,
dbo.BlogEntries.HtmlMetaDescription AS blog_entries_html_meta_description, dbo.BlogEntries.HtmlMetaKeywords AS blog_entries_html_meta_keywords,
dbo.BlogEntries.image1, dbo.BlogEntries.image2, dbo.BlogEntries.image3, dbo.BlogEntries.formSelector, dbo.BlogEntries.image1Alignment,
dbo.BlogEntries.image2Alignment, dbo.BlogEntries.image3Alignment, dbo.BlogEntries.blogEntryDisplayName, dbo.BlogEntries.published AS blog_entries_published,
dbo.BlogEntries.entered_by, dbo.BlogEntries.dateApproved, dbo.BlogEntries.approved_by, dbo.blog_entry_tracking.id AS blog_entry_tracking_id,
dbo.blog_entry_tracking.blog, dbo.blog_entry_tracking.blog_entry, dbo.BlogCategories.ID, dbo.BlogCategories.BlogCategoryName,
dbo.BlogCategories.BlogCategoryComments, dbo.BlogCategories.DateCreated, dbo.BlogCategories.BlogCategoryTitle, dbo.BlogCategories.BlogCategoryTemplate,
dbo.BlogCategories.inActive, dbo.BlogCategories.HtmlMetaDescription, dbo.BlogCategories.HtmlMetaKeywords, dbo.BlogCategories.entry_sort_order,
dbo.BlogCategories.per_page, dbo.BlogCategories.shorten_page_content, dbo.BlogCategories.BlogCategoryDisplayName, dbo.BlogCategories.published,
dbo.BlogCategories.blogParent
FROM dbo.BlogEntries LEFT OUTER JOIN
dbo.blog_entry_tracking ON dbo.BlogEntries.ID = dbo.blog_entry_tracking.blog_entry LEFT OUTER JOIN
dbo.BlogCategories ON dbo.blog_entry_tracking.blog = dbo.BlogCategories.ID
i have some records assigned to 2 different blogcategories, and when i query everything it returns duplicate records.
How do i only return 1 instance of a blog?
Try this one -
SELECT blog_entries_id = be.Id
, be.BlogTitle
, be.BlogEntry
, be.BlogName
, blog_entries_datecreated = be.DateCreated
, blog_entries_in_active = be.inActive
, blog_entries_html_meta_description = be.HtmlMetaDescription
, blog_entries_html_meta_keywords = be.HtmlMetaKeywords
, be.image1
, be.image2
, be.image3
, be.formSelector
, be.image1Alignment
, be.image2Alignment
, be.image3Alignment
, be.blogEntryDisplayName
, blog_entries_published = be.published
, be.entered_by
, be.dateApproved
, be.approved_by
, blog_entry_tracking_id = bet.Id
, bet.blog
, bet.blog_entry
, bc2.Id
, bc2.BlogCategoryName
, bc2.BlogCategoryComments
, bc2.DateCreated
, bc2.BlogCategoryTitle
, bc2.BlogCategoryTemplate
, bc2.inActive
, bc2.HtmlMetaDescription
, bc2.HtmlMetaKeywords
, bc2.entry_sort_order
, bc2.per_page
, bc2.shorten_page_content
, bc2.BlogCategoryDisplayName
, bc2.published
, bc2.blogParent
FROM dbo.BlogEntries be
LEFT JOIN dbo.blog_entry_tracking bet ON be.Id = bet.blog_entry
OUTER APPLY (
SELECT TOP 1 *
FROM dbo.BlogCategories bc
WHERE bet.blog = bc.Id
) bc2
Also, I would like to mention that in this case, using of aliases in the column names decreases the size of your query and makes it more convenient for understanding.
if you just need one record back, you can use
SELECT TOP 1 dbo.BlogEntries.ID AS blog_entries_id, dbo.Bl.... (same as you have now).
it is more efficient than SELECT DISTINCT
Here is a Northwind Example.
It will return only 1 row in the Order Detail table for each Order.
Use Northwind
GO
Select COUNT(*) from dbo.Orders
select COUNT(*) from dbo.[Order Details]
select * from dbo.Orders ord
join
(select ROW_NUMBER() OVER(PARTITION BY OrderID ORDER BY UnitPrice DESC) AS "MyRowID" , * from dbo.[Order Details] innerOD) derived1
on ord.OrderID = derived1.OrderID
Where
derived1.MyRowID = 1
Order by ord.OrderID