WHERE not returning the right result [closed] - sql

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 5 months ago.
Improve this question
I'm cracking my head why the WHERE is not returning the right results
(it is not returning properly the < and > results)
could someone help please? Thank you!
SELECT
country_txt AS Country
,DATE_FROM_PARTS(iyear, imonth, iday) AS Date
,count(eventid) AS Number of events
,SUM(Col1-Col2) AS Number of men
,SUM(Col3) AS Numbe of women
,SUM(Col4) AS Number of kids
FROM xxx
WHERE iyear=('2022') AND Country in ('USA') AND eventid >= 10 AND (Col1-Col2) >= 8
GROUP BY Country, Date
ORDER BY Country ASC, Date ASC
;

Where you've put 'AND eventid >= 10 AND (Col1-Col2) >= 8' in your conditions, are you looking for cases with ten or more events and eight or more men? If so, get rid of that and stick the below under the 'group by' line:
HAVING COUNT(eventid) >= 10 AND SUM(Col1-Col2) >= 8

Related

Hot to get maximum value but all column show sql [closed]

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 5 days ago.
Improve this question
i want my code to show month, categ and max(items)
SELECT extract(month from ot.delivered_at) as month,
p.category as categ, count(*) as items
FROM bigquery-public-data.thelook_ecommerce.products p
INNER JOIN bigquery-public-data.thelook_ecommerce.order_items ot
ON p.id = ot.product_id
where status = "Complete"
and delivered_at >= "2022-01-01"
and delivered_at < "2022-10-01"
GROUP BY month, category
data that show
enter image description here
month | categ | items
1 intimate 111
etc

SQL query to get time spent on specific statuses based on single datetime column [closed]

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
I need to build a SQL query in which I can get time spent on multiple statuses (onHold,Waiting for customer,Resolved,Closed), so basically I do not want to include time spent on this statues and table looks like as below
So I need a query in which I can get actual time spent on ticket or time spent on status which I have mentioned so far I have tried below solutions and tried Cross APPLY but seems all did not help me as expected.
Tried below query so far and that gives me correct time spent on first status on-hold not after that:
SELECT t1.TICKETNUMBER,SUM(DATEDIFF(MINUTE,TICKETTIME,CloseTime)) as TotalMinutes
FROM [Admin].[TbtrnTicketHistory] t1
CROSS APPLY(SELECT TOP 1 TICKETTIME as CloseTime FROM [Admin].[TbtrnTicketHistory] t2 WHERE t1.TICKETNUMBER = t2.TICKETNUMBER and t2.TICKETHISTORYID > t1.TICKETHISTORYID ORDER BY t2.TICKETTIME) as t2
WHERE t1.CURRENTSTATUS_ANALYST not in('On-Hold','Waiting For Customer','Resolved','Closed') and t1.ticketnumber = '211135'
GROUP BY t1.TICKETNUMBER;
calculate difference between two times in two rows in sql
Calculate Time Difference Between Two Consecutive Rows
with SQL Server you can use those very usefull windowed functions LEAD and FIRST_VALUE :
select *
,[duration(sec)] = DATEDIFF(SECOND
,ticketTime
,LEAD(ticketTime,1,ticketTime)over(partition by ticketNumber order by ticketTime)
)
,[cumulative duration(sec)] = DATEDIFF( SECOND
, FIRST_VALUE(ticketTime)over(partition by ticketNumber order by ticketTime)
, ticketTime)
from (values
(1,cast('20211101 10:00:01' as datetime))
,(1,'20211101 10:00:33')
,(1,'20211101 10:01:59')
)T(ticketNumber,ticketTime)
ticketNumber
ticketTime
duration(sec)
cumulative duration(sec)
1
2021-11-01 10:00:01.000
32
0
1
2021-11-01 10:00:33.000
86
32
1
2021-11-01 10:01:59.000
0
118

SQL no such function YEAR [closed]

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
There are 2 tables
CUSTOMERS(ID, FIRSTNAME, LASTNAME, ADDRESS);
ORDERS (ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER, ID_CUSTOMER, AMOUNT);
The task is to show all orders (all fields), that have been made before year 2015 and sort the data by ID.
This my code:
SELECT * FROM ORDERS
WHERE YEAR(DATE_ORDER) < 2015
ORDER BY ID
Error when executed:
no such function YEAR
I tried to do that like this:
SELECT * FROM ORDERS
WHERE strfttime('%Y', DATE_ORDER) < 2015
ORDER BY ID
and still doesn't work
I would advise you to write the code using date logic:
where date_order < date('2015-01-01') -> "I tried this one and it worked" - author of the post
Standard SQL uses the function extract():
where extract(year from date_order) < 2015
However, the exact syntax for both depends on the database you are using.
This smells like SQLite. Assuming that your dates be in the format YYYY-MM-DD you could try:
SELECT *
FROM ORDERS
WHERE CAST(SUBSTR(DATE_ORDER, 1, 4) AS integer) < 2015
ORDER BY ID;

SQL advanced sorting [closed]

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'm facing a problem with sorting groups of data in a very old (13 years or so) SQL database program. The data looks like this:
Entry Batch Time
1 1 7-1-2013 13:35
2 1 3-3-2014 7:48
3 1 1-2-2014 18:49
4 2 3-1-2011 13:23
5 2 5-3-2014 20:48
6 2 7-2-2014 3:11
7 3 2-3-2012 15:09
8 3 5-3-2014 10:37
9 3 6-2-2014 7:16
I want to sort the entries by grouping those from the same batches together, and then sort them based on the lowest time entry in those groups. In this example, the group order would be 2-3-1 and the entry order would be 4-6-5-9-7-8-1-3-2.
Is there any easy way to do this? I tried working with order by (select blah), but no success so far. Any help would be much appreciated :)
If I am reading correctly, you want to sort on groups by the oldest date in the group first and then by time.
If this were oracle or sql server, you could use analytics for this:
select Entry,
Batch,
Time
from ( select Entry,
Batch,
Time,
MIN(Time) OVER (PARTITION BY Batch) AS MinTime
from MyTable ) MyTable2
order by MinTime, Time
If you do not have a database that supports this, you could try the following:
select MyTable.Entry,
MyTable.Batch,
MyTable.Time
from MyTable
join ( select Batch, MIN(Time) AS MinTime
from MyTable ) MyTable2 on MyTable.Batch = MyTable2.Batch
order by MyTable2.MinTime, MyTable.Time

How to get the count of rows in a column between a range in a sql table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my table I have a column (Wt%) which has the following values 5, 6, 7, 9, 1, 3, 4. I would like a script which will go through and get the count of the values in this column which fall between the range 5 and 9 inclusive.
eg. 4.
Thanks
Select COUNT(*)
FROM yourTable
WHERE columnName >=5 and columnName <=9
SELECT COUNT (*) FROM MyTable
WHERE Wt% <= 9 AND Wt% >= 5
select count(*) FROM Table WHERE Wt% >= 5 AND Wt% <= 9
SELECT COUNT(*) FROM table WHERE Wt >= 5 AND Wt <= 9