MS Access SQL query counting values in more than one column - sql

I'm having an issue that basically invovles an SQL query in MS Access counting criteria from multiple fields. The data I am working with is as follows:-
Option 1 Option 2 Option 3
abc def ghi
abc ghi def
def abc jkl
And how I want the query to display my data:
abc - 3
def - 3
ghi - 2
jkl - 1
Forgive me if this is quite a basic question. I didn't know how best to put into words the nature of my enquiry and I am relatively new to SQL.
Thanks.

SELECT OPTION,sum(count) from (
SELECT OPTION1 as OPTION,COUNT(*) as count FROM Table GROUP BY OPTION1
union all
SELECT OPTION2,COUNT(*) as count FROM Table GROUP BY OPTION2
union all
SELECT OPTION3,COUNT(*) as count FROM Table GROUP BY OPTION3
)group by OPTION
This should do the trick, not sure if that is the correct syntax but just adjust it.
Its a two steps query, first - count for each column grouping by their name. And second - sum the total unioning all the results

Related

Oracle SQL - Need to eliminate data if at least one of the particular condition is not satisfied

My question is related to Oracle sql. I have a two tables say, study table and another one is study part table. Stdyno is the primary key in study table and (stydyno + sqncno) is the primary key in studypart table.
EG: studypart table has data as below.
studyNo sqnc part approvalIN
--------------------------------
123 1 fgh Y
123 2 jhf N
123 3 rty N
456 1 wer N
456 2 wdg N
456 3 ghg N
I need query in such a way that my output from studypart table gives result
as study number which has all the approvalIn as N. If it has at least one of the approvalIn as 'Y'
then that studyno should be excluded from the result.
Desired output:
studyno: 456
I tried this implementation in stored procedure taking Y and N approvalIn count separately ie,
if a studyno has both the count then exclude it and
if it has only one count say either N or Y the include it.
But i would like to know how to achieve this is query.
You can do it by excluding those rows whose count of "approvalIN = 'N'" does not match the total count of "approvalIN" values.
SELECT STUDYNO
FROM tab
GROUP BY STUDYNO
HAVING SUM(CASE WHEN approvalIN = 'N' THEN 1 END) = COUNT(approvalIN)
Check the demo here.

merging multiple rows into one based on id

i have the data in this format in an amazon redshift database:
id
answer
1
house
1
apple
1
moon
1
money
2
123
2
xyz
2
abc
and what i am looking for would be:
id
answer
1
house, apple, moon, money
2
123, xyz, abc
any idea? the thing is that i cannot hard code the answers as they will be variable, so preferably a solution that would simply scoop the answers for each id's row and put them together separated by a delimiter.
you can use aggregate function listagg:
select id , listagg(answer,',')
from table
group by id
You can use string_agg(concat(answer,''),',') with group by so it will be like that:
select id , string_agg(concat(answer,''),',') as answer
from table
group by id
tested here
Edit:
you don't need concatenate, you can just use string_agg(answer,',')

Alternative for GROUP BY and STUFF in SQL

I am writing some SQL queries in AWS Athena. I have 3 tables search, retrieval and intent. In search table I have 2 columns id and term i.e.
id term
1 abc
1 bcd
2 def
1 ghd
What I want is to write a query to get:
id term
1 abc, bcd, ghd
2 def
I know this can be done using STUFF and FOR XML PATH but, in Athena all the features of SQL are yet not supported. Is there any other way to achieve this. My current query is:
select search.id , STUFF(
(select ',' + search.term
from search
FOR XML PATH('')),1,1,'')
FROM search
group by search.id
Also, I have one more question. I have retrieval table that consist of 3 columns i.e.:
id time term
1 0 abc
1 20 bcd
1 100 gfh
2 40 hfg
2 60 lkf
What I want is:
id time term
1 100 gfh
2 60 lkf
I want to write a query to get the id and term on the basis of max value of time. Here is my current query:
select retrieval.id, max(retrieval.time), retrieval.term
from search
group by retrieval.id, retrieval.term
order by max(retrieval.time)
I am getting duplicate id's along with the term. I think it is because, I am doing group by on id and term both. But, I am not sure how can I achieve it without using group by.
The XML method is brokenness in SQL Server. No reason to attempt it in any other database.
One method uses arrays:
select s.id, array_agg(s.term)
from search s
group by s.id;
Because the database supports arrays, you should learn to use them. You can convert the array to a string:
select s.id, array_join(array_agg(s.term), ',') as terms
from search s
group by s.id;
Group by is a group operation: think that you are clubbing the results and have to find min, max, count etc.
I am answering only one question. Use it to find the answer to question 1
For question 2:
select
from (select id, max(time) as time
from search
group by id, term
order by max(time)
) search_1, search as search_2
where search_1.id = search_2.id
and search_1.time = search_2.time

