I am developing one chart in that chart I want to show last six month's details separately.
I want to show the details in bar chart.
But I am not getting the any solution.
I am bit weak in sql queries.
Till now I got the data for last month.
But I want the data for last six month.
here's my code.
DECLARE #StartDate DATETIME, #EndDate DATETIME
SET #StartDate = DATEADD(mm, DATEDIFF(mm,0,getdate())-1, 0)
SET #EndDate = DATEADD(mm, 1, #StartDate)
SELECT sum(quantity)
FROM RS_Sell_Order_Master as s
left outer join RS_Sell_Order_Mapping as sm on sm.sell_order_no = s.sell_order_no
left outer join RS_GIN_Master as GIN on gin.product_id = sm.product_id
WHERE del_date BETWEEN #StartDate AND #EndDate
I want o/p of last six month like this suppose I have sell 10 quantity last month & 20 prevous to that month & I didnt sell before that So o/p should be
null
null
null
null
10
20
some thing like this.
This will assign a date exactly 6 months ago - there doesn't seem to be a reason why you would need to go back to the zero datum of SqlServer's DATETIME:
SET #StartDate = DATEADD(mm, -6, CURRENT_TIMESTAMP);
Edit
Assuming that you want month's aligned from the first to the end of each month,
and assuming because you have nulls but still want to show data that we will need to manufacture the missing months (e.g. with a recursive CTE)
DECLARE #StartDate DATETIME;
SET #StartDate = DATEADD(mm, -6, CURRENT_TIMESTAMP);
WITH cte AS
(
SELECT 0 AS TheMonth
UNION ALL
SELECT TheMonth + 1
FROM cte
WHERE TheMonth < 5
)
SELECT sum(quantity)
FROM
cte
LEFT OUTER JOIN RS_Sell_Order_Master as s
ON del_date >= DATEADD(MM, cte.TheMonth, #StartDate) AND del_date < DATEADD(MM, cte.TheMonth + 1, #StartDate)
-- Other Joins here
GROUP BY cte.TheMonth
ORDER BY cte.TheMonth;
SqlFiddle here
total no of items per month can be calculated like
select sum(items),left(date,6)+'01' from yourTable
group by left(date,6)
assuming your date is in 112 format....and it is of varchar(8) type..
Related
It is actually possible to use # (the at sign) with sqlite to be able to use a calculated value as a constant in an other query ?
I am using a variable(a total) that i calculated previously to get an other variable (a proportion) over two time periods.
Total amout of sale
Proportion of sale between the first semester and second semester.
I copy the first query to get the constant and i had the first query to the second.
The answer is no BUT:-
This could possibly be done in a single query.
Consider this simple demo with hopefully easy to understand all-in-one queries:-
First the sales table:-
i.e. 2 columns semester and amount
10 rows in total so 1000 is the total amount
6 rows are S1 (amount is 600) so 60%
4 rows are S2 (amount is 400) so 40%
Created and populated using:-
CREATE TABLE IF NOT EXISTS sales (semester TEXT, amount REAL);
INSERT INTO sales VALUES('S1',100),('S1',100),('S1',100),('S1',100),('S1',100),('S1',100),('S2',100),('S2',100),('S2',100),('S2',100);
So you could use an all-in-one query such as:-
SELECT
(SELECT sum(amount) FROM sales) AS total,
(SELECT sum(amount) FROM sales WHERE semester = 'S1') AS s1total,
((SELECT sum(amount) FROM sales WHERE semester = 'S1') / (SELECT sum(amount) FROM sales)) * 100 AS s1prop,
(SELECT sum(amount) FROM sales WHERE semester = 'S2') AS s2total,
((SELECT sum(amount) FROM sales WHERE semester = 'S2') / (SELECT sum(amount) FROM sales)) * 100 AS s2prop
;
This would result in
i.e. s1prop and s2prop the expected results (the other columns may be useful)
An alternative, using a CTE (Common Table Expressions) that does the same could be:-
WITH cte_total(total,s1total,s2total) AS (SELECT
(SELECT sum(amount) FROM sales),
(SELECT sum(amount) FROM sales WHERE semester = 'S1'),
(SELECT sum(amount) FROM sales WHERE semester = 'S2')
)
SELECT total, s1total, (s1total / total) * 100 AS s1prop, s2total, (s2total / total) * 100 AS s2prop FROM cte_total;
you can have multiple CTE's and gather data from other tables or even being passed as parameters. They can be extremely useful and would even allow values to be accessed throughout.
e.g.
Here's an example where a 2nd cte is added (as the first cte) that mimics passing 3 dates (instead of the hard coded values ?'s could be coded and the parameters passed via parameter binding).
As the sales table has no date for the sale a literal value has been coded, this would be normally be the column with the sale date instead of WHERE '2023-01-01' /*<<<<< would be the column that holds the date */
the hard coded date has purposefully been used so result in the BETWEEN clause resulting in true.
if the date column did exist then WHERE criteria for the semester could then be by between the respective dates for the semester.
The example:-
WITH
dates AS (SELECT
'2023-01-01' /*<<<<< ? and can then be passed as bound parameter*/ AS startdate,
'2023-03-01' /*<<<<< ? and can then be passed as bound parameter*/ AS semester2_start,
'2023-05-30' /*<<<<< ? and can then be passed as bound parameter*/as enddate
),
cte_total(total,s1total,s2total) AS (SELECT
(SELECT sum(amount) FROM sales
WHERE '2023-01-01' /*<<<<< would be the column that holds the date */
BETWEEN (SELECT startdate FROM dates)
AND (SELECT enddate FROM dates)),
(SELECT sum(amount) FROM sales WHERE semester = 'S1'),
(SELECT sum(amount) FROM sales WHERE semester = 'S2')
)
SELECT total, s1total, (s1total / total) * 100 AS s1prop, s2total, (s2total / total) * 100 AS s2prop FROM cte_total;
I'm doing a query to return the number of count records for a certain date.
The problem is when I use the GroupBy by a certain day, If the date have no records then the date for that day will not be shown in the output.
How can I achieve that?
I'm doing something like:
SELECT COUNT(History.Id)
FROM History
INNER JOIN User ON User.Id = History.UserId
WHERE (#StartDate = #NullDate OR History.CreatedOnDate >= #StartDate)
AND (#EndDate = #NullDate OR History.CreatedOnDate <= #EndDate)
GROUP BY History.CreatedOnDat
Example
01-08, 3 records
02-08, 2 records
04-08, 5 records
I need to have 03-08 with 0 records.
Create a temp table with one day per row:
Declare #StartDate datetime = '2016-08-01'
Declare #EndDate datetime = '2016-08-31'
declare #temp table
(
oneday datetime
);
WHILE #StartDate <= #EndDate
begin
INSERT INTO #temp VALUES (#StartDate);
SET #StartDate = Dateadd(Day,1, #StartDate);
end
select * from #temp
Then, simply join your query with this temp table.
I am working on financial domain. So I want data from April To May.
Suppose Current Month IS June then I only want April to June of this year and it should group by year and month.
And If Suppose Current Month is February then I want details from last April to Jan i.e. April-2013 to Jan-2014
I have got the solution through CTE If i only run CTE Part of the query it is working perfectly fine
This Part of query..
DECLARE #CurrentDate AS datetime = getdate();
DECLARE #FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, #CurrentDate), #CurrentDate);
DECLARE #LastApril AS datetime = DATEADD(month, -(DATEPART(month, #FirstDayInCurrentMonth) + 8) % 12, #FirstDayInCurrentMonth);
WITH Periods AS (
SELECT #LastApril AS [Period]
UNION ALL
SELECT DATEADD(month, 1, [Period])
FROM Periods
WHERE [Period] < #FirstDayInCurrentMonth
)
SELECT
DATENAME(month, [Period]),
DATEPART(year, [Period]),
FROM Periods
But I want sm.is_approved cloumn also work as above query. when I jon other table with above CTE expression. I want proper group by of sm.is_approved column.
I only have the doubt that what will be the condion for joining the other table.
Please give men the proper condition for the below bold part of the query.
DECLARE #CurrentDate AS datetime = getdate();
DECLARE #FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, #CurrentDate), #CurrentDate);
DECLARE #LastApril AS datetime = DATEADD(month, -(DATEPART(month, #FirstDayInCurrentMonth) + 8) % 12, #FirstDayInCurrentMonth);
WITH Periods AS (
SELECT #LastApril AS [Period]
UNION ALL
SELECT DATEADD(month, 1, [Period])
FROM Periods
WHERE [Period] < #FirstDayInCurrentMonth
)
SELECT
DATENAME(month, [Period]),
DATEPART(year, [Period]),
MONTH(sm.is_approved) as [MONTH],
isnull(sum(ISM.selling_price * siim.qty),0) as Price,
isnull(sum(((tm.tax_amount*(ism.selling_price * siim.qty))/100)),0) as Tax,
isnull(sum((ism.selling_price * siim.qty) + (((tm.tax_amount*(ism.selling_price * siim.qty))/100))),0) as TotalPrice
FROM Periods
**LEFT OUTER JOIN RS_Sell_Order_Master as SM on sm.is_approved >= DATEADD(MM, 1, #LastApril)**
left outer join RS_Sells_Invoice_Info_Master as SIIM on sm.sell_order_no = SIIM.sell_order_no
left outer join RS_Inventory_Selling_Master as ISM on ISM.selling_product_id = SIIM.selling_product_id
left outer join RS_Tax_Master as TM on Tm.tax_id = SIIM.tax_id
group by
DATENAME(month, [Period]),
DATEPART(year, [Period]),
MONTH(sm.is_approved)
Can anyone tell me that how to what will be the condition for above query ie bold part of the above query
ie this part I only want this condtion to be true
LEFT OUTER JOIN RS_Sell_Order_Master as SM on sm.is_approved >= DATEADD(MM, 1, #LastApril)
I tried to understand your question, but I'm not sure if I understood :
CTE retrieves all months since last April and you want to display informations linked to RS_Sell_Order_Master (Invoices, Inventory and Taxes...). This should mean, you have to display for all months since last April thoses details.
So maybe you just simply need to make match Periods CTE months and years with RS_Sell_Order_Master months and years.
So you just simply needs to do that :
FROM Periods as P
left outer join RS_Sell_Order_Master as SM
on MONTH(sm.is_approved) = MONTH(P.[Period])
and YEAR(sm.is_approved) = YEAR(P.[Period])
Here in the full code :
DECLARE #CurrentDate AS datetime = getdate();
DECLARE #FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, #CurrentDate), #CurrentDate);
DECLARE #LastApril AS datetime = DATEADD(month, -(DATEPART(month, #FirstDayInCurrentMonth) + 8) % 12, #FirstDayInCurrentMonth);
WITH Periods AS (
SELECT #LastApril AS [Period]
UNION ALL
SELECT DATEADD(month, 1, [Period])
FROM Periods
WHERE [Period] < #FirstDayInCurrentMonth
)
SELECT
DATENAME(month, [Period]),
DATEPART(year, [Period]),
MONTH(sm.is_approved) as [MONTH],
isnull(sum(ISM.selling_price * siim.qty),0) as Price,
isnull(sum(((tm.tax_amount*(ism.selling_price * siim.qty))/100)),0) as Tax,
isnull(sum((ism.selling_price * siim.qty) + (((tm.tax_amount*(ism.selling_price * siim.qty))/100))),0) as TotalPrice
FROM Periods as P
left outer join RS_Sell_Order_Master as SM
on MONTH(sm.is_approved) = MONTH(P.[Period])
and YEAR(sm.is_approved) = YEAR(P.[Period])
left outer join RS_Sells_Invoice_Info_Master as SIIM
on sm.sell_order_no = SIIM.sell_order_no
left outer join RS_Inventory_Selling_Master as ISM
on ISM.selling_product_id = SIIM.selling_product_id
left outer join RS_Tax_Master as TM
on Tm.tax_id = SIIM.tax_id
group by
DATENAME(month, [Period]),
DATEPART(year, [Period]),
MONTH(sm.is_approved)
If i'm wrong correct me, or if you have another issue just leave a comment.
I am not sure, but seeing at the tables I think your joins should be as follows:
FROM Periods
LEFT OUTER JOIN RS_Sell_Order_Master as SM on sm.is_approved >= DATEADD(MM, 1, #LastApril)
INNER join RS_Sells_Invoice_Info_Master as SIIM on sm.sell_order_no = SIIM.sell_order_no
INNER join RS_Inventory_Selling_Master as ISM on ISM.selling_product_id = SIIM.selling_product_id
INNER join RS_Tax_Master as TM on Tm.tax_id = SIIM.tax_id
And Grouping as:
GROUP BY
DATEPART(year, [Period]),
DATENAME(month, [Period]),
MONTH(sm.is_approved)
Try the code below instead of your first three lines, I basically turned the date into a Date without the time which will probably join onto your periods table properly
DECLARE #CurrentDate AS datetime = CAST(FLOOR(CAST(getdate() AS FLOAT)) AS DATETIME)
DECLARE #FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, #CurrentDate), #CurrentDate)
DECLARE #LastApril AS datetime = DATEADD(month, -(DATEPART(month, #FirstDayInCurrentMonth) + 8) % 12, #FirstDayInCurrentMonth)
first you have to compare between dates e.g. compare Feb-2014 and Apr-2014 if false the you have to subtract one from the year part of April.
You can use functions to compare and substring the date and subtract the date.
First thing, the way you generate dates, you should remove the Hour part using:
DATEADD(hour, 0, DATEDIFF(DAY, 0,#CurrentDate))
Second, try using Inner Join instead of left join.
Still I would suggest you give us better idea of how the data is stored. Possibly add the basic schema and test values in the SQL Fiddle
So I can execute the query and get better idea of what you want.
How can I select data from a table based on weekday or weekend, like
if date is a weekday then select only historical weekday data from the table &
if date is a weekend then select only historical weekend data.
I have tried to do that in this way but no luck
DECLARE #MyDate DATE = '08/17/2013'
SELECT datename(dw,#MyDate)
SELECT * FROM MyTable
WHERE
datename(dw,DateColumnInTable) IN (
CASE WHEN (datename(dw,#MyDate) IN ('Saturday','Sunday')) THEN '''Saturday'',''Sunday'''
ELSE 'Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'
END )
Any I can see lots of data in my table for saturday and sunday but this query is giving me blank record set.
Here's one way:
DECLARE #MyDate DATE = '08/17/2013'
IF (DATEPART(weekday, #MyDate) IN (1,7))
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) IN (1,7)
ELSE
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6
If you would like to do it in one clause you can do something like the following, but it may perform worse:
SELECT *
FROM MyTable
WHERE (DATEPART(weekday, #MyDate) IN (1,7) AND DATEPART(weekday, DateColumnInTable) IN (1,7))
OR (DATEPART(weekday, #MyDate) BETWEEN 2 AND 6 AND DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6)
I need some help to build SQL Query. I have table having data like:
ID Date Name
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
I need to get result something like...
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
4 1/4/2009 Null
5 1/5/2009 Null
6 1/6/2009 Null
7 1/7/2009 Null
8 1/8/2009 Null
............................
............................
............................
30 1/30/2009 Null
31 1/31/2009 Null
I want query something like..
Select * from tbl **where month(Date)=1 AND year(Date)=2010**
Above is not completed query.
I need to get all the record of particular month, even if some date missing..
I guess there must be equi Join in the query, I am trying to build this query using Equi join
Thanks
BIG EDIT
Now understand the OPs question.
Use a common table expression and a left join to get this effect.
DECLARE #FirstDay DATETIME;
-- Set start time
SELECT #FirstDay = '2009-01-01';
WITH Days AS
(
SELECT #FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, 1, #FirstDay)
)
SELECT DATEPART(d,d.CalendarDay), **t.date should be (d.CalendarDay)**, t.Name FROM Days d
LEFT JOIN tbl t
ON
d.CalendarDay = t.Date
ORDER BY
d.CalendarDay;
Left this original answer at bottom
You need DATEPART, sir.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1
If you want to choose month and year, then you can use DATEPART twice or go for a range.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1 AND DATEPART(yyyy,Date) = 2009
Range :-
SELECT * FROM tbl WHERE Date >= '2009-01-01' AND Date < '2009-02-01'
See this link for more info on DATEPART.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
You can use less or equal to.
Like so:
select * from tbl where date > '2009-01-01' and date < '2009-02-01'
However, it is unclear if you want month 1 from all years?
You can check more examples and functions on "Date and Time Functions" from MSDN
Create a temporary table containing all days of that certain month,
Do left outer join between that table and your data table on tempTable.month = #month.
now you have a big table with all days of the desired month and all the records matching the proper dates + empty records for those dates who have no data.
i hope that's what you want.