how to make rows into column using pivot - sql-server-2008

i have an date like this
Name VALUE
ClientID M01010001250
InterviewType 1
InterviewDate 7/8/2011
ClientID M01010001260
InterviewType 1
InterviewDate 7/8/2011
ClientID M01010001260
InterviewType 5
InterviewDate 1869-07-01
ClientID M01010001290
InterviewType 1
InterviewDate 7/8/2011
now my out put should be like this
SEQ ClientID InterviewType InterviewDate
1 M01100016550 5 9/9/2011
2 M01100016550 5 9/9/2011
3 M01030000680 5 9/9/2011
i have written a query using pivot :
SELECT SEQ,ClientID,InterviewType,InterviewDate
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY NAME,VALUE ORDER BY NAME,VALUE) AS SEQ,NAME,VALUE
FROM Table1) DT
PIVOT (MAX(VALUE)FOR NAME IN(ClientID,InterviewType,InterviewDate))DT1
ORDER BY SEQ
even though i am using row_number it is not giving desired output suggest me

Your issue here is to group all the rows in three. I have a working solutions :
;WITH MyCTE AS
(
SELECT ROW_NUMBER() OVER (ORDER BY orderby) AS SEQ,
NAME,
VALUE
FROM (
select 1 as orderby,
*
from Table1
)t
)
SELECT SEQ,
ClientID,
InterviewType,
InterviewDate
FROM (
SELECT ((SEQ-1)/3)+1 AS SEQ,
NAME,
VALUE
FROM MyCTE
) DT
PIVOT (
MAX(VALUE)
FOR NAME
IN(ClientID,InterviewType,InterviewDate)
)DT1
ORDER BY SEQ
You may find an SQL Fiddle Demo

Your query isn't working because it's numbering the rows based on their value, so regardless of the order they go in, the rows with the lowest values will be first. Your row which has InterviewType 5 will always have the highest rownumber if the other rows all have InterviewType =1.
Without a way to uniquely identify which entries are supposed to go together, the order returned from queries in SQL server isn't guaranteed. However, if your data is in that exact format mentioned above - so the rows are always in the format ClientId, then InterviewType then InterviewDate, the following should work.
select p.*
from (select *,
CEILING((ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1) / 3) as [Row]
from Table1 t) t
PIVOT (max(value) for name in (ClientID, InterviewType, InterviewDate)) p
Output on my test data:
0 M01050001250 16 7/8/2011
1 M01010001260 1 7/8/2011
2 M01010001260 5 1869-07-01
3 M01010001290 1 7/8/2011
(The ceiling function is numbering every three rows - first three are 0, then the next three are 1, etc.)

Related

How to get the MAX value in MySQL?

How to get the MAX value in every albumID(45, 12, 22, 8) in the following table?
I tried with this query.
But it returned me the first value, not max value.(3, 6, 5, 6)
SELECT
*
FROM
(
SELECT
*
FROM
contentnew
WHERE
genreID = 1
ORDER BY
albumID DESC,
reg_count DESC
) AS newTB
GROUP BY
albumID;
Look this
If I use the
Once you group by, you can apply aggregate functions such as max on each group. In your example try:
SELECT albumID, max(reg_count) as max_count
FROM contentnew
GROUP BY albumID
This will project each albumID with the max_count in the group. In the select statement you can only use aggregate functions. The reason why we are able to project (or print) albumID is because this is the column we grouped by.
Following comments:
SELECT *
FROM contentnew as c1
WHERE c1.reg_count < (
SELECT max(c2.reg_count)
FROM contentnew as c2
WHERE c1.albumID = c2.albumID
GROUP BY c2.albumID)
You can try
select max(reg_count) from contentnew group by albumID
You are almost there, one thing that might be helpful is to use row_number() function, if you want every column from the table.
with contentnew_test
as
(
select row_number() over (partition by albumId order by reg_count desc) row
,* from
contentnew
)
select * from contentnew_test where row = 1 order by reg_count desc;
I used this as a reference as not sure about the syntax
https://www.mysqltutorial.org/mysql-window-functions/mysql-row_number-function/
Subquery will give you a result set something like this:
row albumId reg_count ...
1 1 8 ...
2 1 7 ...
3 1 3 ...
4 1 1 ...
1 2 22 ...
2 2 9 ...
3 2 6 ...
4 2 1...and so on.

MYSQL - Select Only Records Where Previous Record Column Data Differs

