I'm creating a dashboard in SnowFlake with 2 filters as:
Supplier (:supplier)
Date (:c_date)
I would like to have a Dashboard as below:
Supplier | Selected_Date | Average_12M_Spend | Average_12M_Weight | Current_Spend | Current_Weight
The purpose is when I select a Supplier and Date from the Filters, the information in the dashboard will be changed accordingly.
However, there is an issue with my code as:
Syntax error: unexpected '='. (line 4)
syntax error line 4 at position 78 unexpected ','.
syntax error line 4 at position 79 unexpected ''1900-01-01''.
syntax error line 5 at position 41 unexpected '('.
syntax error line 5 at position 47 unexpected ','.
syntax error line 5 at position 48 unexpected ''1900-01-01''.
syntax error line 5 at position 76 unexpected '+'.
syntax error line 5 at position 80 unexpected '='.
syntax error line 5 at position 95 unexpected ')'.
syntax error line 5 at position 97 unexpected ''1900-01-01''.
syntax error line 6 at position 34 unexpected ')'.
syntax error line 13 at position 85 unexpected '='.
syntax error line 13 at position 101 unexpected ','.
syntax error line 14 at position 4 unexpected 'and'.
syntax error line 14 at position 45 unexpected '('.
syntax error line 14 at position 66 unexpected '='.
syntax error line 14 at position 94 unexpected ')'. (line 4)
My code is:
with c as(select SUPPLIERNAME, sum(PRICEEXCLUDINGGST) as CURRENT_SPEND,sum(CHARGEWEIGHT) as CURRENT_WEIGHT, to_varchar(INVOICEDATE,'YYYY') as YEAR, to_varchar(INVOICEDATE,'MM') as MONTH
from A
where SUPPLIERNAME = :supplier
and INVOICEDATE >= DateAdd(month, DateDiff(month,'1900-01-01', =:c_date),'1900-01-01')
and INVOICEDATE < DateAdd(month, DateDiff(month,'1900-01-01', DateAdd(month,+1,=:c_date)),'1900-01-01')
group by (SUPPLIERNAME,YEAR,MONTH))
select p.*, c.CURRENT_SPEND, c.CURRENT_WEIGHT, c.CURRENT_SPEND/p.AVERAGE_12M_SPEND as SPEND_VARIANCE, c.CURRENT_WEIGHT/p.AVERAGE_12M_WEIGHT as WEIGHT_VARIANCE
from
(select SUPPLIERNAME, round(avg(TOTAL_SPEND),2) as AVERAGE_12M_SPEND, round(avg(TOTAL_WEIGHT),2) as AVERAGE_12M_WEIGHT from
(select SUPPLIERNAME,sum(PRICEEXCLUDINGGST) as TOTAL_SPEND,sum(CHARGEWEIGHT) as TOTAL_WEIGHT, to_varchar(INVOICEDATE,'YYYY') as YEAR, to_varchar(INVOICEDATE,'MM') as MONTH
from A
where SUPPLIERNAME = :supplier
and INVOICEDATE >=DateAdd(month, DateDiff(month,'1900-01-01', DateAdd(month,-12,=:c_date)),'1900-01-01')
and INVOICEDATE < DateAdd(month, DateDiff(month,'1900-01-01', =:c_date),'1900-01-01')
group by (SUPPLIERNAME,YEAR,MONTH)
order by (SUPPLIERNAME,YEAR,MONTH))
group by (SUPPLIERNAME)) p left join c on p.SUPPLIERNAME = c.SUPPLIERNAME
Can you please advise what should I fix my code?
Thanks in advance.
You are using "=:c_date" instead of ":c_date". This is one error.
with c as(select SUPPLIERNAME, sum(PRICEEXCLUDINGGST) as CURRENT_SPEND,sum(CHARGEWEIGHT) as CURRENT_WEIGHT, to_varchar(INVOICEDATE,'YYYY') as YEAR, to_varchar(INVOICEDATE,'MM') as MONTH
from A
where SUPPLIERNAME = :supplier
and INVOICEDATE >= DateAdd(month, DateDiff(month,'1900-01-01', :c_date),'1900-01-01')
and INVOICEDATE < DateAdd(month, DateDiff(month,'1900-01-01', DateAdd(month,+1,:c_date)),'1900-01-01')
group by (SUPPLIERNAME,YEAR,MONTH))
select p.*, c.CURRENT_SPEND, c.CURRENT_WEIGHT, c.CURRENT_SPEND/p.AVERAGE_12M_SPEND as SPEND_VARIANCE, c.CURRENT_WEIGHT/p.AVERAGE_12M_WEIGHT as WEIGHT_VARIANCE
from
(select SUPPLIERNAME, round(avg(TOTAL_SPEND),2) as AVERAGE_12M_SPEND, round(avg(TOTAL_WEIGHT),2) as AVERAGE_12M_WEIGHT from
(select SUPPLIERNAME,sum(PRICEEXCLUDINGGST) as TOTAL_SPEND,sum(CHARGEWEIGHT) as TOTAL_WEIGHT, to_varchar(INVOICEDATE,'YYYY') as YEAR, to_varchar(INVOICEDATE,'MM') as MONTH
from A
where SUPPLIERNAME = :supplier
and INVOICEDATE >=DateAdd(month, DateDiff(month,'1900-01-01', DateAdd(month,-12,:c_date)),'1900-01-01')
and INVOICEDATE < DateAdd(month, DateDiff(month,'1900-01-01', :c_date),'1900-01-01')
group by (SUPPLIERNAME,YEAR,MONTH)
order by (SUPPLIERNAME,YEAR,MONTH))
group by (SUPPLIERNAME)) p left join c on p.SUPPLIERNAME = c.SUPPLIERNAME
Related
select
column1,
case
when column1 = 1 then select ename from emp where sal=2000
when column1 = 2 then select ename from emp where sal=3000
when column1 is null then 'NULL'
else to_number('123')
end as result from values (1), (2), (null);
SQL compilation error: parse error line 4 at position 37 near '32'.
syntax error line 4 at position 44 unexpected 'from'. syntax error
line 4 at position 67 unexpected '` '. parse error line 5 at position
37 near '32'. syntax error line 5 at position 25 unexpected 'then'.
The sub-queries should be in brackets as follows:
select
column1,
case
when column1 = 1 then (select ename from emp where sal=2000)
when column1 = 2 then (select ename from emp where sal=3000)
else to_number('123')
end as result from values (1), (2), (null);
Reference : Select inside CASE THEN
I'm not sure what I'm doing wrong in my code. I get an error message:
Msg 102, Level 15, State 1, Line 82
Incorrect syntax near '0'
Can someone help find what my error is? I'm trying to pivot the table so that the query returns Headers with the age groups instead of the rows.
SELECT *
FROM
(SELECT
ID,
CASE
WHEN Age BETWEEN 0 AND 4 THEN '0-4 Years'
WHEN Age BETWEEN 5 AND 24 THEN '5-24 Years'
WHEN Age BETWEEN 25 AND 49 THEN '25-49 Years'
WHEN Age BETWEEN 50 AND 64 THEN '50-64 Years'
WHEN Age > 64 THEN '> 64 Years'
END AS GroupAge
FROM
#AD) t
PIVOT
(COUNT(ID)
FOR GroupAge IN
(0-4 Years,5-24 Years,25-49 Years,> 64 Years)
) AS pvt
Put square brackets [] around your pivoted column names.
PIVOT
(
COUNT(ID)
FOR GroupAge IN
([0-4 Years],[5-24 Years],[25-49 Years],[50-64 Years],[> 64 Years])
) AS pvt
The error caused you to need to use square brackets in pivot columns, because of the 0-4 Years .. columns isn't normal string.
I would use condition aggregate function do pivot.
SELECT
COUNT(CASE WHEN Age BETWEEN 0 AND 4 THEN ID END) '0-4 Years',
COUNT(CASE WHEN Age BETWEEN 5 AND 24 THEN ID END) '5-24 Years' ,
COUNT(CASE WHEN Age BETWEEN 25 AND 49 THEN ID END) '25-49 Years',
COUNT(CASE WHEN Age BETWEEN 50 AND 64 THEN ID END) '50-64 Years',
COUNT(CASE WHEN Age > 64 THEN ID END) '> 64 Years'
FROM #AD
i got error in pivoting result like below :
select tanggal,[1],[2] from
(
SELECT
(CONVERT(DATE, tanggal, 103)) as tanggal,
id_jenis,
(harga * jumlah) as total
FROM
[dbo].[PNL_TP_SISA_PRODUKSI]
WHERE
YEAR (CONVERT(DATE, tanggal)) = 2016
AND MONTH (CONVERT(DATE, tanggal)) = 8
AND id_unit_pengolah = 40)
c
PIVOT (MAX(total) FOR id_jenis IN([1],[2]))
and this errors show :
[Err] 42000 - [SQL Server]Incorrect syntax near ')'.
kindly confuse because of this error
I think you need to provide alias name for pivot
select tanggal,[1],[2] from
(
SELECT
(CONVERT(DATE, tanggal, 103)) as tanggal,
id_jenis,
(harga * jumlah) as total
FROM
[dbo].[PNL_TP_SISA_PRODUKSI]
WHERE
YEAR (CONVERT(DATE, tanggal)) = 2016
AND MONTH (CONVERT(DATE, tanggal)) = 8
AND id_unit_pengolah = 40)
c
PIVOT (MAX(total) FOR id_jenis IN([1],[2])) as pvt
I want a mechanism where the the number of reported cases for the age ranges will be for a particular date. But the WHERE clause BioData.[Date] = '05/16/2016' is giving an error.
Can someone please help me to fix it.
My query is as following:
SELECT t.[Range] AS [Age Range], COUNT(*) AS [Number of Reported Cases]
FROM (
SELECT
CASE
WHEN Age BETWEEN 0 AND 6 THEN ' 0-6 '
WHEN Age BETWEEN 07 AND 17 THEN '10-19'
WHEN Age BETWEEN 18 AND 60 THEN '20-29'
ELSE '60+'
END AS [Range]
FROM BioData
) t
WHERE BioData.[Date] = '05/16/2016'
GROUP BY t.[Range]
Try reformating your query with a tool like http://www.dpriver.com/pp/sqlformat.htm
SELECT t.range AS [Age Range],
Count(*) AS [Number of Reported Cases]
FROM (SELECT CASE
WHEN age BETWEEN 0 AND 6 THEN ' 0-6 '
WHEN age BETWEEN 07 AND 17 THEN '10-19'
WHEN age BETWEEN 18 AND 60 THEN '20-29'
ELSE '60+'
END AS range
FROM biodata) t
WHERE biodata.[Date] = '05/16/2016'
GROUP BY t.range
You realize [biodata] doesnt exist because your FROM is just [t]
You probably are missing the JOIN biodata but because your question isnt clear I cant guess what else you need.
EDIT: After some digging I think you want this
SELECT t.range AS [Age Range],
Count(*) AS [Number of Reported Cases]
FROM (SELECT CASE
WHEN age BETWEEN 0 AND 6 THEN ' 0-6 '
WHEN age BETWEEN 07 AND 17 THEN '10-19'
WHEN age BETWEEN 18 AND 60 THEN '20-29'
ELSE '60+'
END AS range
FROM biodata
WHERE biodata.[Date] = '05/16/2016' ) t
-- ^^^ move where inside `t`
GROUP BY t.range
I am trying to get data for entered person, I want to pull out data as No of invoices and No of line items for particular person.
The output is
Entered_by No of line items
CD 9
CD 136084
deepa 7
deepa 18
dolly 757
dolly 22350
kroshni 666
kroshni 16161
lokesh 4
lokesh 999
MHeera 639
MHeera 20427
nandini 7
nandini 5318
Here the data in No of line items is mixing of both ’ No of line items’ count and ‘No of invoices’ count, I want to show like
Entered_by No of line items No of invoices
CD 136084 9
deepa 18 7
dolly 22350 757
Please help me with this somebody …..
Here is the T-SQL query
select ENTERED_BY, count(entered_by) 'NO OF LINE ITEMS'
from im_invoice, im_invoice_line_item, im_invoice_inventory
where invoice_rid = invoice_fk
and invoice_inventory_rid = invoice_inv_fk
and enter_date between dateadd(mm, -3, getdate()) and dateadd(mm,0,getdate())
group by entered_by
union
select entered_by, count(invoice_num) 'NO OF INVOICES' from im_invoice
where enter_date between dateadd(mm, -3, getdate()) and dateadd(mm,0,getdate())
group by entered_by
As Joe said, if you give us a more detailed description we can give you better answers, but until then, quick and dirty way to accomplish this is as follows:
Get rid of the union
Turn the 2 queries into derived tables
Select from them joining on entered_by.
Eg.
SELECT LineItems.ENTERED_BY, [NO OF LINE ITEMS], [NO OF INVOICES]
FROM
(SELECT ENTERED_BY,COUNT(entered_by) 'NO OF LINE ITEMS'
FROM im_invoice, im_invoice_line_item,im_invoice_inventory
WHERE invoice_rid = invoice_fk
AND invoice_inventory_rid = invoice_inv_fk
AND enter_date BETWEEN dateadd(mm, -3, getdate()) AND dateadd(mm,0,getdate())
GROUP BY entered_by) AS LineItems
INNER JOIN
(SELECT entered_by, count(invoice_num) 'NO OF INVOICES'
FROM im_invoice
WHERE enter_date BETWEEN dateadd(mm, -3, getdate()) AND dateadd(mm,0,getdate())
GROUP BY entered_by ) AS invoices
ON invoices.entered_by = LineItems.ENTERED_BY