I'm trying to count records in SQL Server and get the same results as I would in Excel by using Countif function.
I've tried Count(*) and other different ways of counting records in SQL Server but couldn't get the same results.
Is there a function or logic that would allow me to get results similar to the table shown below?
Do it with window function:
select Fruits, count(*) over(partition by Fruits) as Counts
from TableName
Related
My report has two datasets, ds_1 and ds_2, pulling from two different SQL servers.
ds_1 has the following query:
SELECT Patient_ID
FROM Server1.dbo.Patient_Table
WHERE Provider_ID = #Provider
#Provider is a parameter entered into the report by the user. This query works fine.
I then have another parameter #Patients which is hidden, multivalue, and based on the Patients value returned from ds_1
ds_2 has the following query:
SELECT *
FROM Server2.dbo.Records
WHERE PatID IN (#Patients)
#Patients might have a thousand or more values in it. I believe this solution works, but the execution time is very slow.
I'm hoping to populate a temp table from the values in #Patients, and then join that temp table to Server2.dbo.Records. I think the execution time would be faster, but would need to test it to be sure.
I imagine ds_2 would be something like:
SELECT Value
INTO #Temp
FROM STRING_SPLIT(#Patients, ',')
SELECT *
FROM Server2.dbo.Records r
JOIN #Temp t on t.Value = r.PatID
However, that gives the error:
An error occurred during local report processing.
An error has occurred during report processing.
Query execution failed for dataset 'ds_2'
Procedure or function STRING_SPLIT has too many arguments specified.
I tried changing ds_2 to something simple, like:
SELECT value FROM STRING_SPLIT(#Patients, ',')
and still get the same error.
If there's a better way to pass the results of ds_1 to a parameter or temp table in ds_2, I'm willing to give it a try. If it matters, I'm not able to make any changes to the SQL servers themselves.
Thanks!
I have two different MySQL count queries like this:
SELECT COUNT(*) AS stored_counter FROM users WHERE ***
SELECT COUNT(*) AS total_taken FROM completed WHERE ***
I'm looking for if the count value of query 1 matches the count value of query 2. Right now I am running these two queries seperately via PHP and then using PHP to see if the two queries return the same values.
But i'm looking to see if it's possible to do this with 1 SQL query?
Thanks
You can use this form
SELECT
(SELECT COUNT(*) FROM users WHERE 1 =1) stored_counter,
(SELECT COUNT(*) FROM completed WHERE 1=1) total_taken
You can add every Select statement that only gives back one Result (one field(one row one column)) only) as its own column
I am new to Mysql,I wanted to use both sum(Max(column)) ?
I don't know why its impossible to use?
whereas count(Distinct(column)) is possible i.e using two aggregate functions together
The reason is SUM() and MAX() both are aggregate functions and these functions returning results by grouping their inputs across the table . While applying one aggregate function over another, it is something like grouping data over the result set of another grouping function and that is not allowed in SQL.
But in SQL Server, from 2008 version onwards introduced a new clause OVER() so that we can specify the grouping criteria in that over clause for that particular column.
In the case of DISTINCT which does not need any grouping and it pull the distinct record set over which we are applying the aggregate function. So that will work.
You can perform in the following way but if you are not using orther grouping columns then it always return 1 column so SUM(MAX(tot)) = MAX(tot) though we are not allow to do it in same place:
SELECT SUM(total) tot FROM (
SELECT
MAX(quantity) total
FROM deliveries) t;
DISTINCT is not an aggregate function. It's a keyword, a basic command.
In your case, when you're getting the MAX from a group you're getting a single value. SUM a single value makes no sense.
If you want so SUM the MAX values from all existing groups, you could try like this:
SELECT SUM(<column>)
FROM <table> t
WHERE NOT EXISTS
(
-- filter all max values from each group
SELECT 1
FROM <table> d
WHERE 1=1
AND d.<group_column1> = t.<group_column1>
AND d.<group_column2> = t.<group_column2>
...
AND d.<column> > t.<column>
)
I have a query with few joins, on running it shows 11 records.
When I run its count query (removed fields from SELECT part and put COUNT(*) there) it returns different number, 16.
Why just converting to count query returns different count than its original query?
Either you have used Select Distinct when you are getting the number of rows 11 in result.
or
you are not using distinct in Count like Count(Distinct fieldname), so Count(*) is giving all the record count.
Most probably your join query returns the same rows twice or more. You can see what i mean by executing select * from... query
I am having a sql query
select devices.id, devices.type_designator_id, devices.color, devices.status,
devices.device_build, users.username
from devices,users
where
devices.user_id=users.id and devices.user_id=1608
ORDER BY devices.id;
Now it will give me 6 output from two tables devices and users.
Now I want to extract only one output from above query (without changing the anything) type_designator_id, to put it as a parameter for next sql query with different table.
Say new table is Type_designators with a parameter name id, which is same as the type_designator_id from the previous query.
You could consider creating a view defined by the query you've shown above, and using that view in your new query.