Hoping this is possible with just sql. I have a query that returns a data set with time_stamp and hash_index columns. Basically something to the effect of:
1 1583365548 6ff11ad5536f28d66098f6d74f97d877
2 1583365554 6ff11ad5536f28d66098f6d74f97d877
3 1583365556 6ff11ad5536f28d66098f6d74f97d877
4 1583365562 a2e99acb2540d49955ef93fb2684ac25
5 1583365571 a2e99acb2540d49955ef93fb2684ac25
6 1583365572 a2e99acb2540d49955ef93fb2684ac25
7 1583365574 a2e99acb2540d49955ef93fb2684ac25
8 1583365578 a2e99acb2540d49955ef93fb2684ac25
9 1583365580 a2e99acb2540d49955ef93fb2684ac25
What I want to do is further filter this query to only include the record if the hash_index differs from the previous record. Is this something I can do without having to dump it into PHP and loop through it?
My current query is below:
SELECT
(#cnt:=#cnt + 1) AS row_number,
time_stamp,
MD5(GROUP_CONCAT(CONCAT(user_state_name,
option_id,
option_code,
item_id,
item_code))) AS hash_index
FROM
user_state
WHERE
user_id = 2
GROUP BY
time_stamp;
What is with a query like this:
SELECT t1.*
FROM user_state t1
LEFT JOIN user_state t2 ON t1.id-1 = t2.id
WHERE t1.hash_index <> t2.hash_index;
If you want to filter out adjacent duplicates, I would just use lag() and dispense with hashing and aggregation:
SELECT us.*
FROM (SELECT us.*,
LAG(time_stamp) OVER (PARTITION BY user_id ORDER BY time_stamp) as prev_ts,
LAG(time_stamp) OVER (PARTITION BY user_id, user_state_nae, option_id, option_code, item_id, item_code ORDER BY time_stamp) as prev_ts_values
FROM user_state us
WHERE user_id = 2
) t
WHERE prev_ts_values is null or prev_ts_values <> prev_ts;
You can select whichever rows you want.

MySql Sum and Count for simple table

Could you help me with simple table SUM and COUNT calculating?
I've simple table 'test'
id name value
1 a 4
2 a 5
3 b 3
4 b 7
5 b 1
I need calculate SUM and Count for "a" and "b". I try this sql request:
SELECT name, SUM( value ) AS val, COUNT( * ) AS count FROM `test`
result:
name val count
a 20 5
But should be
name val count
a 9 2
b 11 3
Could you help me with correct sql request?
Add GROUP BY. That will cause the query to return a count and sum per group you defined (in this case, per name).
Without GROUP BY you just get the totals and any of the names (in your case 'a', but if could just as well have been 'b').
SELECT name, SUM( value ) AS val, COUNT( * ) AS count
FROM `test`
GROUP BY name
You need group by
select
name,
sum(value) as value,
count(*) as `count`
from test group by name ;

how to find the number of consecutive repeats

So I'm trying to write a mysql script to find the number of consecutive repeats in 'value' column of this table.
id value result
-- ----- ------
1 1 0
2 1 1
3 2 0
4 3 0
5 3 1
So in this case I want get the value 2
Get the next value using user variables,
GROUP so consecutive values more than 2 are not counted again,put all in a subquery,and use a simple CASE to increment the value you need in case value=next value.Add salt and pepper.
SELECT SUM(CASE WHEN y.value=y.next_value THEN #var+1 ELSE #var END) consecIds
FROM
(SELECT t.id, t.value, next_id, n.value next_value
FROM
(
SELECT t.id, t.value,
(
SELECT id
FROM table1
WHERE id > t.id
ORDER BY id
LIMIT 1
) next_id
FROM table1 t,(SELECT #var:=0)x
) t LEFT JOIN table1 n
ON t.next_id = n.id
GROUP BY t.value,n.value)y
FIDDLE
SELECT COUNT(DISTINCT column_name) FROM table_name;
DISTINCT will erase duplicated repetitions from specified column in result.
COUNT will count the rows in result.
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column.

SQL : select distinct of one column while ignoring other columns

So I have a table like this :
---------
id, keyid
---------
1, 3
1, 5
1, 6
2, 1
2, 1
2, 3
4, 1
I want the output of the query to be
1,3
2,1
4,1
If i use select distinct(id,keyid) from table it applies the distinct on the pair of id,keyid and not just id alone.
select id, min(keyid) from tbl group by id
If you want the min value of KeyId for each Id, then user194076's answer will definitely work.
If you want the first occurring value of KeyId for each Id, you could use:
WITH CTE AS (
SELECT Id, KeyId, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Id) AS RN
FROM tbl
)
SELECT Id, KeyId FROM CTE WHERE RN = 1
I've tested both using STATISTICS IO and STATISTICS TIME and they appear to be the same in terms of performance, so really depends on what your exact needs are.