Row data into Column - sql

I have this list of student and i want to make the Date record as the column
I want the result to be like this

SQL queries demand that you know the columns to select beforehand. If you know exactly which dates to expect, then you can select them as columns using Pivot or conditional aggregation:
select
max(case when date = {d '2015-08-01'} then student_id end) as date20150801,
max(case when date = {d '2015-08-02'} then student_id end) as date20150802,
...
from mytable
group by student_id;
Usually, however, you would simply select the data as rows with SQL and care about the presentation as columns in a grid with your GUI language (Java, C#, PHP, whatever).

Related

Output of non-existent values when grouping in sql

For example, i have a table with the data:
Screenshot
This table named "table".
I have the SQL query:
select
kind,
count(kind)
from table
where region = 'eng'
group by kind
And I get the result:
Question: how do I write a query that would return all the values that are in the kind field (or any other field that can be in group by)? Even if this value is 0. For the example above, the desired result is
It is mandatory to use group by in the query.
I use a postgresql 10.
Using a conditional aggregation
select
kind,
count(case region when 'eng' then kind end) cnt
from table
group by kind
select
t1.kind,
coalesce(t2.total, 0) total
from
(
select distinct kind from table
) t1
left join
(
select
kind,
count(kind) total
from table
where region = 'eng'
group by kind
)t2
on t1.kind = t2.kind
db fiddle

SQL Query with multiple columns w/ different conditions

How would I create a query that is querying 1 table and wanting to return multiple clauses but has different conditions for each column. I also need a group by clause at the end too. Something like below where my bracket is psuedocode:
Select date, sum(revenue) [where source = X], sum (revenue [where source = Y]
from table1
group by date
Is this possible or do I just need to do different queries
Use conditional aggregation:
select date,
sum(case when source = 'X' then revenue end) as x_revenue,
sum(case when source = 'Y' then revenue end) as y_revenue
from t
group by date;

i want to get the sum of one or two column in one select query

please help me to query
i want to sum one column in the table between entry date
example:
SELECT * FROM `transaction` WHERE entryDate BETWEEN '2019-11-05' AND '2019-11-31'
The simplest way to do this for just a single column is to just take the sum:
SELECT SUM(some_col)
FROM `transaction`
WHERE entryDate BETWEEN '2019-11-05' AND '2019-11-31';
If you wanted to take a number of sums, each with different criteria, then use conditional aggregation:
SELECT
SUM(CASE WHEN entryDate BETWEEN '2019-11-05' AND '2019-11-31'
THEN some_col END) AS some_col_sum,
-- other conditional sums here
FROM `transaction`;
SELECT ID, col1+ col2
FROM TableName

How to transform rows to columns using Postgres SQL?

I have the data in the following structure
Desired Output
Postgres (starting in 9.4) supports the filter syntax for conditional aggregation. So I would recommend:
SELECT customer_id,
MAX(value) FILTER (WHERE name = 'age') as age,
MAX(value) FILTER (WHERE name = 'marketing_consent') as marketing_consent,
MAX(value) FILTER (WHERE name = 'gender') as gender
FROM t
GROUP BY customer_id
SELECT customer_id,
MAX(CASE WHEN name='age' THEN value ELSE NULL END) AS age,
MAX(CASE WHEN name='marketing_consent' THEN value ELSE NULL END) AS marketing_consent,
MAX(CASE WHEN name='gender' THEN value ELSE NULL END) AS gender
FROM table
GROUP BY customer_id;
You just group by customer_id and pick out each value in its own column. You need an aggregate function on the various values columns for syntactical reasons, which is why each column has a Max function.
Not e that it would of course be better to store the data in a normalized fashion. Also, with newer versions of postgres, you can use filter instead of case, which I find a bit more readable.

unique count of the columns?

i want to get a unique count of the of multiple columns containing the similar or different data...i am using sql server 2005...for one column i am able to take the unique count... but to take a count of multiple columns at a time, what's the query ?
You can run the following selected, getting the data from a derived table:
select count(*) from (select distinct c1, c2, from t1) dt
To get the count of combined unique column values, use
SELECT COUNT(*) FROM TableName GROUP BY UniqueColumn1, UniqueColumn2
To get the unique counts of multiple individual columns, use
SELECT COUNT(DISTINCT Column1), COUNT(DISTINCT Column2)
FROM TableName
Your question is not clear what exactly you want to achieve.
I think what you're getting at is individual SUMS from two unique columns in one query. I was able to accomplish this be using
SELECT FiscalYear, SUM(Col1) AS Col1Total, SUM(Col2) AS Col2Total
FROM TableName
GROUP BY FiscalYear
If your data is not numerical in nature, you can use CASE statements
SELECT FiscalYear, SUM(CASE WHEN ColA = 'abc' THEN 1 ELSE 0 END) AS ColATotal,
SUM(CASE WHEN ColB = 'xyz' THEN 1 ELSE 0 END) AS ColBTotal
FROM TableName
GROUP BY FiscalYear
Hope this helps!