this is my query
SELECT *
FROM (SELECT CurDate, YEAR(CurDate) AS orderyear, Warranty_Info
FROM eod_main where year(CurDate)>=2009 and year(CurDate)<=2011) AS D
PIVOT(SUM(Warranty_Info) FOR orderyear IN([2009],[2010],[2011])) AS P
the above query return data but CurDate return date it is is return multiple date for same month.
i want that SUM(Warranty_Info) should return only once for every month and year
output should look like
Month 2009 2010 2011 2012 2013
----- ---- ---- ---- ---- -----
1 10 0 11 32 98
2 20 10 21 11 44
3 0 224 33 77 31
some kind of problem is there in my query and that is why it is returning multiple data for same month like
please help me to have the right query. thanks
Instead of using CurDate as the full date with year, month and day, I would suggest using the Month() function to return the numeric value of each month. This will allow you to group by month instead of the full date:
SELECT dateMonth, [2009],[2010],[2011]
FROM
(
SELECT month(CurDate) dateMonth,
YEAR(CurDate) AS orderyear, Warranty_Info
FROM eod_main
where year(CurDate)>=2009
and year(CurDate)<=2011
) AS D
PIVOT
(
SUM(Warranty_Info)
FOR orderyear IN([2009],[2010],[2011])
) AS P;
See SQL Fiddle with Demo
Related
I have sample data as below:
Date
Index
26-07-2022
26
26-06-2022
23
24-07-2022
12
19-06-2022
16
26-04-2022
01
26-05-2022
10
26-07-2022
12
I want to select data of latest day from each month. For example if today's date is 26-07-2022 then I want to select all records where date is 26th.
So my output should look like below:
Date
Index
26-07-2022
26
26-06-2022
23
26-04-2022
01
26-05-2022
10
26-07-2022
12
Do anybody know how can I achieve this. Thanks.
Assuming that the data type of the column [Date] is DATE, use the function DAY():
SELECT *
FROM tablename
WHERE DAY([Date]) = DAY(GETDATE())
AND [Date] <= GETDATE(); -- you may remove this if it is not needed
See the demo.
My dataset is of the following form:
Start Date End Date Count Contact
2019-01-20 2019-05-10 50 A
2019-03-05 2019-06-07 20 A
2019-03-05 2019-06-07 20 B
....
I want a timeseries chart where the X axis is months, and the Y axis is the total count.
E.g.
The entries would be
Month TotalCount Contact
Jan 50 A
Jan 0 B
Feb 50 A
Feb 0 B
Mar 70 A
Mar 20 B
Apr 70 A
Apr 20 B
May 70 A
May 20 B
Jun 20 A
Jun 20 B
Jul 0 A
Jul 0 B
...
How can I achieve this in Data Studio? The data is coming from bigquery.
You wouldn't be able to do that in Data Studio visually alone without manipulating your dataset first.
You could instead use a custom query to generate the a date range of month starts (https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#generate_date_array) and then join all rows from your dataset where the month start or between your start date and end date.
Example code for your example
WITH CTE AS (
SELECT date('2019-01-20') start_date, date('2019-05-10') end_date, 50 count, 'A' contact
UNION ALL SELECT date('2019-03-05'), date('2019-06-07'), 20, 'A'
UNION ALL SELECT date('2019-03-05'), date('2019-06-07'), 20, 'B'
), monthDates AS (
SELECT months FROM
UNNEST(GENERATE_DATE_ARRAY('2019-01-01', '2019-07-01', INTERVAL 1 MONTH)) months
), monthContact AS (
SELECT months, contact
FROM monthDates m
CROSS JOIN CTE
GROUP BY 1,2
)
SELECT months, ifnull(sum(count),0) count, m.contact
FROM monthContact m
LEFT JOIN CTE c ON m.months BETWEEN start_date AND end_date AND m.contact = c.contact
GROUP BY 1,3
ORDER BY 1,3
I want to pull the last 24 months of data but excluding the most 2 recent quarters (current quarter and last quarter) from our netteza database. The format of date field in the database is like this: YYYY-Q-MM
2015411 = Year=2016 Quarter=4 month= 11
2013108 = Year=2013 Quarter=1 month= 8
The expected time frame should be data from October 2014 to September 2016. We are using calendar year Jan to Mar is Q1 and Apr to Jun is Q2 etc. This is the query i am using but it pulls everything for the last 27 months but i only want the last 24 months but excluding the most 2 recent Quarters.
select * from myTable where substring (month_key,1,4) || substring (month_key, 6,7) || ''01'' > CURRENT_DATE - INTERVAL ''27 months''
You can utilize the built in quarter evaluation.
The query below highlights the desired values
select add_months(to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ'),-24) as "24 months prior to 2 quarters ago"
,to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ') "2 quarters ago"
This is it in your example:
select * from myTable
where date_value between select add_months(to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ'),-24)
and to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ')
I try to get data year wise. I tried this query
select
year(SStartDate) as joinedyear ,
count(*) joined_total
from
Detail
where
client = 81
Group By
YEAR(StartDate)
union all
select
year(SEndDate) as leftyear,
count(*) left_total
from
Detail
where
client = 81
Group By
YEAR(SEndDate)
This shows correct data but this shows data like this
1900 12
2001 1
2012 3
2013 3
2016 45
1900 23
2002 34
2004 34
2015 1
2016 56
where as I want data like this
joinedyear joined_total leftyear left_total
1900 12 1900 45
2001 1 2002 34
2012 3 2004 34
2013 3 2015 1
2016 45 2016 56
Try This below query..it mat help you
select * from (
select year(SStartDate) as joinedyear ,
count(*) joined_total from Detail
where client=81
Group By YEAR(StartDate)
) as a
full outer join
(
select year(SEndDate) as leftyear , count(*) left_total from Detail
where client=81
Group By YEAR(SEndDate)
) as b
on a.joinedyear=b.leftyear
I have the below data, and I performed a ROW_NUMBER(partition by ID order by YEAR) function based on year which's ranking my data as below.
I want to bring in name for every id based on their latest year. I want to bring in NULL data if that's the only data available and bring in latest NON NULL data for every other record. But rownumber only lets me bring in recent name which could be NULL. How do I query below data to bring in most recent NON NULL name?
ID year name rownum
10 2011 abc 1
10 2010 abc 2
11 2011 ghi 1
11 2010 ghi 1
13 2010 NULL 1
13 2009 jkl 2
14 2014 NULL 1
14 2014 mno 2
15 2015 NULL 1
I want to bring in names jkl, mno for ID's 13 and 14 and not NULLS in my final result. Any suggestion on how to achieve that?
The output I desire is below - I want to display data for ROW NUM=1
10 2011 abc
11 2011 ghi
13 2009 jkl
14 2014 mno
15 2015 NULL
Sort non-null rows ahead of null rows:
select ID, year, name
from (select *,
row_number() over (partition by ID
order by case when name is null then 1 else 0 end, year desc) as RN
from #t) _
where rn = 1
See also SQL Server equivalent to Oracle's NULLS FIRST?, SQL Server ORDER BY date and nulls last &