![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' |
#3
| |||
| |||
|
|
"whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180340909.593482.109010 (AT) z28g2000prd (DOT) googlegroups.com... Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Let's stop right here. Customer_id should not be a column in the Groups table. The way you've placed it, it looks like a customer can belong to many groups, but a group can only have one customer in it. I doubt that this is your intent. More likely would be a "group_id" column in the Customers table. Do you agree?- Hide quoted text - - Show quoted text - |
#4
| |||
| |||
|
|
On May 29, 12:23 am, "David Cressey" <cresse... (AT) verizon (DOT) net> wrote: "whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180340909.593482.109010 (AT) z28g2000prd (DOT) googlegroups.com... Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Let's stop right here. Customer_id should not be a column in the Groups table. The way you've placed it, it looks like a customer can belong to many groups, but a group can only have one customer in it. I doubt that this is your intent. More likely would be a "group_id" column in the Customers table. Do you agree?- Hide quoted text - - Show quoted text - I'm not having an issue with the table structure, it is the SQL required to extract the information I want. |
|
A Group can have multiple customers in it and a customer can be a part of multiple groups. |
#5
| |||
| |||
|
|
"whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180410766.420129.71610 (AT) o11g2000prd (DOT) googlegroups.com... On May 29, 12:23 am, "David Cressey" <cresse... (AT) verizon (DOT) net> wrote: "whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180340909.593482.109010 (AT) z28g2000prd (DOT) googlegroups.com... Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Let's stop right here. Customer_id should not be a column in the Groups table. The way you've placed it, it looks like a customer can belong to many groups, but a group can only have one customer in it. I doubt that this is your intent. More likely would be a "group_id" column in the Customers table. Do you agree?- Hide quoted text - - Show quoted text - I'm not having an issue with the table structure, it is the SQL required to extract the information I want. Perhaps you should be having an issue with it. If your table structure is wrong, you won't be able to store the right facts (data). If your database doesn't have the right data, working out a correct query is going to be awfully difficult, perhaps impossible. A Group can have multiple customers in it and a customer can be a part of multiple groups. In that case, you need an extra table, call it "customer_groups", with two columns: customer_id and group_id. The PKEY of this table is both columns. Do you agree that this is the correct table structure for your data? If so, then we can proceed to work out what the query is going to look like.- Hide quoted text - - Show quoted text - |
#6
| |||
| |||
|
|
On May 29, 8:35 pm, "David Cressey" <cresse... (AT) verizon (DOT) net> wrote: "whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180410766.420129.71610 (AT) o11g2000prd (DOT) googlegroups.com... On May 29, 12:23 am, "David Cressey" <cresse... (AT) verizon (DOT) net> wrote: "whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180340909.593482.109010 (AT) z28g2000prd (DOT) googlegroups.com... Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Let's stop right here. Customer_id should not be a column in the Groups table. The way you've placed it, it looks like a customer can belong to many groups, but a group can only have one customer in it. I doubt that this is your intent. More likely would be a "group_id" column in the Customers table. Do you agree?- Hide quoted text - - Show quoted text - I'm not having an issue with the table structure, it is the SQL required to extract the information I want. Perhaps you should be having an issue with it. If your table structure is wrong, you won't be able to store the right facts (data). If your database doesn't have the right data, working out a correct query is going to be awfully difficult, perhaps impossible. A Group can have multiple customers in it and a customer can be a part of multiple groups. In that case, you need an extra table, call it "customer_groups", with two columns: customer_id and group_id. The PKEY of this table is both columns. Do you agree that this is the correct table structure for your data? If so, then we can proceed to work out what the query is going to look like.- Hide quoted text - - Show quoted text - You guys are missing the point - The database structure works fine for our needs - sure, it could be refined or improved but that is not what I am seeking assistance with!!!!!! I'm tring to create an SQL statement to extract a count of the search & prder log files. Here is where I have started: SELECT COUNT(SEARCHES.SEARCH_ID), COUNT(ORDERS.ORDER_ID) FROM SEARCHES INNER JOIN ORDERS ON SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID INNER JOIN GROUPS ON GROUPS.CUSTOMER_ID = SEARCHES.CUSTOMER_ID WHERE (GROUPS.GROUP_ID = 17) |
#7
| |||
| |||
|
|
On May 29, 8:35 pm, "David Cressey" <cresse... (AT) verizon (DOT) net> wrote: "whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180410766.420129.71610 (AT) o11g2000prd (DOT) googlegroups.com... On May 29, 12:23 am, "David Cressey" <cresse... (AT) verizon (DOT) net> wrote: "whitsey" <lysterfiel... (AT) gmail (DOT) com> wrote in message news:1180340909.593482.109010 (AT) z28g2000prd (DOT) googlegroups.com... Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Let's stop right here. Customer_id should not be a column in the Groups table. The way you've placed it, it looks like a customer can belong to many groups, but a group can only have one customer in it. I doubt that this is your intent. More likely would be a "group_id" column in the Customers table. Do you agree?- Hide quoted text - - Show quoted text - I'm not having an issue with the table structure, it is the SQL required to extract the information I want. Perhaps you should be having an issue with it. If your table structure is wrong, you won't be able to store the right facts (data). If your database doesn't have the right data, working out a correct query is going to be awfully difficult, perhaps impossible. A Group can have multiple customers in it and a customer can be a part of multiple groups. In that case, you need an extra table, call it "customer_groups", with two columns: customer_id and group_id. The PKEY of this table is both columns. Do you agree that this is the correct table structure for your data? If so, then we can proceed to work out what the query is going to look like.- Hide quoted text - - Show quoted text - You guys are missing the point - The database structure works fine for our needs - sure, it could be refined or improved but that is not what I am seeking assistance with!!!!!! I'm tring to create an SQL statement to extract a count of the search & prder log files. Here is where I have started: SELECT COUNT(SEARCHES.SEARCH_ID), COUNT(ORDERS.ORDER_ID) FROM SEARCHES INNER JOIN ORDERS ON SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID INNER JOIN GROUPS ON GROUPS.CUSTOMER_ID = SEARCHES.CUSTOMER_ID WHERE (GROUPS.GROUP_ID = 17) |
#8
| |||
| |||
|
|
Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Mth 1 1244(Searches) 846(Orders) Mth 2 2345(Searches) 1834(Orders) Mth 3 2342(Searches) 1954(Orders) Mth 4 2425(Searches) 2134(Orders) Mth 5 5235(Searches) 4253(Orders) I am clearly nowhere near the level I need to be to achieve this. Can it be achieved in a single statement? Yes. |
#9
| |||
| |||
|
|
"whitsey" <lysterfieldcc (AT) gmail (DOT) com> wrote in message news:1180340909.593482.109010 (AT) z28g2000prd (DOT) googlegroups.com... Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Mth 1 1244(Searches) 846(Orders) Mth 2 2345(Searches) 1834(Orders) Mth 3 2342(Searches) 1954(Orders) Mth 4 2425(Searches) 2134(Orders) Mth 5 5235(Searches) 4253(Orders) I am clearly nowhere near the level I need to be to achieve this. Can it be achieved in a single statement? Yes. Let's tackle a simpler query first, one that just delivers the months and the searches. select DatePart ("m", s.date) as Month, count (s.search_id) as Searches from searches s inner join groups g on s.customer_id = g.customer_id where g.group_id = 17 group by DatePart ("m", s.date); Note: DatePart is on MS SQL. You may need to use a different date function. The m inside double quotes is MS dialect. You may need single quotes. I'm assuming Group_id is a number. If this delivers correct results, the next step is to devise a similar query, but for orders. Later, we'll combine the two queries. I still have criticisms of your table design, but you are clearly not ready to hear them. In particular, the foreign key: ORDERS.SEARCH_ID restricts the data to many orders connected to one search. It sounds like that's not your intent. But given how you reacted to the last criticism I made of the table design, this comment is probably wasted. /* The following gets the search count by date: */ |
#10
| |||
| |||
|
|
Help! I am trying to write an SQL statement (without much luck) to extract the total number of searches and orders from a particular customer, grouped into days/months/weeks (depending on the report type. The tables are as follows: Searches -------------- search_id (PKEY) customer_id (FKEY) product_id cost date .... Orders ----------- order_id (PKEY) search_id (FKEY) order_value date ... Groups ----------- group_id (PKEY) customer_id ... Customers --------------- customer_id (PKEY) .... What I want to achieve from this is For all customers with Group_id = '17' Mth 1 1244(Searches) 846(Orders) Mth 2 2345(Searches) 1834(Orders) Mth 3 2342(Searches) 1954(Orders) Mth 4 2425(Searches) 2134(Orders) Mth 5 5235(Searches) 4253(Orders) I am clearly nowhere near the level I need to be to achieve this. Can it be achieved in a single statement? Thanks |
![]() |
| Thread Tools | |
| Display Modes | |
| |