Aggregation depends from value [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a sample table
ID
Invoice_ID
docType
1
100
email
2
100
sms
3
200
email
4
200
email
5
300
sms
and I have to get results with only rows that have invoices with email and sms docType. For this example its only invoice_id 100

One approach is to query those type and count the distinct number of different types that appear:
SELECT invoice_id
FROM invoices
WHERE doctype IN ('sms', 'email')
GROUP BY invoice_id
HAVING COUNT(DISTINCT doctype) = 2

One method uses aggregation:
select invoice_id
from t
where docType in ('email', 'sms')
group by invoice_id
having count(distinct doctype) = 2;

Related

SQL - Return "True" if a customer purchased product A, and "False" otherwise [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a simple SQL table with a list of customer IDs, Invoices, Products they have purchased (Product A, Product B..), etc..
I need to write a query that returns two columns - one for the list of existing customers and another boolean column which says "True" if the customer purchased product A, and false otherwise.
How do I do that?
You can use aggregation:
select customerid,
max(case when productid = 'A' then 1 else 0 end)
from a
group by customerid;
Note: This uses 1 for "true" and 0 for "false". If you want strings, you can use:
select customerid,
max(case when productid = 'A' then 'True' else 'False' end)
from a
group by customerid;

How can I get SQL query result rows as columns with different header names? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Sales Type Name
Sales Type
Cash
500
Visa
1200
This how I would like my results to look like:
cash
visa
500
1200
You can use PIVOT:
SELECT [Cash], [Visa]
FROM (
SELECT SalesTypeName, SalesType
FROM t
) src
PIVOT
(
SUM (SalesType)
FOR SalesTypeName IN ([Cash], [Visa])
) pvt
If you have more cases of SalesTypeName you can easily extend the query by adding the values in the FOR clause of the PIVOT. For a dynamic number of values you have to use dynamic PIVOT.
Demo here
I would recommend conditional aggregation:
select sum(case when sales_type_name = 'Cash' then Sales_Type end) as cash,
sum(case when sales_type_name = 'Visa' then Sales_Type end) as visa
from t

query to check multiple revsions on a table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Need a query to check ids with multiple revisionnums
id reviionnum
1 0
2 0
1 1
3 1
2 1
The query should result
id revisionnum
3 1
Please help
If you want ids with only one revision number, you can use aggregation:
select id, min(revissionnum)
from t
group by id
having count(*) = 1;
The min() is the value if there is only one row.

Hive output query for selecting particular value based on groupby clause [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a table with data like:
cust_id, acct_no, ind
123111, 1233, Y
123111, 2311, N
222111, 1112, N
222111, 2111, N
I have to get output as cust_id, 1 (a binary indicator if any of the acct under that customer is Y)
so from the above table I have to get below output.
123111 1
222111 0
A simple way to achieve this is something like:
select cust_id, max(case when ind = 'Y' then 1 else 0 end) as flag from customers group by cust_id;

Counting frequency of customer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this table on sql sever
cstomer |No_Nota
CUS000 | 98342
CUS000 | 98343
CUS000 | 98343
CUS001 | 98355
CUS001 | 98355
I would like to count the frequency of each customer. For similar number of no_nota the value is 1.
I'd like a result like this:
cstomer |Frequent
CUS000 | 2
CUS001 | 1
You want the distinct count of the column no_nota, so that's what you should select...
select customer, count(distinct no_nota) as frequent
from my_table
group by customer
You want to count the result of your select query:
SELECT COUNT(expression)
FROM tables
WHERE predicates;
Example:
SELECT COUNT(No_Nota) FROM your_table WHERE No_Nota > 0;
Sounds like you just want a group by statement to get a count of all individual customers. Something like:
SELECT cstomer, SUM(1) as Frequent FROM table GROUP BY cstomer, No_Nota