Add COUNT column in SQL output - sql

I have a 10k row data set where a column, "mode", has either a '0' or '1' integer value. I am attempting to write a query that simply counts the number of '0' and '1' values.
These work but don't tell the whole story:
SELECT mode
FROM unpopular_songs
GROUP BY mode;
This shows the mode column with just two rows (0 and 1).
SELECT
COUNT(mode) AS minor
FROM unpopular_songs
WHERE mode = '0';
This shows one column labeled as "MINOR", with one row with a count as 3905. So, this tells part of the story. However, I need another column "MAJOR" that shows the count of values of '1'.
This was my best shot at getting the output I wanted, but it's throwing an error:
SELECT
COUNT(mode = '0') AS minor
COUNT(mode = '1') AS major
FROM unpopular_songs;

You need some grouping:
SELECT COUNT(1), mode
FROM unpopular_songs
GROUP BY mode;

Related

SQL Query getting not results

I have a Select statement that gets the items based on the greatest date. it works but these are selected value from asp.net controls and one of the controls is a multiselect listbox
as you can see DynamicAtrributeID 937766 is show one time:
I need it to show all the rows that were in 937766 as well as the other rows which could be just one row. Is this possible
SELECT VotingValueDynamicId,
DecisionValueID,
DynamicAttributeID,
VotingValue,
DecisionSurveyID,
VALUEDATEUPDATED,
RECORDSTATUS,
FROM Adjudicate.ONCOLOGY_DynamicDecisionValuesForCaseManager AS a
WHERE (VALUEDATEUPDATED =
(SELECT MAX(VALUEDATEUPDATED) AS Expr1
FROM Adjudicate.ONCOLOGY_DynamicDecisionValuesForCaseManager AS b
WHERE (a.DecisionValueID = DecisionValueID) AND a.DynamicAttributeID = DynamicAttributeID) ) AND (RECORDSTATUS <> 'D')
In the example shown, all the rows with DynamicAtrributeID = 937766 have different values on VALUEDATEUPDATED, so the query is showing one row for that DynamicAtrributeID because that's what you're doing, filtering and getting only the row wich has the max VALUEDATEUPDATED.
In other words, you can't get more than one row for a DynamicAtrributeID if you're filtering by his max(VALUEDATEUPDATED).
As #necoflecap1 said , the issue is your filter condition, For VALUEDATEUPDATED , you are filtering max(VALUEDATEUPDATED) which any way going to give 1 value per DynamicAttributeID and DecisionValueID combination,since your filter in subquery is
WHERE (a.DecisionValueID = DecisionValueID) AND (a.DynamicAttributeID =
DynamicAttributeID)
I can see two possibilities here , either add one more filter for VotingValue column
WHERE (a.DecisionValueID = DecisionValueID) AND (a.DynamicAttributeID =
DynamicAttributeID) and (a.VotingValue=b.VotingValue)
or Do a
group By VotingValue
inside filter subquery for max(VALUEDATEUPDATED) so that aggregation will perform for every VotingValue (which is the column I can see coming with a different value for DynamicAtrributeID 937766)

How to count unique occurences of string in table for separate records in apex 5

I am trying to automatically count the unique occurrences of a string saved in the table. Currently I have a count of a string but only when a user selects the string and it gives every record the same count value.
For example
Below is a image of my current table:
From the image you can see that there is a Requirement column and a count column. I have got it to the point were when the user would select a requirement record (each requirement record has a link) it would insert the requirement text into a requirement item called 'P33_REQUIREMENT' so the count can have a value to compare to.
This is the SQL that I have at current:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT
group by REQUIREMENT
) AS COUNT,
DPD.DIA_SELECTED,
DPD.Q_NUMBER_SELECTED,
DPD.SECTION_SELECTED,
DPD.ASSIGNED_TO_PERSON,
DAQD.REFERENCE,
DAQD.REQUIREMENT,
DAQD.PROGRESS,
DAQD.ACTION_DUE_DATE,
DAQD.COMPLETION_DATE,
DAQD.DIA_REF,
DA.DIA,
DA.ORG_RISK_SCORE
FROM DIA_PROPOSED_DETAIL DPD,
DIA_ASSOCIATED_QMS_DOCUMENTS DAQD,
DIA_ASSESSMENTS DA
WHERE DPD.DIA_SELECTED = DAQD.DIA_REF
AND DPD.DIA_SELECTED = DA.DIA
This is the sql used to make the table in the image.
This issue with this is, it is giving every record the same count when the user selects a requirement value. I can kind of fix this by also adding in AND DIA_SELECTED = :P33_DIA into the where clause of the count. DIA_SELECTED being the first column in the table and :P33_DIA being the item that stores the DIA ref number relating to the record chosen.
The output of this looks like:
As you can see there is only one count. Still doesn't fix the entire issue but a bit better.
So to sum up is there a way to have the count, count the occurrences individually and insert them in the requirements that are the same. So if there are three tests like in the images there would be a '3' in the count column where requirement = 'test', and if there is one record with 'test the system' there would be a '1' in the count column.
Also for more context I wont know what the user will input into the requirement so I can't compare to pre-determined strings.
I'm new to stack overflow I am hoping I have explained enough and its not too confusing.
The following extract:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT group by REQUIREMENT ) AS COUNT
Could be replaced by
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = DAQD.REQUIREMENT ) AS COUNT
Which would give you - for each line, the number of requirements that are identical.
I'm not completely certain it is what you are after, but if it isn't, it should give you some ideas on how to progress (or allow you to indicate where I failed to understand your request)