SQL Server Stored Procedure SELECT DISTINCT

I'm working on deciphering some stored procedures and have minimal vocabulary on the subject. Can someone please explain to me what role this '1' serves in the below statement? I can not find any DISTINCT syntax tutorials to explain this. I'm referring to the actual "1" one in the statement.
USE TEST
GO
SET ANSI_NULLS, QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].sp_F_SQL
(#Id int)
WITH ENCRYPTION AS
SELECT DISTINCT
dbo.MAP_SQL.rID,
dbo.MAP_SQL.lID,
dbo.MAP_SQL.cID,
**1** as RESPFACT,
dbo.MAP_SQL.Longitude,
dbo.MAP_SQL.Latitude,
dbo.MAP_SQL.Altitude,
...
The 1 has nothing to do with DISTINCT. It just adds an output column titled RESPFACT that has a value of 1 for all rows. I suspect whatever is consuming the output need that column.
SELECT DISTINCT only returns the "distinct" rows from the output - meaning rows where ALL column values are equal.
e.g. if your output without distinct was
1 2 ABC DEF
2 3 GHI JLK
2 1 ABC DEF
1 2 ABC DEF
Then rows 1 and 4 would be seen as "equal" and ony one would be returned:
1 2 ABC DEF
2 3 GHI JLK
2 1 ABC DEF
Note that rows 1 and 3 are NOT equal even though 3 of the 4 column values match.
The 1 generates a column called RESPFACT. This always has the value of 1.
I cannot say why this is important for the sp_F_SQL procedure.
The distinct returns unique rows. If there are duplicate values for the columns in the select then only one row is returned. Clearly, the RESPFACT column is the same in all rows, so it does not affect the rows being returned.

grouping items in sql query

I have a table with columns like (in sql server 2000)
MailCode Mode Name Group
-------- ----- --------- -------
1 1 abc 0
1 1 def 0
1 1 qwe 1
2 2 aaw 0
2 2 aad 0
I want to group the Name field based on the rest of the fileds so that the result looks like this (there should be only one unique mailCode, Mode and group combination)
MailCode Mode Names Group
--------- ------ ------------ -------
1 1 abc, def 0
1 1 qwe 1
2 2 aaw, aad 0
How can I create the sql query for this?
I had a similar problem where I had to concatenate a field in the select, my solution at the time was to create a procedure that returned the result and called it like this
select x as field1, y as field2, dbo.procedure as field3
SQL Server 2000 solution
Luckily, COALESCE is supported in 2000, so you can use the COALESCE trick to create a comma delimited list of values, demonstrated in this link. Because of the variable usage, you'll need to create a function/procedure and call it within the main query. Basically, just replace the STUFF() in the query below with the function call.
SQL Server 2005+ solution:
SELECT x.mailcode,
x.mode,
STUFF((SELECT y.name
FROM TABLE y
WHERE y.mailcode = x.mailcode
AND y.mode = x.mode
AND y.gropu = x.group
GROUP BY y.mailcode, y.mode, y.group
FOR XML PATH(', ')), 1, 1, '') AS name,
x.group
FROM TABLE x
GROUP BY x.mailcode, x.mode, x.group
I can't think of a simple query that will get the result you're looking for, but some logic along these lines should get you where you want:
1) Loop through distinct MailCode, Mode, Group Rows
A) select all names in group
A.1) Loop through names
A.2) Concatenate them together into temp variable
B) insert all data (MailCode, Mode, Group, temp variable) into temp table
Fair waring, looping in SQL tends to have a huge performance hit when it comes to large datasets. I unfortunately don't know a better way to do it.