How to count values in a query result - sql

I want to count the values on a query result but within the same query if that makes sense. the origional query is
SELECT CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER as BAL, total_closing_balance
FROM statement
this returns over 4000 rows. I want to check the two add up. is there a way to use the count function in the same query to count the first two values of the select statement?
Or would i have to use something like a temp table and then count?

try this if total_closing_balance is static
SELECT SUM(CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER) as BAL, MAX(total_closing_balance) AS total_closing_balance FROM isql.VW_300_statement WHERE brand = '1'AND DAT = '2013-01-18 00:00:00.00000'AND INS_TYPE =''group by Brand,DAT

use Select sum(CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER )
it will add up all these details.

In order to count the rows of a query result you have to write a query as following:
select count(*)
from (SELECT CLOSING_FEE+CLOSING_FEE_INT+CLOSING_INS+CLOSING_INT+CLOSING_OTHER as BAL, total_closing_balance
FROM isql.VW_300_statement
WHERE brand = '1'
AND DAT = '2013-01-18 00:00:00.00000'
AND INS_TYPE ='')

Related

SQL ManyToOne condition

But if I add one more for my query (that not related to this), for example:
I should get nothing
(but it needs to be workable :D)
You can do a GROUP BY, and use HAVING to make sure all desired tag_id's are there:
SELECT sp.perfume_id
FROM sp_tag_to_perfume sp
WHERE sp.tag_id IN (2070, 127)
GROUP BY sp.perfume_id
HAVING COUNT(DISTINCT sp.tag_id) = 2; -- number of tag_id values (in this case 2070 and 127)
This can get all the columns and data containing perfume id 199.
Select * from sp_tag_to_perfume sp where sp.perfume_id=199;
And from tag_id...
Select distinct perfume_id from sp_tag_to_perfume sp where sp.tags_id=127;
You can use distinct keyword to get unique values of the column.

Return each name and other field corresponding to maximal value of third field in SQL

I currently have the table as follows on the picture.
I would like to write a query which returns all the names and the travel_date with the maximal 'total' value for each name. For example, I would like the query to return in this case:
Armand 2012-07-18 and Elish 2012-06-18. How could I do that ? Thanks in advance
In most cases, you'll find that the procedure for this is relatively universal. The following example will work in MySQL, MSSQL and DB2 (among others).
SELECT
a.name,
a.travel_date,
a.total
FROM test_table AS a
INNER JOIN ( SELECT `name`, MAX(total) AS `total` FROM test_table GROUP BY `name` ) AS b
ON a.name = b.name
AND a.total = b.total;
Here's an example of the sample data I worked with including the results after running the query.
-
Edit: As jarlh, the initial query I wrote was indeed wrong. The following query should provide the results that you requested in the comment below.
SELECT name, MAX(travel_date)
FROM my_table
GROUP BY name;

AS ____ not working in "Union select statement"

I have this:
SELECT
COUNT(resurs_id) AS 'antalLikes'
FROM kopplaResursLikesUsers
WHERE resurs_id = 19
UNION
SELECT user_id AS 'gillarJagEller'
FROM kopplaResursLikesUsers
WHERE user_id = 5
And I thought the output would be:
antalLikes = 2
gillarJagEller = 5
but the result I get is:
antalLikes[0] = 2
antalLikes[1] = 5
Any ideas why it completely ignores my 2nd "AS" statement?
UNION does make an UNION for COLUMS, not for rows
So, it's taking the first select, get the column 'antalLikes'
and then make the union by the ordinal order.
So, it put the values from your 'gillarJagEller' column in the first one
to get your desired result, I suggest this:
select
(
SELECT
COUNT(resurs_id)
FROM kopplaResursLikesUsers
WHERE resurs_id = 19) AS 'antalLikes',
(
SELECT user_id
FROM kopplaResursLikesUsers
WHERE user_id = 5) AS 'gillarJagEller'
UNION takes as column names only the names specified in the first select statement, in this case, "antalLikes".
To get this:
antalLikes = 2 gillarJagEller = 5
You need something like this:
SELECT
COUNT(resurs_id) AS 'antalLikes',
(SELECT top 1 user_id
FROM kopplaResursLikesUsers
WHERE user_id = 5) AS 'gillarJagEller'
FROM kopplaResursLikesUsers
WHERE resurs_id = 19
Column names and column data types in a UNION are provided by the first select statement in the UNION. Any column names in subsequent `select statements are ignored.
Subsequent select statements in the UNION are required to have the same number of columns as the first select statement. Further, each column in each subsequent select statement must have a datatype that is identical to the column with the same ordinal number in the first select statement or is implicitly convertible to that data type.
WRT to nullity, I believe UNION sets the nullity of each column in the final result set by examining the nullity of that same column in each component select statement. Only if that column is non-nullable across each component select statement is the column in the final result set non-nullable.

How to give the output of the first query(which has two values) as the input to the second?

i get 2 names as the output of the first query....
eg: paul,peter
now this should be the input for the second query,
which has to display paul's and peter's email ids....
For nested queries I would strongly recommend WITH clause. It makes long complex queries order of magnitude easier to understand / construct / modify:
WITH
w_users AS( -- you can name it whatever you want
SELECT id
FROM users
WHERE < long condition here >
),
w_other_subquery AS(
...
)
SELECT email_id
FROM ...
WHERE user_id IN (SELECT id FROM w_users)
You can use like this
LIKE
SELECT USER_ID,EMAIL_ID FROM USERS where user_id IN
(SELECT PRODUCT_MEMBERS FROM PRODUCT WHERE PRODUCT_NAME='ICP/RAA');
Just use the IN clause '=' is used for matching one result
You can use In Command to get result
ex:
SELECT email FROM tableName WHERE (Name IN ('paul', 'peter'))

How do I select only 1 row in sybase without using rowcount

How do I select only 1 row in sybase without using rowcount? I don't have the privilege to set rowcount in sybase. Is there a way to select only 1 row?
For example:
select * from table where name = 'jack'
This returns two rows; how do I select only one row from the result set without using set rowcount?
Try the query:
SELECT TOP 1 * FROM mytable
WHERE name = 'jack'
As you might guess, this selects the TOP 1 matching results. If you wanted more (which you don't here) you could use any number (TOP 100 or TOP 1000, etc).
A more comprehensive example can be found on w3schools: http://www.w3schools.com/Sql/sql_top.asp
There seems to be a reason, why you're getting more than 1 row for "WHERE name = 'jack'", it looks as if the rows differ.
But if, the rows do not differ you can try adding "distinct":
SELECT DISTINCT * FROM TABLE WHERE name = 'jack'
or try with "GROUP BY" statement, then you should type explicitly all columns, eg.:
SELECT name FROM TABLE WHERE name = 'jack' GROUP BY name
if this is not what you wanted, can you paste here how the 2 rows look exactly?
If you want a single result, use 'GROUP BY' and 'HAVING column = max(column)'. Or replace max() with min().
This should work unless the max or min values are also not unique.