postgresql group two counts - postgresql

I was wondering if there is anyway of combining these two counts in the same table, like (Titulo, count1, count2).
First one:
select Titulo, count(genero)
from livro natural inner join genero
group by titulo;
Output:
titulo count
1 A lei 2
2 Olhar misterioso 2
3 Pensamento ao anoitecer 2
4 Ajudar e proteger 2
5 A corrupcao 2
6 O crime do seculo 2
7 Sem volta 2
8 Andar protegido 2
9 A bem ou mal 2
10 Diarios de um policia 2
Second one:
select Titulo, count(IDMemb)
from genero natural inner join livro natural inner join gosta
group by titulo;
Output:
titulo count
1 A lei 6
2 Olhar misterioso 4
3 Pensamento ao anoitecer. 4
4 Ajudar e proteger 4
5 A corrupcao 6
6 O crime do seculo 6
7 Sem volta 4
8 Andar protegido 4
9 A bem ou mal 4
10 Diarios de um policia 4
Desired output:
titulo count. count
1 A lei 2 6
2 Olhar misterioso 2 4
3 Pensamento ao anoitecer 2 4
4 Ajudar e proteger 2 4
5 A corrupcao 2 6
6 O crime do seculo 2 6
7 Sem volta 2 4
8 Andar protegido 2 4
9 A bem ou mal 2 4
10 Diarios de um policia 2 4
Thanks for your help

You can count only distinct genero for second SQL like this
select Titulo, count(distinct genero), count(IDMemb)
from genero natural inner join livro natural inner join gosta
group by titulo;

Related

Overwriting group of values with in same column another set of group based on other column group

Input:
Name GroupId Processed NewGroupId NgId
Mike 1 N 9 NULL
Mikes 1 N 9 NULL
Miken 5 Y 9 5
Mikel 5 Y 9 5
Output:
Name GroupId Processed NewGroupId NgId
Mike 1 N 9 5
Mikes 1 N 9 5
Miken 5 Y 9 5
Mikel 5 Y 9 5
below query worked in sql server, due to correlated subquery same is not working in spark sql.
Is there any alternate either with spark sql or pyspark dataframe.
SELECT Name,groupid,IsProcessed,ngid,
CASE WHEN ngid IS NULL THEN
COALESCE((SELECT top 1 ngid FROM temp D
WHERE D.NewGroupId = T.NewGroupId AND
D.ngid IS NOT NULL ), null)
ELSE ngid
END AS ngid
FROM temp T
worked with below in sparksql.
spark.sql("select LKUP,groupid,IsProcessed,NewGroupId ,coalesce((select Max(D.ngid) from test2 D where D.NewGroupId = T.NewGroupId AND D.ngidis not null),null) as ngid from test2 T")

kdb+ Q : select from table where column value =

