I run a Bridal Shop. I have made a table, where we insert all the dresses which are tried on by the brides. I have the fields, branch, model, date and ordered. If a dress will be ordered, then ordered will be set to 1. I have also the field "status" which will be set to 2 if the dress will be deleted from this table.
I have this statement to show year, month, branch, model and the quantity each model has been tried on:
SELECT count(*) as Anzahl, MONTHNAME( date ) AS Monat, YEAR( date ) AS Jahr,
branch as Branch FROM wccrm_anprobe where status = 1 GROUP BY MONTHNAME(date),
YEAR(date), branch ORDER BY date,branch ASC
What can I do to achieve the following: I want to count the field "ordered" and put it to my select? I want to see, how many dresses have been sold per year, month and branch.
I tried:
SELECT count(*) as Anzahl, MONTHNAME( date ) AS Monat, YEAR( date ) AS Jahr,
branch as Branch, ordered as Ordered FROM wccrm_anprobe where status = 1 GROUP
BY MONTHNAME(date),
YEAR(date), branch, ordered ORDER BY date,branch ASC
but I do not get the correct results as I think the ordered field will displayed once - it will not be count.
So for example, last month I sold 6 dresses, but the statement above just shows 3 dresses.
Thanks for any suggestions.
Best Regards,
Stefan
You can use conditional aggregation, in your case it should look something like this:
COUNT(CASE `ordered` WHEN 1 THEN 1 ELSE NULL END) AS numberOrdered
COUNT, like most aggregate functions, ignores NULL values. Alternatively, you could use SUM
SUM(CASE `ordered` WHEN 1 THEN 1 ELSE 0 END) AS numberOrdered
Related
My table is like this:
root_tstamp
userId
2022-01-26T00:13:24.725+00:00
d2212
2022-01-26T00:13:24.669+00:00
ad323
2022-01-26T00:13:24.629+00:00
adfae
2022-01-26T00:13:24.573+00:00
adfa3
2022-01-26T00:13:24.552+00:00
adfef
...
...
2021-01-26T00:12:24.725+00:00
d2212
2021-01-26T00:15:24.669+00:00
daddfe
2021-01-26T00:14:24.629+00:00
adfda
2021-01-26T00:12:24.573+00:00
466eff
2021-01-26T00:12:24.552+00:00
adfafe
I want to get the number of users in the current year and in previous year like below using SQL.
Date Users previous_year
2022-01-01 10 5
2022-01-02 20 15
The code is written as follows.
select CAST(root_tstamp as DATE) as Date,
count(DISTINCT userid) as users,
count(Distinct case when CAST(root_tstamp as DATE) = dateadd(MONTH,-12,CAST(root_tstamp as DATE)) then userid end) as previous_year
FROM table1
But it returns 0 for previous_year values.
How can I fix that?
Possible solution for SQL Server:
WITH cte AS ( SELECT 2022 [year]
UNION ALL
SELECT 2021 )
SELECT cte.[year],
COUNT(DISTINCT test.userId) current_users_amount,
COUNT(DISTINCT CASE WHEN YEAR(test.root_tstamp) < cte.[year]
THEN test.userId
END) previous_users_amount
FROM test
JOIN cte ON YEAR(test.root_tstamp) <= cte.[year]
GROUP BY cte.[year]
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=88b78aad9acd965bdbac4c85a0b81927
This query (for MySql) returns unique number of userids where the root_timestamp is in the current year, by day, and the number of unique userids for the same day last year. If there is no record for a day in the current year nothing will be displayed for that day. If there are rows for the current year, but no rows for the same day last year, then NULL will be shown for that lastyear column.
SELECT cast(ty.root_tstamp as date) as Dte,
COUNT(DISTINCT ty.userId) as users_this_day,
count(distinct lysd.userid) as users_sameday_lastyear
FROM test ty
left join
test lysd
on cast(lysd.root_tstamp as date)=date_add(cast(ty.root_tstamp as date), interval -1 year)
WHERE YEAR(ty.root_tstamp) = year(current_date())
GROUP BY Dte
If you wish to show output rows for calendar days even if there are no rows in current year and/or last year, then you also need a calendar table to be introduced (let's hope that it is not what you need)
I am doing the data cleaning in a BigQuery. I managed to count each of the variables. However there are some redundant info with different variables, so I need to merge the number and save the overall total in a single row.
This is my work:
SELECT
day,
COUNT(*) as Total,
FROM
table
where day<> 'null'
GROUP BY day
-- HAVING COUNT(*) >= 10?
ORDER BY COUNT(*) DESC;
The result is
What should I add so the Monday and Mndy is added in a single row? Thanks
SELECT
CASE WHEN day in ('Monday', 'Mndy') THEN 'Monday' ELSE day END day,
COUNT(*) as Total,
FROM
table
where day<> 'null'
GROUP BY 1
-- HAVING COUNT(*) >= 10?
ORDER BY COUNT(*) DESC;
I have a SQL/Java code issue. The basic overlay is as follows: a MySQL database with a table. In this table there are multiple columns. One column consists of names. An associated column is months. In the third column there is counts. So a sample table would be
john - january - 5
john - january - 6
mary - january - 5
Alex - February- 5
John - February - 6
John - February - 4
Mary - February - 3
John - march - 4
The table continues to month May.
So John appears in five months, Mary in 3, and Alex in one. Currently, my SQL query somewhat looks like this.
select name, sum(count)/4
from table where (category ='something'
AND month not like 'May') group by name;
Basically, what this query is supposed to do is just display each name with the average counts per month. Hence, the sum will be divided by four (because I exclude May, so it must divide Jan-April/4). However, the issue is that some names only appear in one month (or two or three).
This means for that name, the sum of the counts would only be divided by that specific number, to get the average counts over the months. How would I go about this? I feel as if this will require some if statement in a where clause. Kind of like where if the count of the distinct (because months may repeat) is a certain number, then divide the sum(count) by that number for each name?
Also, I think it may not be a where if clause issue. I've read some forums where possibly some use of case could be utilized?
If you need average per month, you can GROUP BY name and month and use AVG function:
SELECT `name`, `month`, avg(`count`)
FROM table
WHERE `category` ='something' AND `month` NOT LIKE 'May'
GROUP BY `name`, `month`;
If you need average for all period, just GROUP BY name and AVG count:
SELECT `name`, avg(`count`)
FROM table
WHERE `category` ='something' AND `month` NOT LIKE 'May'
GROUP BY `name`;
And another option, if you don't like AVG:
SELECT `name`, sum(`count`)/(SELECT count(*) FROM `table` AS `t2` WHERE `category` ='something' AND `month` NOT LIKE 'May' and `t1`.`name` = `t2`.`name`)
FROM `table` AS `t1`
WHERE `category` ='something' AND `month` NOT LIKE 'May')
GROUP BY name;
But I would stay with AVG.
Actually, i prefer to use != instead of NOT LIKE it's improves readability
Just for completness sake here is a WORKING FIDDLE. using the AVG function is the way to go as it will do the average per person per month. look at John in January.. his result is 5.5 when the count (in january) is 5 and 6.. average = 5.5.
SELECT
person,
month,
avg(counter)
FROM testing
where
(
category ='something'
AND month <> 'May'
)
GROUP BY person, month;
If you want to see the data in one like as it sounds like that from your post then you can do this. ANOTHER FIDDLE
SELECT
person,
group_concat(month),
group_concat(average_count)
FROM(
SELECT
person,
month,
avg(counter) as average_count
FROM testing
where
(
category ='something'
AND month <> 'May'
)
GROUP BY person, month
) as t
group by person;
Try this :
SELECT name, SUM(count) / COUNT(DISTINCT month)
FROM table
WHERE month != 'May'
AND category = 'something'
GROUP BY name
I have a table with emp_id, income, etc.
I wish to get number of records for a query like
select * from table_name where income <= 500;
There will be at least 3 such income groups - which will b given at report generation time.
Further I wish to get all 3 Counts - and group the results by the count of their respective income group - all this in a single query.
What is the easiest way to do this?
Can you try this ,if this doesn't suite your need you may need to write a custom stored procedure
SELECT
sum((income <= 500)) as range1,
sum((income <= 1000)) as range2
FROM table_name
sample fiddle
You can use a CASE expression to create your categories, and then a GROUP BY to summarize the categories.
SELECT COUNT(*) AS num,
CASE WHEN income IS NULL THEN 'missing'
WHEN income <= 0 THEN '0'
WHEN income <= 500 THEN '1 - 500'
WHEN income <= 1000 THEN '501-1000'
ELSE '> 1000'
END AS category
FROM table_name
GROUP BY category WITH ROLLUP
Including the WITH ROLLUP clause will give you an overall count as well as the count of each category.
I have to create a mysql query to get a voting distribution of each day exceeding a particular date, something like this...
date yes_votes no_votes
------------------------------------------
2010-01-07 21 22
2010-01-07 2 0
My table is like this..
post_votes
--------------------------
id(longint)
date(timestamp)
flag(tinyint) // this stores the yes/no votes 1-yes, 2-no
I am stuck at this....
SELECT COUNT(*) AS count, DATE(date) FROM post_votes WHERE date > '2010-07-01' GROUP BY DATE(date)
this gives the total number of votes per day, but not the distribution that I want.
SELECT COUNT(*) AS count
, DATE(date)
, SUM(flag = 1) AS yes_votes
, SUM(flag = 2) AS no_votes
FROM post_votes
WHERE date > '2010-07-01'
GROUP BY DATE(date)
This is a trick that works in MySQL, as flag=1 will either be True or False. But True = 1 and False = 0 in MySQL so you can add the 1s and 0s using the SUM() function.
Other solutions with IF or CASE would be better for clarity or if there is any chance you want to move the database to another RDBMS.
Comments not related to the question:
It's bad habit to use reserved words like date or count for naming fields or tables.
It's also not good to use "date" when you actually store a timestamp. Names should reflect use.
For table names it's recommended to use singular (post_vote) and not plural - although many use plural, it gets confusing in the end. Plural is good for some fields or calulated fields, like your yes_votes and no_votes where we have a counting.
Sum it:
select date(date) as date,
sum(case when flag = 1 then 1 else 0) as yes,
sum(case when flag = 2 then 1 else 0) as no
from post_votes
where date > '2010-07-01'
group by date(date)
you are almost at the solution :)
i would recommend the use of an IF condition in a SUM method like so:
SELECT SUM(IF(flag = 'yes',1,0)) AS yes_count,
SUM(IF(flag = 'no',1,0)) AS no_count,
DATE(date)
FROM post_votes
WHERE date > '2010-07-01'
GROUP BY DATE(date)
this will allow for the function to add 1 to each sum only if the value is equal to yes/no
SELECT DATE(date) as dt,
sum(if(flag=1,1,0)) as yes,
sum(if(flag=2,1,0)) as no
FROM post_votes WHERE date > '2010-07-01'
GROUP BY dt
I had that problem too. The best solution of that I can think of, is to split the "flag" in two fields, like:
upvote(tinyint)
downvote(tinyint)
Then you are able to count them very easy and without mysql-voodoo:
SELECT
SUM(upvote) AS up,
SUM(downvote) AS down,
DATE(`date`) AS Created_at
FROM post_votes
WHERE Created_at > '2010-07-01'
GROUP BY Created_at
Btw.: You should not name a column date, because it's a MySQL-Keyword.