display row data as column in sqlite - sqlite

I have a DB table with below structure in SQLITE
Name Count Type
Roy 3 CA
Roy 2 BT
John 2 CA
John 1 BT
Need a query that can display the above data like below(sqlite)
Name count CA BT
Roy 5 3 2
John 3 2 1
thanks

There is no pivoting in SQLite, but you can use a lot of subquerys:
SELECT DISTINCT name,
(SELECT sum([count]) FROM test tst WHERE tst.name = test.name) as count,
(SELECT sum([count]) FROM test tst WHERE tst.name = test.name AND type='CA') as CA,
(SELECT sum([count]) FROM test tst WHERE tst.name = test.name AND type='BT') as BT
FROM test
assuming tablename "test"

Related

Parsing Database string in SQL Select command

So basically I have this column called Signature inserted in the database
Id = 1
Value = John, Micheal, Sara
Id = 2
Value = Mike, Steve, John
.
.
Now in asp.net I'm not sure how can I do select command and know if the value
John, Micheal, Sara
Has "Micheal" in it
To answer you question:
CHARINDEX() searches for a substring within a larger string, and returns the position of the match, or 0 if no match is found
SELECT * FROM [Table] WHERE CHARINDEX(Signature, 'Michael') > 0
Or
You can just use wildcards in the query (after IF, WHERE or ON):
SELECT * FROM [Table] WHERE Signature LIKE '%' + 'Michael' + '%'
But really you should be storing this data in a separate related table.
No. Don't.
Add a new table with rows for each signature name, and use a row for each signatory. That is the point of relational databases.
1 John
1 Michael
1 Sara
2 Mike
2 Steve
2 John
Or better still, add two new tables - one for the signatories, and one for the relation between that and your initial table
Signatories
1 John
2 Michael
ItemSignatures
ItemID SignatoryID
1 1
1 2
2 1
select *
from Signature
where value like '%Micheal%'

need help to build a query that takes the dynamic column name return by another query as record

I have 2 tables:
Employee_tbl
empid empname empsalary emplocation
----- ------- --------- ------------
1 santhosh 15000 East godavari
2 Srinivas 25000 Westgodavari
3 sandeep 35000 Hyderabad
4 prathap 55000 Hyderabad
5 praveen 45000 West godavari
configuration_tbl
config_id column_name
--------- -----------
1 empid
2 empname
3 empsalary
4 emplocation
When I pass input as config_id it should display the values from that column.
Ex: If I pass config_id then it should display all empname from employee_tbl.
When there is only a limited number of columns I advice you to use a SQL with a case statement. Depending on whether you want to fold numbers into one text field or not, you may need two case columns. Execute immediate is possible but has additional overhead.

I can not just join it because I need to group DVDs by movie_id first? - ORACLE