How do i select all rows from a table where a specific column value equals something?
i have tried the following:
select from tablname where columnvalue = value
thanks
you could do:
q)table:([]a:1 2 3 4 5;b:`a`b`c`d`e;c:`hi`bye`bye`bye`hi)
q)table
a b c
-------
1 a hi
2 b bye
3 c bye
4 d bye
5 e hi
q)select from table where c=`bye
a b c
-------
2 b bye
3 c bye
4 d bye
You could do:
q)tbl:([] a:1 2 3;b:4 5 6;c:7 8 9)
q)tbl
a b c
-----
1 4 7
2 5 8
3 6 9
q)select a from tbl
a
-
1
2
3

Selecting Rows within Separate IDs until a first occurrence of given value

I am try to get all rows within each separate ID until the first occurrence of a given value, in this case "CR" but have to reverse the order of the rows. Here is how the data is stored on the table:
ID DebtMth YearMth Status Balance
1 5 2015-02 DR 10.00
1 4 2015-03 DR 10.00
1 3 2015-04 CR 00.00
1 2 2015-06 DR 10.00
1 1 2015-07 DR 10.00
2 10 2011-01 DR 20.00
2 9 2011-02 DR 20.00
2 8 2011-03 CR 20.00
3 11 2012-02 DR 30.00
3 10 2012-03 DR 30.00
3 8 2012-05 CR 00.00
3 7 2012-06 CR 00.00
3 6 2012-07 DR 30.00
I need to reverse the order so the last row within each ID group becomes the first and so on. So the table would be sorted as follows.
ID DebtMth YearMth Status Balance
1 1 2015-07 DR 10.00
1 2 2015-06 DR 10.00
1 3 2015-04 CR 00.00
1 4 2015-03 DR 10.00
1 5 2015-02 DR 10.00
2 8 2011-03 CR 20.00
2 9 2011-02 DR 20.00
2 10 2011-01 DR 20.00
3 6 2012-07 DR 30.00
3 7 2012-06 CR 00.00
3 8 2012-05 CR 00.00
3 10 2012-03 DR 30.00
3 11 2012-02 DR 30.00
Now I need to select rows within each ID group up until the Status is 'CR' and exclude any ID whose first row is 'CR'. So the output would look like this.
ID DebtMth YearMth Status Balance
1 1 2015-07 DR 10.00
1 2 2015-06 DR 10.00
3 6 2012-07 DR 30.00
I am using the Query Designer in Report Builder 3 connecting to an Microsoft SQL2012 Server.
I would very much appreciate any suggestions.
Martin
SELECT
id , DebtMth , YearMth , Status , Balance
FROM
(
SELECT
*
, MAX(CASE WHEN status = 'CR' THEN YearMth END) OVER(PARTITION BY id) AS first_cr_yearMth
FROM YourTable
) AS T
WHERE YearMth > first_cr_yearMth OR first_cr_yearMth IS NULL

update column from another column with conditional

I have table1 and tbl_cat. I want to update value of x where animal is cat
table1
id id_animal animal x
2 1 cat 3
3 2 cat 5
4 1 dog 7
5 2 dog 8
6 3 dog 9
tbl_cat
id x
1 10
2 30
Result Expectation:
table1
id id_animal animal x
2 1 cat 10
3 2 cat 30
4 1 dog 7
5 2 dog 8
6 3 dog 9
I use this query, but it's not work:
update table1
set table1.x = tbl_cat.x
from table1 inner join tbl_cat
on (table1.id_animal=tbl_cat.id)
where table1.animal='cat'
The correct syntax in Postgres is:
update table1
set table1.x = tbl_cat.x
from tbl_cat
where table1.id_animal = tbl_cat.id and
table1.animal = 'cat';
For some inexplicable reason, the three major databases that support join in update clauses (MySQL, SQL Server, and Postgres) all have different syntax. Your syntax is the SQL Server syntax.

Get Data from 2 Tables using Join

I have 2 tables :
1. transfer
2. data
in table data 2 records :
id name
1. 2 PQR
2. 3 XYZ
in table transfer 5 records :
id to from amount type
1. 1 2 3 100.00 C
2. 2 3 2 200.00 C
3. 3 2 3 150.00 D
4. 4 3 2 150.00 C
5. 5 2 3 300.00 D
now I want to form query that will take 2 in where condition and give me result
from transfer table that when 2 is in to column then from data should be shown
and when 2 is in from column then to data should be print.
And in result I want other columns that are amount and type.
I want data using join (Any), I am totally confused that how to perform this task.
Expected Result :
from/to amount type
3 100.00 C
3 200.00 C
3 150.00 D
3 300.00 D
Any Guidance on this..
Try Like this
select
case when "from"=2 then "to" when "to"=2 then "from" end "from/to"
,amount,type from transfer
Out put is
form/to amount type
3 100 C
3 200 C
3 150 D
3 150 C
3 100 D
OR
select case when "from"=2 then d.name when "to"=2 then data.name end "from/to",
amount,type from transfer inner join data on ("to"=data.id)
inner join data as d on("from"=d.id)
Out put is
form/to amount type
XYZ 100 C
XYZ 200 C
XYZ 150 D
XYZ 150 C
XYZ 100 D
ADDITION:
prove of working query: http://ideone.com/64kIov