cant show pivot result - sql-server

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

Related

Retrieve data 14 days prior to their "date of manufacture" irrespective of Year in SQL Server

I need all the rows that are 14 days next to my current date and that should not depend upon the Year. For example if today is 2nd of September, then the query should return all the rows of which u_dateofmanufacture is (2+14) 16th September, no matter which year it belongs. the focus is only on the date and the month.
I have also attached the screenshot and the column "date of Manufacture" is highlighted.
According to the screenshot only the rows 2,3 and 4 should be returned. They all have different year but the day and month is same(09-16).
I am using SQL Server.
declare #date DATE = dateadd(d,14,getdate())
select * from your table where month(u_dateofmanufacture)= month(#date) and day(u_dateofmanufacture) = day(#date)
You have a problem with leap years and the first 14 days of the year. If we skip these problems, you can use:
where month(datecol + 14) * 100 + day(datecol + 14) >= month(getdate()) * 100 + day(getdate()) and
month(datecol) * 100 + day(datecol) < month(getdate()) * 100 + day(getdate())
I would use a join on a virtual table containing all the possible years
SELECT t.*
FROM YourTable t
JOIN (
SELECT
starting = DATEADD(day, -14, DATEFROMPARTS(v.year, MONTH(GETDATE(), DAY(GETDATE())),
ending = DATEFROMPARTS(v.year, MONTH(GETDATE(), DAY(GETDATE()))
FROM (VALUES
(2023),(2022),(2022),(2021),(2020),(2019),(2018),(2017),(2016),(2015),(2014),(2013),(2012),(2011)
) v(year)
) v ON t.u_dateofmanufacture >= v.starting
AND t.u_dateofmanufacture < v.ending
you can use a query like below
select * from yourtable
where month(u_dateofmanufacture)= month(dateadd(d,14,getdate()))
and day(u_dateofmanufacture) =day(dateadd(d,14,getdate()))
optimally
declare #seekdate = dateadd(d,14,getdate())
select * from yourtable
where month(u_dateofmanufacture)= month(#seekdate )
and day(u_dateofmanufacture) =day(#seekdate)

Not able to fix syntactical error in sql query

I am trying to insert data from a staging table into original table for a particular time period in Microsoft SQL Server Management Studio 18 using below query.
INSERT INTO dbo.trial ([turbineid], [createddate], [lastupdatedby],[lastupdateddate])
SELECT
[turbineid], [createddate], [lastupdatedby], [lastupdateddate]
FROM
dbo.trial_January
WHERE
createddate BETWEEN (DATEPART(mm, createddate) = 01
AND DATEPART(dd, createddate) = 01))
AND (DATEPART(mm, createddate) = 01
AND DATEPART(dd, createddate) = 30)
When trying to execute this query, I get this error:
Incorrect syntax near '='.
You are trying to compare a date with some integers.
Your where clause should look more like this
WHERE createddate BETWEEN (DATEFROMPARTS(DATEPART(YY,createddate),1,1)) AND (DATEFROMPARTS(DATEPART(YY,createddate),1,30))
This claus doesn't make sense
where createddate between
(DATEPART(mm , createddate) = 01 AND DATEPART(dd, createddate) = 01))
AND (DATEPART(mm, createddate) = 01 AND DATEPART(dd, createddate) = 30)
Between state needs two date to check if the given date is between two dates.
for exemple :
SELECT 'true' WHERE GETDATE() BETWEEN GETDATE() -1 AND GETDATE() + 1
And the DATEPART(mm, CreatedDate) returns the month of your date.
SELECT 'true' where DATEPART(mm , GETDATE()) = 10 //Checks if the month of the given date is 10.
According your query, you want to get records of january. So you can try this
INSERT INTO dbo.trial ([turbineid], [createddate], [lastupdatedby],[lastupdateddate])
SELECT
[turbineid], [createddate], [lastupdatedby], [lastupdateddate]
FROM
dbo.trial_January
WHERE
DATEPART(MONTH, createddate) = 01
AND DATEPART(YEAR, createddate) = 2020 -- the year you want
your where condition is not correct
where DATEPART(mm , createddate) = 1
gives you results for first month. you dont need to have anything else for the day if your intention is to look for whole january

How to create stored procedure for alternative working Saturdays?

I want to create a stored procedure for displaying working Saturdays.
For example:
Last working Saturday 08/08/2015 (dd/mm/yyyy), then next working Saturday should be 22/08/2015 (dd/mm/yyyy)
It should omit between Saturday (15/08/2015). Likewise, I want to show for particular year
Try this. The assumption is, 3rd Jan 2015 was the first working Saturday.
DECLARE #firstsaturday date = '2015-01-03'
;WITH CTE AS
(
SELECT #firstsaturday AS StartDate
UNION ALL
SELECT DATEADD(WK,2,StartDate)
FROM CTE
WHERE YEAR(StartDate) = '2015'
)
SELECT * FROM CTE
WHERE YEAR(StartDate) = '2015'
Try something like this
DECLARE #start_sat_day DATE = Dateadd(d, -( Datepart(dw, Getdate()) % 7 ), Getdate());
WITH cte
AS (SELECT #start_sat_day AS sat_days
UNION ALL
SELECT Dateadd(dd, 14, sat_days)
FROM cte
WHERE Year(sat_days) = Year(#start_sat_day))
SELECT sat_days
FROM cte
WHERE Year(sat_days) = Year(#start_sat_day)
Result :
sat_days
---------
2015-08-08
2015-08-22
2015-09-05
2015-09-19
2015-10-03
2015-10-17
2015-10-31
2015-11-14
2015-11-28
2015-12-12
2015-12-26

limit the number of months in ssrs

I have a dataset, output of a sql query which gives me the below output :
Year Month TotalSales TotalProducts
2013 1 23233 45
2013 2 3344 43
2013 3 232 11
2013 4 2232 23
I am trying to represent the above dataset in a table and a bar graph using SSRS.
Is there a way I can limit the months to the last three months on SSRS ??
I am using the current month as a parameter in SSRS.
So example : I choose month 4 as a parameter , i would like to see the results only for the months 4,3 & 2 in the bar chart and the table
Is there a way I can do it ?
The actual query looks something like this :
SELECT ISNULL(x.[Month],z.[Month]) AS [Month],
ISNULL(x.Sum_Stores, 0) - ISNULL(y.Sum_SalesStores, 0) AS Difference , ISNULL(Sum_onetonine, 0) as EcontractsbetweenOneandNine........................
FROM
(
SELECT [Month], Sum(Stores) AS Sum_Stores
FROM SUM_XXX
WHERE [Year] = '2013' and Name = 'Pro'
GROUP BY [Month]
) AS x
FULL OUTER JOIN
(
SELECT [Month], Sum(tracts) AS Sum_SalesStores
FROM SUM_yyy
WHERE [Year] = '2013' and Name = 'Pro'
GROUP BY [Month]
) AS y ON x.[Month] = y.[Month]
............................
declare #ParamDate DateTime =null; -- Pass the required date in datetime format
-- as per your example pass #ParamDate as '20130401'
SET #ParamDate = getdate(); --setting the current date for demo purpose
SELECT ISNULL(x.[Month],z.[Month]) AS [Month],
ISNULL(x.Sum_Stores, 0) - ISNULL(y.Sum_SalesStores, 0) AS Difference , ISNULL(Sum_onetonine, 0) as EcontractsbetweenOneandNine........................
FROM
(
SELECT [Month], Sum(Stores) AS Sum_Stores
FROM SUM_XXX
WHERE ([Year] = year(#ParamDate) or [Year] = year(dateadd(m,-2,#ParamDate)) )
and ([Month] = month(#ParamDate) or [Month] = month(dateadd(m,-2,#ParamDate)) )
and Name = 'Pro'
GROUP BY [Month]
) AS x
FULL OUTER JOIN
(
SELECT [Month], Sum(tracts) AS Sum_SalesStores
FROM SUM_yyy
WHERE ([Year] = year(#ParamDate) or [Year] = year(dateadd(m,-2,#ParamDate)) )
and ([Month] = month(#ParamDate) or [Month] = month(dateadd(m,-2,#ParamDate)) )
and Name = 'Pro'
GROUP BY [Month]
) AS y ON x.[Month] = y.[Month]
............................
There are several ways to achieve this in SSRS.
Do you want to limit the results returned by the query if so use a query parameter link
Or you can apply a filter to the dataset / dataregion link
What you will essentially want to do would be (pseudo)
DataSet!Month.Value BETWEEN Parameter!Month.Value-2 AND Parameter!Month.Value
That doesn't take into considertion months 1 and 2 properly. Will update when I have a better solution then a convoluted if-else.
Edit: If you added a MonthStart field to the result set as a proper DATETIME and used MonthStart as values to the parameter then you could do
DataSet!Month.Value BETWEEN DATEADD (MONTH, -2, Parameter!Month.Value) AND Parameter!Month.Value

select between years case when statement

I wrote a query
WITH sample AS (
SELECT CAST('2010-01-01' AS DATETIME) AS d
UNION ALL
SELECT DATEADD(dd, 1, i.vrp_notificationdate) as deneme
FROM Incident i
WHERE DATEADD(dd, 1, i.vrp_notificationdate) <= CAST('2011-11-01' AS DATETIME))
SELECT count(DATENAME(mm,l.vrp_notificationdate) +' '+DATENAME(yy,l.vrp_notificationdate)) as Toplam,DATENAME(mm,l.vrp_notificationdate) +' '+DATENAME(yy,l.vrp_notificationdate)
FROM Incident as l
group by
DATENAME(mm,l.vrp_notificationdate) +' '+DATENAME(yy,l.vrp_notificationdate)
the query show
10 November 2011
101 October 2011
4 September 2011
but i want to show this .
0 january 2010
0 february 2010
-
-
-
10 November 2011
101 October 2011
4 september 2011
0 december 2011
0 february 2011
i tried case when statement,but query show same result
How to solve what i use
Best regards.
You are trying to show data that doesn't exist. The idea of this solution is to
Add dummy dates
Assign a count of 0 to each dummy date
Union your original results with this dummy dates
Group into a final result
SQL Statement
DECLARE #StartDate DATE = '01-01-2010'
;WITH Dates(d) AS (
SELECT #StartDate
UNION ALL
SELECT DATEADD(mm, 1, d) AS Date
FROM Dates
WHERE d < '12-01-2012'
)
,WITH sample AS (
SELECT CAST('2010-01-01' AS DATETIME) AS d
UNION ALL
SELECT DATEADD(dd, 1, i.vrp_notificationdate) as deneme
FROM Incident i
WHERE DATEADD(dd, 1, i.vrp_notificationdate) <= CAST('2011-11-01' AS DATETIME)
)
SELECT SUM(Toplam) AS Toplam
, DateFormat
FROM (
SELECT COUNT(DATENAME(mm,l.vrp_notificationdate) + ' ' + DATENAME(yy,l.vrp_notificationdate)) AS Toplam
, DATENAME(mm,l.vrp_notificationdate) + ' ' + DATENAME(yy,l.vrp_notificationdate) AS DateFormat
, CONVERT(VARCHAR(6), l.vrp_notificationdate, 112) as orderDate
FROM Incident as l
GROUP BY
DATENAME(mm,l.vrp_notificationdate) + ' ' + DATENAME(yy,l.vrp_notificationdate)
, CONVERT(VARCHAR(6), l.vrp_notificationdate, 112)
UNION ALL
SELECT 0 AS Toplam
, DATENAME(mm, d) + ' ' + DATENAME(yy, d)
, CONVERT(VARCHAR(6), d.d, 112) as orderDate
FROM Dates d
) g
GROUP BY
DateFormat
ORDER BY
g.orderDate
Well, if the date doesn't appear, it means that there isn't any value present for that date. So, it's impossible to get that date to appear in the result.
To pass this the simplest solution is to:
Create a table with all the possible dates and another column that will start with zero.
Update that table with the results of the query that you made (update the column that start with zero to have the count values).
Display the end results from the new table:
select countValues + ' ' + dateValues from newTable
If you have any doubt, just ask :)
Why do you provide "WITH sample AS" when you do not use sample?
I'ld use this recursive query to build all dates for your result table.
WITH sample (d) AS (
SELECT CAST('2010-01-01' AS DATETIME)
UNION ALL
SELECT DATEADD(mm, 1, d) FROM sample WHERE DATEADD(mm, 1, d) <= CAST('2011-11-01' AS DATETIME)
)
select * from sample
You could then outer join this table with your Incident table to get the count of incidents for each month.
If you want to see a lot of months or even days, you might get an MSG 530 because of too much recursion. In this case you need the add OPTION (MAXRECURSION ) after "select * from sample".

Resources