I have this query that has no problem:
SELECT m.movie_name, cd.times_requested
FROM movie m,
(select *
from(
select movie_id, count(movie_id) as times_requested
from movie_queue
where status_id=0 or status_id=1
group by movie_id
) ab
where times_requested>1) cd
WHERE m.movie_id=cd.movie_id;
It returns the following list.
MOVIE_NAME TIMES_REQUESTED
----------------------------------------------------------------------
E.T. the Extra-Terrestrial 2
Indiana Jones and the Kingdom of the Crystal Skull 2
War of the Worlds 3
Unbreakable 3
Question:
How do I add another column showing the amount of DVDs available for each movie, I can not just join it because I need to group DVDs by movie_id first?.
Table above is OK, but I want to add a third column, the third column will contain information about the number of DVDs available for each movie. the problem is that the number of dvds is stored in another table call DVDS. The structure of the table DVDs is similar to this:
DVD_ID MOVIE_ID DVD_ENTRY_DATE
---------- ---------- --------------
1 1 24-JUL-12
2 1 24-JUL-12
3 1 24-JUL-12
4 2 24-JUL-12
5 2 24-JUL-12
Desired Result:
Final table should look similar to the one below:
MOVIE_NAME TIMES_REQUESTED DVDS_AVAILABLE
-------------------------------------------------------------------
E.T. the Extra-Terrestrial 2 3
Indiana Jones and the Kingdom 2 1
War of the Worlds 3 3
Unbreakable 3 1
I tried the following code, but did not get the result I wanted
I am assuming I need to go to dvd table first and find all dvds that match the movie_id I want and group them by movie_id. I tried the code below, but instead of returning the 4 rows I want it is returning 72.
SELECT m.movie_name, pomid.times_requested, d.dvds_available
FROM movie m,
(select *
from(
select movie_id, count(movie_id) as times_requested
from movie_queue
where status_id=0 or status_id=1
group by movie_id
) mid
where times_requested>1) pomid,
(select movie_id, count(movie_id) as dvds_available
from dvd
group by movie_id) d
WHERE m.movie_id=pomid.movie_id;
Thanks for your suggestions in how to fix this.
A join to d.movie_id is missing, that's why you get too many rows. (Quick check: how many tables do I have? How many joins do I have?)
And I'd also add an outer join to get all movies, even when there are no dvd or movie_queue entries.
SELECT m.movie_name
,NVL(pomid.times_requested,0) times_requested
,NVL(d.dvds_available,0) dvds_available
FROM movie m
,(SELECT *
FROM (SELECT movie_id
,COUNT (movie_id) AS times_requested
FROM movie_queue
WHERE status_id = 0
OR status_id = 1
GROUP BY movie_id) mid
WHERE times_requested > 1) pomid
,(SELECT movie_id
,COUNT (movie_id) AS dvds_available
FROM dvd
GROUP BY movie_id) d
WHERE m.movie_id = pomid.movie_id(+)
AND d.movie_id(+) = pomid.movie_id;
http://www.sqlfiddle.com/#!4/5437a/11
Not sure I really understand your table structure, but this might get you started:
select movie_id,
sum(case when status_id in (0,1) then 1 else 0 end) as times_requested,
count(movie_id) as dvd_available,
from movie_queue
group by movie_id;

Filtering in Oracle based on a group of values contained in a list of values

I have following two tables:
ID_PERSON NAME
-----------------
1 John
2 Joe
3 Peter
ID_PERSON ID_SPECIALIZATION
------------------------------
1 5
1 6
1 7
2 5
2 1
3 6
3 10
I need to filter data based on group of ids ID_SPECIALIZATION that will be provided. For example
I want to display only those persons who has specialization in 5 and 6 so it will return only first person. In ASP.NET Web form there will be two listboxes, left and right button, in first LB there will be all possible specializations and user will choose some of them to second LB as filtering options. I have no idea how to put this filtering condition in sql query. Thanks for help.
You could use the following:
SQL> SELECT p.id_person, p.NAME
2 FROM person p
3 JOIN person_spe s ON p.id_person = s.id_person
4 WHERE id_specialization IN (5, 6)
5 GROUP BY p.id_person, p.NAME
6 HAVING COUNT(*) = 2;
ID_PERSON NAME
---------- -----
1 John
One way to do it:
SELECT
ID_PERSON
, NAME
FROM
Person AS p
WHERE EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 5
)
AND EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 6
)
SELECT d1.id_person, d1.name FROM tbl_table1 d1
INNER JOIN tbl_table2 d1
ON d1.ID_PERSON=d2.ID_PERSON
WHERE ID_SPECILIZATION = ?
Theres the query but I'm not sure how asp.net works and passing in the value. It might be work looking up bind variables which allows you to use place holders in the sql which oracle then caches the query and just uses the values that you pass in at run tuime using EXECUTE IMMEDIATE.

Parent Child table record - Building SQL query

Here is my table and data of these tables
Table name: Code
CID Code
1 abc
2 def
3 xyz
Table Name : Details
ID Name CID
1 a 1
2 b 2
Resultant Table:
ID Code Name
1 abc a
2 abc Null
3 def b
4 def Null
5 xyz Null
6 xyz Null
I nned to get all record from the code table and against each code I have to get all the rows from the details table, if some code have value their need value and if not then Null
Thanks
Sounds like you're looking for the cartesian product:
SELECT
c.CID * d.ID AS ID,
c.Code,
CASE
WHEN c.CID = d.CID THEN d.Name
ELSE NULL
END AS Name
FROM Code c
CROSS JOIN Details d
Although cartesian products are quite slow for larger tables... so be sure that this is what you really want.

Resources