Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 22 hours ago.
Improve this question
I have consecutive rows by date as below
ID StartDate EndDate
-----------------------------
1234 19951203 19961202
1234 19961203 19971202
1234 19971203 19981202
1234 19981203 19991202
1234 19991203 20000704
1234 20020701 20021109
1234 20050907 20060906
1234 20060907 20070906
1234 20070907 20080906
1234 20080907 20080914
1234 20090119 20090307
I want to group the consecutive rows in 1 rows as below (required output)
ID StartDate EndDate
----------------------------
1234 19951203 20000704
1234 20020701 20021109
1234 20050907 20090307
Regards,
I tried lead function and row_number but didn't reach yet
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My tables:
sinvoiced
num_facture
ITEM_REF
date
1
a
2010-01-31 00:00:00.000
2
b
2011-01-31 00:00:00.000
3
c
2012-01-31 00:00:00.000
4
d
2013-01-31 00:00:00.000
itmsales
ITEM_REF
a
b
c
d
e
f
sql: display Items without any sales in 2010
I want to display items without any sale in 2010
I translated as following.
sql: display Items without any sales in 2010
I want to display item without any sale in 2010
select ITEM_REF
from itmsales
where ITEM_REF not in
(select ITEM_REF
from sinvoiced
where date between '2010-01-01 00:00:00.000' and '2010-12-31 23:59:59.999');
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I think I might know enough to do parts individually but is it possible to do it with one statement? I need to display a model count for each year it appears in.
I have the following data:
id model year
-----------------
1 45A 1992
2 45A 1992
3 45B 1992
4 45A 1996
5 45B 1996
6 33C 2000
7 33C 2000
8 45B 2000
9 45B 2010
It should come out something like:
year model count
------------------
1992 45A 2
1992 45B 1
1996 45A 1
1996 45B 1
2000 33C 2
2000 45B 1
2010 45B 1
How do I accomplish this in SQL? Is it a group by year and count the models?
Unless I'm overlooking something, you just need to GROUP BY the two columns you're interested in. I changed the name of the last column to avoid any keyword issues.
SELECT
year,
model,
count(*) as modelcount
FROM
table
GROUP BY
year,
model
ORDER BY
year;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I Have following table which contains details of Employee
EmpId EmpName Mgr Salary Dept
1 Deepak 2 20000 1
2 Annu NULL 22000 1
3 Jai 2 19500 1
4 Jitendra 1 18000 2
5 Vaishali 1 18000 2
6 Philip 4 15000 3
I wants to show salary of each dept with each employee details,if it repeats no issues as shown below
EmpId EmpName Mgr Salary Dept DeptSal
1 Deepak 2 20000 1 61500
2 Annu NULL 22000 1 61500
3 Jai 2 19500 1 61500
4 Jitendra 1 18000 2 36000
5 Vaishali 1 18000 2 36000
6 Philip 4 15000 3 15000
You should look into the SUM() OVER(PARTITION) windowing functions in SQL Server.
See this MSDN link
This link should help you in solving your problem.
If you are still compelled to get a solution rather than understanding how you can solve these type of problems, then answer is mentioned in a single line as spoiler below
select *, DeptSal=sum(Salary) over (partition by Dept ) from t
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to find # of records in next 30 days from start date for each record
I have a table:
Patid Start_date
1234 1/1/2015
1234 1/10/2015
1234 1/30/2015
1234 2/19/2015
1234 3/5/2015
1234 3/6/2015
1234 3/7/2015
I want to write a simple sql query that should give me the following result:
patid: Start_Date #of Records in Next 30 Days
1234 1/1/2015 2
1234 1/10/2015 2
1234 1/30/2015 1
1234 2/19/2015 3
1234 3/5/2015 2
1234 3/6/2015 1
1234 3/7/2015 0
Best Regards,
Sunny
In generic SQL,the easiest way is with a correlated subquery:
select t.*,
(select count(*)
from table t2
where t2.patid = t.patid and
t2.start_date > t.start_date and
t2.start_date <= t.start_date + interval '30 days'
) as Next30Days
from table t;
This uses ANSI standard syntax for the date arithmetic -- a standard mostly observed in the breach. Each database seems to have its own rules for massaging dates.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this data from a sql table, but do not know how to put into the table structure below. What is the best method for converting the data to that structure?
2011/07/13 Wednesday 10:00 1
2011/07/13 Wednesday 10:30 1
2011/07/13 Wednesday 11:00 0
...
2011/07/15 Friday 10:00 1
2011/07/15 Friday 10:30 0
2011/07/15 Friday 11:00 0
2011/07/15 Friday 11:30 1
...
2011/07/16 Saturday 09:00 0
2011/07/16 Saturday 09:30 1
2011/07/16 Saturday 10:00 1
...
2011/07/17 Sunday 10:00 1
2011/07/17 Sunday 10:30 0
2011/07/17 Sunday 11:00 0
2011/07/17 Sunday 11:30 1
...
If your RDBMS doesn't support PIVOT, you could do something like this:
SELECT
TIME,
(SELECT FLAG FROM SCHED S WHERE DATE = '7/13/2011' AND TIME = SCHED.TIME) AS [7/13/2011],
(SELECT FLAG FROM SCHED S WHERE DATE = '7/14/2011' AND TIME = SCHED.TIME) AS [7/14/2011]
... other date columns ...
FROM
SCHED
This is assuming your table has this structure:
CREATE TABLE SCHED
(
[DATE] date,
TIME char(5),
FLAG tinyint
)
And you probably want to use the date/time functions to calculate dates relative to the current date in your subqueries rather than hard-code them like I have, but you get the idea :-)
Use the pivot operator (MS-SQL)
See here:
How to transform a datatable to a ReportingService-like matrix?