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');
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 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
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 2 years ago.
Improve this question
I have two tables:
currency_table_1
ID - currency_1
------------------
01 - EUR
02 - EUR
03 - EUR
04 - USD
05 - USD
06 - USD
currency_table_2
ID - currency_2
------------------
01 - EUR
02 - EUR
04 - JPY
05 - JPY
06 - JPY
07 - JPY
I want to FULL OUTER JOIN the two tables on the ID. In the result table I like to add a COUNT column which sums the ocurrences of unique currency combinations of the two tables. If there is no ID/currency in the other table, respectively, the currency value in that combination will show as [null]. For above example the result table would look like:
Count - currency_1 - currency_2
----------------------------------
3 - USD - JPY
2 - EUR - EUR
1 - EUR - [null]
1 - [null] - JPY
How does the sql look like? I am familiar with group by and joins, but didn't have success on that one so far.
Thanks for your input!
I think this is what you want:
select ct1.currency_1, ct2.currency_2, count(*)
from currency_table1 ct1 full join
currency_table2 ct2
on ct1.id = ct2.id
group by ct1.currency_1, ct2.currency_2;
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 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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Following is a sample data of the:-
emp doa
1 2014-01-01 00:00:00.000
1 2014-01-05 00:00:00.000
1 2014-01-08 00:00:00.000
1 2014-01-12 00:00:00.000
1 2014-01-15 00:00:00.000
2 2014-01-01 00:00:00.000
2 2014-01-05 00:00:00.000
2 2014-01-10 00:00:00.000
2 2014-01-12 00:00:00.000
2 2014-01-15 00:00:00.000
3 2014-01-01 00:00:00.000
3 2014-01-05 00:00:00.000
4 2014-01-10 00:00:00.000
4 2014-01-12 00:00:00.000
4 2014-01-15 00:00:00.000
doa - Date of assigning a project
The requirement is to select the records which is closest to today - giving higher preference to past dates - which would indicate current assignment.
The expected results are-
emp doa
1 2014-01-08 00:00:00.000
2 2014-01-05 00:00:00.000
3 2014-01-05 00:00:00.000
4 2014-01-10 00:00:00.000
There are a few restrictions which I have:-
The database (table) can't be changed - normalization is out of question.
The actual data is huge - over 6 million records - performance is must.
The actual data has some more fields (like payment per assignment, hours worked etc.) and hence grouping can't be done either.
The database is in SQL Server 2008 R2. Looking ahead for a solution.
This seems like a job for row_number(). The hard part is getting the order by clause correct. I think the following encapsulates the logic you are looking for:
select ed.*
from (select ed.*,
row_number() over (partition by emp
order by (case when doa <= getdate() then 0 else 1 end),
abs(datediff(day, doa, getdate()))
) as seqnum
from empdoa ed
) ed
where seqnum = 1;