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

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

Related

WHERE not returning the right result [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 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

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;

How to write a sql query to get following result from 1 table? [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 6 years ago.
Improve this question
How to write a query to get the following output:
Main Table/ source table:
Here is one method that uses union all and aggregation:
select weeknumber, sum(opened) as opened, sum(closed) as closed
from ((select OpenWeekNumber as weeknumber, 1 as Opened, 0 as Closed
from maintable
) union all
(select ClosedWeekNumber as weeknumber, 0 as Opened, 1 as Closed
from maintable
where ClosedWeekNumber > 0
)
) t
group by weeknumber
order by weeknumber;

How to compute percentage using SQL(postgresql)? [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 8 years ago.
Improve this question
Say we have a table "crime" which has 2 columns "activitydate"(timestamp without time zone) and "zipcode"(character varying(5))
eg:
table "crimes":
activity date zipcode
2014-11-22 00:52:00 12345
2014-10-22 00:52:00 12345
2014-10-24 00:52:00 12345
2014-12-22 00:52:00 54321
...
Given a start date, end date and zip code, how to compute the percentage of zip codes that have a higher number of crimes within the given period than the current zip code?
The following is a quick and nasty way to get the count of zipcodes which have a greater count then the provided zipcode. You can then use this to get the percentage. (There is probably a more efficient way to get it though)
SELECT COUNT(zipcode) as numberAbove
FROM (SELECT
zipcode
FROM
crime
WHERE
activitydate >= '<START DATE>'
AND
activitydate < '<END DATE>'
GROUP BY
zipcode
HAVING
COUNT(zipcode) > (select COUNT(zipcode)
from crime
where zipcode = '<ZIP CODE TO USE>'
and activitydate >= '<START DATE>'
and activitydate < '<END DATE>' )) subq;
sqlfiddle = http://sqlfiddle.com/#!15/9e5f3/23