need to look through many records of one table for each one in another

In Oracle SQL Developer, I get the error, "ORA-01427: single-row subquery returns more than one row" when I try to run this line of code.
UPDATE stream_log sl
set recordnum =
(SELECT recordnum
FROM STREAM_LOG_HISTORY slh
WHERE substr(slh.recordnum, 2, 5) = sl.recordnum);
In order to try to fix it, I tried changing my code to this:
UPDATE stream_log sl
set recordnum =
(SELECT recordnum
FROM STREAM_LOG_HISTORY slh
WHERE substr(slh.recordnum, 2, 5) = sl.recordnum).first;
but it says the period is syntactically erroneous.
I need to retrieve the first character of strings that match the last 5 of the given one, but to do that I need to look through many records of one table for each one in another. Any advice is appreciated.
I need to retrieve the first character of strings that match the last
5 of the given one,
The "first character" that matches cannot be guaranteed to be a particular string, every time you run the update, unless you have any other unique column which could be used to order the results, preferably using row_number() or FIRST_VALUE functions. So, other option is to use MAX or MIN to get a single record for each match.
UPDATE stream_log sl
SET recordnum = (
SELECT MAX(recordnum)
FROM stream_log_history slh
WHERE Substr(slh.recordnum, 2, 5) = sl.recordnum
);

Create a Counter that resets with every change in name

This site has been a great resource for me over the years but I finally ran into an issue i couldn't find a solution for already posted.
I have a list of names, dates and values. I need to turn the dates into the numbers and for them to start over for each well where value is greater than 0.
My thought was to create a loop but I'm short on skills to make it happen.
I found a post to create the counter but not how to loop it:
SELECT Name, row_number() over (order by (select NULL))
FROM [$ST_MASTER]
The Yellow Column Is What I Want to Create
You're in the right direction. Your over by need to change in two ways:
1) You need to restart the row number with each name.
2) You need to restart the row number when the value becomes zero.
After this, we just need to discard the row numbers for zero quantity rows. You'll see I did this by using the ZeroOnePartSort value.
One flow in this logic: This assumes (per your data sample) that once quantities go positive for a name, they don't go to zero again.
WITH ValSort AS (
SELECT Name, CASE WHEN value = 0 THEN 0 ELSE 1 END AS ZeroOnePart,
DateStamp, Value FROM dbo.STMaster
)
SELECT vs.Name, vs.DateStamp, vs.Value,
ROW_NUMBER() OVER(PARTITION BY vs.Name, vs.ZeroOnePart
ORDER BY vs.DateStamp) * vs.ZeroOnePart
FROM ValSort vs

Select case mess in JFreeChart

I have a Column(cliente_x_hora, a numeric field) i put in a interval and count the number in each interval.I have 3 textfields(number of intervals,value between intervals and initial value). When I select the two first(with 5 intervals and 1000 value), the query run flawless and generate the expect barchart.
Query(with two select textfields):
SELECT INTERVAL, COUNT(*) TOTAL FROM (
SELECT CASE WHEN CLIENTE_X_HORA>0 AND CLIENTE_X_HORA<=1000.00 THEN '0<CLIENTE_X_HORA> <=1000.00'
WHEN CLIENTE_X_HORA>1000.00 AND CLIENTE_X_HORA<=2000.00 THEN '1000.00<CLIENTE_X_HORA><=2000.00'
WHEN CLIENTE_X_HORA>2000.00 AND CLIENTE_X_HORA<=3000.00 THEN '2000.00<CLIENTE_X_HORA><=3000.00'
WHEN CLIENTE_X_HORA>3000.00 AND CLIENTE_X_HORA<=4000.00 THEN '3000.00<CLIENTE_X_HORA><=4000.00'
ELSE '4000.00<CLIENTE_X_HORA' END INTERVAL, CLIENTE_X_HORA FROM SGD_CAUSA)
GROUP BY INTERVAL ORDER BY TOTAL
The barchart is
The problem is when I select the last field(initial value with, per example 2000), my barchart go crazy(i believe is adding up the discarded values below 2000):
That ELSE(>6000) should be much smaller than is showing.How can I solve that?
Best Regards,
DDias
CLARIFICATION from OP:
The query is the same as above but begins in 2000:
SELECT CASE WHEN CLIENTE_X_HORA>2000 AND CLIENTE_X_HORA<=3000.00... and ends in 6000:ELSE '6000.00<CLIENTE_X_HORA' END INTERVAL, CLIENTE_X_HORA FROM SGD_CAUSA) GROUP BY INTERVAL ORDER BY TOTAL
put the result in table form is impractical(we are talking about over 87 thousand rows) That happens always when i give an initial value different than ZERO.
Your ELSE is just that. It includes everything that is not matched by specific WHENs.
So if you do not start from zero, that last column will include everything below a lowest limit in addition to greater than highest limit.
So if you do not want this behavior, do not use ELSE at all. Use WHEN CLIENTE_X_HORA > 6000.00 (or whatever your highest limit is) as the last condition.
EDIT:
In your internal query filter out (with WHERE) the values that are below the lowest limit.
Since we no longer have unneeded low range, you no longer need the HAVING clause we added and you can even go back to using ELSE.
If your lowest limit is zero, then you will be filtering everything below 0, which I assume is nothing.