My Select Statement won't give me column from Table - sql-server-2012

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;

Related

converting a sql query without sub query

Is there a way to convert this query into one without having the subquery in where clause?
select distinct
ie.install_id
, ie.sp_id
, ie.device_config_id
from d1_sp sp
inner join install_evt ie
on ie.sp_id = sp.sp_id
where ie.install_dttm = (select max(iemax.install_dttm)
from install_evt iemax
where iemax.sp_id = sp.sp_id)
select distinct
ie.install_id
, ie.sp_id
, ie.device_config_id
, max(ie.install_dttm)
from d1_sp sp
inner join install_evt ie
on ie.sp_id = sp.sp_id
group by ie.install_id, ie.sp_id, ie.device_config_id
if you need additional grouping you can use an order by

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>%'
;

Two select statements turn into one

I have set of two queries. In first query, if is separate from second, I got good results.
First query
SELECT *
FROM
(
SELECT nks.[Id]
, nks.[IdNarudzbe]
, nks.[IdArtikla] as artikal
, nks.[IdUsluge]
, nks.[Naziv]
, nks.Kolicina
, p.Naziv as kupac
, p.Id as kupacId
, p.Adresa
, p.Telefon
, nkz.[BrojDokumenta] AS nalog
, nkz.[BrojDokumentaKroz] AS nalogKroz
, nkz.[RokIsporuke]
, nkz.[IdNastaloOdDokumenta]
, d.Naziv as drzava
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0
--...
from first query nkz.[IdNastaloOdDokumenta] is important to second
SELECT BrojDokumenta
, BrojDokumentaKroz
FROM .[dbo].[NarudzbeKupacaZaglavlje]
where id = nkz.[IdNastaloOdDokumenta]
For ex. In first query I got nkz.[IdNastaloOdDokumenta] = 20. Number 20 I use in second query in where statement, and value I get from BrojDokumenta, I would like to join to first query.
I was wondering if is possible to make one query out of these two. I think I can not union operator because number of column from these two queries don't match.
The same table, and the same columns are already in the first query. Perhaps you want a self-join, like this:
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz2
ON nkz2.Id = nkz.[IdNastaloOdDokumenta]
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0

GROUP BY syntax Error

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

Multiple Joins And Writing to Destination Table with BigQuery

I have the following query that works fine if I DON'T set a destination table.
SELECT soi.customer_id
, p.department
, p.category
, p.subcategory
, p.tier1
, p.tier2
, pc.bucket as categorization
, SUM(soi.price) as demand
, COUNT(1) as cnt
FROM store.sales_item soi
INNER JOIN datamart.product p ON (soi.product_id = p.product_id)
INNER JOIN daily_customer_fact.dcf_product_categorization pc
ON (p.department = pc.department
AND p.category = pc.category
AND p.subcategory = pc.subcategory
AND p.tier1 = pc.tier1
AND p.tier2 = pc.tier2)
WHERE DATE(soi.created_timestamp) < current_date()
GROUP EACH BY 1,2,3,4,5,6,7 LIMIT 10
However, if I set a destination table, it fails with
Error: Ambiguous field name 'app_version' in JOIN. Please use the table qualifier before field name.
That column exists on the store.sales_item table, but I'm not selecting nor joining to that column.
I've seen this error message before, and it points to the following:
Your query job when specifying a destination table is setting flattenResults to false.
Both of the store.sales_item and datamart.product tables contain a field named "app_version".
If so, I recommend looking at this answer:
https://stackoverflow.com/a/28996481/4001094
As well as this issue report: https://code.google.com/p/google-bigquery/issues/detail?id=459
In your case, you should be able to make your query succeed by doing something like the following, using suggestion #3 from the answer linked above. I'm unable to test it as I don't have access to your source tables, but it should be close to working with flattenResults set to false.
SELECT soi_and_p.customer_id
, soi_and_p.department
, soi_and_p.category
, soi_and_p.subcategory
, soi_and_p.tier1
, soi_and_p.tier2
, pc.bucket as categorization
, SUM(soi_and_p.price) as demand
, COUNT(1) as cnt
FROM
(SELECT soi.customer_id AS customer_id
, p.department AS department
, p.subcategory AS subcategory
, p.tier1 AS tier1
, p.tier2 AS tier2
, soi.price AS price
, soi.created_timestamp AS created_timestamp
FROM store.sales_item soi
INNER JOIN datamart.product p ON (soi.product_id = p.product_id)
) as soi_and_p
INNER JOIN daily_customer_fact.dcf_product_categorization pc
ON (soi_and_p.department = pc.department
AND soi_and_p.category = pc.category
AND soi_and_p.subcategory = pc.subcategory
AND soi_and_p.tier1 = pc.tier1
AND soi_and_p.tier2 = pc.tier2)
WHERE DATE(soi_and_p.created_timestamp) < current_date()
GROUP EACH BY 1,2,3,4,5,6,7 LIMIT 10